1 package com.stoutner.privacybrowser;
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.graphics.Bitmap;
6 import android.net.Uri;
7 import android.os.Bundle;
8 import android.support.v4.widget.SwipeRefreshLayout;
9 import android.support.v7.app.ActionBar;
10 import android.support.v7.app.AppCompatActivity;
11 import android.util.Patterns;
12 import android.view.KeyEvent;
13 import android.view.Menu;
14 import android.view.MenuItem;
15 import android.view.View;
16 import android.view.ViewTreeObserver;
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 SwipeRefreshLayout swipeToRefresh;
35 static EditText urlTextBox;
36 static ImageView favoriteIcon;
37 static final String homepage = "https://www.duckduckgo.com";
40 protected void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 setContentView(R.layout.activity_webview);
44 urlTextBox = (EditText) findViewById(R.id.urlTextBox);
45 swipeToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayoutContainer);
46 mainWebView = (WebView) findViewById(R.id.mainWebView);
47 progressBar = (ProgressBar) findViewById(R.id.progressBar);
48 favoriteIcon = (ImageView) findViewById(R.id.favoriteIcon);
50 // Remove the title from the action bar.
51 final ActionBar actionBar = getSupportActionBar();
52 if (actionBar != null) {
53 actionBar.setDisplayShowTitleEnabled(false);
54 // actionBar.setHideOnContentScrollEnabled(true);
57 // Implement swipe down to refresh.
58 swipeToRefresh.setColorSchemeColors(0xFF0097FF);
59 swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
61 public void onRefresh() {
62 mainWebView.loadUrl(formattedUrlString);
66 // Only enable swipeToRefresh if is mainWebView is scrolled to the top.
67 mainWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
69 public void onScrollChanged() {
70 if (mainWebView.getScrollY() == 0) {
71 swipeToRefresh.setEnabled(true);
73 swipeToRefresh.setEnabled(false);
78 mainWebView.setWebViewClient(new WebViewClient() {
80 // setWebViewClient makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
81 // Save the URL to formattedUrlString and update urlTextBox before loading mainWebView.
83 public boolean shouldOverrideUrlLoading(WebView view, String url) {
84 mainWebView.loadUrl(url);
88 // Update the URL in urlTextBox when the page starts to load.
90 public void onPageStarted(WebView view, String url, Bitmap favicon) {
91 urlTextBox.setText(url);
94 // Update formattedUrlString and urlTextBox. It is necessary to do this after the page finishes loading because the final URL can change during load.
96 public void onPageFinished(WebView view, String url) {
97 formattedUrlString = url;
98 urlTextBox.setText(formattedUrlString);
102 mainWebView.setWebChromeClient(new WebChromeClient() {
104 // Update the progress bar when a page is loading.
106 public void onProgressChanged(WebView view, int progress) {
107 progressBar.setProgress(progress);
108 if (progress < 100) {
109 progressBar.setVisibility(View.VISIBLE);
111 progressBar.setVisibility(View.GONE);
113 // Stop the refreshing indicator if it is running.
114 swipeToRefresh.setRefreshing(false);
118 // Set the favorite icon when it changes.
120 public void onReceivedIcon(WebView view, Bitmap icon) {
121 favoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
125 // Set the "go" button on the keyboard to load the URL.
126 urlTextBox.setOnKeyListener(new View.OnKeyListener() {
127 public boolean onKey(View v, int keyCode, KeyEvent event) {
129 // If the event is a key-down event on the "enter" button, load the URL.
130 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
131 (keyCode == KeyEvent.KEYCODE_ENTER)) {
132 // Load the URL into the mainWebView and consume the event.
134 loadUrlFromTextBox(mainWebView);
135 } catch (UnsupportedEncodingException e) {
138 // If the enter key was pressed, consume the event.
141 // If any other key was pressed, do not consume the event.
146 // Allow pinch to zoom.
147 mainWebView.getSettings().setBuiltInZoomControls(true);
149 // Hide zoom controls.
150 mainWebView.getSettings().setDisplayZoomControls(false);
152 // Enable JavaScript.
153 mainWebView.getSettings().setJavaScriptEnabled(true);
155 // Enable DOM Storage.
156 mainWebView.getSettings().setDomStorageEnabled(true);
158 // Get the intent information that started the app.
159 final Intent intent = getIntent();
161 if (intent.getData() != null) {
162 // Get the intent data and convert it to a string.
163 final Uri intentUriData = intent.getData();
164 formattedUrlString = intentUriData.toString();
167 // If formattedUrlString is null assign the homepage to it.
168 if (formattedUrlString == null) {
169 formattedUrlString = homepage;
172 // Load the initial website.
173 mainWebView.loadUrl(formattedUrlString);
177 public boolean onCreateOptionsMenu(Menu menu) {
178 // Inflate the menu; this adds items to the action bar if it is present.
179 getMenuInflater().inflate(R.menu.menu_webview, menu);
184 public boolean onOptionsItemSelected(MenuItem menuItem) {
185 int menuItemId = menuItem.getItemId();
187 // Sets the commands that relate to the menu entries.
188 switch (menuItemId) {
190 mainWebView.loadUrl(homepage);
194 mainWebView.goBack();
198 mainWebView.goForward();
202 return super.onOptionsItemSelected(menuItem);
205 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
207 public void onBackPressed() {
208 if (mainWebView.canGoBack()) {
209 mainWebView.goBack();
211 super.onBackPressed();
215 public void loadUrlFromTextBox(View view) throws UnsupportedEncodingException {
216 // Get the text from urlTextInput and convert it to a string.
217 String unformattedUrlString = urlTextBox.getText().toString();
218 URL unformattedUrl = null;
219 Uri.Builder formattedUri = new Uri.Builder();
222 // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search.
223 if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
225 // Add http:// at the beginning if it is missing. Otherwise the app will segfault.
226 if (!unformattedUrlString.startsWith("http")) {
227 unformattedUrlString = "http://" + unformattedUrlString;
230 // 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.
232 unformattedUrl = new URL(unformattedUrlString);
233 } catch (MalformedURLException e) {
237 if (unformattedUrl.getProtocol() != null) {
238 scheme = unformattedUrl.getProtocol();
243 final String authority = unformattedUrl.getAuthority();
244 final String path = unformattedUrl.getPath();
245 final String query = unformattedUrl.getQuery();
246 final String fragment = unformattedUrl.getRef();
248 formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
249 formattedUrlString = formattedUri.build().toString();
252 // Sanitize the search input.
253 final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
254 formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
257 // Place formattedUrlString back in the address bar and load the website.
258 mainWebView.loadUrl(formattedUrlString);
260 // Hides the keyboard so we can see the webpage.
261 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
262 inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);