]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmarkFolderDialog.java
2e9532aa5ff34ce923c81b15d6dadb7c7b52d620
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateBookmarkFolderDialog.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.database.Cursor;
28 import android.graphics.Bitmap;
29 import android.os.Bundle;
30 import android.text.Editable;
31 import android.text.TextWatcher;
32 import android.view.KeyEvent;
33 import android.view.View;
34 import android.view.WindowManager;
35 import android.widget.Button;
36 import android.widget.EditText;
37 import android.widget.ImageView;
38
39 import androidx.annotation.NonNull;
40 import androidx.fragment.app.DialogFragment;  // The AndroidX dialog fragment must be used or an error is produced on API <=22.
41
42 import com.stoutner.privacybrowser.R;
43 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
44 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
45
46 public class CreateBookmarkFolderDialog extends DialogFragment {
47     // The public interface is used to send information back to the parent activity.
48     public interface CreateBookmarkFolderListener {
49         void onCreateBookmarkFolder(DialogFragment dialogFragment);
50     }
51
52     // `createBookmarkFolderListener` is used in `onAttach()` and `onCreateDialog`.
53     private CreateBookmarkFolderListener createBookmarkFolderListener;
54
55     public void onAttach(Context context) {
56         super.onAttach(context);
57
58         // Get a handle for `createBookmarkFolderListener` from the launching context.
59         createBookmarkFolderListener = (CreateBookmarkFolderListener) context;
60     }
61
62     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
63     @SuppressLint("InflateParams")
64     @Override
65     @NonNull
66     public Dialog onCreateDialog(Bundle savedInstanceState) {
67         // Use an alert dialog builder to create the alert dialog.
68         AlertDialog.Builder dialogBuilder;
69
70         // Set the style according to the theme.
71         if (MainWebViewActivity.darkTheme) {
72             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
73         } else {
74             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
75         }
76
77         // Set the title.
78         dialogBuilder.setTitle(R.string.create_folder);
79
80         // Remove the warning below that `getLayoutInflater()` might be null.
81         assert getActivity() != null;
82
83         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
84         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_folder_dialog, null));
85
86         // Set an `onClick()` listener for the negative button.
87         dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
88             // Do nothing.  The `AlertDialog` will close automatically.
89         });
90
91         // Set an `onClick()` listener fo the positive button.
92         dialogBuilder.setPositiveButton(R.string.create, (DialogInterface dialog, int which) -> {
93             // Return the `DialogFragment` to the parent activity on create.
94             createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolderDialog.this);
95         });
96
97
98         // Create an alert dialog from the `AlertDialog.Builder`.
99         final AlertDialog alertDialog = dialogBuilder.create();
100
101         // Remove the warning below that `getWindow()` might be null.
102         assert alertDialog.getWindow() != null;
103
104         // Disable screenshots if not allowed.
105         if (!MainWebViewActivity.allowScreenshots) {
106             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
107         }
108
109         // The alert dialog must be shown before items in the alert dialog can be modified.
110         alertDialog.show();
111
112         // Get handles for the views in the dialog.
113         final Button createButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
114         EditText folderNameEditText = alertDialog.findViewById(R.id.create_folder_name_edittext);
115         ImageView webPageIconImageView = alertDialog.findViewById(R.id.create_folder_web_page_icon);
116
117         // Initially disable the create button.
118         createButton.setEnabled(false);
119
120         // Initialize the database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
121         final BookmarksDatabaseHelper bookmarksDatabaseHelper = new BookmarksDatabaseHelper(getContext(), null, null, 0);
122
123         // Enable the create button if the new folder name is unique.
124         folderNameEditText.addTextChangedListener(new TextWatcher() {
125             @Override
126             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
127                 // Do nothing.
128             }
129
130             @Override
131             public void onTextChanged(CharSequence s, int start, int before, int count) {
132                 // Do nothing.
133             }
134
135             @Override
136             public void afterTextChanged(Editable s) {
137                 // Convert the current text to a string.
138                 String folderName = s.toString();
139
140                 // Check if a folder with the name already exists.
141                 Cursor folderExistsCursor = bookmarksDatabaseHelper.getFolder(folderName);
142
143                 // Enable the create button if the new folder name is not empty and doesn't already exist.
144                 createButton.setEnabled(!folderName.isEmpty() && (folderExistsCursor.getCount() == 0));
145             }
146         });
147
148         // Allow the enter key on the keyboard to create the folder from `create_folder_name_edittext`.
149         folderNameEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
150             // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
151             if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER) && createButton.isEnabled()) {  // The enter key was pressed and the create button is enabled.
152                 // Trigger `createBookmarkFolderListener` and return the `DialogFragment` to the parent activity.
153                 createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolderDialog.this);
154                 // Manually dismiss the `AlertDialog`.
155                 alertDialog.dismiss();
156                 // Consume the event.
157                 return true;
158             } else {  // If any other key was pressed, or if the create button is currently disabled, do not consume the event.
159                 return false;
160             }
161         });
162
163         // Get a copy of the favorite icon bitmap.
164         Bitmap favoriteIconBitmap = MainWebViewActivity.favoriteIconBitmap;
165
166         // Scale the favorite icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
167         if ((favoriteIconBitmap.getHeight() > 256) || (favoriteIconBitmap.getWidth() > 256)) {
168             favoriteIconBitmap = Bitmap.createScaledBitmap(favoriteIconBitmap, 256, 256, true);
169         }
170
171         // Display the current favorite icon.
172         webPageIconImageView.setImageBitmap(favoriteIconBitmap);
173
174         // Return the alert dialog.
175         return alertDialog;
176     }
177 }