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