]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/CreateBookmark.java
Remove the third-party cookies icon from the list of additional icons to display...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / 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;
21
22 import android.app.Activity;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.DialogInterface;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.Drawable;
28 import android.os.Bundle;
29 // If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
30 import android.support.v7.app.AlertDialog;
31 import android.view.KeyEvent;
32 import android.view.View;
33 import android.view.WindowManager;
34 import android.widget.EditText;
35
36 public class CreateBookmark extends DialogFragment {
37     // The public interface is used to send information back to the parent activity.
38     public interface CreateBookmarkListener {
39         void onCreateBookmark(DialogFragment dialogFragment);
40     }
41
42     // `createBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
43     private CreateBookmarkListener createBookmarkListener;
44
45
46     public void onAttach(Activity parentActivity) {
47         super.onAttach(parentActivity);
48
49         // Get a handle for `CreateBookmarkListener` from `parentActivity`.
50         try {
51             createBookmarkListener = (CreateBookmarkListener) parentActivity;
52         } catch(ClassCastException exception) {
53             throw new ClassCastException(parentActivity.toString() + " must implement CreateBookmarkListener.");
54         }
55     }
56
57     @Override
58     public Dialog onCreateDialog(Bundle savedInstanceState) {
59         // Create a drawable version of the favorite icon.
60         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
61
62         // Use `AlertDialog.Builder` to create the `AlertDialog`.  The style formats the color of the button text.
63         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
64         dialogBuilder.setTitle(R.string.create_bookmark);
65         dialogBuilder.setIcon(favoriteIconDrawable);
66         // The parent view is `null` because it will be assigned by the `AlertDialog`.
67         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_dialog, null));
68
69         // Set an `onClick()` listener for the negative button.
70         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
71             @Override
72             public void onClick(DialogInterface dialog, int which) {
73                 // Do nothing.  The `AlertDialog` will close automatically.
74             }
75         });
76
77         // Set an `onClick()` listener for the positive button.
78         dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
79             @Override
80             public void onClick(DialogInterface dialog, int which) {
81                 // Return the `DialogFragment` to the parent activity on create.
82                 createBookmarkListener.onCreateBookmark(CreateBookmark.this);
83             }
84         });
85
86
87         // Create an `AlertDialog` from the `AlertDialog.Builder`.
88         final AlertDialog alertDialog = dialogBuilder.create();
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 bookmark from `create_bookmark_name_edittext`.
97         EditText createBookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_name_edittext);
98         assert createBookmarkNameEditText != null;  // Remove the warning below that `createBookmarkNameEditText` might be `null`.
99         createBookmarkNameEditText.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` button, select the `PositiveButton` `Create`.
102                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
103                     // Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity.
104                     createBookmarkListener.onCreateBookmark(CreateBookmark.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         // Set the formattedUrlString as the initial text of `create_bookmark_url_edittext`.
116         EditText createBookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_url_edittext);
117         assert createBookmarkUrlEditText != null;// Remove the warning below that `createBookmarkUrlEditText` might be `null`.
118         createBookmarkUrlEditText.setText(MainWebViewActivity.formattedUrlString);
119
120         // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
121         createBookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
122             public boolean onKey(View v, int keyCode, KeyEvent event) {
123                 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
124                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
125                     // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity.
126                     createBookmarkListener.onCreateBookmark(CreateBookmark.this);
127                     // Manually dismiss the `AlertDialog`.
128                     alertDialog.dismiss();
129                     // Consume the event.
130                     return true;
131                 } else { // If any other key was pressed, do not consume the event.
132                     return false;
133                 }
134             }
135         });
136
137         // `onCreateDialog()` requires the return of an `AlertDialog`.
138         return alertDialog;
139     }
140 }