]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/EditBookmark.java
Slight update to the Privacy Policy wording.
[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.annotation.SuppressLint;
23 import android.app.Dialog;
24 import android.content.Context;
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.annotation.NonNull;
32 import android.support.v7.app.AlertDialog;
33 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
34 import android.support.v7.app.AppCompatDialogFragment;
35 import android.view.KeyEvent;
36 import android.view.View;
37 import android.view.WindowManager;
38 import android.widget.EditText;
39 import android.widget.ImageView;
40
41 public class EditBookmark extends AppCompatDialogFragment {
42     // The public interface is used to send information back to the parent activity.
43     public interface EditBookmarkListener {
44         void onSaveEditBookmark(AppCompatDialogFragment dialogFragment);
45     }
46
47     // `editBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
48     private EditBookmarkListener editBookmarkListener;
49
50     public void onAttach(Context context) {
51         super.onAttach(context);
52
53         // Get a handle for `EditBookmarkListener` from `context`.
54         try {
55             editBookmarkListener = (EditBookmarkListener) context;
56         } catch(ClassCastException exception) {
57             throw new ClassCastException(context.toString() + " must implement EditBookmarkListener.");
58         }
59     }
60
61     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
62     @SuppressLint("InflateParams")
63     @Override
64     @NonNull
65     public Dialog onCreateDialog(Bundle savedInstanceState) {
66         // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
67         long[] selectedBookmarkLongArray = BookmarksActivity.checkedItemIds;
68         int selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
69
70         // Get a `Cursor` with the specified bookmark and move it to the first position.
71         Cursor bookmarkCursor = BookmarksActivity.bookmarksDatabaseHandler.getBookmarkCursor(selectedBookmarkDatabaseId);
72         bookmarkCursor.moveToFirst();
73
74         // Use `AlertDialog.Builder` to create the `AlertDialog`.  The style formats the color of the button text.
75         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
76         dialogBuilder.setTitle(R.string.edit_bookmark);
77         // The parent view is `null` because it will be assigned by `AlertDialog`.
78         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_dialog, null));
79
80         // Set an `onClick()` listener for the negative button.
81         dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
82             @Override
83             public void onClick(DialogInterface dialog, int which) {
84                 // Do nothing.  The `AlertDialog` will close automatically.
85             }
86         });
87
88         // Set the `onClick()` listener fo the positive button.
89         dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
90             @Override
91             public void onClick(DialogInterface dialog, int which) {
92                 // Return the `DialogFragment` to the parent activity on save.
93                 editBookmarkListener.onSaveEditBookmark(EditBookmark.this);
94             }
95         });
96
97
98         // Create an `AlertDialog` from the `AlertDialog.Builder`.
99         final AlertDialog alertDialog = dialogBuilder.create();
100
101         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
102         assert alertDialog.getWindow() != null;
103
104         // Show the keyboard when `alertDialog` is displayed on the screen.
105         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
106
107         // We need to show the `AlertDialog` before we can modify items in the layout.
108         alertDialog.show();
109
110         // Get the current favorite icon byte array from the `Cursor`.
111         byte[] currentIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
112         // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
113         Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length);
114         // Display `currentIconBitmap` in `edit_bookmark_current_icon`.
115         ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_current_icon);
116         assert currentIconImageView != null;  // Remove the warning below that `currentIconImageView` might be null;
117         currentIconImageView.setImageBitmap(currentIconBitmap);
118
119         // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_bookmark_web_page_favorite_icon`.
120         ImageView newFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_web_page_favorite_icon);
121         assert newFavoriteIconImageView != null;  // Remove the warning below that `newFavoriteIcon` might be null.
122         newFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon);
123
124         // Load the text for `edit_bookmark_name_edittext`.
125         EditText bookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_name_edittext);
126         assert bookmarkNameEditText != null;  // Remove the warning below that `bookmarkNameEditText` might be null.
127         bookmarkNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)));
128
129         // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
130         bookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
131             @Override
132             public boolean onKey(View v, int keyCode, KeyEvent event) {
133                 // If the event is an `ACTION_DOWN` on the `enter` key, save the bookmark.
134                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
135                     // Trigger `onSaveEditBookmark()` and return the `DialogFragment` to the parent activity.
136                     editBookmarkListener.onSaveEditBookmark(EditBookmark.this);
137                     // Manually dismiss `alertDialog`.
138                     alertDialog.dismiss();
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         // Load the text for `edit_bookmark_url_edittext`.
148         EditText bookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_url_edittext);
149         assert bookmarkUrlEditText != null;// Remove the warning below that `bookmarkUrlEditText` might be null.
150         bookmarkUrlEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.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(EditBookmark.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 }