]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateHomeScreenShortcutDialog.java
cb9accc6fba439ecf0d9caee28f023cc38211829
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateHomeScreenShortcutDialog.java
1 /*
2  * Copyright © 2015-2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs;
21
22 import android.annotation.SuppressLint;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.graphics.drawable.BitmapDrawable;
28 import android.graphics.drawable.Drawable;
29 import android.os.Bundle;
30 import android.view.KeyEvent;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.WindowManager;
34 import android.widget.EditText;
35
36 import androidx.annotation.NonNull;
37 import androidx.fragment.app.DialogFragment;  // The AndroidX dialog fragment must be used or an error is produced on API <=22.
38
39 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
40 import com.stoutner.privacybrowser.R;
41
42 public class CreateHomeScreenShortcutDialog extends DialogFragment {
43     // The public interface is used to send information back to the parent activity.
44     public interface CreateHomeScreenShortcutListener {
45         void onCreateHomeScreenShortcut(DialogFragment dialogFragment);
46     }
47
48     //`createHomeScreenShortcutListener` is used in `onAttach()` and `onCreateDialog()`.
49     private CreateHomeScreenShortcutListener createHomeScreenShortcutListener;
50
51     // Check to make sure that the parent activity implements the listener.
52     public void onAttach(Context context) {
53         // Run the default commands.
54         super.onAttach(context);
55
56         // Get a handle for `CreateHomeScreenShortcutListener` from the launching context.
57         createHomeScreenShortcutListener = (CreateHomeScreenShortcutListener) context;
58     }
59
60     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
61     @SuppressLint("InflateParams")
62     @Override
63     @NonNull
64     public Dialog onCreateDialog(Bundle savedInstanceState) {
65         // Remove the incorrect lint warning below that `getLayoutInflater()` might be null.
66         assert getActivity() != null;
67
68         // Get the activity's layout inflater.
69         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
70
71         // Create a drawable version of the favorite icon.
72         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
73
74         // Use `AlertDialog.Builder` to create the `AlertDialog`.
75         AlertDialog.Builder dialogBuilder;
76
77         // Set the style according to the theme.
78         if (MainWebViewActivity.darkTheme) {
79             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
80         } else {
81             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
82         }
83
84         // Set the title and icon.
85         dialogBuilder.setTitle(R.string.create_shortcut);
86         dialogBuilder.setIcon(favoriteIconDrawable);
87
88         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
89         dialogBuilder.setView(layoutInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null));
90
91         // Setup the negative button.  Using null closes the dialog without doing anything else.
92         dialogBuilder.setNegativeButton(R.string.cancel, null);
93
94         // Set an `onClick` listener on the positive button.
95         dialogBuilder.setPositiveButton(R.string.create, (DialogInterface dialog, int which) -> createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcutDialog.this));
96
97         // Create an alert dialog from the alert dialog builder.
98         final AlertDialog alertDialog = dialogBuilder.create();
99
100         // Remove the warning below that `getWindow()` might be null.
101         assert alertDialog.getWindow() != null;
102
103         // Disable screenshots if not allowed.
104         if (!MainWebViewActivity.allowScreenshots) {
105             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
106         }
107
108         // Show the keyboard when the Dialog is displayed on the screen.
109         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
110
111         // We need to show the alert dialog before we can call `setOnKeyListener()` below.
112         alertDialog.show();
113
114         // Get a handle for `shortcut_name_edittext`.
115         EditText shortcutNameEditText = alertDialog.findViewById(R.id.shortcut_name_edittext);
116
117         // Set the current `WebView` title as the text for `shortcutNameEditText`.
118         shortcutNameEditText.setText(MainWebViewActivity.webViewTitle);
119
120         // Allow the "enter" key on the keyboard to create the shortcut.
121         shortcutNameEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
122             // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
123             if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
124                 // Trigger the create listener.
125                 createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcutDialog.this);
126
127                 // Manually dismiss `alertDialog`.
128                 alertDialog.dismiss();
129
130                 // Consume the event.
131                 return true;
132             } else {  // If any other key was pressed, do not consume the event.
133                 return false;
134             }
135         });
136
137         // `onCreateDialog` requires the return of an `AlertDialog`.
138         return alertDialog;
139     }
140 }