1 package com.stoutner.privacybrowser;
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;
22 public class Webview extends AppCompatActivity {
24 static String formattedUrlString;
25 static WebView mainWebView;
26 static ProgressBar progressBar;
27 static final String homepage = "https://www.duckduckgo.com";
30 protected void onCreate(Bundle savedInstanceState) {
31 super.onCreate(savedInstanceState);
32 setContentView(R.layout.activity_webview);
34 final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
35 mainWebView = (WebView) findViewById(R.id.mainWebView);
36 progressBar = (ProgressBar) findViewById(R.id.progressBar);
38 // setWebViewClient makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
39 mainWebView.setWebViewClient(new WebViewClient());
41 // Update the progress bar when a page is loading.
42 mainWebView.setWebChromeClient(new WebChromeClient() {
43 public void onProgressChanged(WebView view, int progress) {
45 progressBar.setVisibility(View.VISIBLE);
47 progressBar.setVisibility(View.GONE);
49 progressBar.setProgress(progress);
53 // Allow pinch to zoom.
54 mainWebView.getSettings().setBuiltInZoomControls(true);
56 // Hide zoom controls.
57 mainWebView.getSettings().setDisplayZoomControls(false);
60 mainWebView.getSettings().setJavaScriptEnabled(true);
62 // Enable DOM Storage.
63 mainWebView.getSettings().setDomStorageEnabled(true);
65 // Get the intent information that started the app.
66 final Intent intent = getIntent();
68 if (intent.getData() != null) {
69 // Get the intent data.
70 final Uri intentUriData = intent.getData();
72 // Try to parse the intent data and store it in urlData.
75 urlData = new URL(intentUriData.getScheme(), intentUriData.getHost(), intentUriData.getPath());
76 } catch (Exception e) {
80 Webview.formattedUrlString = urlData.toString();
83 // If formattedUrlString is null assign the homepage to it.
84 if (formattedUrlString == null) {
85 formattedUrlString = homepage;
88 // Place the formattedUrlString in the address bar and load the website.
89 urlTextBox.setText(formattedUrlString);
90 mainWebView.loadUrl(formattedUrlString);
92 // Set the "go" button on the keyboard to load the URL.
93 urlTextBox.setOnKeyListener(new View.OnKeyListener() {
94 public boolean onKey(View v, int keyCode, KeyEvent event) {
95 // If the event is a key-down event on the "enter" button
96 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
97 (keyCode == KeyEvent.KEYCODE_ENTER)) {
98 // Load the URL into the mainWebView and consume the event.
102 // Do not consume the event.
110 public boolean onCreateOptionsMenu(Menu menu) {
111 // Inflate the menu; this adds items to the action bar if it is present.
112 getMenuInflater().inflate(R.menu.menu_webview, menu);
117 public boolean onOptionsItemSelected(MenuItem menuItem) {
118 int menuItemId = menuItem.getItemId();
119 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
121 // Use the menu items to go forward or back.
122 switch (menuItemId) {
124 mainWebView.goBack();
127 mainWebView.goForward();
131 return super.onOptionsItemSelected(menuItem);
134 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
136 public void onBackPressed() {
137 if (mainWebView.canGoBack()) {
138 mainWebView.goBack();
140 super.onBackPressed();
144 public void loadUrl(View view) {
145 // Get the text from urlTextInput and convert it to a string.
146 final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
147 final String unformattedUrlString = urlTextBox.getText().toString();
149 // Don't do anything unless unformattedUrlString is at least 6 characters long.
150 if (unformattedUrlString.length() < 6) { return; }
152 // Add correct protocol formatting to the beginning of the URL if needed.
153 final String firstSixCharacters = unformattedUrlString.substring(0, 6);
155 switch (firstSixCharacters) {
157 formattedUrlString = unformattedUrlString;
160 formattedUrlString = unformattedUrlString;
163 formattedUrlString = unformattedUrlString;
166 formattedUrlString = "http://" + unformattedUrlString;
170 // Parse the unformattedURLString into a Uri.
172 final Uri uriData = Uri.parse(unformattedUrlString);
174 // Convert the Uri to a URL, chicking for any problems with the formatting.
178 urlData = new URL(uriData.getScheme(), uriData.getHost(), uriData.getPath());
179 } catch (Exception e) {
183 // Convert urlData to a string, which is reqauired by loadUrl method.
184 formattedUrlString = urlData.toString();
187 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
189 // Place the URL text back in the address bar and load the website.
190 urlTextBox.setText(formattedUrlString);
191 mainWebView.loadUrl(formattedUrlString);
193 // Hides the keyboard so we can see the webpage.
194 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
195 inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);