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 // 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);
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);
54 progressBar.setVisibility(View.VISIBLE);
56 progressBar.setVisibility(View.GONE);
61 // Allow pinch to zoom.
62 mainWebView.getSettings().setBuiltInZoomControls(true);
64 // Hide zoom controls.
65 mainWebView.getSettings().setDisplayZoomControls(false);
68 mainWebView.getSettings().setJavaScriptEnabled(true);
70 // Enable DOM Storage.
71 mainWebView.getSettings().setDomStorageEnabled(true);
73 // Get the intent information that started the app.
74 final Intent intent = getIntent();
76 if (intent.getData() != null) {
77 // Get the intent data and convert it to a string.
78 final Uri intentUriData = intent.getData();
79 Webview.formattedUrlString = intentUriData.toString();
82 // If formattedUrlString is null assign the homepage to it.
83 if (formattedUrlString == null) {
84 formattedUrlString = homepage;
87 // Place the formattedUrlString in the address bar and load the website.
88 urlTextBox.setText(formattedUrlString);
89 mainWebView.loadUrl(formattedUrlString);
91 // Set the "go" button on the keyboard to load the URL.
92 urlTextBox.setOnKeyListener(new View.OnKeyListener() {
93 public boolean onKey(View v, int keyCode, KeyEvent event) {
94 // If the event is a key-down event on the "enter" button
95 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
96 (keyCode == KeyEvent.KEYCODE_ENTER)) {
97 // Load the URL into the mainWebView and consume the event.
98 loadUrlFromTextBox(mainWebView);
101 // Do not consume the event.
109 public boolean onCreateOptionsMenu(Menu menu) {
110 // Inflate the menu; this adds items to the action bar if it is present.
111 getMenuInflater().inflate(R.menu.menu_webview, menu);
116 public boolean onOptionsItemSelected(MenuItem menuItem) {
117 int menuItemId = menuItem.getItemId();
118 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
120 // Use the menu items to go forward or back.
121 switch (menuItemId) {
123 mainWebView.goBack();
126 mainWebView.goForward();
130 return super.onOptionsItemSelected(menuItem);
133 // Override onBackPressed so that if mainWebView can go back it does when the system back button is pressed.
135 public void onBackPressed() {
136 if (mainWebView.canGoBack()) {
137 mainWebView.goBack();
139 super.onBackPressed();
143 public void loadUrlFromTextBox(View view) {
144 // Get the text from urlTextInput and convert it to a string.
145 final EditText urlTextBox = (EditText) findViewById(R.id.urlTextBox);
146 final String unformattedUrlString = urlTextBox.getText().toString();
148 // Don't do anything unless unformattedUrlString is at least 6 characters long.
149 if (unformattedUrlString.length() < 6) { return; }
151 // Add correct protocol formatting to the beginning of the URL if needed.
152 final String firstSixCharacters = unformattedUrlString.substring(0, 6);
154 switch (firstSixCharacters) {
156 formattedUrlString = unformattedUrlString;
159 formattedUrlString = unformattedUrlString;
162 formattedUrlString = unformattedUrlString;
165 formattedUrlString = "http://" + unformattedUrlString;
168 final WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
170 // Place the URL text back in the address bar and load the website.
171 urlTextBox.setText(formattedUrlString);
172 mainWebView.loadUrl(formattedUrlString);
174 // Hides the keyboard so we can see the webpage.
175 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
176 inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);