]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/Webview.java
Suppress Android Studio's SetJavaScriptEnabled warning.
[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.v4.widget.SwipeRefreshLayout;
10 import android.support.v7.app.ActionBar;
11 import android.support.v7.app.AppCompatActivity;
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.ViewTreeObserver;
18 import android.view.inputmethod.InputMethodManager;
19 import android.webkit.WebChromeClient;
20 import android.webkit.WebView;
21 import android.webkit.WebViewClient;
22 import android.widget.EditText;
23 import android.widget.ImageView;
24 import android.widget.ProgressBar;
25 import java.io.UnsupportedEncodingException;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.net.URLEncoder;
29
30 public class Webview extends AppCompatActivity {
31
32     static String formattedUrlString;
33     static WebView mainWebView;
34     static ProgressBar progressBar;
35     static SwipeRefreshLayout swipeToRefresh;
36     static EditText urlTextBox;
37     static ImageView favoriteIcon;
38     static final String homepage = "https://www.duckduckgo.com";
39
40     // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
41     @SuppressLint("SetJavaScriptEnabled")
42
43     @Override
44     protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         setContentView(R.layout.activity_webview);
47
48         urlTextBox = (EditText) findViewById(R.id.urlTextBox);
49         swipeToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayoutContainer);
50         mainWebView = (WebView) findViewById(R.id.mainWebView);
51         progressBar = (ProgressBar) findViewById(R.id.progressBar);
52         favoriteIcon = (ImageView) findViewById(R.id.favoriteIcon);
53
54         // Remove the title from the action bar.
55         final ActionBar actionBar = getSupportActionBar();
56         if (actionBar != null) {
57             actionBar.setDisplayShowTitleEnabled(false);
58             // actionBar.setHideOnContentScrollEnabled(true);
59         }
60
61         // Implement swipe down to refresh.
62         swipeToRefresh.setColorSchemeColors(0xFF0097FF);
63         swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
64             @Override
65             public void onRefresh() {
66                 mainWebView.loadUrl(formattedUrlString);
67             }
68         });
69
70         // Only enable swipeToRefresh if is mainWebView is scrolled to the top.
71         mainWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
72             @Override
73             public void onScrollChanged() {
74                 if (mainWebView.getScrollY() == 0) {
75                     swipeToRefresh.setEnabled(true);
76                 } else {
77                     swipeToRefresh.setEnabled(false);
78                 }
79             }
80         });
81
82         mainWebView.setWebViewClient(new WebViewClient() {
83
84             // setWebViewClient makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
85             // Save the URL to formattedUrlString and update urlTextBox before loading mainWebView.
86             @Override
87             public boolean shouldOverrideUrlLoading(WebView view, String url) {
88                 mainWebView.loadUrl(url);
89                 return true;
90             }
91
92             // Update the URL in urlTextBox when the page starts to load.
93             @Override
94             public void onPageStarted(WebView view, String url, Bitmap favicon) {
95                 urlTextBox.setText(url);
96             }
97
98             // Update formattedUrlString and urlTextBox.  It is necessary to do this after the page finishes loading because the final URL can change during load.
99             @Override
100             public void onPageFinished(WebView view, String url) {
101                 formattedUrlString = url;
102                 urlTextBox.setText(formattedUrlString);
103             }
104         });
105
106         mainWebView.setWebChromeClient(new WebChromeClient() {
107
108             // Update the progress bar when a page is loading.
109             @Override
110             public void onProgressChanged(WebView view, int progress) {
111                 progressBar.setProgress(progress);
112                 if (progress < 100) {
113                     progressBar.setVisibility(View.VISIBLE);
114                 } else {
115                     progressBar.setVisibility(View.GONE);
116
117                     // Stop the refreshing indicator if it is running.
118                     swipeToRefresh.setRefreshing(false);
119                 }
120             }
121
122             // Set the favorite icon when it changes.
123             @Override
124             public void onReceivedIcon(WebView view, Bitmap icon) {
125                 favoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
126             }
127         });
128
129         // Set the "go" button on the keyboard to load the URL.
130         urlTextBox.setOnKeyListener(new View.OnKeyListener() {
131             public boolean onKey(View v, int keyCode, KeyEvent event) {
132
133                 // If the event is a key-down event on the "enter" button, load the URL.
134                 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
135                         (keyCode == KeyEvent.KEYCODE_ENTER)) {
136                     // Load the URL into the mainWebView and consume the event.
137                     try {
138                         loadUrlFromTextBox(mainWebView);
139                     } catch (UnsupportedEncodingException e) {
140                         e.printStackTrace();
141                     }
142                     // If the enter key was pressed, consume the event.
143                     return true;
144                 }
145                 // If any other key was pressed, do not consume the event.
146                 return false;
147             }
148         });
149
150         // Allow pinch to zoom.
151         mainWebView.getSettings().setBuiltInZoomControls(true);
152
153         // Hide zoom controls.
154         mainWebView.getSettings().setDisplayZoomControls(false);
155
156         // Enable JavaScript.
157         mainWebView.getSettings().setJavaScriptEnabled(true);
158
159         // Enable DOM Storage.
160         mainWebView.getSettings().setDomStorageEnabled(true);
161
162         // Get the intent information that started the app.
163         final Intent intent = getIntent();
164
165         if (intent.getData() != null) {
166             // Get the intent data and convert it to a string.
167             final Uri intentUriData = intent.getData();
168             formattedUrlString = intentUriData.toString();
169         }
170
171         // If formattedUrlString is null assign the homepage to it.
172         if (formattedUrlString == null) {
173             formattedUrlString = homepage;
174         }
175
176         // Load the initial website.
177         mainWebView.loadUrl(formattedUrlString);
178     }
179
180     @Override
181     public boolean onCreateOptionsMenu(Menu menu) {
182         // Inflate the menu; this adds items to the action bar if it is present.
183         getMenuInflater().inflate(R.menu.menu_webview, menu);
184         return true;
185     }
186
187     @Override
188     public boolean onOptionsItemSelected(MenuItem menuItem) {
189         int menuItemId = menuItem.getItemId();
190
191         // Sets the commands that relate to the menu entries.
192         switch (menuItemId) {
193             case R.id.home:
194                 mainWebView.loadUrl(homepage);
195                 break;
196
197             case R.id.back:
198                 mainWebView.goBack();
199                 break;
200
201             case R.id.forward:
202                 mainWebView.goForward();
203                 break;
204         }
205
206         return super.onOptionsItemSelected(menuItem);
207     }
208
209     // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
210     @Override
211     public void onBackPressed() {
212         if (mainWebView.canGoBack()) {
213             mainWebView.goBack();
214         } else {
215             super.onBackPressed();
216         }
217     }
218
219     public void loadUrlFromTextBox(View view) throws UnsupportedEncodingException {
220         // Get the text from urlTextInput and convert it to a string.
221         String unformattedUrlString = urlTextBox.getText().toString();
222         URL unformattedUrl = null;
223         Uri.Builder formattedUri = new Uri.Builder();
224
225         // Check to see if unformattedUrlString is a valid URL.  Otherwise, convert it into a Duck Duck Go search.
226         if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
227
228             // Add http:// at the beginning if it is missing.  Otherwise the app will segfault.
229             if (!unformattedUrlString.startsWith("http")) {
230                 unformattedUrlString = "http://" + unformattedUrlString;
231             }
232
233             // 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.
234             try {
235                 unformattedUrl = new URL(unformattedUrlString);
236             } catch (MalformedURLException e) {
237                 e.printStackTrace();
238             }
239
240             // The ternary operator (? :) makes sure that unformattedUrl.get() does not cause a null pointer exception.
241             final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
242             final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
243             final String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
244             final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
245             final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
246
247             formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
248             formattedUrlString = formattedUri.build().toString();
249
250         } else {
251             // Sanitize the search input.
252             final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
253             formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
254         }
255
256         mainWebView.loadUrl(formattedUrlString);
257
258         // Hides the keyboard so we can see the webpage.
259         InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
260         inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
261     }
262 }