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