]> 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 fb72f87ce2e414d1ad55f8238c97f0560d7486a6..7d5b63fda042f7577743acfb58cc74c183b36ff9 100644 (file)
@@ -1,12 +1,15 @@
 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.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.util.Patterns;
@@ -14,7 +17,6 @@ import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
-import android.view.ViewTreeObserver;
 import android.view.inputmethod.InputMethodManager;
 import android.webkit.WebChromeClient;
 import android.webkit.WebView;
@@ -32,10 +34,9 @@ public class Webview extends AppCompatActivity {
     static String formattedUrlString;
     static WebView mainWebView;
     static ProgressBar progressBar;
-    static SwipeRefreshLayout swipeToRefresh;
     static EditText urlTextBox;
     static ImageView favoriteIcon;
-    static final String homepage = "https://www.duckduckgo.com";
+    static final String homepage = "https://www.duckduckgo.com/";
 
     // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
     @SuppressLint("SetJavaScriptEnabled")
@@ -45,39 +46,22 @@ public class Webview extends AppCompatActivity {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_webview);
 
-        urlTextBox = (EditText) findViewById(R.id.urlTextBox);
-        swipeToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayoutContainer);
         mainWebView = (WebView) findViewById(R.id.mainWebView);
-        progressBar = (ProgressBar) findViewById(R.id.progressBar);
-        favoriteIcon = (ImageView) findViewById(R.id.favoriteIcon);
 
-        // Remove the title from the action bar.
         final ActionBar actionBar = getSupportActionBar();
         if (actionBar != null) {
+            // Remove the title from the action bar.
             actionBar.setDisplayShowTitleEnabled(false);
-            // actionBar.setHideOnContentScrollEnabled(true);
-        }
 
-        // Implement swipe down to refresh.
-        swipeToRefresh.setColorSchemeColors(0xFF0097FF);
-        swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
-            @Override
-            public void onRefresh() {
-                mainWebView.loadUrl(formattedUrlString);
-            }
-        });
+            // Add the custom app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
+            actionBar.setCustomView(R.layout.app_bar);
+            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 
-        // Only enable swipeToRefresh if is mainWebView is scrolled to the top.
-        mainWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
-            @Override
-            public void onScrollChanged() {
-                if (mainWebView.getScrollY() == 0) {
-                    swipeToRefresh.setEnabled(true);
-                } else {
-                    swipeToRefresh.setEnabled(false);
-                }
-            }
-        });
+            // Initialize the variables for favoriteIcon, urlTextBox, and progressBar
+            favoriteIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.favoriteIcon);
+            urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
+            progressBar = (ProgressBar) actionBar.getCustomView().findViewById(R.id.progressBar);
+        }
 
         mainWebView.setWebViewClient(new WebViewClient() {
 
@@ -113,9 +97,6 @@ public class Webview extends AppCompatActivity {
                     progressBar.setVisibility(View.VISIBLE);
                 } else {
                     progressBar.setVisibility(View.GONE);
-
-                    // Stop the refreshing indicator if it is running.
-                    swipeToRefresh.setRefreshing(false);
                 }
             }
 
@@ -184,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) {
@@ -194,6 +178,10 @@ public class Webview extends AppCompatActivity {
                 mainWebView.loadUrl(homepage);
                 break;
 
+            case R.id.refresh:
+                mainWebView.loadUrl(formattedUrlString);
+                break;
+
             case R.id.back:
                 mainWebView.goBack();
                 break;
@@ -201,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);
@@ -237,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;