]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/CreateBookmark.java
Create Java subpackage folders.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / CreateBookmark.java
1 /**
2  * Copyright 2016 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.Dialog;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.Drawable;
28 import android.os.Bundle;
29 import android.support.annotation.NonNull;
30 // If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
31 import android.support.v7.app.AlertDialog;
32 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
33 import android.support.v7.app.AppCompatDialogFragment;
34 import android.view.KeyEvent;
35 import android.view.View;
36 import android.view.WindowManager;
37 import android.widget.EditText;
38
39 import com.stoutner.privacybrowser.activities.MainWebView;
40 import com.stoutner.privacybrowser.R;
41
42 public class CreateBookmark extends AppCompatDialogFragment {
43     // The public interface is used to send information back to the parent activity.
44     public interface CreateBookmarkListener {
45         void onCreateBookmark(AppCompatDialogFragment 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 `context`.
56         try {
57             createBookmarkListener = (CreateBookmarkListener) context;
58         } catch(ClassCastException exception) {
59             throw new ClassCastException(context.toString() + " must implement CreateBookmarkListener.");
60         }
61     }
62
63     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
64     @SuppressLint("InflateParams")
65     @Override
66     @NonNull
67     public Dialog onCreateDialog(Bundle savedInstanceState) {
68         // Create a drawable version of the favorite icon.
69         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebView.favoriteIcon);
70
71         // Use `AlertDialog.Builder` to create the `AlertDialog`.  The style formats the color of the button text.
72         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
73         dialogBuilder.setTitle(R.string.create_bookmark);
74         dialogBuilder.setIcon(favoriteIconDrawable);
75         // The parent view is `null` because it will be assigned by the `AlertDialog`.
76         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_dialog, null));
77
78         // Set an `onClick()` listener for the negative button.
79         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
80             @Override
81             public void onClick(DialogInterface dialog, int which) {
82                 // Do nothing.  The `AlertDialog` will close automatically.
83             }
84         });
85
86         // Set an `onClick()` listener for the positive button.
87         dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
88             @Override
89             public void onClick(DialogInterface dialog, int which) {
90                 // Return the `DialogFragment` to the parent activity on create.
91                 createBookmarkListener.onCreateBookmark(CreateBookmark.this);
92             }
93         });
94
95
96         // Create an `AlertDialog` from the `AlertDialog.Builder`.
97         final AlertDialog alertDialog = dialogBuilder.create();
98
99         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
100         assert alertDialog.getWindow() != null;
101
102         // Show the keyboard when the `Dialog` is displayed on the screen.
103         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
104
105         // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below.
106         alertDialog.show();
107
108         // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_name_edittext`.
109         EditText createBookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_name_edittext);
110         assert createBookmarkNameEditText != null;  // Remove the warning below that `createBookmarkNameEditText` might be `null`.
111         createBookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
112             public boolean onKey(View v, int keyCode, KeyEvent event) {
113                 // If the event is a key-down on the `enter` button, select the `PositiveButton` `Create`.
114                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
115                     // Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity.
116                     createBookmarkListener.onCreateBookmark(CreateBookmark.this);
117                     // Manually dismiss the `AlertDialog`.
118                     alertDialog.dismiss();
119                     // Consume the event.
120                     return true;
121                 } else {  // If any other key was pressed, do not consume the event.
122                     return false;
123                 }
124             }
125         });
126
127         // Set the formattedUrlString as the initial text of `create_bookmark_url_edittext`.
128         EditText createBookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_url_edittext);
129         assert createBookmarkUrlEditText != null;// Remove the warning below that `createBookmarkUrlEditText` might be `null`.
130         createBookmarkUrlEditText.setText(MainWebView.formattedUrlString);
131
132         // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
133         createBookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
134             public boolean onKey(View v, int keyCode, KeyEvent event) {
135                 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
136                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
137                     // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity.
138                     createBookmarkListener.onCreateBookmark(CreateBookmark.this);
139                     // Manually dismiss the `AlertDialog`.
140                     alertDialog.dismiss();
141                     // Consume the event.
142                     return true;
143                 } else { // If any other key was pressed, do not consume the event.
144                     return false;
145                 }
146             }
147         });
148
149         // `onCreateDialog()` requires the return of an `AlertDialog`.
150         return alertDialog;
151     }
152 }