]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmarkDialog.java
b2966c14a4fb9fd067c22a55aac95a3c6b6ccfee
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateBookmarkDialog.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.graphics.drawable.BitmapDrawable;
28 import android.graphics.drawable.Drawable;
29 import android.os.Bundle;
30 import android.support.annotation.NonNull;
31 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <= 22.
32 import android.support.v7.app.AppCompatDialogFragment;
33 import android.view.KeyEvent;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.EditText;
37
38 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39 import com.stoutner.privacybrowser.R;
40
41 public class CreateBookmarkDialog extends AppCompatDialogFragment {
42     // The public interface is used to send information back to the parent activity.
43     public interface CreateBookmarkListener {
44         void onCreateBookmark(AppCompatDialogFragment dialogFragment);
45     }
46
47     // `createBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
48     private CreateBookmarkListener createBookmarkListener;
49
50
51     public void onAttach(Context context) {
52         super.onAttach(context);
53
54         // Get a handle for `CreateBookmarkListener` from `context`.
55         try {
56             createBookmarkListener = (CreateBookmarkListener) context;
57         } catch(ClassCastException exception) {
58             throw new ClassCastException(context.toString() + " must implement `CreateBookmarkListener`.");
59         }
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         // Create a drawable version of the favorite icon.
68         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
69
70         // Use `AlertDialog.Builder` to create the `AlertDialog`.
71         AlertDialog.Builder dialogBuilder;
72
73         // Set the style according to the theme.
74         if (MainWebViewActivity.darkTheme) {
75             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
76         } else {
77             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
78         }
79
80         // Set the title and icon.
81         dialogBuilder.setTitle(R.string.create_bookmark);
82         dialogBuilder.setIcon(favoriteIconDrawable);
83
84         // Set the view.  The parent view is `null` because it will be assigned by the `AlertDialog`.
85         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_dialog, null));
86
87         // Set an `onClick()` listener for the negative button.
88         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
89             @Override
90             public void onClick(DialogInterface dialog, int which) {
91                 // Do nothing.  The `AlertDialog` will close automatically.
92             }
93         });
94
95         // Set an `onClick()` listener for the positive button.
96         dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
97             @Override
98             public void onClick(DialogInterface dialog, int which) {
99                 // Return the `DialogFragment` to the parent activity.
100                 createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
101             }
102         });
103
104
105         // Create an `AlertDialog` from the `AlertDialog.Builder`.
106         final AlertDialog alertDialog = dialogBuilder.create();
107
108         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
109         assert alertDialog.getWindow() != null;
110
111         // Show the keyboard when the `AlertDialog` is displayed on the screen.
112         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
113
114         // The `AlertDialog` needs to be shown before `setOnKeyListener()` can be called.
115         alertDialog.show();
116
117         // Get a handle for `create_bookmark_name_edittext`.
118         EditText createBookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_name_edittext);
119
120         // Set the current `WebView` title as the text for `create_bookmark_name_edittext`.
121         createBookmarkNameEditText.setText(MainWebViewActivity.webViewTitle);
122
123         // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_name_edittext`.
124         createBookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
125             public boolean onKey(View view, int keyCode, KeyEvent event) {
126                 // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
127                 if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
128                     // Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity.
129                     createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
130
131                     // Manually dismiss the `AlertDialog`.
132                     alertDialog.dismiss();
133
134                     // Consume the event.
135                     return true;
136                 } else {  // If any other key was pressed, do not consume the event.
137                     return false;
138                 }
139             }
140         });
141
142         // Set the formattedUrlString as the initial text of `create_bookmark_url_edittext`.
143         EditText createBookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_url_edittext);
144         createBookmarkUrlEditText.setText(MainWebViewActivity.formattedUrlString);
145
146         // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
147         createBookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
148             public boolean onKey(View v, int keyCode, KeyEvent event) {
149                 // If the event is a key-down on the "enter" key, select the PositiveButton "Create".
150                 if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
151                     // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity.
152                     createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
153
154                     // Manually dismiss the `AlertDialog`.
155                     alertDialog.dismiss();
156
157                     // Consume the event.
158                     return true;
159                 } else { // If any other key was pressed, do not consume the event.
160                     return false;
161                 }
162             }
163         });
164
165         // `onCreateDialog()` requires the return of an `AlertDialog`.
166         return alertDialog;
167     }
168 }