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