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