]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java
Migrate to AndroidX from the Android Support Library. https://redmine.stoutner.com...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateHomeScreenShortcutDialog.java
index 89e27f5439204e43c981175c86bd8bc90f1ef08b..cb9accc6fba439ecf0d9caee28f023cc38211829 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2015-2017 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2015-2019 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -27,35 +27,34 @@ import android.content.DialogInterface;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
-import android.support.annotation.NonNull;
-// We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
-import android.support.v7.app.AppCompatDialogFragment;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.WindowManager;
 import android.widget.EditText;
 
+import androidx.annotation.NonNull;
+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.R;
 
-public class CreateHomeScreenShortcutDialog extends AppCompatDialogFragment {
+public class CreateHomeScreenShortcutDialog extends DialogFragment {
     // The public interface is used to send information back to the parent activity.
-    public interface CreateHomeScreenSchortcutListener {
-        void onCreateHomeScreenShortcut(AppCompatDialogFragment dialogFragment);
+    public interface CreateHomeScreenShortcutListener {
+        void onCreateHomeScreenShortcut(DialogFragment dialogFragment);
     }
 
     //`createHomeScreenShortcutListener` is used in `onAttach()` and `onCreateDialog()`.
-    private CreateHomeScreenSchortcutListener createHomeScreenShortcutListener;
+    private CreateHomeScreenShortcutListener createHomeScreenShortcutListener;
 
     // Check to make sure that the parent activity implements the listener.
     public void onAttach(Context context) {
+        // Run the default commands.
         super.onAttach(context);
-        try {
-            createHomeScreenShortcutListener = (CreateHomeScreenSchortcutListener) context;
-        } catch(ClassCastException exception) {
-            throw new ClassCastException(context.toString() + " must implement CreateHomeScreenShortcutListener.");
-        }
+
+        // Get a handle for `CreateHomeScreenShortcutListener` from the launching context.
+        createHomeScreenShortcutListener = (CreateHomeScreenShortcutListener) context;
     }
 
     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
@@ -63,70 +62,75 @@ public class CreateHomeScreenShortcutDialog extends AppCompatDialogFragment {
     @Override
     @NonNull
     public Dialog onCreateDialog(Bundle savedInstanceState) {
+        // Remove the incorrect lint warning below that `getLayoutInflater()` might be null.
+        assert getActivity() != null;
+
         // Get the activity's layout inflater.
         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
 
         // Create a drawable version of the favorite icon.
         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
 
-        // Use `AlertDialog.Builder` to create the `AlertDialog`.  `R.style.LightAlertDialog` formats the color of the button text.
-        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
+        // Use `AlertDialog.Builder` to create the `AlertDialog`.
+        AlertDialog.Builder dialogBuilder;
+
+        // Set the style according to the theme.
+        if (MainWebViewActivity.darkTheme) {
+            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
+        } else {
+            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
+        }
+
+        // Set the title and icon.
         dialogBuilder.setTitle(R.string.create_shortcut);
         dialogBuilder.setIcon(favoriteIconDrawable);
-        // The parent view is `null` because it will be assigned by `AlertDialog`.
+
+        // Set the view.  The parent view is null because it will be assigned by the alert dialog.
         dialogBuilder.setView(layoutInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null));
 
-        // Set an `onClick` listener on the negative button.
-        dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
-            @Override
-            public void onClick(DialogInterface dialog, int which) {
-                // Do nothing if `Cancel` is clicked.
-            }
-        });
+        // Setup the negative button.  Using null closes the dialog without doing anything else.
+        dialogBuilder.setNegativeButton(R.string.cancel, null);
 
         // Set an `onClick` listener on the positive button.
-        dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
-            @Override
-            public void onClick(DialogInterface dialog, int which) {
-                createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcutDialog.this);
-            }
-        });
+        dialogBuilder.setPositiveButton(R.string.create, (DialogInterface dialog, int which) -> createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcutDialog.this));
 
-
-        // Create an `AlertDialog` from the `AlertDialog.Builder`.
+        // Create an alert dialog from the alert dialog builder.
         final AlertDialog alertDialog = dialogBuilder.create();
 
-        // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
+        // Remove the warning below that `getWindow()` might be null.
         assert alertDialog.getWindow() != null;
 
+        // Disable screenshots if not allowed.
+        if (!MainWebViewActivity.allowScreenshots) {
+            alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
+        }
+
         // Show the keyboard when the Dialog is displayed on the screen.
         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
 
-        // We need to show `alertDialog` before we can call `setOnKeyListener()` below.
+        // We need to show the alert dialog before we can call `setOnKeyListener()` below.
         alertDialog.show();
 
         // Get a handle for `shortcut_name_edittext`.
-        EditText shortcutNameEditText = (EditText) alertDialog.findViewById(R.id.shortcut_name_edittext);
+        EditText shortcutNameEditText = alertDialog.findViewById(R.id.shortcut_name_edittext);
 
         // Set the current `WebView` title as the text for `shortcutNameEditText`.
         shortcutNameEditText.setText(MainWebViewActivity.webViewTitle);
 
         // Allow the "enter" key on the keyboard to create the shortcut.
-        shortcutNameEditText.setOnKeyListener(new View.OnKeyListener() {
-            public boolean onKey(View v, int keyCode, KeyEvent event) {
-                // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
-                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
-                    // Trigger the create listener.
-                    createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcutDialog.this);
-
-                    // Manually dismiss `alertDialog`.
-                    alertDialog.dismiss();
-
-                    // Consume the event.
-                    return true;
-                } else {  // If any other key was pressed, do not consume the event.
-                    return false;
-                }
+        shortcutNameEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
+            // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
+            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
+                // Trigger the create listener.
+                createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcutDialog.this);
+
+                // Manually dismiss `alertDialog`.
+                alertDialog.dismiss();
+
+                // Consume the event.
+                return true;
+            } else {  // If any other key was pressed, do not consume the event.
+                return false;
             }
         });