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=93e0ec86697d23b1263affea30f3441beba5208e;hp=95a3ef5ed2bc7312cf585996d93bcc0dc4043692;hb=c1c9a0bf83ecef671356d554bb6e4927392b1cc8;hpb=231b7c038227e36f96ed28e9c2ff8bde793133ec 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 95a3ef5e..93e0ec86 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java @@ -56,15 +56,12 @@ 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 RadioButton openWithPrivacyBrowserRadioButton; - private Button createButton; + // 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(); @@ -75,38 +72,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 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"); @@ -115,14 +112,8 @@ 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) { // Get a handle for the shared preferences. SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); @@ -162,7 +153,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. @@ -183,7 +174,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { 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); + Button createButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); // Populate the edit texts. shortcutNameEditText.setText(initialShortcutName); @@ -204,7 +195,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { @Override public void afterTextChanged(Editable s) { // Update the create button. - updateCreateButton(); + updateCreateButton(createButton); } }); @@ -223,7 +214,7 @@ public class CreateHomeScreenShortcutDialog extends DialogFragment { @Override public void afterTextChanged(Editable s) { // Update the create button. - updateCreateButton(); + updateCreateButton(createButton); } }); @@ -234,7 +225,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(); @@ -258,7 +249,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(); @@ -279,7 +270,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(); @@ -288,7 +279,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();