X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2FMainWebView.java;h=f58f1a2b02313e570de57a202d570b802c962b1f;hb=cb90dabb95b908c3e76c0616ac201106199e200a;hp=3582df9f9f9aa6364b7590eb9e12c8fe48f93a47;hpb=e90fe1c8be4ede280e8f9e4b17710330f11f9355;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/MainWebView.java b/app/src/main/java/com/stoutner/privacybrowser/MainWebView.java index 3582df9f..f58f1a2b 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/MainWebView.java +++ b/app/src/main/java/com/stoutner/privacybrowser/MainWebView.java @@ -60,30 +60,32 @@ import java.net.URLEncoder; public class MainWebView extends AppCompatActivity implements CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener { // favoriteIcon is public static so it can be accessed from CreateHomeScreenShortcut. public static Bitmap favoriteIcon; - // mainWebView is public static so it can be accessed from AboutDialog. It is also used in onCreate and onOptionsItemSelected. + // mainWebView is public static so it can be accessed from AboutDialog. It is also used in onCreate(), onOptionsItemSelected(), and loadUrlFromTextBox(). public static WebView mainWebView; - // formattedUrlString is used in onCreate, onOptionsItemSelected, onCreateHomeScreenShortcutCreate, and loadUrlFromTextBox. + // mainMenu is used in onCreateOptionsMenu() and onOptionsItemSelected(). + private Menu mainMenu; + // formattedUrlString is used in onCreate(), onOptionsItemSelected(), onCreateHomeScreenShortcutCreate(), and loadUrlFromTextBox(). private String formattedUrlString; - // homepage is used in onCreate and onOptionsItemSelected. + // homepage is used in onCreate() and onOptionsItemSelected(). private String homepage = "https://www.duckduckgo.com/"; - // enableJavaScript is used in onCreate, onCreateOptionsMenu, and onOptionsItemSelected. - private boolean enableJavaScript; - // enableDomStorage is used in onCreate, onCreateOptionsMenu, and onOptionsItemSelected. - private boolean enableDomStorage; - - /* enableSaveFormData does nothing until database storage is implemented. - // enableSaveFormData is used in onCreate, onCreateOptionsMenu, and onOptionsItemSelected. - private boolean enableSaveFormData; + // javaScriptEnabled is used in onCreate(), onCreateOptionsMenu(), onOptionsItemSelected(), and loadUrlFromTextBox(). + private boolean javaScriptEnabled; + // domStorageEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected(). + private boolean domStorageEnabled; + + /* saveFormDataEnabled does nothing until database storage is implemented. + // saveFormDataEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected(). + private boolean saveFormDataEnabled; */ - // cookieManager is used in onCreate and onOptionsItemSelected. + // cookieManager is used in onCreate() and onOptionsItemSelected(). private CookieManager cookieManager; - // enableCookies is used in onCreate, onCreateOptionsMenu, and onOptionsItemSelected. - private boolean enableCookies; + // cookiesEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected(). + private boolean cookiesEnabled; - // actionBar is used in onCreate and onOptionsItemSelected. - private ActionBar actionBar; + // urlTextBox is used in onCreate(), onOptionsItemSelected(), and loadUrlFromTextBox(). + private EditText urlTextBox; @Override // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. @@ -94,9 +96,9 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh final FrameLayout fullScreenVideoFrameLayout = (FrameLayout) findViewById(R.id.fullScreenVideoFrameLayout); final Activity mainWebViewActivity = this; + final ActionBar actionBar = getSupportActionBar(); mainWebView = (WebView) findViewById(R.id.mainWebView); - actionBar = getSupportActionBar(); if (actionBar != null) { // Remove the title from the action bar. @@ -107,7 +109,7 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); // Set the "go" button on the keyboard to load the URL in urlTextBox. - EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox); + urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox); urlTextBox.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button, load the URL. @@ -145,21 +147,14 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh // Update the URL in urlTextBox when the page starts to load. @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { - if (actionBar != null) { - EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox); - urlTextBox.setText(url); - } + urlTextBox.setText(url); } // Update formattedUrlString and urlTextBox. It is necessary to do this after the page finishes loading because the final URL can change during load. @Override public void onPageFinished(WebView view, String url) { formattedUrlString = url; - - if (actionBar != null) { - EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox); - urlTextBox.setText(formattedUrlString); - } + urlTextBox.setText(formattedUrlString); } }); @@ -195,8 +190,8 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh // Enter full screen video @Override public void onShowCustomView(View view, CustomViewCallback callback) { - if (getSupportActionBar() != null) { - getSupportActionBar().hide(); + if (actionBar != null) { + actionBar.hide(); } fullScreenVideoFrameLayout.addView(view); @@ -227,8 +222,8 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh // Exit full screen video public void onHideCustomView() { - if (getSupportActionBar() != null) { - getSupportActionBar().show(); + if (actionBar != null) { + actionBar.show(); } mainWebView.setVisibility(View.VISIBLE); @@ -268,23 +263,23 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh } // Set JavaScript initial status. - enableJavaScript = false; - mainWebView.getSettings().setJavaScriptEnabled(enableJavaScript); + javaScriptEnabled = false; + mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled); // Set DOM Storage initial status. - enableDomStorage = false; - mainWebView.getSettings().setDomStorageEnabled(enableDomStorage); + domStorageEnabled = false; + mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled); /* Save Form Data does nothing until database storage is implemented. // Set Save Form Data initial status. - enableSaveFormData = true; - mainWebView.getSettings().setSaveFormData(enableSaveFormData); + saveFormDataEnabled = true; + mainWebView.getSettings().setSaveFormData(saveFormDataEnabled); */ // Set Cookies initial status. - enableCookies = false; + cookiesEnabled = false; cookieManager = CookieManager.getInstance(); - cookieManager.setAcceptCookie(enableCookies); + cookieManager.setAcceptCookie(cookiesEnabled); // Get the intent information that started the app. final Intent intent = getIntent(); @@ -306,7 +301,7 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh @Override protected void onNewIntent(Intent intent) { - // Sets the new intent as the activity intent, so that any future getIntent() picks up this one. + // Sets the new intent as the activity intent, so that any future getIntent()s pick up this one instead of creating a new activity. setIntent(intent); if (intent.getData() != null) { @@ -324,6 +319,9 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_webview, menu); + // Set mainMenu so it can be used by onOptionsItemSelected. + mainMenu = menu; + // Get MenuItems for checkable menu items. MenuItem toggleJavaScript = menu.findItem(R.id.toggleJavaScript); MenuItem toggleDomStorage = menu.findItem(R.id.toggleDomStorage); @@ -333,18 +331,22 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh MenuItem toggleCookies = menu.findItem(R.id.toggleCookies); // Set the initial icon for toggleJavaScript - if (enableJavaScript) { - toggleJavaScript.setIcon(R.drawable.javascript_on); + if (javaScriptEnabled) { + toggleJavaScript.setIcon(R.drawable.javascript_enabled); } else { - toggleJavaScript.setIcon(R.drawable.javascript_off); + if (domStorageEnabled || cookiesEnabled) { + toggleJavaScript.setIcon(R.drawable.warning); + } else { + toggleJavaScript.setIcon(R.drawable.privacy_mode); + } } // Set the initial status of the menu item checkboxes. - toggleDomStorage.setChecked(enableDomStorage); + toggleDomStorage.setChecked(domStorageEnabled); /* toggleSaveFormData does nothing until database storage is implemented. - toggleSaveFormData.setChecked(enableSaveFormData); + toggleSaveFormData.setChecked(saveFormDataEnabled); */ - toggleCookies.setChecked(enableCookies); + toggleCookies.setChecked(cookiesEnabled); return true; } @@ -379,49 +381,87 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh @SuppressWarnings("deprecation") public boolean onOptionsItemSelected(MenuItem menuItem) { int menuItemId = menuItem.getItemId(); + + // Some options need to access the clipboard. ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + // Some options need to update the drawable for toggleJavaScript. + MenuItem toggleJavaScript = mainMenu.findItem(R.id.toggleJavaScript); + // Sets the commands that relate to the menu entries. switch (menuItemId) { case R.id.toggleJavaScript: - if (enableJavaScript) { - enableJavaScript = false; - menuItem.setIcon(R.drawable.javascript_off); + if (javaScriptEnabled) { + javaScriptEnabled = false; mainWebView.getSettings().setJavaScriptEnabled(false); mainWebView.reload(); - Toast.makeText(getApplicationContext(), "JavaScript Disabled", Toast.LENGTH_SHORT).show(); + + // Update the toggleJavaScript icon and display a toast message. + if (domStorageEnabled || cookiesEnabled) { + menuItem.setIcon(R.drawable.warning); + if (domStorageEnabled && cookiesEnabled) { + Toast.makeText(getApplicationContext(), "JavaScript disabled, DOM Storage and Cookies still enabled", Toast.LENGTH_SHORT).show(); + } else { + if (domStorageEnabled) { + Toast.makeText(getApplicationContext(), "JavaScript disabled, DOM Storage still enabled", Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(getApplicationContext(), "JavaScript disabled, Cookies still enabled", Toast.LENGTH_SHORT).show(); + } + } + } else { + menuItem.setIcon(R.drawable.privacy_mode); + Toast.makeText(getApplicationContext(), "Privacy Mode", Toast.LENGTH_SHORT).show(); + } } else { - enableJavaScript = true; - menuItem.setIcon(R.drawable.javascript_on); + javaScriptEnabled = true; + menuItem.setIcon(R.drawable.javascript_enabled); mainWebView.getSettings().setJavaScriptEnabled(true); mainWebView.reload(); - Toast.makeText(getApplicationContext(), "JavaScript Enabled", Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(), "JavaScript enabled", Toast.LENGTH_SHORT).show(); } return true; case R.id.toggleDomStorage: - if (enableDomStorage) { - enableDomStorage = false; + if (domStorageEnabled) { + domStorageEnabled = false; menuItem.setChecked(false); mainWebView.getSettings().setDomStorageEnabled(false); mainWebView.reload(); + + // Update the toggleJavaScript icon and display a toast message if appropriate. + if (!javaScriptEnabled && !cookiesEnabled) { + toggleJavaScript.setIcon(R.drawable.privacy_mode); + Toast.makeText(getApplicationContext(), "Privacy Mode", Toast.LENGTH_SHORT).show(); + } else { + if (cookiesEnabled) { + toggleJavaScript.setIcon(R.drawable.warning); + Toast.makeText(getApplicationContext(), "Cookies still enabled", Toast.LENGTH_SHORT).show(); + } // Else Do nothing because JavaScript is enabled. + } } else { - enableDomStorage = true; + domStorageEnabled = true; menuItem.setChecked(true); mainWebView.getSettings().setDomStorageEnabled(true); mainWebView.reload(); + + // Update the toggleJavaScript icon if appropriate. + if (!javaScriptEnabled) { + toggleJavaScript.setIcon(R.drawable.warning); + } // Else Do nothing because JavaScript is enabled. + + Toast.makeText(getApplicationContext(), "DOM Storage enabled", Toast.LENGTH_SHORT).show(); } return true; /* toggleSaveFormData does nothing until database storage is implemented. case R.id.toggleSaveFormData: - if (enableSaveFormData) { - enableSaveFormData = false; + if (saveFormDataEnabled) { + saveFormDataEnabled = false; menuItem.setChecked(false); mainWebView.getSettings().setSaveFormData(false); mainWebView.reload(); } else { - enableSaveFormData = true; + saveFormDataEnabled = true; menuItem.setChecked(true); mainWebView.getSettings().setSaveFormData(true); mainWebView.reload(); @@ -430,16 +470,34 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh */ case R.id.toggleCookies: - if (enableCookies) { - enableCookies = false; + if (cookiesEnabled) { + cookiesEnabled = false; menuItem.setChecked(false); cookieManager.setAcceptCookie(false); mainWebView.reload(); + + // Update the toggleJavaScript icon and display a toast message if appropriate. + if (!javaScriptEnabled && !domStorageEnabled) { + toggleJavaScript.setIcon(R.drawable.privacy_mode); + Toast.makeText(getApplicationContext(), "Privacy Mode", Toast.LENGTH_SHORT).show(); + } else { + if (domStorageEnabled) { + toggleJavaScript.setIcon(R.drawable.warning); + Toast.makeText(getApplicationContext(), "DOM Storage still enabled", Toast.LENGTH_SHORT).show(); + } // Else Do nothing because JavaScript is enabled. + } } else { - enableCookies = true; + cookiesEnabled = true; menuItem.setChecked(true); cookieManager.setAcceptCookie(true); mainWebView.reload(); + + // Update the toggleJavaScript icon if appropriate. + if (!javaScriptEnabled) { + toggleJavaScript.setIcon(R.drawable.warning); + } // Else Do nothing because JavaScript is enabled. + + Toast.makeText(getApplicationContext(), "Cookies enabled", Toast.LENGTH_SHORT).show(); } return true; @@ -475,37 +533,25 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh return true; case R.id.copyURL: - // Make sure that actionBar is not null. - if (actionBar != null) { - EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox); - clipboard.setPrimaryClip(ClipData.newPlainText("URL", urlTextBox.getText())); - } + clipboard.setPrimaryClip(ClipData.newPlainText("URL", urlTextBox.getText())); return true; case R.id.pasteURL: - // Make sure that actionBar is not null. - if (actionBar != null) { - ClipData.Item clipboardData = clipboard.getPrimaryClip().getItemAt(0); - EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox); - urlTextBox.setText(clipboardData.coerceToText(this)); + ClipData.Item clipboardData = clipboard.getPrimaryClip().getItemAt(0); + urlTextBox.setText(clipboardData.coerceToText(this)); try { loadUrlFromTextBox(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } - } return true; case R.id.shareURL: - // Make sure that actionBar is not null. - if (actionBar != null) { - EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox); - Intent shareIntent = new Intent(); - shareIntent.setAction(Intent.ACTION_SEND); - shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString()); - shareIntent.setType("text/plain"); - startActivity(Intent.createChooser(shareIntent, "Share URL")); - } + Intent shareIntent = new Intent(); + shareIntent.setAction(Intent.ACTION_SEND); + shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString()); + shareIntent.setType("text/plain"); + startActivity(Intent.createChooser(shareIntent, "Share URL")); return true; case R.id.addToHomescreen: @@ -593,53 +639,50 @@ public class MainWebView extends AppCompatActivity implements CreateHomeScreenSh } public void loadUrlFromTextBox() throws UnsupportedEncodingException { - // Make sure that actionBar is not null. - ActionBar actionBar = getSupportActionBar(); - if (actionBar != null) { - final WebView mainWebView = (WebView) findViewById(R.id.mainWebView); - EditText urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox); - - // Get the text from urlTextInput and convert it to a string. - String unformattedUrlString = urlTextBox.getText().toString(); - URL unformattedUrl = null; - Uri.Builder formattedUri = new Uri.Builder(); - - // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search. - if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) { - - // Add http:// at the beginning if it is missing. Otherwise the app will segfault. - if (!unformattedUrlString.startsWith("http")) { - unformattedUrlString = "http://" + unformattedUrlString; - } + // Get the text from urlTextBox and convert it to a string. + String unformattedUrlString = urlTextBox.getText().toString(); + URL unformattedUrl = null; + Uri.Builder formattedUri = new Uri.Builder(); + + // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search. + if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) { + // Add http:// at the beginning if it is missing. Otherwise the app will segfault. + if (!unformattedUrlString.startsWith("http")) { + unformattedUrlString = "http://" + unformattedUrlString; + } - // Convert unformattedUrlString to a URL, then to a URI, and then back to a string, which sanitizes the input and adds in any missing components. - try { - unformattedUrl = new URL(unformattedUrlString); - } catch (MalformedURLException e) { - e.printStackTrace(); - } + // Convert unformattedUrlString to a URL, then to a URI, and then back to a string, which sanitizes the input and adds in any missing components. + try { + unformattedUrl = new URL(unformattedUrlString); + } catch (MalformedURLException e) { + e.printStackTrace(); + } - // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value. - final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null; - final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null; - final String path = unformattedUrl != null ? unformattedUrl.getPath() : null; - final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null; - final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null; + // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value. + final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null; + final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null; + final String path = unformattedUrl != null ? unformattedUrl.getPath() : null; + final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null; + final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null; - formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment); - formattedUrlString = formattedUri.build().toString(); + formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment); + formattedUrlString = formattedUri.build().toString(); + } else { + // Sanitize the search input and convert it to a DuckDuckGo search. + final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8"); - } else { - // Sanitize the search input and convert it to a DuckDuckGo search. - final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8"); + // Use the correct search URL based on javaScriptEnabled. + if (javaScriptEnabled) { formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString; + } else { + formattedUrlString = "https://duckduckgo.com/html/?q=" + encodedUrlString; } + } - mainWebView.loadUrl(formattedUrlString); + mainWebView.loadUrl(formattedUrlString); - // Hides the keyboard so we can see the webpage. - InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); - inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0); - } + // Hides the keyboard so we can see the webpage. + InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); + inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0); } -} \ No newline at end of file +}