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