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;
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) {
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);
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;
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" >
<LinearLayout
android:id="@+id/addressBarLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:orientation="horizontal">
+ android:orientation="horizontal" >
<ImageView
android:id="@+id/favoriteIcon"
android:orderInCategory="300"
android:icon="@drawable/ic_forward"
app:showAsAction="never" />
+
+ <!-- tools:targetApi="11" is required because copy and paste was not available on earlier version of Android. -->
+ <item
+ android:id="@+id/copyURL"
+ android:title="@string/copyURL"
+ android:orderInCategory="400"
+ tools:targetApi="11"
+ app:showAsAction="never" />
+
+ <!-- tools:targetApi="11" is required because copy and paste was not available on earlier version of Android. -->
+ <item
+ android:id="@+id/pasteURL"
+ android:title="@string/pasteURL"
+ android:orderInCategory="500"
+ tools:targetApi="11"
+ app:showAsAction="never" />
+
+ <item
+ android:id="@+id/shareURL"
+ android:title="@string/shareURL"
+ android:orderInCategory="600"
+ app:showAsAction="never" />
</menu>