]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/Webview.java
Remove app bar scrolling because it broke pinch to zoom and some web pages.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / Webview.java
1 package com.stoutner.privacybrowser;
2
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;
26 import java.net.URL;
27 import java.net.URLEncoder;
28
29 public class Webview extends AppCompatActivity {
30
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/";
37
38     // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
39     @SuppressLint("SetJavaScriptEnabled")
40
41     @Override
42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44         setContentView(R.layout.activity_webview);
45
46         mainWebView = (WebView) findViewById(R.id.mainWebView);
47
48         final ActionBar actionBar = getSupportActionBar();
49         if (actionBar != null) {
50             // Remove the title from the action bar.
51             actionBar.setDisplayShowTitleEnabled(false);
52
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);
56
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);
61         }
62
63         mainWebView.setWebViewClient(new WebViewClient() {
64
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.
67             @Override
68             public boolean shouldOverrideUrlLoading(WebView view, String url) {
69                 mainWebView.loadUrl(url);
70                 return true;
71             }
72
73             // Update the URL in urlTextBox when the page starts to load.
74             @Override
75             public void onPageStarted(WebView view, String url, Bitmap favicon) {
76                 urlTextBox.setText(url);
77             }
78
79             // Update formattedUrlString and urlTextBox.  It is necessary to do this after the page finishes loading because the final URL can change during load.
80             @Override
81             public void onPageFinished(WebView view, String url) {
82                 formattedUrlString = url;
83                 urlTextBox.setText(formattedUrlString);
84             }
85         });
86
87         mainWebView.setWebChromeClient(new WebChromeClient() {
88
89             // Update the progress bar when a page is loading.
90             @Override
91             public void onProgressChanged(WebView view, int progress) {
92                 progressBar.setProgress(progress);
93                 if (progress < 100) {
94                     progressBar.setVisibility(View.VISIBLE);
95                 } else {
96                     progressBar.setVisibility(View.GONE);
97                 }
98             }
99
100             // Set the favorite icon when it changes.
101             @Override
102             public void onReceivedIcon(WebView view, Bitmap icon) {
103                 favoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
104             }
105         });
106
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) {
110
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.
115                     try {
116                         loadUrlFromTextBox(mainWebView);
117                     } catch (UnsupportedEncodingException e) {
118                         e.printStackTrace();
119                     }
120                     // If the enter key was pressed, consume the event.
121                     return true;
122                 }
123                 // If any other key was pressed, do not consume the event.
124                 return false;
125             }
126         });
127
128         // Allow pinch to zoom.
129         mainWebView.getSettings().setBuiltInZoomControls(true);
130
131         // Hide zoom controls.
132         mainWebView.getSettings().setDisplayZoomControls(false);
133
134         // Enable JavaScript.
135         mainWebView.getSettings().setJavaScriptEnabled(true);
136
137         // Enable DOM Storage.
138         mainWebView.getSettings().setDomStorageEnabled(true);
139
140         // Get the intent information that started the app.
141         final Intent intent = getIntent();
142
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();
147         }
148
149         // If formattedUrlString is null assign the homepage to it.
150         if (formattedUrlString == null) {
151             formattedUrlString = homepage;
152         }
153
154         // Load the initial website.
155         mainWebView.loadUrl(formattedUrlString);
156     }
157
158     @Override
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);
162         return true;
163     }
164
165     @Override
166     public boolean onOptionsItemSelected(MenuItem menuItem) {
167         int menuItemId = menuItem.getItemId();
168
169         // Sets the commands that relate to the menu entries.
170         switch (menuItemId) {
171             case R.id.home:
172                 mainWebView.loadUrl(homepage);
173                 break;
174
175             case R.id.refresh:
176                 mainWebView.loadUrl(formattedUrlString);
177                 break;
178
179             case R.id.back:
180                 mainWebView.goBack();
181                 break;
182
183             case R.id.forward:
184                 mainWebView.goForward();
185                 break;
186         }
187
188         return super.onOptionsItemSelected(menuItem);
189     }
190
191     // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
192     @Override
193     public void onBackPressed() {
194         if (mainWebView.canGoBack()) {
195             mainWebView.goBack();
196         } else {
197             super.onBackPressed();
198         }
199     }
200
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();
206
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()) {
209
210             // Add http:// at the beginning if it is missing.  Otherwise the app will segfault.
211             if (!unformattedUrlString.startsWith("http")) {
212                 unformattedUrlString = "http://" + unformattedUrlString;
213             }
214
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.
216             try {
217                 unformattedUrl = new URL(unformattedUrlString);
218             } catch (MalformedURLException e) {
219                 e.printStackTrace();
220             }
221
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;
228
229             formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
230             formattedUrlString = formattedUri.build().toString();
231
232         } else {
233             // Sanitize the search input.
234             final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
235             formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
236         }
237
238         mainWebView.loadUrl(formattedUrlString);
239
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);
243     }
244 }