]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/Webview.java
Allow search from the URL bar.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / Webview.java
1 package com.stoutner.privacybrowser;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.net.Uri;
6 import android.os.Bundle;
7 import android.support.v7.app.AppCompatActivity;
8 import android.util.Patterns;
9 import android.view.KeyEvent;
10 import android.view.Menu;
11 import android.view.MenuItem;
12 import android.view.View;
13 import android.view.inputmethod.InputMethodManager;
14 import android.webkit.WebChromeClient;
15 import android.webkit.WebView;
16 import android.webkit.WebViewClient;
17 import android.widget.EditText;
18 import android.widget.ProgressBar;
19 import java.io.UnsupportedEncodingException;
20 import java.net.MalformedURLException;
21 import java.net.URL;
22 import java.net.URLEncoder;
23
24 public class Webview extends AppCompatActivity {
25
26     static String formattedUrlString;
27     static WebView mainWebView;
28     static ProgressBar progressBar;
29     static final String homepage = "https://www.duckduckgo.com";
30
31     @Override
32     protected void onCreate(Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34         setContentView(R.layout.activity_webview);
35
36         final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
37         mainWebView = (WebView) findViewById(R.id.mainWebView);
38         progressBar = (ProgressBar) findViewById(R.id.progressBar);
39
40         // setWebViewClient makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
41         // Save the URL to formattedUrlString and update urlTextBox before loading mainWebView.
42         mainWebView.setWebViewClient(new WebViewClient() {
43             public boolean shouldOverrideUrlLoading(WebView view, String url) {
44                 formattedUrlString=url;
45                 urlTextBox.setText(formattedUrlString);
46                 mainWebView.loadUrl(formattedUrlString);
47                 return true;
48             }
49         });
50
51         // Update the progress bar when a page is loading.
52         mainWebView.setWebChromeClient(new WebChromeClient() {
53             public void onProgressChanged(WebView view, int progress) {
54                 progressBar.setProgress(progress);
55                 if (progress < 100) {
56                     progressBar.setVisibility(View.VISIBLE);
57                 } else {
58                     progressBar.setVisibility(View.GONE);
59                 }
60             }
61         });
62
63         // Allow pinch to zoom.
64         mainWebView.getSettings().setBuiltInZoomControls(true);
65
66         // Hide zoom controls.
67         mainWebView.getSettings().setDisplayZoomControls(false);
68
69         // Enable JavaScript.
70         mainWebView.getSettings().setJavaScriptEnabled(true);
71
72         // Enable DOM Storage.
73         mainWebView.getSettings().setDomStorageEnabled(true);
74
75         // Get the intent information that started the app.
76         final Intent intent = getIntent();
77
78         if (intent.getData() != null) {
79             // Get the intent data and convert it to a string.
80             final Uri intentUriData = intent.getData();
81             Webview.formattedUrlString = intentUriData.toString();
82         }
83
84         // If formattedUrlString is null assign the homepage to it.
85         if (formattedUrlString == null) {
86             formattedUrlString = homepage;
87         }
88
89         // Place the formattedUrlString in the address bar and load the website.
90         urlTextBox.setText(formattedUrlString);
91         mainWebView.loadUrl(formattedUrlString);
92
93         // Set the "go" button on the keyboard to load the URL.
94         urlTextBox.setOnKeyListener(new View.OnKeyListener() {
95             public boolean onKey(View v, int keyCode, KeyEvent event) {
96                 // If the event is a key-down event on the "enter" button
97                 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
98                         (keyCode == KeyEvent.KEYCODE_ENTER)) {
99                     // Load the URL into the mainWebView and consume the event.
100                     try {
101                         loadUrlFromTextBox(mainWebView);
102                     } catch (UnsupportedEncodingException e) {
103                         e.printStackTrace();
104                     }
105                     return true;
106                 }
107                 // Do not consume the event.
108                 return false;
109             }
110         });
111
112     }
113
114     @Override
115     public boolean onCreateOptionsMenu(Menu menu) {
116         // Inflate the menu; this adds items to the action bar if it is present.
117         getMenuInflater().inflate(R.menu.menu_webview, menu);
118         return true;
119     }
120
121     @Override
122     public boolean onOptionsItemSelected(MenuItem menuItem) {
123         int menuItemId = menuItem.getItemId();
124         final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
125
126         // Sets the commands that relate to the menu entries.
127         switch (menuItemId) {
128             case R.id.home:
129                 mainWebView.loadUrl(homepage);
130                 break;
131             case R.id.back:
132                 mainWebView.goBack();
133                 break;
134             case R.id.forward:
135                 mainWebView.goForward();
136                 break;
137         }
138
139         return super.onOptionsItemSelected(menuItem);
140     }
141
142     // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
143     @Override
144     public void onBackPressed() {
145         if (mainWebView.canGoBack()) {
146             mainWebView.goBack();
147         } else {
148             super.onBackPressed();
149         }
150     }
151
152     public void loadUrlFromTextBox(View view) throws UnsupportedEncodingException {
153         // Get the text from urlTextInput and convert it to a string.
154         final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
155         final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
156         String unformattedUrlString = urlTextBox.getText().toString();
157         URL unformattedUrl = null;
158         Uri.Builder formattedUri = new Uri.Builder();
159         String scheme;
160
161         // Check to see if unformattedUrlString is a valid URL.  Otherwise, convert it into a Duck Duck Go search.
162         if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
163
164             // Add http:// at the beginning if it is missing.  Otherwise the app will segfault.
165             if (!unformattedUrlString.startsWith("http")) {
166                 unformattedUrlString = "http://" + unformattedUrlString;
167             }
168
169             // 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.
170             try {
171                 unformattedUrl = new URL(unformattedUrlString);
172             } catch (MalformedURLException e) {
173                 e.printStackTrace();
174             }
175
176             if (unformattedUrl.getProtocol() != null) {
177                 scheme = unformattedUrl.getProtocol();
178             } else {
179                 scheme = "http";
180             }
181
182             final String authority = unformattedUrl.getAuthority();
183             final String path = unformattedUrl.getPath();
184             final String query = unformattedUrl.getQuery();
185             final String fragment = unformattedUrl.getRef();
186
187             formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
188             formattedUrlString = formattedUri.build().toString();
189
190         } else {
191             // Sanitize the search input.
192             final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
193             formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
194         }
195
196         // Place formattedUrlString back in the address bar and load the website.
197         urlTextBox.setText(formattedUrlString);
198         mainWebView.loadUrl(formattedUrlString);
199
200         // Hides the keyboard so we can see the webpage.
201         InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
202         inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
203     }
204 }