From 5241a83576c30f1fbb6f3527dfb0849b1b81d876 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Sat, 12 Jan 2019 17:03:39 -0700 Subject: [PATCH] Create an `Open with App` menu item. https://redmine.stoutner.com/issues/336 --- .../activities/MainWebViewActivity.java | 94 ++++++++++--------- .../main/res/menu/webview_options_menu.xml | 63 ++++++++----- app/src/main/res/values/strings.xml | 2 + 3 files changed, 89 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index 411972bb..83fff5ab 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -2857,21 +2857,39 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook printManager.print(getString(R.string.privacy_browser_web_page), printDocumentAdapter, null); return true; + case R.id.find_on_page: + // Hide the URL app bar. + supportAppBar.setVisibility(View.GONE); + + // Show the Find on Page `RelativeLayout`. + findOnPageLinearLayout.setVisibility(View.VISIBLE); + + // Display the keyboard. We have to wait 200 ms before running the command to work around a bug in Android. + // http://stackoverflow.com/questions/5520085/android-show-softkeyboard-with-showsoftinput-is-not-working + findOnPageEditText.postDelayed(() -> { + // Set the focus on `findOnPageEditText`. + findOnPageEditText.requestFocus(); + + // Display the keyboard. `0` sets no input flags. + inputMethodManager.showSoftInput(findOnPageEditText, 0); + }, 200); + return true; + + case R.id.add_to_homescreen: + // Show the alert dialog. + AppCompatDialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcutDialog(); + createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getString(R.string.create_shortcut)); + + //Everything else will be handled by the alert dialog and the associated listener below. + return true; + case R.id.view_source: // Launch the View Source activity. Intent viewSourceIntent = new Intent(this, ViewSourceActivity.class); startActivity(viewSourceIntent); return true; - case R.id.proxy_through_orbot: - // Toggle the proxy through Orbot variable. - proxyThroughOrbot = !proxyThroughOrbot; - - // Apply the proxy through Orbot settings. - applyProxyThroughOrbot(true); - return true; - - case R.id.share: + case R.id.share_url: // Setup the share string. String shareString = webViewTitle + " – " + urlTextBox.getText().toString(); @@ -2884,56 +2902,40 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook startActivity(Intent.createChooser(shareIntent, getString(R.string.share_url))); return true; - case R.id.open_with: - // Convert the URL to an URI. - Uri shareUri = Uri.parse(formattedUrlString); - - // Get the host. - String shareHost = shareUri.getHost(); - + case R.id.open_with_app: // Create the open with intent with `ACTION_VIEW`. - Intent openWithIntent = new Intent(Intent.ACTION_VIEW); - - // Set the data based on the host. - if ((shareHost != null) && (shareHost.endsWith("youtube.com") || shareHost.equals("play.google.com") || shareHost.equals("f-droid.org"))) { // Handle App URLs. - // Set the URI but not the MIME type. This should open all available apps. - openWithIntent.setData(shareUri); - } else { // Handle a generic URL. - // Set the URI and the MIME type. `"text/html"` should load browser options. - openWithIntent.setDataAndType(shareUri, "text/html"); - } + Intent openWithAppIntent = new Intent(Intent.ACTION_VIEW); + + // Set the URI but not the MIME type. This should open all available apps. + openWithAppIntent.setData(Uri.parse(formattedUrlString)); // Flag the intent to open in a new task. - openWithIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + openWithAppIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Show the chooser. - startActivity(Intent.createChooser(openWithIntent, getString(R.string.open_with))); + startActivity(Intent.createChooser(openWithAppIntent, getString(R.string.open_with))); return true; - case R.id.find_on_page: - // Hide the URL app bar. - supportAppBar.setVisibility(View.GONE); + case R.id.open_with_browser: + // Create the open with intent with `ACTION_VIEW`. + Intent openWithBrowserIntent = new Intent(Intent.ACTION_VIEW); - // Show the Find on Page `RelativeLayout`. - findOnPageLinearLayout.setVisibility(View.VISIBLE); + // Set the URI and the MIME type. `"text/html"` should load browser options. + openWithBrowserIntent.setDataAndType(Uri.parse(formattedUrlString), "text/html"); - // Display the keyboard. We have to wait 200 ms before running the command to work around a bug in Android. - // http://stackoverflow.com/questions/5520085/android-show-softkeyboard-with-showsoftinput-is-not-working - findOnPageEditText.postDelayed(() -> { - // Set the focus on `findOnPageEditText`. - findOnPageEditText.requestFocus(); + // Flag the intent to open in a new task. + openWithBrowserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - // Display the keyboard. `0` sets no input flags. - inputMethodManager.showSoftInput(findOnPageEditText, 0); - }, 200); + // Show the chooser. + startActivity(Intent.createChooser(openWithBrowserIntent, getString(R.string.open_with))); return true; - case R.id.add_to_homescreen: - // Show the `CreateHomeScreenShortcutDialog` `AlertDialog` and name this instance `R.string.create_shortcut`. - AppCompatDialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcutDialog(); - createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getString(R.string.create_shortcut)); + case R.id.proxy_through_orbot: + // Toggle the proxy through Orbot variable. + proxyThroughOrbot = !proxyThroughOrbot; - //Everything else will be handled by `CreateHomeScreenShortcutDialog` and the associated listener below. + // Apply the proxy through Orbot settings. + applyProxyThroughOrbot(true); return true; case R.id.refresh: diff --git a/app/src/main/res/menu/webview_options_menu.xml b/app/src/main/res/menu/webview_options_menu.xml index b7760ed2..acf69eaf 100644 --- a/app/src/main/res/menu/webview_options_menu.xml +++ b/app/src/main/res/menu/webview_options_menu.xml @@ -324,49 +324,64 @@ android:orderInCategory="970" app:showAsAction="never" /> + + + + - - + android:orderInCategory="1000" + app:showAsAction="never" > - + + - + + + + + View Source Share Share URL + Open with App + Open with Browser Find on Page Print Privacy Browser Web Page -- 2.45.2