1 package com.stoutner.privacybrowser;
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;
22 import java.net.URLEncoder;
24 public class Webview extends AppCompatActivity {
26 static String formattedUrlString;
27 static WebView mainWebView;
28 static ProgressBar progressBar;
29 static final String homepage = "https://www.duckduckgo.com";
32 protected void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.activity_webview);
36 final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
37 mainWebView = (WebView) findViewById(R.id.mainWebView);
38 progressBar = (ProgressBar) findViewById(R.id.progressBar);
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);
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);
56 progressBar.setVisibility(View.VISIBLE);
58 progressBar.setVisibility(View.GONE);
63 // Allow pinch to zoom.
64 mainWebView.getSettings().setBuiltInZoomControls(true);
66 // Hide zoom controls.
67 mainWebView.getSettings().setDisplayZoomControls(false);
70 mainWebView.getSettings().setJavaScriptEnabled(true);
72 // Enable DOM Storage.
73 mainWebView.getSettings().setDomStorageEnabled(true);
75 // Get the intent information that started the app.
76 final Intent intent = getIntent();
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();
84 // If formattedUrlString is null assign the homepage to it.
85 if (formattedUrlString == null) {
86 formattedUrlString = homepage;
89 // Place the formattedUrlString in the address bar and load the website.
90 urlTextBox.setText(formattedUrlString);
91 mainWebView.loadUrl(formattedUrlString);
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.
101 loadUrlFromTextBox(mainWebView);
102 } catch (UnsupportedEncodingException e) {
107 // Do not consume the event.
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);
122 public boolean onOptionsItemSelected(MenuItem menuItem) {
123 int menuItemId = menuItem.getItemId();
124 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
126 // Sets the commands that relate to the menu entries.
127 switch (menuItemId) {
129 mainWebView.loadUrl(homepage);
132 mainWebView.goBack();
135 mainWebView.goForward();
139 return super.onOptionsItemSelected(menuItem);
142 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
144 public void onBackPressed() {
145 if (mainWebView.canGoBack()) {
146 mainWebView.goBack();
148 super.onBackPressed();
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();
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()) {
164 // Add http:// at the beginning if it is missing. Otherwise the app will segfault.
165 if (!unformattedUrlString.startsWith("http")) {
166 unformattedUrlString = "http://" + unformattedUrlString;
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.
171 unformattedUrl = new URL(unformattedUrlString);
172 } catch (MalformedURLException e) {
176 if (unformattedUrl.getProtocol() != null) {
177 scheme = unformattedUrl.getProtocol();
182 final String authority = unformattedUrl.getAuthority();
183 final String path = unformattedUrl.getPath();
184 final String query = unformattedUrl.getQuery();
185 final String fragment = unformattedUrl.getRef();
187 formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
188 formattedUrlString = formattedUri.build().toString();
191 // Sanitize the search input.
192 final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
193 formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
196 // Place formattedUrlString back in the address bar and load the website.
197 urlTextBox.setText(formattedUrlString);
198 mainWebView.loadUrl(formattedUrlString);
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);