]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmarkFolderDialog.java
Create a dark theme for the `AlertDialogs`.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateBookmarkFolderDialog.java
1 /*
2  * Copyright © 2016-2017 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.os.Bundle;
28 import android.support.annotation.NonNull;
29 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
30 import android.support.v7.app.AppCompatDialogFragment;
31 import android.view.KeyEvent;
32 import android.view.View;
33 import android.view.WindowManager;
34 import android.widget.EditText;
35 import android.widget.ImageView;
36
37 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
38 import com.stoutner.privacybrowser.R;
39
40 public class CreateBookmarkFolderDialog extends AppCompatDialogFragment {
41     // The public interface is used to send information back to the parent activity.
42     public interface CreateBookmarkFolderListener {
43         void onCreateBookmarkFolder(AppCompatDialogFragment dialogFragment);
44     }
45
46     // `createBookmarkFolderListener` is used in `onAttach()` and `onCreateDialog`.
47     private CreateBookmarkFolderListener createBookmarkFolderListener;
48
49     public void onAttach(Context context) {
50         super.onAttach(context);
51
52         // Get a handle for `createBookmarkFolderListener` from `context`.
53         try {
54             createBookmarkFolderListener = (CreateBookmarkFolderListener) context;
55         } catch(ClassCastException exception) {
56             throw new ClassCastException(context.toString() + " must implement CreateBookmarkFolderListener.");
57         }
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         // Use `AlertDialog.Builder` to create the `AlertDialog`.
66         AlertDialog.Builder dialogBuilder;
67
68         // Set the style according to the theme.
69         if (MainWebViewActivity.darkTheme) {
70             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
71         } else {
72             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
73         }
74
75         // Set the title.
76         dialogBuilder.setTitle(R.string.create_folder);
77
78         // Set the view.  The parent view is `null` because it will be assigned by the `AlertDialog`.
79         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_folder_dialog, null));
80
81         // Set an `onClick()` listener for the negative button.
82         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
83             @Override
84             public void onClick(DialogInterface dialog, int which) {
85                 // Do nothing.  The `AlertDialog` will close automatically.
86             }
87         });
88
89         // Set an `onClick()` listener fo the positive button.
90         dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
91             @Override
92             public void onClick(DialogInterface dialog, int which) {
93                 // Return the `DialogFragment` to the parent activity on create.
94                 createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolderDialog.this);
95             }
96         });
97
98
99         // Create an `AlertDialog` from the `AlertDialog.Builder`.
100         final AlertDialog alertDialog = dialogBuilder.create();
101
102         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
103         assert alertDialog.getWindow() != null;
104
105         // Show the keyboard when the `Dialog` is displayed on the screen.
106         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
107
108         // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below.
109         alertDialog.show();
110
111         // Allow the `enter` key on the keyboard to create the folder from `create_folder_name_edittext`.
112         EditText createFolderNameEditText = (EditText) alertDialog.findViewById(R.id.create_folder_name_edittext);
113         createFolderNameEditText.setOnKeyListener(new View.OnKeyListener() {
114             public boolean onKey(View v, int keyCode, KeyEvent event) {
115                 // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
116                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
117                     // Trigger `createBookmarkFolderListener` and return the `DialogFragment` to the parent activity.
118                     createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolderDialog.this);
119                     // Manually dismiss the `AlertDialog`.
120                     alertDialog.dismiss();
121                     // Consume the event.
122                     return true;
123                 } else {  // If any other key was pressed do not consume the event.
124                     return false;
125                 }
126             }
127         });
128
129         // Display the current favorite icon.
130         ImageView webPageIconImageView = (ImageView) alertDialog.findViewById(R.id.create_folder_web_page_icon);
131         webPageIconImageView.setImageBitmap(MainWebViewActivity.favoriteIconBitmap);
132
133         // `onCreateDialog()` requires the return of an `AlertDialog`.
134         return alertDialog;
135     }
136 }