1 package com.stoutner.privacybrowser;
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;
28 import java.net.URLEncoder;
30 public class Webview extends AppCompatActivity {
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";
40 // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
41 @SuppressLint("SetJavaScriptEnabled")
44 protected void onCreate(Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
46 setContentView(R.layout.activity_webview);
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);
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);
61 // Implement swipe down to refresh.
62 swipeToRefresh.setColorSchemeColors(0xFF0097FF);
63 swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
65 public void onRefresh() {
66 mainWebView.loadUrl(formattedUrlString);
70 // Only enable swipeToRefresh if is mainWebView is scrolled to the top.
71 mainWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
73 public void onScrollChanged() {
74 if (mainWebView.getScrollY() == 0) {
75 swipeToRefresh.setEnabled(true);
77 swipeToRefresh.setEnabled(false);
82 mainWebView.setWebViewClient(new WebViewClient() {
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.
87 public boolean shouldOverrideUrlLoading(WebView view, String url) {
88 mainWebView.loadUrl(url);
92 // Update the URL in urlTextBox when the page starts to load.
94 public void onPageStarted(WebView view, String url, Bitmap favicon) {
95 urlTextBox.setText(url);
98 // Update formattedUrlString and urlTextBox. It is necessary to do this after the page finishes loading because the final URL can change during load.
100 public void onPageFinished(WebView view, String url) {
101 formattedUrlString = url;
102 urlTextBox.setText(formattedUrlString);
106 mainWebView.setWebChromeClient(new WebChromeClient() {
108 // Update the progress bar when a page is loading.
110 public void onProgressChanged(WebView view, int progress) {
111 progressBar.setProgress(progress);
112 if (progress < 100) {
113 progressBar.setVisibility(View.VISIBLE);
115 progressBar.setVisibility(View.GONE);
117 // Stop the refreshing indicator if it is running.
118 swipeToRefresh.setRefreshing(false);
122 // Set the favorite icon when it changes.
124 public void onReceivedIcon(WebView view, Bitmap icon) {
125 favoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
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) {
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.
138 loadUrlFromTextBox(mainWebView);
139 } catch (UnsupportedEncodingException e) {
142 // If the enter key was pressed, consume the event.
145 // If any other key was pressed, do not consume the event.
150 // Allow pinch to zoom.
151 mainWebView.getSettings().setBuiltInZoomControls(true);
153 // Hide zoom controls.
154 mainWebView.getSettings().setDisplayZoomControls(false);
156 // Enable JavaScript.
157 mainWebView.getSettings().setJavaScriptEnabled(true);
159 // Enable DOM Storage.
160 mainWebView.getSettings().setDomStorageEnabled(true);
162 // Get the intent information that started the app.
163 final Intent intent = getIntent();
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();
171 // If formattedUrlString is null assign the homepage to it.
172 if (formattedUrlString == null) {
173 formattedUrlString = homepage;
176 // Load the initial website.
177 mainWebView.loadUrl(formattedUrlString);
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);
188 public boolean onOptionsItemSelected(MenuItem menuItem) {
189 int menuItemId = menuItem.getItemId();
191 // Sets the commands that relate to the menu entries.
192 switch (menuItemId) {
194 mainWebView.loadUrl(homepage);
198 mainWebView.goBack();
202 mainWebView.goForward();
206 return super.onOptionsItemSelected(menuItem);
209 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
211 public void onBackPressed() {
212 if (mainWebView.canGoBack()) {
213 mainWebView.goBack();
215 super.onBackPressed();
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();
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()) {
228 // Add http:// at the beginning if it is missing. Otherwise the app will segfault.
229 if (!unformattedUrlString.startsWith("http")) {
230 unformattedUrlString = "http://" + unformattedUrlString;
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.
235 unformattedUrl = new URL(unformattedUrlString);
236 } catch (MalformedURLException e) {
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;
247 formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
248 formattedUrlString = formattedUri.build().toString();
251 // Sanitize the search input.
252 final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8");
253 formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
256 mainWebView.loadUrl(formattedUrlString);
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);