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.v7.app.ActionBar;
16 import android.support.v7.app.AppCompatActivity;
17 import android.util.Patterns;
18 import android.view.KeyEvent;
19 import android.view.Menu;
20 import android.view.MenuItem;
21 import android.view.View;
22 import android.view.inputmethod.InputMethodManager;
23 import android.webkit.DownloadListener;
24 import android.webkit.WebChromeClient;
25 import android.webkit.WebResourceError;
26 import android.webkit.WebResourceRequest;
27 import android.webkit.WebView;
28 import android.webkit.WebViewClient;
29 import android.widget.EditText;
30 import android.widget.FrameLayout;
31 import android.widget.ImageView;
32 import android.widget.ProgressBar;
33 import android.widget.RelativeLayout;
34 import android.widget.Toast;
35 import java.io.UnsupportedEncodingException;
36 import java.net.MalformedURLException;
38 import java.net.URLEncoder;
40 public class Webview extends AppCompatActivity {
42 private String formattedUrlString;
43 private String homepage = "https://www.duckduckgo.com/";
45 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
46 @SuppressLint("SetJavaScriptEnabled")
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 setContentView(R.layout.activity_webview);
53 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
54 final FrameLayout fullScreenVideoFrameLayout = (FrameLayout) findViewById(R.id.fullScreenVideoFrameLayout);
55 final RelativeLayout rootRelativeLayout = (RelativeLayout) findViewById(R.id.rootRelativeLayout);
56 final Activity mainWebViewActivity = this;
58 final ActionBar actionBar = getSupportActionBar();
59 if (actionBar != null) {
60 // Remove the title from the action bar.
61 actionBar.setDisplayShowTitleEnabled(false);
63 // Add the custom app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
64 actionBar.setCustomView(R.layout.app_bar);
65 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
67 // Set the "go" button on the keyboard to load the URL in urlTextBox.
68 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
69 urlTextBox.setOnKeyListener(new View.OnKeyListener() {
70 public boolean onKey(View v, int keyCode, KeyEvent event) {
71 // If the event is a key-down event on the "enter" button, load the URL.
72 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
73 (keyCode == KeyEvent.KEYCODE_ENTER)) {
74 // Load the URL into the mainWebView and consume the event.
77 } catch (UnsupportedEncodingException e) {
80 // If the enter key was pressed, consume the event.
83 // If any other key was pressed, do not consume the event.
89 mainWebView.setWebViewClient(new WebViewClient() {
90 // shouldOverrideUrlLoading makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
92 public boolean shouldOverrideUrlLoading(WebView view, String url) {
93 mainWebView.loadUrl(url);
97 public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
98 Toast.makeText(mainWebViewActivity, "Error loading " + request + " Error: " + error, Toast.LENGTH_LONG).show();
101 // Update the URL in urlTextBox when the page starts to load.
103 public void onPageStarted(WebView view, String url, Bitmap favicon) {
104 if (actionBar != null) {
105 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
106 urlTextBox.setText(url);
110 // Update formattedUrlString and urlTextBox. It is necessary to do this after the page finishes loading because the final URL can change during load.
112 public void onPageFinished(WebView view, String url) {
113 formattedUrlString = url;
115 if (actionBar != null) {
116 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
117 urlTextBox.setText(formattedUrlString);
122 mainWebView.setWebChromeClient(new WebChromeClient() {
123 // Update the progress bar when a page is loading.
125 public void onProgressChanged(WebView view, int progress) {
126 // Make sure that actionBar is not null.
127 if (actionBar != null) {
128 ProgressBar progressBar = (ProgressBar) actionBar.getCustomView().findViewById(R.id.progressBar);
129 progressBar.setProgress(progress);
130 if (progress < 100) {
131 progressBar.setVisibility(View.VISIBLE);
133 progressBar.setVisibility(View.GONE);
138 // Set the favorite icon when it changes.
140 public void onReceivedIcon(WebView view, Bitmap icon) {
141 // Make sure that actionBar is not null.
142 if (actionBar != null) {
143 ImageView favoriteIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.favoriteIcon);
144 favoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
148 // Enter full screen video
150 public void onShowCustomView(View view, CustomViewCallback callback) {
151 getSupportActionBar().hide();
153 fullScreenVideoFrameLayout.addView(view);
154 fullScreenVideoFrameLayout.setVisibility(View.VISIBLE);
156 mainWebView.setVisibility(View.GONE);
158 /* SYSTEM_UI_FLAG_HIDE_NAVIGATION hides the navigation bars on the bottom or right of the screen.
159 ** SYSTEM_UI_FLAG_FULLSCREEN hides the status bar across the top of the screen.
160 ** SYSTEM_UI_FLAG_IMMERSIVE_STICKY makes the navigation and status bars ghosted overlays and automatically rehides them.
163 // Set the one flag supported by API >= 14.
164 if (Build.VERSION.SDK_INT >= 14) {
165 view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
168 // Set the two flags that are supported by API >= 16.
169 if (Build.VERSION.SDK_INT >= 16) {
170 view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN);
173 // Set all three flags that are supported by API >= 19.
174 if (Build.VERSION.SDK_INT >= 19) {
175 view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
179 // Exit full screen video
180 public void onHideCustomView() {
181 getSupportActionBar().show();
183 mainWebView.setVisibility(View.VISIBLE);
185 fullScreenVideoFrameLayout.removeAllViews();
186 fullScreenVideoFrameLayout.setVisibility(View.GONE);
190 mainWebView.setDownloadListener(new DownloadListener() {
191 // Launch the Android download manager when a link leads to a download.
193 public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
194 DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
195 DownloadManager.Request requestUri = new DownloadManager.Request(Uri.parse(url));
197 // Add the URL as the description for the download.
198 requestUri.setDescription(url);
200 // Show the download notification after the download is completed if the API is 11 or greater.
201 if (Build.VERSION.SDK_INT >= 11) {
202 requestUri.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
205 downloadManager.enqueue(requestUri);
206 Toast.makeText(mainWebViewActivity, "Download started", Toast.LENGTH_SHORT).show();
210 // Allow pinch to zoom.
211 mainWebView.getSettings().setBuiltInZoomControls(true);
213 // Hide zoom controls if the API is 11 or greater.
214 if (Build.VERSION.SDK_INT >= 11) {
215 mainWebView.getSettings().setDisplayZoomControls(false);
218 // Enable JavaScript.
219 mainWebView.getSettings().setJavaScriptEnabled(true);
221 // Enable DOM Storage.
222 mainWebView.getSettings().setDomStorageEnabled(true);
224 // Get the intent information that started the app.
225 final Intent intent = getIntent();
227 if (intent.getData() != null) {
228 // Get the intent data and convert it to a string.
229 final Uri intentUriData = intent.getData();
230 formattedUrlString = intentUriData.toString();
233 // If formattedUrlString is null assign the homepage to it.
234 if (formattedUrlString == null) {
235 formattedUrlString = homepage;
238 // Load the initial website.
239 mainWebView.loadUrl(formattedUrlString);
243 public boolean onCreateOptionsMenu(Menu menu) {
244 // Inflate the menu; this adds items to the action bar if it is present.
245 getMenuInflater().inflate(R.menu.menu_webview, menu);
249 // @TargetApi(11) turns off the errors regarding copy and paste, which are removed from view in menu_webview.xml for lower version of Android.
252 public boolean onOptionsItemSelected(MenuItem menuItem) {
253 int menuItemId = menuItem.getItemId();
254 ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
255 ActionBar actionBar = getSupportActionBar();
256 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
258 // Sets the commands that relate to the menu entries.
259 switch (menuItemId) {
261 mainWebView.loadUrl(homepage);
265 mainWebView.loadUrl(formattedUrlString);
269 mainWebView.goBack();
273 mainWebView.goForward();
277 // Make sure that actionBar is not null.
278 if (actionBar != null) {
279 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
280 clipboard.setPrimaryClip(ClipData.newPlainText("URL", urlTextBox.getText()));
285 // Make sure that actionBar is not null.
286 if (actionBar != null) {
287 ClipData.Item clipboardData = clipboard.getPrimaryClip().getItemAt(0);
288 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
289 urlTextBox.setText(clipboardData.coerceToText(this));
291 loadUrlFromTextBox();
292 } catch (UnsupportedEncodingException e) {
299 // Make sure that actionBar is not null.
300 if (actionBar != null) {
301 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
302 Intent shareIntent = new Intent();
303 shareIntent.setAction(Intent.ACTION_SEND);
304 shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString());
305 shareIntent.setType("text/plain");
306 startActivity(Intent.createChooser(shareIntent, "Share URL"));
311 // Launch the system Download Manager.
312 Intent downloadManangerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
314 // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
315 downloadManangerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
317 startActivity(downloadManangerIntent);
320 return super.onOptionsItemSelected(menuItem);
323 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
325 public void onBackPressed() {
326 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
328 if (mainWebView.canGoBack()) {
329 mainWebView.goBack();
331 super.onBackPressed();
335 public void loadUrlFromTextBox() throws UnsupportedEncodingException {
336 // Make sure that actionBar is not null.
337 ActionBar actionBar = getSupportActionBar();
338 if (actionBar != null) {
339 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
340 EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
342 // Get the text from urlTextInput and convert it to a string.
343 String unformattedUrlString = urlTextBox.getText().toString();
344 URL unformattedUrl = null;
345 Uri.Builder formattedUri = new Uri.Builder();
347 // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search.
348 if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
350 // Add http:// at the beginning if it is missing. Otherwise the app will segfault.
351 if (!unformattedUrlString.startsWith("http")) {
352 unformattedUrlString = "http://" + unformattedUrlString;
355 // 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.
357 unformattedUrl = new URL(unformattedUrlString);
358 } catch (MalformedURLException e) {
362 // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value.
363 final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
364 final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
365 final String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
366 final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
367 final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
369 formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
370 formattedUrlString = formattedUri.build().toString();
373 // Sanitize the search input and convert it to a DuckDuckGo search.
374 final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
375 formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
378 mainWebView.loadUrl(formattedUrlString);
380 // Hides the keyboard so we can see the webpage.
381 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
382 inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);