]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/EditBookmarkDialog.java
974346ded97a5a05ebf51cbce4c6f6989a24f11c
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / EditBookmarkDialog.java
1 /*
2  * Copyright 2016-2017 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.AlertDialog;
24 import android.app.Dialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.database.Cursor;
28 import android.graphics.Bitmap;
29 import android.graphics.BitmapFactory;
30 import android.os.Bundle;
31 import android.support.annotation.NonNull;
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 import android.widget.ImageView;
39
40 import com.stoutner.privacybrowser.activities.BookmarksActivity;
41 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
42 import com.stoutner.privacybrowser.R;
43 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
44
45 public class EditBookmarkDialog extends AppCompatDialogFragment {
46     // The public interface is used to send information back to the parent activity.
47     public interface EditBookmarkListener {
48         void onSaveEditBookmark(AppCompatDialogFragment dialogFragment);
49     }
50
51     // `editBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
52     private EditBookmarkListener editBookmarkListener;
53
54     public void onAttach(Context context) {
55         super.onAttach(context);
56
57         // Get a handle for `EditBookmarkListener` from `context`.
58         try {
59             editBookmarkListener = (EditBookmarkListener) context;
60         } catch(ClassCastException exception) {
61             throw new ClassCastException(context.toString() + " must implement EditBookmarkListener.");
62         }
63     }
64
65     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
66     @SuppressLint("InflateParams")
67     @Override
68     @NonNull
69     public Dialog onCreateDialog(Bundle savedInstanceState) {
70         // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
71         long[] selectedBookmarkLongArray = BookmarksActivity.checkedItemIds;
72         int selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
73
74         // Get a `Cursor` with the specified bookmark and move it to the first position.
75         Cursor bookmarkCursor = BookmarksActivity.bookmarksDatabaseHelper.getBookmarkCursor(selectedBookmarkDatabaseId);
76         bookmarkCursor.moveToFirst();
77
78         // Use `AlertDialog.Builder` to create the `AlertDialog`.  The style formats the color of the button text.
79         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
80         dialogBuilder.setTitle(R.string.edit_bookmark);
81         // The parent view is `null` because it will be assigned by `AlertDialog`.
82         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_dialog, null));
83
84         // Set an `onClick()` listener for the negative button.
85         dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
86             @Override
87             public void onClick(DialogInterface dialog, int which) {
88                 // Do nothing.  The `AlertDialog` will close automatically.
89             }
90         });
91
92         // Set the `onClick()` listener fo the positive button.
93         dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
94             @Override
95             public void onClick(DialogInterface dialog, int which) {
96                 // Return the `DialogFragment` to the parent activity on save.
97                 editBookmarkListener.onSaveEditBookmark(EditBookmarkDialog.this);
98             }
99         });
100
101
102         // Create an `AlertDialog` from the `AlertDialog.Builder`.
103         final AlertDialog alertDialog = dialogBuilder.create();
104
105         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
106         assert alertDialog.getWindow() != null;
107
108         // Show the keyboard when `alertDialog` is displayed on the screen.
109         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
110
111         // We need to show the `AlertDialog` before we can modify items in the layout.
112         alertDialog.show();
113
114         // Get the current favorite icon byte array from the `Cursor`.
115         byte[] currentIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
116         // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
117         Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length);
118         // Display `currentIconBitmap` in `edit_bookmark_current_icon`.
119         ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_current_icon);
120         currentIconImageView.setImageBitmap(currentIconBitmap);
121
122         // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_bookmark_web_page_favorite_icon`.
123         ImageView newFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_web_page_favorite_icon);
124         newFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon);
125
126         // Load the text for `edit_bookmark_name_edittext`.
127         EditText bookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_name_edittext);
128         bookmarkNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
129
130         // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
131         bookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
132             @Override
133             public boolean onKey(View v, int keyCode, KeyEvent event) {
134                 // If the event is an `ACTION_DOWN` on the `enter` key, save the bookmark.
135                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
136                     // Trigger `onSaveEditBookmark()` and return the `DialogFragment` to the parent activity.
137                     editBookmarkListener.onSaveEditBookmark(EditBookmarkDialog.this);
138                     // Manually dismiss `alertDialog`.
139                     alertDialog.dismiss();
140                     // Consume the event.
141                     return true;
142                 } else {  // If any other key was pressed, do not consume the event.
143                     return false;
144                 }
145             }
146         });
147
148         // Load the text for `edit_bookmark_url_edittext`.
149         EditText bookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_url_edittext);
150         bookmarkUrlEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)));
151
152         // Allow the "enter" key on the keyboard to save the bookmark from `edit_bookmark_url_edittext`.
153         bookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
154             public boolean onKey(View v, int keyCode, KeyEvent event) {
155                 // If the event is a key-down on the `enter` button, select the PositiveButton `Save`.
156                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
157                     // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
158                     editBookmarkListener.onSaveEditBookmark(EditBookmarkDialog.this);
159                     // Manually dismiss the `AlertDialog`.
160                     alertDialog.dismiss();
161                     // Consume the event.
162                     return true;
163                 } else { // If any other key was pressed, do not consume the event.
164                     return false;
165                 }
166             }
167         });
168
169         // `onCreateDialog` requires the return of an `AlertDialog`.
170         return alertDialog;
171     }
172 }