1 package com.stoutner.privacybrowser;
3 import android.annotation.SuppressLint;
4 import android.app.Activity;
5 import android.content.Intent;
6 import android.graphics.Bitmap;
7 import android.net.Uri;
8 import android.os.Bundle;
9 import android.support.v7.app.ActionBar;
10 import android.support.v7.app.AppCompatActivity;
11 import android.support.v7.app.AppCompatDelegate;
12 import android.util.Patterns;
13 import android.view.KeyEvent;
14 import android.view.Menu;
15 import android.view.MenuItem;
16 import android.view.View;
17 import android.view.inputmethod.InputMethodManager;
18 import android.webkit.WebChromeClient;
19 import android.webkit.WebView;
20 import android.webkit.WebViewClient;
21 import android.widget.EditText;
22 import android.widget.ImageView;
23 import android.widget.ProgressBar;
24 import java.io.UnsupportedEncodingException;
25 import java.net.MalformedURLException;
27 import java.net.URLEncoder;
29 public class Webview extends AppCompatActivity {
31 static String formattedUrlString;
32 static WebView mainWebView;
33 static ProgressBar progressBar;
34 static EditText urlTextBox;
35 static ImageView favoriteIcon;
36 static final String homepage = "https://www.duckduckgo.com/";
38 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
39 @SuppressLint("SetJavaScriptEnabled")
42 protected void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 setContentView(R.layout.activity_webview);
46 mainWebView = (WebView) findViewById(R.id.mainWebView);
48 final ActionBar actionBar = getSupportActionBar();
49 if (actionBar != null) {
50 // Remove the title from the action bar.
51 actionBar.setDisplayShowTitleEnabled(false);
53 // Add the custom app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
54 actionBar.setCustomView(R.layout.app_bar);
55 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
57 // Initialize the variables for favoriteIcon, urlTextBox, and progressBar
58 favoriteIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.favoriteIcon);
59 urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
60 progressBar = (ProgressBar) actionBar.getCustomView().findViewById(R.id.progressBar);
63 mainWebView.setWebViewClient(new WebViewClient() {
65 // setWebViewClient makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
66 // Save the URL to formattedUrlString and update urlTextBox before loading mainWebView.
68 public boolean shouldOverrideUrlLoading(WebView view, String url) {
69 mainWebView.loadUrl(url);
73 // Update the URL in urlTextBox when the page starts to load.
75 public void onPageStarted(WebView view, String url, Bitmap favicon) {
76 urlTextBox.setText(url);
79 // Update formattedUrlString and urlTextBox. It is necessary to do this after the page finishes loading because the final URL can change during load.
81 public void onPageFinished(WebView view, String url) {
82 formattedUrlString = url;
83 urlTextBox.setText(formattedUrlString);
87 mainWebView.setWebChromeClient(new WebChromeClient() {
89 // Update the progress bar when a page is loading.
91 public void onProgressChanged(WebView view, int progress) {
92 progressBar.setProgress(progress);
94 progressBar.setVisibility(View.VISIBLE);
96 progressBar.setVisibility(View.GONE);
100 // Set the favorite icon when it changes.
102 public void onReceivedIcon(WebView view, Bitmap icon) {
103 favoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
107 // Set the "go" button on the keyboard to load the URL.
108 urlTextBox.setOnKeyListener(new View.OnKeyListener() {
109 public boolean onKey(View v, int keyCode, KeyEvent event) {
111 // If the event is a key-down event on the "enter" button, load the URL.
112 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
113 (keyCode == KeyEvent.KEYCODE_ENTER)) {
114 // Load the URL into the mainWebView and consume the event.
116 loadUrlFromTextBox(mainWebView);
117 } catch (UnsupportedEncodingException e) {
120 // If the enter key was pressed, consume the event.
123 // If any other key was pressed, do not consume the event.
128 // Allow pinch to zoom.
129 mainWebView.getSettings().setBuiltInZoomControls(true);
131 // Hide zoom controls.
132 mainWebView.getSettings().setDisplayZoomControls(false);
134 // Enable JavaScript.
135 mainWebView.getSettings().setJavaScriptEnabled(true);
137 // Enable DOM Storage.
138 mainWebView.getSettings().setDomStorageEnabled(true);
140 // Get the intent information that started the app.
141 final Intent intent = getIntent();
143 if (intent.getData() != null) {
144 // Get the intent data and convert it to a string.
145 final Uri intentUriData = intent.getData();
146 formattedUrlString = intentUriData.toString();
149 // If formattedUrlString is null assign the homepage to it.
150 if (formattedUrlString == null) {
151 formattedUrlString = homepage;
154 // Load the initial website.
155 mainWebView.loadUrl(formattedUrlString);
159 public boolean onCreateOptionsMenu(Menu menu) {
160 // Inflate the menu; this adds items to the action bar if it is present.
161 getMenuInflater().inflate(R.menu.menu_webview, menu);
166 public boolean onOptionsItemSelected(MenuItem menuItem) {
167 int menuItemId = menuItem.getItemId();
169 // Sets the commands that relate to the menu entries.
170 switch (menuItemId) {
172 mainWebView.loadUrl(homepage);
176 mainWebView.loadUrl(formattedUrlString);
180 mainWebView.goBack();
184 mainWebView.goForward();
188 return super.onOptionsItemSelected(menuItem);
191 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
193 public void onBackPressed() {
194 if (mainWebView.canGoBack()) {
195 mainWebView.goBack();
197 super.onBackPressed();
201 public void loadUrlFromTextBox(View view) throws UnsupportedEncodingException {
202 // Get the text from urlTextInput and convert it to a string.
203 String unformattedUrlString = urlTextBox.getText().toString();
204 URL unformattedUrl = null;
205 Uri.Builder formattedUri = new Uri.Builder();
207 // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search.
208 if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
210 // Add http:// at the beginning if it is missing. Otherwise the app will segfault.
211 if (!unformattedUrlString.startsWith("http")) {
212 unformattedUrlString = "http://" + unformattedUrlString;
215 // 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.
217 unformattedUrl = new URL(unformattedUrlString);
218 } catch (MalformedURLException e) {
222 // The ternary operator (? :) makes sure that unformattedUrl.get() does not cause a null pointer exception.
223 final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
224 final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
225 final String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
226 final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
227 final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
229 formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
230 formattedUrlString = formattedUri.build().toString();
233 // Sanitize the search input.
234 final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
235 formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
238 mainWebView.loadUrl(formattedUrlString);
240 // Hides the keyboard so we can see the webpage.
241 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
242 inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);