]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/CreateBookmark.java
f844ca340ba820b1f6eb8f2ebfc5e11b435a157f
[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 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 import android.view.KeyEvent;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.EditText;
37
38 public class CreateBookmark extends DialogFragment {
39     // The public interface is used to send information back to the parent activity.
40     public interface CreateBookmarkListener {
41         void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment);
42
43         void onCreateBookmarkCreate(DialogFragment createBookmarkDialogFragment);
44     }
45
46     // createBookmarkListener is used in onAttach() and onCreateDialog()
47     private CreateBookmarkListener createBookmarkListener;
48
49     // Check to make sure the parent activity implements the listener.
50     public void onAttach(Activity parentActivity) {
51         super.onAttach(parentActivity);
52         try {
53             createBookmarkListener = (CreateBookmarkListener) parentActivity;
54         } catch(ClassCastException exception) {
55             throw new ClassCastException(parentActivity.toString() + " must implement CreateBookmarkListener.");
56         }
57     }
58
59     // onCreateDialog requires @NonNull.
60     @Override
61     @NonNull
62     public Dialog onCreateDialog(Bundle savedInstanceState) {
63         // Create a drawable version of the favorite icon.
64         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
65
66         // Get the activity's layout inflater.
67         LayoutInflater customDialogInflater = getActivity().getLayoutInflater();
68
69         // Use AlertDialog.Builder to create the AlertDialog.  The style formats the color of the button text.
70         AlertDialog.Builder createBookmarkDialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowser_AlertDialog);
71         createBookmarkDialogBuilder.setTitle(R.string.create_bookmark);
72         createBookmarkDialogBuilder.setIcon(favoriteIconDrawable);
73         // The parent view is "null" because it will be assigned by AlertDialog.
74         createBookmarkDialogBuilder.setView(customDialogInflater.inflate(R.layout.create_bookmark_dialog, null));
75
76         // Set an onClick listener on the negative button.
77         createBookmarkDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
78             @Override
79             public void onClick(DialogInterface dialog, int which) {
80                 createBookmarkListener.onCreateBookmarkCancel(CreateBookmark.this);
81             }
82         });
83
84         // Set an onClick listener on the positive button.
85         createBookmarkDialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
86             @Override
87             public void onClick(DialogInterface dialog, int which) {
88                 createBookmarkListener.onCreateBookmarkCreate(CreateBookmark.this);
89             }
90         });
91
92
93         // Create an AlertDialog from the AlertDialog.Builder.
94         final AlertDialog createBookmarkDialog = createBookmarkDialogBuilder.create();
95
96         // Show the keyboard when the Dialog is displayed on the screen.
97         createBookmarkDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
98
99         // We need to show the AlertDialog before we can call setOnKeyListener() below.
100         createBookmarkDialog.show();
101
102         // Allow the "enter" key on the keyboard to create the bookmark from "create_bookmark_name_edittext".
103         EditText createBookmarkNameEditText = (EditText) createBookmarkDialog.findViewById(R.id.create_bookmark_name_edittext);
104         assert createBookmarkNameEditText != null;  // Remove the warning below that createBookmarkNameEditText might be null.
105         createBookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
106             public boolean onKey(View v, int keyCode, KeyEvent event) {
107                 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
108                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
109                     // Trigger the createBookmarkListener.
110                     createBookmarkListener.onCreateBookmarkCreate(CreateBookmark.this);
111
112                     // Manually dismiss the AlertDialog.
113                     createBookmarkDialog.dismiss();
114
115                     // Consume the event.
116                     return true;
117                 } else {  // If any other key was pressed, do not consume the event.
118                     return false;
119                 }
120             }
121         });
122
123         // Set the formattedUrlString as the initial text of "create_bookmark_url_edittext".
124         EditText createBookmarkUrlEditText = (EditText) createBookmarkDialog.findViewById(R.id.create_bookmark_url_edittext);
125         assert createBookmarkUrlEditText != null;// Remove the warning below that createBookmarkUrlEditText might be null.
126         createBookmarkUrlEditText.setText(MainWebViewActivity.formattedUrlString);
127
128         // Allow the "enter" key on the keyboard to create the bookmark from "create_bookmark_url_edittext".
129         createBookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
130             public boolean onKey(View v, int keyCode, KeyEvent event) {
131                 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
132                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
133                     // Trigger the create listener.
134                     createBookmarkListener.onCreateBookmarkCreate(CreateBookmark.this);
135
136                     // Manually dismiss the AlertDialog.
137                     createBookmarkDialog.dismiss();
138
139                     // Consume the event.
140                     return true;
141                 } else { // If any other key was pressed, do not consume the event.
142                     return false;
143                 }
144             }
145         });
146
147         // onCreateDialog requires the return of an AlertDialog
148         return createBookmarkDialog;
149     }
150 }