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