]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java
Handle the options menu.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateHomeScreenShortcutDialog.java
index 32cd5facd7cffac1bd57bfa8ca1fbc2ce76122c1..95a3ef5ed2bc7312cf585996d93bcc0dc4043692 100644 (file)
@@ -25,12 +25,14 @@ import android.app.Dialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Bundle;
+import android.preference.PreferenceManager;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.view.KeyEvent;
@@ -39,15 +41,16 @@ import android.view.View;
 import android.view.WindowManager;
 import android.widget.Button;
 import android.widget.EditText;
+import android.widget.RadioButton;
 
 import androidx.annotation.NonNull;
-// `ShortcutInfoCompat`, `ShortcutManagerCompat`, and `IconCompat` can be switched to the non-compat versions once API >= 26.
+// `ShortcutInfoCompat`, `ShortcutManagerCompat`, and `IconCompat` can be switched to the non-compat versions once the minimum API >= 26.
 import androidx.core.content.pm.ShortcutInfoCompat;
 import androidx.core.content.pm.ShortcutManagerCompat;
 import androidx.core.graphics.drawable.IconCompat;
 import androidx.fragment.app.DialogFragment;  // The AndroidX dialog fragment must be used or an error is produced on API <=22.
 
-import com.stoutner.privacybrowser.activities.MainWebViewActivity;
+import com.stoutner.privacybrowser.BuildConfig;
 import com.stoutner.privacybrowser.R;
 
 import java.io.ByteArrayOutputStream;
@@ -59,6 +62,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment {
     private Bitmap favoriteIconBitmap;
     private EditText shortcutNameEditText;
     private EditText urlEditText;
+    private RadioButton openWithPrivacyBrowserRadioButton;
     private Button createButton;
 
     public static CreateHomeScreenShortcutDialog createDialog(String shortcutName, String urlString, Bitmap favoriteIconBitmap) {
@@ -97,7 +101,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment {
         // Get the arguments.
         Bundle arguments = getArguments();
 
-        // Remove the incorrect lint warning that the arguments might be null.
+        // Remove the incorrect lint warning below that the arguments might be null.
         assert arguments != null;
 
         // Store the strings in class variables.
@@ -119,7 +123,14 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment {
     @Override
     @NonNull
     public Dialog onCreateDialog(Bundle savedInstanceState) {
-        // Remove the incorrect lint warning below that `getLayoutInflater()` might be null.
+        // Get a handle for the shared preferences.
+        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
+
+        // Get the theme and screenshot preferences.
+        boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
+        boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
+
+        // Remove the incorrect lint warning below that the layout inflater might be null.
         assert getActivity() != null;
 
         // Get the activity's layout inflater.
@@ -132,7 +143,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment {
         AlertDialog.Builder dialogBuilder;
 
         // Set the style according to the theme.
-        if (MainWebViewActivity.darkTheme) {
+        if (darkTheme) {
             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
         } else {
             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
@@ -161,24 +172,23 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment {
         assert alertDialog.getWindow() != null;
 
         // Disable screenshots if not allowed.
-        if (!MainWebViewActivity.allowScreenshots) {
+        if (allowScreenshots) {
             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
         }
 
         // The alert dialog must be shown before the contents may be edited.
         alertDialog.show();
 
-        // Get a handle for the edit texts.
+        // Get handles for the views.
         shortcutNameEditText = alertDialog.findViewById(R.id.shortcut_name_edittext);
         urlEditText = alertDialog.findViewById(R.id.url_edittext);
+        openWithPrivacyBrowserRadioButton = alertDialog.findViewById(R.id.open_with_privacy_browser_radiobutton);
+        createButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
 
         // Populate the edit texts.
         shortcutNameEditText.setText(initialShortcutName);
         urlEditText.setText(initialUrlString);
 
-        // Get a handle for the create button.
-        createButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
-
         // Add a text change listener to the shortcut name edit text.
         shortcutNameEditText.addTextChangedListener(new TextWatcher() {
             @Override
@@ -292,8 +302,16 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment {
         // Convert the favorite icon bitmap to an icon.  `IconCompat` must be used until the minimum API >= 26.
         IconCompat favoriteIcon = IconCompat.createWithBitmap(favoriteIconBitmap);
 
-        // Setup the shortcut intent.
+        // Create a shortcut intent.
         Intent shortcutIntent = new Intent(Intent.ACTION_VIEW);
+
+        // Check to see if the shortcut should open up Privacy Browser explicitly.
+        if (openWithPrivacyBrowserRadioButton.isChecked()) {
+            // Set the current application ID as the target package.
+            shortcutIntent.setPackage(BuildConfig.APPLICATION_ID);
+        }
+
+        // Add the URL to the intent.
         shortcutIntent.setData(Uri.parse(urlString));
 
         // Create a shortcut info builder.  The shortcut name becomes the shortcut ID.