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=cb9accc6fba439ecf0d9caee28f023cc38211829;hp=dbda6c791afe5c0fe9aca4745da68fba7185264c;hb=ba40295dffd761ccdc95d3b46ca7acbad1f00d5e;hpb=5bcf4ca90f27512b94fb7aca4fad37b4e4774655 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 dbda6c79..cb9accc6 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java @@ -1,5 +1,5 @@ /* - * Copyright © 2015-2017 Soren Stoutner . + * Copyright © 2015-2019 Soren Stoutner . * * This file is part of 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,6 +62,9 @@ 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(); @@ -83,60 +85,52 @@ public class CreateHomeScreenShortcutDialog extends AppCompatDialogFragment { dialogBuilder.setTitle(R.string.create_shortcut); dialogBuilder.setIcon(favoriteIconDrawable); - // Set the view. 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; } });