From: Soren Stoutner Date: Thu, 19 Nov 2015 20:57:47 +0000 (-0700) Subject: Add menu items for copy, paste, and share URL. X-Git-Tag: v1.0~29 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=6e2e99a589d3027b908106f60cc03cf4fcbfdb43;p=PrivacyBrowserAndroid.git Add menu items for copy, paste, and share URL. --- diff --git a/app/src/main/java/com/stoutner/privacybrowser/Webview.java b/app/src/main/java/com/stoutner/privacybrowser/Webview.java index 99a27344..7d5b63fd 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/Webview.java +++ b/app/src/main/java/com/stoutner/privacybrowser/Webview.java @@ -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; @@ -162,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) { @@ -183,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); @@ -219,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; diff --git a/app/src/main/res/layout/app_bar.xml b/app/src/main/res/layout/app_bar.xml index ece3f5d9..80cdb814 100644 --- a/app/src/main/res/layout/app_bar.xml +++ b/app/src/main/res/layout/app_bar.xml @@ -5,13 +5,13 @@ android:id="@+id/addressBarFrameLayout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" - android:layout_height="wrap_content"> + android:layout_height="wrap_content" > + android:orientation="horizontal" > + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 71142ba8..2e838ee3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -10,5 +10,7 @@ Refresh Back Forward - + Copy URL + Paste URL + Share URL