]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/Webview.java
Implement setWebViewClient to allow Privacy Browser to follow links.
[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.support.v7.app.ActionBarActivity;
7 import android.os.Bundle;
8 import android.view.KeyEvent;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.inputmethod.InputMethodManager;
13 import android.webkit.WebView;
14 import android.webkit.WebViewClient;
15 import android.widget.EditText;
16
17 import java.net.URL;
18
19
20 public class Webview extends ActionBarActivity {
21
22     static String formattedUrlString;
23     static WebView mainWebView;
24     static final String homepage = "https://www.duckduckgo.com";
25
26     @Override
27     protected void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.activity_webview);
30
31         final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
32         mainWebView = (WebView) findViewById(R.id.mainWebView);
33
34         // setWebViewClient makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
35         mainWebView.setWebViewClient(new WebViewClient());
36
37         // Allow pinch to zoom.
38         mainWebView.getSettings().setBuiltInZoomControls(true);
39
40         // Hide zoom controls.
41         mainWebView.getSettings().setDisplayZoomControls(false);
42
43         // Enable JavaScript.
44         mainWebView.getSettings().setJavaScriptEnabled(true);
45
46         // Enable DOM Storage.
47         mainWebView.getSettings().setDomStorageEnabled(true);
48
49         // Get the intent information that started the app.
50         final Intent intent = getIntent();
51
52         if (intent.getData() != null) {
53             // Get the intent data.
54             final Uri intentUriData = intent.getData();
55
56             // Try to parse the intent data and store it in urlData.
57             URL urlData = null;
58             try {
59                 urlData = new URL(intentUriData.getScheme(), intentUriData.getHost(), intentUriData.getPath());
60             } catch (Exception e) {
61                 e.printStackTrace();
62             }
63
64             Webview.formattedUrlString = urlData.toString();
65         }
66
67         // If formattedUrlString is null assign the homepage to it.
68         if (formattedUrlString == null) {
69             formattedUrlString = homepage;
70         }
71
72         // Place the formattedUrlString in the address bar and load the website.
73         urlTextBox.setText(formattedUrlString);
74         mainWebView.loadUrl(formattedUrlString);
75
76         // Set the "go" button on the keyboard to load the URL.
77         urlTextBox.setOnKeyListener(new View.OnKeyListener() {
78             public boolean onKey(View v, int keyCode, KeyEvent event) {
79                 // If the event is a key-down event on the "enter" button
80                 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
81                         (keyCode == KeyEvent.KEYCODE_ENTER)) {
82                     // Load the URL into the mainWebView and consume the event.
83                     loadUrl(mainWebView);
84                     return true;
85                 }
86                 // Do not consume the event.
87                 return false;
88             }
89         });
90
91     }
92
93     @Override
94     public boolean onCreateOptionsMenu(Menu menu) {
95         // Inflate the menu; this adds items to the action bar if it is present.
96         getMenuInflater().inflate(R.menu.menu_webview, menu);
97         return true;
98     }
99
100     @Override
101     public boolean onOptionsItemSelected(MenuItem menuItem) {
102         int menuItemId = menuItem.getItemId();
103         final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
104
105         // Use the menu items to go forward or back.
106         switch (menuItemId) {
107             case R.id.back:
108                 mainWebView.goBack();
109                 break;
110             case R.id.forward:
111                 mainWebView.goForward();
112                 break;
113         }
114
115         return super.onOptionsItemSelected(menuItem);
116     }
117
118     // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
119     @Override
120     public void onBackPressed() {
121         if (mainWebView.canGoBack()) {
122             mainWebView.goBack();
123         } else {
124             super.onBackPressed();
125         }
126     }
127
128     public void loadUrl(View view) {
129         // Get the text from urlTextInput and convert it to a string.
130         final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
131         final String unformattedUrlString = urlTextBox.getText().toString();
132
133         // Don't do anything unless unformattedUrlString is at least 6 characters long.
134         if (unformattedUrlString.length() < 6) { return; }
135
136         // Add correct protocol formatting to the beginning of the URL if needed.
137         final String firstSixCharacters = unformattedUrlString.substring(0, 6);
138
139         switch (firstSixCharacters) {
140             case "http:/":
141                 formattedUrlString = unformattedUrlString;
142                 break;
143             case "https:":
144                 formattedUrlString = unformattedUrlString;
145                 break;
146             case "ftp://":
147                 formattedUrlString = unformattedUrlString;
148                 break;
149             default:
150                 formattedUrlString = "http://" + unformattedUrlString;
151         }
152
153         /*
154         // Parse the unformattedURLString into a Uri.
155
156         final Uri uriData = Uri.parse(unformattedUrlString);
157
158         // Convert the Uri to a URL, chicking for any problems with the formatting.
159         URL urlData = null;
160
161         try {
162             urlData = new URL(uriData.getScheme(), uriData.getHost(), uriData.getPath());
163         } catch (Exception e) {
164             e.printStackTrace();
165         }
166
167         //  Convert urlData to a string, which is reqauired by loadUrl method.
168         formattedUrlString = urlData.toString();
169         */
170
171         final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
172
173         // Place the URL text back in the address bar and load the website.
174         urlTextBox.setText(formattedUrlString);
175         mainWebView.loadUrl(formattedUrlString);
176
177         // Hides the keyboard so we can see the webpage.
178         InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
179         inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
180     }
181 }