]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmarkDialog.java
Scale bookmark favorite icons larger than 256 x 256 to fix a crash. https://redmine...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateBookmarkDialog.java
1 /*
2  * Copyright © 2016-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.Bitmap;
28 import android.graphics.drawable.BitmapDrawable;
29 import android.graphics.drawable.Drawable;
30 import android.os.Bundle;
31 import android.view.KeyEvent;
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 CreateBookmarkDialog extends DialogFragment {
43     // The public interface is used to send information back to the parent activity.
44     public interface CreateBookmarkListener {
45         void onCreateBookmark(DialogFragment dialogFragment);
46     }
47
48     // `createBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
49     private CreateBookmarkListener createBookmarkListener;
50
51
52     public void onAttach(Context context) {
53         super.onAttach(context);
54
55         // Get a handle for `CreateBookmarkListener` from the launching context.
56         createBookmarkListener = (CreateBookmarkListener) context;
57     }
58
59     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
60     @SuppressLint("InflateParams")
61     @Override
62     @NonNull
63     public Dialog onCreateDialog(Bundle savedInstanceState) {
64         // Get a copy of the favorite icon bitmap.
65         Bitmap favoriteIconBitmap = MainWebViewActivity.favoriteIconBitmap;
66
67         // Scale the favorite icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
68         if ((favoriteIconBitmap.getHeight() > 256) || (favoriteIconBitmap.getWidth() > 256)) {
69             favoriteIconBitmap = Bitmap.createScaledBitmap(favoriteIconBitmap, 256, 256, true);
70         }
71
72         // Create a drawable version of the favorite icon.
73         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), favoriteIconBitmap);
74
75         // Use an alert dialog builder to create the alert dialog.
76         AlertDialog.Builder dialogBuilder;
77
78         // Set the style according to the theme.
79         if (MainWebViewActivity.darkTheme) {
80             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
81         } else {
82             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
83         }
84
85         // Set the title and icon.
86         dialogBuilder.setTitle(R.string.create_bookmark);
87         dialogBuilder.setIcon(favoriteIconDrawable);
88
89         // Remove the warning below that `getLayoutInflater()` might be null.
90         assert getActivity() != null;
91
92         // Set the view.  The parent view is `null` because it will be assigned by the `AlertDialog`.
93         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_dialog, null));
94
95         // Set an `onClick()` listener for the negative button.
96         dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
97             // Do nothing.  The `AlertDialog` will close automatically.
98         });
99
100         // Set an `onClick()` listener for the positive button.
101         dialogBuilder.setPositiveButton(R.string.create, (DialogInterface dialog, int which) -> {
102             // Return the `DialogFragment` to the parent activity.
103             createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
104         });
105
106         // Create an `AlertDialog` from the `AlertDialog.Builder`.
107         final AlertDialog alertDialog = dialogBuilder.create();
108
109         // Remove the warning below that `getWindow()` might be null.
110         assert alertDialog.getWindow() != null;
111
112         // Disable screenshots if not allowed.
113         if (!MainWebViewActivity.allowScreenshots) {
114             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
115         }
116
117         // Show the keyboard when the `AlertDialog` is displayed on the screen.
118         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
119
120         // The alert dialog needs to be shown before `setOnKeyListener()` can be called.
121         alertDialog.show();
122
123         // Get a handle for `create_bookmark_name_edittext`.
124         EditText createBookmarkNameEditText = alertDialog.findViewById(R.id.create_bookmark_name_edittext);
125
126         // Set the current `WebView` title as the text for `create_bookmark_name_edittext`.
127         createBookmarkNameEditText.setText(MainWebViewActivity.webViewTitle);
128
129         // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_name_edittext`.
130         createBookmarkNameEditText.setOnKeyListener((View view, int keyCode, KeyEvent event) -> {
131             // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
132             if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
133                 // Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity.
134                 createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
135
136                 // Manually dismiss the `AlertDialog`.
137                 alertDialog.dismiss();
138
139                 // Consume the event.
140                 return true;
141             } else {  // If any other key was pressed, do not consume the event.
142                 return false;
143             }
144         });
145
146         // Set the formattedUrlString as the initial text of `create_bookmark_url_edittext`.
147         EditText createBookmarkUrlEditText = alertDialog.findViewById(R.id.create_bookmark_url_edittext);
148         createBookmarkUrlEditText.setText(MainWebViewActivity.formattedUrlString);
149
150         // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
151         createBookmarkUrlEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
152             // If the event is a key-down on the "enter" key, select the PositiveButton "Create".
153             if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
154                 // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity.
155                 createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
156
157                 // Manually dismiss the `AlertDialog`.
158                 alertDialog.dismiss();
159
160                 // Consume the event.
161                 return true;
162             } else { // If any other key was pressed, do not consume the event.
163                 return false;
164             }
165         });
166
167         // Return the alert dialog.
168         return alertDialog;
169     }
170 }