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.google.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);
45 // FEATURE_ACTION_BAR_OVERLAY is required to scroll the actionBar.
46 supportRequestWindowFeature(AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR_OVERLAY);
48 setContentView(R.layout.activity_webview);
50 mainWebView = (WebView) findViewById(R.id.mainWebView);
52 final ActionBar actionBar = getSupportActionBar();
53 if (actionBar != null) {
54 // Remove the title from the action bar.
55 actionBar.setDisplayShowTitleEnabled(false);
57 // Add the custom app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
58 actionBar.setCustomView(R.layout.app_bar);
59 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
61 // Initialize the variables for favoriteIcon, urlTextBox, and progressBar
62 favoriteIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.favoriteIcon);
63 urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
64 progressBar = (ProgressBar) actionBar.getCustomView().findViewById(R.id.progressBar);
66 // Scroll the actionBar.
67 actionBar.setHideOnContentScrollEnabled(true);
70 mainWebView.setWebViewClient(new WebViewClient() {
72 // setWebViewClient makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
73 // Save the URL to formattedUrlString and update urlTextBox before loading mainWebView.
75 public boolean shouldOverrideUrlLoading(WebView view, String url) {
76 mainWebView.loadUrl(url);
80 // Update the URL in urlTextBox when the page starts to load.
82 public void onPageStarted(WebView view, String url, Bitmap favicon) {
83 urlTextBox.setText(url);
86 // Update formattedUrlString and urlTextBox. It is necessary to do this after the page finishes loading because the final URL can change during load.
88 public void onPageFinished(WebView view, String url) {
89 formattedUrlString = url;
90 urlTextBox.setText(formattedUrlString);
94 mainWebView.setWebChromeClient(new WebChromeClient() {
96 // Update the progress bar when a page is loading.
98 public void onProgressChanged(WebView view, int progress) {
99 progressBar.setProgress(progress);
100 if (progress < 100) {
101 progressBar.setVisibility(View.VISIBLE);
103 progressBar.setVisibility(View.GONE);
107 // Set the favorite icon when it changes.
109 public void onReceivedIcon(WebView view, Bitmap icon) {
110 favoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
114 // Set the "go" button on the keyboard to load the URL.
115 urlTextBox.setOnKeyListener(new View.OnKeyListener() {
116 public boolean onKey(View v, int keyCode, KeyEvent event) {
118 // If the event is a key-down event on the "enter" button, load the URL.
119 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
120 (keyCode == KeyEvent.KEYCODE_ENTER)) {
121 // Load the URL into the mainWebView and consume the event.
123 loadUrlFromTextBox(mainWebView);
124 } catch (UnsupportedEncodingException e) {
127 // If the enter key was pressed, consume the event.
130 // If any other key was pressed, do not consume the event.
135 // Allow pinch to zoom.
136 mainWebView.getSettings().setBuiltInZoomControls(true);
138 // Hide zoom controls.
139 mainWebView.getSettings().setDisplayZoomControls(false);
141 // Enable JavaScript.
142 mainWebView.getSettings().setJavaScriptEnabled(true);
144 // Enable DOM Storage.
145 mainWebView.getSettings().setDomStorageEnabled(true);
147 // Get the intent information that started the app.
148 final Intent intent = getIntent();
150 if (intent.getData() != null) {
151 // Get the intent data and convert it to a string.
152 final Uri intentUriData = intent.getData();
153 formattedUrlString = intentUriData.toString();
156 // If formattedUrlString is null assign the homepage to it.
157 if (formattedUrlString == null) {
158 formattedUrlString = homepage;
161 // Load the initial website.
162 mainWebView.loadUrl(formattedUrlString);
166 public boolean onCreateOptionsMenu(Menu menu) {
167 // Inflate the menu; this adds items to the action bar if it is present.
168 getMenuInflater().inflate(R.menu.menu_webview, menu);
173 public boolean onOptionsItemSelected(MenuItem menuItem) {
174 int menuItemId = menuItem.getItemId();
176 // Sets the commands that relate to the menu entries.
177 switch (menuItemId) {
179 mainWebView.loadUrl(homepage);
183 mainWebView.loadUrl(formattedUrlString);
187 mainWebView.goBack();
191 mainWebView.goForward();
195 return super.onOptionsItemSelected(menuItem);
198 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
200 public void onBackPressed() {
201 if (mainWebView.canGoBack()) {
202 mainWebView.goBack();
204 super.onBackPressed();
208 public void loadUrlFromTextBox(View view) throws UnsupportedEncodingException {
209 // Get the text from urlTextInput and convert it to a string.
210 String unformattedUrlString = urlTextBox.getText().toString();
211 URL unformattedUrl = null;
212 Uri.Builder formattedUri = new Uri.Builder();
214 // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search.
215 if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
217 // Add http:// at the beginning if it is missing. Otherwise the app will segfault.
218 if (!unformattedUrlString.startsWith("http")) {
219 unformattedUrlString = "http://" + unformattedUrlString;
222 // 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.
224 unformattedUrl = new URL(unformattedUrlString);
225 } catch (MalformedURLException e) {
229 // The ternary operator (? :) makes sure that unformattedUrl.get() does not cause a null pointer exception.
230 final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
231 final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
232 final String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
233 final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
234 final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
236 formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
237 formattedUrlString = formattedUri.build().toString();
240 // Sanitize the search input.
241 final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
242 formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
245 mainWebView.loadUrl(formattedUrlString);
247 // Hides the keyboard so we can see the webpage.
248 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
249 inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);