]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java
Partial Find on Page implimentation.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / MainWebViewActivity.java
index 104522b600f0b9f467e539d27dc6a69d29d84bce..7872677dcceaa4f9c83eb532584bd4dd0b63a191 100644 (file)
@@ -23,6 +23,7 @@ import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.app.DialogFragment;
 import android.app.DownloadManager;
+import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
@@ -35,6 +36,8 @@ import android.net.http.SslError;
 import android.os.Build;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
+import android.print.PrintDocumentAdapter;
+import android.print.PrintManager;
 import android.support.annotation.NonNull;
 import android.support.design.widget.NavigationView;
 import android.support.design.widget.Snackbar;
@@ -78,7 +81,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         SslCertificateError.SslCertificateErrorListener, DownloadFile.DownloadFileListener {
 
     // `appBar` is public static so it can be accessed from `OrbotProxyHelper`.
-    // It is also used in `onCreate()`.
+    // It is also used in `onCreate()` and `onOptionsItemSelected()`.
     public static ActionBar appBar;
 
     // `favoriteIcon` is public static so it can be accessed from `CreateHomeScreenShortcut`, `BookmarksActivity`, `CreateBookmark`, `CreateBookmarkFolder`, and `EditBookmark`.
@@ -151,6 +154,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     // `sslErrorHandler` is used in `onCreate()`, `onSslErrorCancel()`, and `onSslErrorProceed`.
     private SslErrorHandler sslErrorHandler;
 
+    private MenuItem toggleJavaScript;
+
     @Override
     // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.  The whole premise of Privacy Browser is built around an understanding of these dangers.
     @SuppressLint("SetJavaScriptEnabled")
@@ -166,8 +171,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // This is needed to get rid of the Android Studio warning that appBar might be null.
         assert appBar != null;
 
-        // Add the custom url_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
-        appBar.setCustomView(R.layout.url_bar);
+        // Add the custom url_app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
+        appBar.setCustomView(R.layout.url_app_bar);
         appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 
         // Set the "go" button on the keyboard to load the URL in urlTextBox.
@@ -438,6 +443,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         updatePrivacyIcons();
 
         // Get handles for the menu items.
+        toggleJavaScript = menu.findItem(R.id.toggleJavaScript);
         MenuItem toggleFirstPartyCookies = menu.findItem(R.id.toggleFirstPartyCookies);
         MenuItem toggleThirdPartyCookies = menu.findItem(R.id.toggleThirdPartyCookies);
         MenuItem toggleDomStorage = menu.findItem(R.id.toggleDomStorage);
@@ -729,6 +735,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 mainWebView.getSettings().setTextZoom(200);
                 return true;
 
+            case R.id.find_on_page:
+                appBar.setCustomView(R.layout.find_on_page_app_bar);
+                toggleJavaScript.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+                appBar.invalidateOptionsMenu();
+                return true;
+
             case R.id.share:
                 Intent shareIntent = new Intent();
                 shareIntent.setAction(Intent.ACTION_SEND);
@@ -745,6 +757,17 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 //Everything else will be handled by `CreateHomeScreenShortcut` and the associated listener below.
                 return true;
 
+            case R.id.print:
+                // Get a `PrintManager` instance.
+                PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
+
+                // Convert `mainWebView` to `printDocumentAdapter`.
+                PrintDocumentAdapter printDocumentAdapter = mainWebView.createPrintDocumentAdapter();
+
+                // Print the document.  The print attributes are `null`.
+                printManager.print(getResources().getString(R.string.privacy_browser_web_page), printDocumentAdapter, null);
+                return true;
+
             case R.id.refresh:
                 mainWebView.reload();
                 return true;
@@ -882,7 +905,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         adView = findViewById(R.id.adView);
 
         // `invalidateOptionsMenu` should recalculate the number of action buttons from the menu to display on the app bar, but it doesn't because of the this bug:  https://code.google.com/p/android/issues/detail?id=20493#c8
-        invalidateOptionsMenu();
+        // invalidateOptionsMenu();
     }
 
     @Override