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