]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/CreateBookmarkFolder.java
7374104756f2f09dbf935be9c070278acea6f6d0
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / CreateBookmarkFolder.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;
21
22 import android.annotation.SuppressLint;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.os.Bundle;
28 // If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
29 import android.support.v7.app.AlertDialog;
30 import android.view.KeyEvent;
31 import android.view.View;
32 import android.view.WindowManager;
33 import android.widget.EditText;
34 import android.widget.ImageView;
35
36 public class CreateBookmarkFolder extends DialogFragment {
37     // The public interface is used to send information back to the parent activity.
38     public interface CreateBookmarkFolderListener {
39         void onCreateBookmarkFolder(DialogFragment dialogFragment);
40     }
41
42     // `createBookmarkFolderListener` is used in `onAttach()` and `onCreateDialog`.
43     private CreateBookmarkFolderListener createBookmarkFolderListener;
44
45     public void onAttach(Context context) {
46         super.onAttach(context);
47
48         // Get a handle for `createBookmarkFolderListener` from `context`.
49         try {
50             createBookmarkFolderListener = (CreateBookmarkFolderListener) context;
51         } catch(ClassCastException exception) {
52             throw new ClassCastException(context.toString() + " must implement CreateBookmarkFolderListener.");
53         }
54     }
55
56     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
57     @SuppressLint("InflateParams")
58     @Override
59     public Dialog onCreateDialog(Bundle savedInstanceState) {
60         // Use `AlertDialog.Builder` to create the `AlertDialog`.  The style formats the color of the button text.
61         final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
62         dialogBuilder.setTitle(R.string.create_folder);
63         // The parent view is `null` because it will be assigned by the `AlertDialog`.
64         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_folder_dialog, null));
65
66         // Set an `onClick()` listener for the negative button.
67         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
68             @Override
69             public void onClick(DialogInterface dialog, int which) {
70                 // Do nothing.  The `AlertDialog` will close automatically.
71             }
72         });
73
74         // Set an `onClick()` listener fo the positive button.
75         dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
76             @Override
77             public void onClick(DialogInterface dialog, int which) {
78                 // Return the `DialogFragment` to the parent activity on create.
79                 createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolder.this);
80             }
81         });
82
83
84         // Create an `AlertDialog` from the `AlertDialog.Builder`.
85         final AlertDialog alertDialog = dialogBuilder.create();
86
87         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
88         assert alertDialog.getWindow() != null;
89
90         // Show the keyboard when the `Dialog` is displayed on the screen.
91         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
92
93         // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below.
94         alertDialog.show();
95
96         // Allow the `enter` key on the keyboard to create the folder from `create_folder_name_edittext`.
97         EditText createFolderNameEditText = (EditText) alertDialog.findViewById(R.id.create_folder_name_edittext);
98         assert createFolderNameEditText != null;  // Remove the warning below that `createFolderNameEditText` might be `null`.
99         createFolderNameEditText.setOnKeyListener(new View.OnKeyListener() {
100             public boolean onKey(View v, int keyCode, KeyEvent event) {
101                 // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
102                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
103                     // Trigger `createBookmarkFolderListener` and return the `DialogFragment` to the parent activity.
104                     createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolder.this);
105                     // Manually dismiss the `AlertDialog`.
106                     alertDialog.dismiss();
107                     // Consume the event.
108                     return true;
109                 } else {  // If any other key was pressed do not consume the event.
110                     return false;
111                 }
112             }
113         });
114
115         // Display the current favorite icon.
116         ImageView webPageIconImageView = (ImageView) alertDialog.findViewById(R.id.create_folder_web_page_icon);
117         assert webPageIconImageView != null;  // Remove the warning that `webPageIconImageView` may be null.
118         webPageIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon);
119
120         // `onCreateDialog()` requires the return of an `AlertDialog`.
121         return alertDialog;
122     }
123 }