]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/Webview.java
Add menu items for copy, paste, and share URL.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / Webview.java
index ff718ff961bd5a43f83b752bc35f00cec61b1c72..7d5b63fda042f7577743acfb58cc74c183b36ff9 100644 (file)
@@ -1,14 +1,17 @@
 package com.stoutner.privacybrowser;
 
 import android.annotation.SuppressLint;
+import android.annotation.TargetApi;
 import android.app.Activity;
+import android.content.ClipData;
+import android.content.ClipboardManager;
+import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Bundle;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
-import android.support.v7.app.AppCompatDelegate;
 import android.util.Patterns;
 import android.view.KeyEvent;
 import android.view.Menu;
@@ -33,7 +36,7 @@ public class Webview extends AppCompatActivity {
     static ProgressBar progressBar;
     static EditText urlTextBox;
     static ImageView favoriteIcon;
-    static final String homepage = "https://www.google.com/";
+    static final String homepage = "https://www.duckduckgo.com/";
 
     // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
     @SuppressLint("SetJavaScriptEnabled")
@@ -41,10 +44,6 @@ public class Webview extends AppCompatActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-
-        // FEATURE_ACTION_BAR_OVERLAY is required to scroll the actionBar.
-        supportRequestWindowFeature(AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR_OVERLAY);
-
         setContentView(R.layout.activity_webview);
 
         mainWebView = (WebView) findViewById(R.id.mainWebView);
@@ -62,9 +61,6 @@ public class Webview extends AppCompatActivity {
             favoriteIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.favoriteIcon);
             urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
             progressBar = (ProgressBar) actionBar.getCustomView().findViewById(R.id.progressBar);
-
-            // Scroll the actionBar.
-            actionBar.setHideOnContentScrollEnabled(true);
         }
 
         mainWebView.setWebViewClient(new WebViewClient() {
@@ -169,9 +165,12 @@ public class Webview extends AppCompatActivity {
         return true;
     }
 
+    // @TargetApi(11) turns off the errors regarding copy and paste, which are removied from view in menu_webview.xml for lower version of Android.
     @Override
+    @TargetApi(11)
     public boolean onOptionsItemSelected(MenuItem menuItem) {
         int menuItemId = menuItem.getItemId();
+        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
 
         // Sets the commands that relate to the menu entries.
         switch (menuItemId) {
@@ -190,6 +189,28 @@ public class Webview extends AppCompatActivity {
             case R.id.forward:
                 mainWebView.goForward();
                 break;
+
+            case R.id.copyURL:
+                clipboard.setPrimaryClip(ClipData.newPlainText("URL", urlTextBox.getText()));
+                break;
+
+            case R.id.pasteURL:
+                ClipData.Item clipboardData = clipboard.getPrimaryClip().getItemAt(0);
+                urlTextBox.setText(clipboardData.coerceToText(this));
+                try {
+                    loadUrlFromTextBox(mainWebView);
+                } catch (UnsupportedEncodingException e) {
+                    e.printStackTrace();
+                }
+                break;
+
+            case R.id.shareURL:
+                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"));
+                break;
         }
 
         return super.onOptionsItemSelected(menuItem);
@@ -226,7 +247,7 @@ public class Webview extends AppCompatActivity {
                 e.printStackTrace();
             }
 
-            // The ternary operator (? :) makes sure that unformattedUrl.get() does not cause a null pointer exception.
+            // 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;