1 package com.stoutner.privacybrowser;
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;
19 public class Webview extends ActionBarActivity {
21 static String formattedUrlString;
22 static WebView mainWebView;
23 static final String homepage = "https://www.duckduckgo.com";
26 protected void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.activity_webview);
30 final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
31 mainWebView = (WebView) findViewById(R.id.mainWebView);
33 // Allow pinch to zoom.
34 mainWebView.getSettings().setBuiltInZoomControls(true);
36 // Hide zoom controls.
37 mainWebView.getSettings().setDisplayZoomControls(false);
40 mainWebView.getSettings().setJavaScriptEnabled(true);
42 // Enable DOM Storage.
43 mainWebView.getSettings().setDomStorageEnabled(true);
45 // Get the intent information that started the app.
46 final Intent intent = getIntent();
48 if (intent.getData() != null) {
49 // Get the intent data.
50 final Uri intentUriData = intent.getData();
52 // Try to parse the intent data and store it in urlData.
55 urlData = new URL(intentUriData.getScheme(), intentUriData.getHost(), intentUriData.getPath());
56 } catch (Exception e) {
60 Webview.formattedUrlString = urlData.toString();
63 // If formattedUrlString is null assign the homepage to it.
64 if (formattedUrlString == null) {
65 formattedUrlString = homepage;
68 // Place the formattedUrlString in the address bar and load the website.
69 urlTextBox.setText(formattedUrlString);
70 mainWebView.loadUrl(formattedUrlString);
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.
82 // Do not consume the event.
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);
97 public boolean onOptionsItemSelected(MenuItem menuItem) {
98 int menuItemId = menuItem.getItemId();
99 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
101 // Use the menu items to go forward or back.
102 switch (menuItemId) {
104 mainWebView.goBack();
107 mainWebView.goForward();
111 return super.onOptionsItemSelected(menuItem);
114 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
116 public void onBackPressed() {
117 if (mainWebView.canGoBack()) {
118 mainWebView.goBack();
120 super.onBackPressed();
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();
129 // Don't do anything unless unformattedUrlString is at least 6 characters long.
130 if (unformattedUrlString.length() < 6) { return; }
132 // Add correct protocol formatting to the beginning of the URL if needed.
133 final String firstSixCharacters = unformattedUrlString.substring(0, 6);
135 switch (firstSixCharacters) {
137 formattedUrlString = unformattedUrlString;
140 formattedUrlString = unformattedUrlString;
143 formattedUrlString = unformattedUrlString;
146 formattedUrlString = "http://" + unformattedUrlString;
150 // Parse the unformattedURLString into a Uri.
152 final Uri uriData = Uri.parse(unformattedUrlString);
154 // Convert the Uri to a URL, chicking for any problems with the formatting.
158 urlData = new URL(uriData.getScheme(), uriData.getHost(), uriData.getPath());
159 } catch (Exception e) {
163 // Convert urlData to a string, which is reqauired by loadUrl method.
164 formattedUrlString = urlData.toString();
167 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
169 // Place the URL text back in the address bar and load the website.
170 urlTextBox.setText(formattedUrlString);
171 mainWebView.loadUrl(formattedUrlString);
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);