1 package com.stoutner.privacybrowser;
3 import android.annotation.SuppressLint;
4 import android.annotation.TargetApi;
5 import android.app.Activity;
6 import android.app.DownloadManager;
7 import android.content.ClipData;
8 import android.content.ClipboardManager;
9 import android.content.Context;
10 import android.content.Intent;
11 import android.graphics.Bitmap;
12 import android.net.Uri;
13 import android.os.Build;
14 import android.os.Bundle;
15 import android.support.v4.app.DialogFragment;
16 import android.support.v7.app.ActionBar;
17 import android.support.v7.app.AppCompatActivity;
18 import android.support.v7.app.AppCompatDialogFragment;
19 import android.util.Patterns;
20 import android.view.KeyEvent;
21 import android.view.Menu;
22 import android.view.MenuItem;
23 import android.view.View;
24 import android.view.inputmethod.InputMethodManager;
25 import android.webkit.DownloadListener;
26 import android.webkit.WebChromeClient;
27 import android.webkit.WebResourceError;
28 import android.webkit.WebResourceRequest;
29 import android.webkit.WebView;
30 import android.webkit.WebViewClient;
31 import android.widget.EditText;
32 import android.widget.FrameLayout;
33 import android.widget.ImageView;
34 import android.widget.ProgressBar;
35 import android.widget.Toast;
36 import java.io.UnsupportedEncodingException;
37 import java.net.MalformedURLException;
39 import java.net.URLEncoder;
41 public class Webview extends AppCompatActivity implements CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener {
42 // favoriteIcon is public static so it can be accessed from CreateHomeScreenShortcut.
43 public static Bitmap favoriteIcon;
45 private String formattedUrlString;
46 private String homepage = "https://www.duckduckgo.com/";
48 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
49 @SuppressLint("SetJavaScriptEnabled")
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54 setContentView(R.layout.activity_webview);
56 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
57 final FrameLayout fullScreenVideoFrameLayout = (FrameLayout) findViewById(R.id.fullScreenVideoFrameLayout);
58 final Activity mainWebViewActivity = this;
60 final ActionBar actionBar = getSupportActionBar();
61 if (actionBar != null) {
62 // Remove the title from the action bar.
63 actionBar.setDisplayShowTitleEnabled(false);
65 // Add the custom app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
66 actionBar.setCustomView(R.layout.app_bar);
67 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
69 // Set the "go" button on the keyboard to load the URL in urlTextBox.
70 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
71 urlTextBox.setOnKeyListener(new View.OnKeyListener() {
72 public boolean onKey(View v, int keyCode, KeyEvent event) {
73 // If the event is a key-down event on the "enter" button, load the URL.
74 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
75 // Load the URL into the mainWebView and consume the event.
78 } catch (UnsupportedEncodingException e) {
81 // If the enter key was pressed, consume the event.
84 // If any other key was pressed, do not consume the event.
91 mainWebView.setWebViewClient(new WebViewClient() {
92 // shouldOverrideUrlLoading makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
94 public boolean shouldOverrideUrlLoading(WebView view, String url) {
95 mainWebView.loadUrl(url);
99 public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
100 Toast.makeText(mainWebViewActivity, "Error loading " + request + " Error: " + error, Toast.LENGTH_LONG).show();
103 // Update the URL in urlTextBox when the page starts to load.
105 public void onPageStarted(WebView view, String url, Bitmap favicon) {
106 if (actionBar != null) {
107 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
108 urlTextBox.setText(url);
112 // Update formattedUrlString and urlTextBox. It is necessary to do this after the page finishes loading because the final URL can change during load.
114 public void onPageFinished(WebView view, String url) {
115 formattedUrlString = url;
117 if (actionBar != null) {
118 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
119 urlTextBox.setText(formattedUrlString);
124 mainWebView.setWebChromeClient(new WebChromeClient() {
125 // Update the progress bar when a page is loading.
127 public void onProgressChanged(WebView view, int progress) {
128 // Make sure that actionBar is not null.
129 if (actionBar != null) {
130 ProgressBar progressBar = (ProgressBar) actionBar.getCustomView().findViewById(R.id.progressBar);
131 progressBar.setProgress(progress);
132 if (progress < 100) {
133 progressBar.setVisibility(View.VISIBLE);
135 progressBar.setVisibility(View.GONE);
140 // Set the favorite icon when it changes.
142 public void onReceivedIcon(WebView view, Bitmap icon) {
143 // Save a copy of the favorite icon for use if a shortcut is added to the home screen.
146 // Place the favorite icon in the actionBar if it is not null.
147 if (actionBar != null) {
148 ImageView imageViewFavoriteIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.favoriteIcon);
149 imageViewFavoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
153 // Enter full screen video
155 public void onShowCustomView(View view, CustomViewCallback callback) {
156 getSupportActionBar().hide();
158 fullScreenVideoFrameLayout.addView(view);
159 fullScreenVideoFrameLayout.setVisibility(View.VISIBLE);
161 mainWebView.setVisibility(View.GONE);
163 /* SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bars on the bottom or right of the screen.
164 ** SYSTEM_UI_FLAG_FULLSCREEN hides the status bar across the top of the screen.
165 ** SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the navigation and status bars ghosted overlays and automatically rehides them.
168 // Set the one flag supported by API >= 14.
169 if (Build.VERSION.SDK_INT >= 14) {
170 view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
173 // Set the two flags that are supported by API >= 16.
174 if (Build.VERSION.SDK_INT >= 16) {
175 view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN);
178 // Set all three flags that are supported by API >= 19.
179 if (Build.VERSION.SDK_INT >= 19) {
180 view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
184 // Exit full screen video
185 public void onHideCustomView() {
186 getSupportActionBar().show();
188 mainWebView.setVisibility(View.VISIBLE);
190 fullScreenVideoFrameLayout.removeAllViews();
191 fullScreenVideoFrameLayout.setVisibility(View.GONE);
195 mainWebView.setDownloadListener(new DownloadListener() {
196 // Launch the Android download manager when a link leads to a download.
198 public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
199 DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
200 DownloadManager.Request requestUri = new DownloadManager.Request(Uri.parse(url));
202 // Add the URL as the description for the download.
203 requestUri.setDescription(url);
205 // Show the download notification after the download is completed if the API is 11 or greater.
206 if (Build.VERSION.SDK_INT >= 11) {
207 requestUri.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
210 downloadManager.enqueue(requestUri);
211 Toast.makeText(mainWebViewActivity, "Download started", Toast.LENGTH_SHORT).show();
215 // Allow pinch to zoom.
216 mainWebView.getSettings().setBuiltInZoomControls(true);
218 // Hide zoom controls if the API is 11 or greater.
219 if (Build.VERSION.SDK_INT >= 11) {
220 mainWebView.getSettings().setDisplayZoomControls(false);
223 // Enable JavaScript.
224 mainWebView.getSettings().setJavaScriptEnabled(true);
226 // Enable DOM Storage.
227 mainWebView.getSettings().setDomStorageEnabled(true);
229 // Get the intent information that started the app.
230 final Intent intent = getIntent();
232 if (intent.getData() != null) {
233 // Get the intent data and convert it to a string.
234 final Uri intentUriData = intent.getData();
235 formattedUrlString = intentUriData.toString();
238 // If formattedUrlString is null assign the homepage to it.
239 if (formattedUrlString == null) {
240 formattedUrlString = homepage;
243 // Load the initial website.
244 mainWebView.loadUrl(formattedUrlString);
248 public boolean onCreateOptionsMenu(Menu menu) {
249 // Inflate the menu; this adds items to the action bar if it is present.
250 getMenuInflater().inflate(R.menu.menu_webview, menu);
254 // @TargetApi(11) turns off the errors regarding copy and paste, which are removed from view in menu_webview.xml for lower version of Android.
257 public boolean onOptionsItemSelected(MenuItem menuItem) {
258 int menuItemId = menuItem.getItemId();
259 ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
260 ActionBar actionBar = getSupportActionBar();
261 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
263 // Sets the commands that relate to the menu entries.
264 switch (menuItemId) {
266 mainWebView.loadUrl(homepage);
270 mainWebView.loadUrl(formattedUrlString);
274 mainWebView.goBack();
278 mainWebView.goForward();
282 // Make sure that actionBar is not null.
283 if (actionBar != null) {
284 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
285 clipboard.setPrimaryClip(ClipData.newPlainText("URL", urlTextBox.getText()));
290 // Make sure that actionBar is not null.
291 if (actionBar != null) {
292 ClipData.Item clipboardData = clipboard.getPrimaryClip().getItemAt(0);
293 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
294 urlTextBox.setText(clipboardData.coerceToText(this));
296 loadUrlFromTextBox();
297 } catch (UnsupportedEncodingException e) {
304 // Make sure that actionBar is not null.
305 if (actionBar != null) {
306 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
307 Intent shareIntent = new Intent();
308 shareIntent.setAction(Intent.ACTION_SEND);
309 shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString());
310 shareIntent.setType("text/plain");
311 startActivity(Intent.createChooser(shareIntent, "Share URL"));
315 case R.id.addToHomescreen:
316 // Show the CreateHomeScreenShortcut AlertDialog and name this instance createShortcut.
317 AppCompatDialogFragment shortcutDialog = new CreateHomeScreenShortcut();
318 shortcutDialog.show(getSupportFragmentManager(), "createShortcut");
320 //Everything else will be handled by CreateHomeScreenShortcut and the associated listeners below.
324 // Launch the system Download Manager.
325 Intent downloadManangerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
327 // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
328 downloadManangerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
330 startActivity(downloadManangerIntent);
334 return super.onOptionsItemSelected(menuItem);
338 public void onCreateHomeScreenShortcutCancel(DialogFragment dialog) {
339 // Do nothing because the user selected "Cancel".
343 public void onCreateHomeScreenShortcutCreate(DialogFragment dialog) {
344 // Get shortcutNameEditText from the alert dialog.
345 EditText shortcutNameEditText = (EditText) dialog.getDialog().findViewById(R.id.shortcutNameEditText);
347 // Create the bookmark shortcut based on formattedUrlString.
348 Intent bookmarkShortcut = new Intent();
349 bookmarkShortcut.setAction(Intent.ACTION_VIEW);
350 bookmarkShortcut.setData(Uri.parse(formattedUrlString));
352 // Place the bookmark shortcut on the home screen.
353 Intent placeBookmarkShortcut = new Intent();
354 placeBookmarkShortcut.putExtra("android.intent.extra.shortcut.INTENT", bookmarkShortcut);
355 placeBookmarkShortcut.putExtra("android.intent.extra.shortcut.NAME", shortcutNameEditText.getText().toString());
356 placeBookmarkShortcut.putExtra("android.intent.extra.shortcut.ICON", favoriteIcon);
357 placeBookmarkShortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
358 sendBroadcast(placeBookmarkShortcut);
361 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
363 public void onBackPressed() {
364 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
366 if (mainWebView.canGoBack()) {
367 mainWebView.goBack();
369 super.onBackPressed();
373 public void loadUrlFromTextBox() throws UnsupportedEncodingException {
374 // Make sure that actionBar is not null.
375 ActionBar actionBar = getSupportActionBar();
376 if (actionBar != null) {
377 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
378 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
380 // Get the text from urlTextInput and convert it to a string.
381 String unformattedUrlString = urlTextBox.getText().toString();
382 URL unformattedUrl = null;
383 Uri.Builder formattedUri = new Uri.Builder();
385 // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search.
386 if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
388 // Add http:// at the beginning if it is missing. Otherwise the app will segfault.
389 if (!unformattedUrlString.startsWith("http")) {
390 unformattedUrlString = "http://" + unformattedUrlString;
393 // Convert unformattedUrlString to a URL, then to a URI, and then back to a string, which sanitizes the input and adds in any missing components.
395 unformattedUrl = new URL(unformattedUrlString);
396 } catch (MalformedURLException e) {
400 // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value.
401 final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
402 final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
403 final String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
404 final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
405 final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
407 formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
408 formattedUrlString = formattedUri.build().toString();
411 // Sanitize the search input and convert it to a DuckDuckGo search.
412 final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
413 formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
416 mainWebView.loadUrl(formattedUrlString);
418 // Hides the keyboard so we can see the webpage.
419 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
420 inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);