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