X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FCreateHomeScreenShortcutDialog.java;h=8bb889ed738623744fdd2a396b3472b84b2f554a;hp=32cd5facd7cffac1bd57bfa8ca1fbc2ce76122c1;hb=f52255e6eeb1a6be9f190e733563cc37b5d1f2b5;hpb=047f37ce55e2299817d187f5128247eaa2d653dd diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java b/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java index 32cd5fac..8bb889ed 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java @@ -20,17 +20,18 @@ package com.stoutner.privacybrowser.dialogs; import android.annotation.SuppressLint; -import android.app.AlertDialog; 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,28 +40,27 @@ 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. +import androidx.appcompat.app.AlertDialog; 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 androidx.fragment.app.DialogFragment; -import com.stoutner.privacybrowser.activities.MainWebViewActivity; +import com.stoutner.privacybrowser.BuildConfig; import com.stoutner.privacybrowser.R; import java.io.ByteArrayOutputStream; public class CreateHomeScreenShortcutDialog extends DialogFragment { - // Create the class variables. - private String initialShortcutName; - private String initialUrlString; - private Bitmap favoriteIconBitmap; + // Define the class variables. private EditText shortcutNameEditText; private EditText urlEditText; - private Button createButton; + private RadioButton openWithPrivacyBrowserRadioButton; + // The public constructor. public static CreateHomeScreenShortcutDialog createDialog(String shortcutName, String urlString, Bitmap favoriteIconBitmap) { // Create a favorite icon byte array output stream. ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream(); @@ -71,38 +71,38 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { // Convert the byte array output stream to a byte array. byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray(); - // Create a bundle. - Bundle bundle = new Bundle(); + // Create an arguments bundle. + Bundle argumentsBundle = new Bundle(); // Store the variables in the bundle. - bundle.putString("shortcut_name", shortcutName); - bundle.putString("url_string", urlString); - bundle.putByteArray("favorite_icon_byte_array", favoriteIconByteArray); + argumentsBundle.putString("shortcut_name", shortcutName); + argumentsBundle.putString("url_string", urlString); + argumentsBundle.putByteArray("favorite_icon_byte_array", favoriteIconByteArray); // Create a new instance of the dialog. CreateHomeScreenShortcutDialog createHomeScreenShortcutDialog = new CreateHomeScreenShortcutDialog(); // Add the bundle to the dialog. - createHomeScreenShortcutDialog.setArguments(bundle); + createHomeScreenShortcutDialog.setArguments(argumentsBundle); // Return the new dialog. return createHomeScreenShortcutDialog; } + // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`. + @SuppressLint("InflateParams") @Override - public void onCreate(Bundle savedInstanceState) { - // Run the default commands. - super.onCreate(savedInstanceState); - + @NonNull + public Dialog onCreateDialog(Bundle savedInstanceState) { // 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. - initialShortcutName = arguments.getString("shortcut_name"); - initialUrlString = arguments.getString("url_string"); + // Get the strings from the arguments. + String initialShortcutName = arguments.getString("shortcut_name"); + String initialUrlString = arguments.getString("url_string"); // Get the favorite icon byte array. byte[] favoriteIconByteArray = arguments.getByteArray("favorite_icon_byte_array"); @@ -111,15 +111,16 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { assert favoriteIconByteArray != null; // Convert the favorite icon byte array to a bitmap and store it in a class variable. - favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); - } + Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); - // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`. - @SuppressLint("InflateParams") - @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 +133,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); @@ -151,7 +152,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { // Set an `onClick` listener on the create button. dialogBuilder.setPositiveButton(R.string.create, (DialogInterface dialog, int which) -> { // Create the home screen shortcut. - createHomeScreenShortcut(); + createHomeScreenShortcut(favoriteIconBitmap); }); // Create an alert dialog from the alert dialog builder. @@ -161,24 +162,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); + Button 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 @@ -194,7 +194,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { @Override public void afterTextChanged(Editable s) { // Update the create button. - updateCreateButton(); + updateCreateButton(createButton); } }); @@ -213,7 +213,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { @Override public void afterTextChanged(Editable s) { // Update the create button. - updateCreateButton(); + updateCreateButton(createButton); } }); @@ -224,7 +224,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { // Check the status of the create button. if (createButton.isEnabled()) { // The create button is enabled. // Create the home screen shortcut. - createHomeScreenShortcut(); + createHomeScreenShortcut(favoriteIconBitmap); // Manually dismiss the alert dialog. alertDialog.dismiss(); @@ -248,7 +248,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { // Check the status of the create button. if (createButton.isEnabled()) { // The create button is enabled. // Create the home screen shortcut. - createHomeScreenShortcut(); + createHomeScreenShortcut(favoriteIconBitmap); // Manually dismiss the alert dialog. alertDialog.dismiss(); @@ -269,7 +269,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { return alertDialog; } - private void updateCreateButton() { + private void updateCreateButton(Button createButton) { // Get the contents of the edit texts. String shortcutName = shortcutNameEditText.getText().toString(); String urlString = urlEditText.getText().toString(); @@ -278,7 +278,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { createButton.setEnabled(!shortcutName.isEmpty() && !urlString.isEmpty()); } - private void createHomeScreenShortcut() { + private void createHomeScreenShortcut(Bitmap favoriteIconBitmap) { // Get a handle for the context. Context context = getContext(); @@ -292,8 +292,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.