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.webkit.WebViewClient;
15 import android.widget.EditText;
20 public class Webview extends ActionBarActivity {
22 static String formattedUrlString;
23 static WebView mainWebView;
24 static final String homepage = "https://www.duckduckgo.com";
27 protected void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 setContentView(R.layout.activity_webview);
31 final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
32 mainWebView = (WebView) findViewById(R.id.mainWebView);
34 // setWebViewClient makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
35 mainWebView.setWebViewClient(new WebViewClient());
37 // Allow pinch to zoom.
38 mainWebView.getSettings().setBuiltInZoomControls(true);
40 // Hide zoom controls.
41 mainWebView.getSettings().setDisplayZoomControls(false);
44 mainWebView.getSettings().setJavaScriptEnabled(true);
46 // Enable DOM Storage.
47 mainWebView.getSettings().setDomStorageEnabled(true);
49 // Get the intent information that started the app.
50 final Intent intent = getIntent();
52 if (intent.getData() != null) {
53 // Get the intent data.
54 final Uri intentUriData = intent.getData();
56 // Try to parse the intent data and store it in urlData.
59 urlData = new URL(intentUriData.getScheme(), intentUriData.getHost(), intentUriData.getPath());
60 } catch (Exception e) {
64 Webview.formattedUrlString = urlData.toString();
67 // If formattedUrlString is null assign the homepage to it.
68 if (formattedUrlString == null) {
69 formattedUrlString = homepage;
72 // Place the formattedUrlString in the address bar and load the website.
73 urlTextBox.setText(formattedUrlString);
74 mainWebView.loadUrl(formattedUrlString);
76 // Set the "go" button on the keyboard to load the URL.
77 urlTextBox.setOnKeyListener(new View.OnKeyListener() {
78 public boolean onKey(View v, int keyCode, KeyEvent event) {
79 // If the event is a key-down event on the "enter" button
80 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
81 (keyCode == KeyEvent.KEYCODE_ENTER)) {
82 // Load the URL into the mainWebView and consume the event.
86 // Do not consume the event.
94 public boolean onCreateOptionsMenu(Menu menu) {
95 // Inflate the menu; this adds items to the action bar if it is present.
96 getMenuInflater().inflate(R.menu.menu_webview, menu);
101 public boolean onOptionsItemSelected(MenuItem menuItem) {
102 int menuItemId = menuItem.getItemId();
103 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
105 // Use the menu items to go forward or back.
106 switch (menuItemId) {
108 mainWebView.goBack();
111 mainWebView.goForward();
115 return super.onOptionsItemSelected(menuItem);
118 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
120 public void onBackPressed() {
121 if (mainWebView.canGoBack()) {
122 mainWebView.goBack();
124 super.onBackPressed();
128 public void loadUrl(View view) {
129 // Get the text from urlTextInput and convert it to a string.
130 final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
131 final String unformattedUrlString = urlTextBox.getText().toString();
133 // Don't do anything unless unformattedUrlString is at least 6 characters long.
134 if (unformattedUrlString.length() < 6) { return; }
136 // Add correct protocol formatting to the beginning of the URL if needed.
137 final String firstSixCharacters = unformattedUrlString.substring(0, 6);
139 switch (firstSixCharacters) {
141 formattedUrlString = unformattedUrlString;
144 formattedUrlString = unformattedUrlString;
147 formattedUrlString = unformattedUrlString;
150 formattedUrlString = "http://" + unformattedUrlString;
154 // Parse the unformattedURLString into a Uri.
156 final Uri uriData = Uri.parse(unformattedUrlString);
158 // Convert the Uri to a URL, chicking for any problems with the formatting.
162 urlData = new URL(uriData.getScheme(), uriData.getHost(), uriData.getPath());
163 } catch (Exception e) {
167 // Convert urlData to a string, which is reqauired by loadUrl method.
168 formattedUrlString = urlData.toString();
171 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
173 // Place the URL text back in the address bar and load the website.
174 urlTextBox.setText(formattedUrlString);
175 mainWebView.loadUrl(formattedUrlString);
177 // Hides the keyboard so we can see the webpage.
178 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
179 inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);