2 * Copyright 2016 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser;
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.graphics.drawable.BitmapDrawable;
30 import android.graphics.drawable.Drawable;
31 import android.os.Bundle;
32 // If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
33 import android.support.v7.app.AlertDialog;
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;
40 public class EditBookmark extends DialogFragment {
41 // The public interface is used to send information back to the parent activity.
42 public interface EditBookmarkListener {
43 void onCancelEditBookmark(DialogFragment dialogFragment);
45 void onSaveEditBookmark(DialogFragment dialogFragment);
48 // `editBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
49 private EditBookmarkListener editBookmarkListener;
51 public void onAttach(Activity parentActivity) {
52 super.onAttach(parentActivity);
54 // Get a handle for `EditBookmarkListener` from `parentActivity`.
56 editBookmarkListener = (EditBookmarkListener) parentActivity;
57 } catch(ClassCastException exception) {
58 throw new ClassCastException(parentActivity.toString() + " must implement EditBookmarkListener.");
63 public Dialog onCreateDialog(Bundle savedInstanceState) {
64 // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
65 long[] selectedBookmarkLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds();
66 int selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
68 // Get a `Cursor` with the specified bookmark and move it to the first position.
69 Cursor bookmarkCursor = BookmarksActivity.bookmarksDatabaseHandler.getBookmarkCursor(selectedBookmarkDatabaseId);
70 bookmarkCursor.moveToFirst();
72 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
73 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
74 dialogBuilder.setTitle(R.string.edit_bookmark);
75 // The parent view is `null` because it will be assigned by `AlertDialog`.
76 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_dialog, null));
78 // Set an `onClick()` listener for the negative button.
79 dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
81 public void onClick(DialogInterface dialog, int which) {
82 // Return the `DialogFragment` to the parent activity on cancel.
83 editBookmarkListener.onCancelEditBookmark(EditBookmark.this);
87 // Set the `onClick()` listener fo the positive button.
88 dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
90 public void onClick(DialogInterface dialog, int which) {
91 // Return the `DialogFragment` to the parent activity on save.
92 editBookmarkListener.onSaveEditBookmark(EditBookmark.this);
97 // Create an `AlertDialog` from the `AlertDialog.Builder`.
98 final AlertDialog alertDialog = dialogBuilder.create();
100 // Show the keyboard when the `Dialog` is displayed on the screen.
101 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
103 // We need to show the `AlertDialog` before we can modify items in the layout.
106 // Get the current favorite icon byte array from the `Cursor`.
107 byte[] currentIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
108 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
109 Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length);
110 // Display `currentIconBitmap` in `edit_bookmark_current_icon`.
111 ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_current_icon);
112 assert currentIconImageView != null; // Remove the warning below that `currentIconImageView` might be null;
113 currentIconImageView.setImageBitmap(currentIconBitmap);
115 // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_bookmark_web_page_favorite_icon`.
116 ImageView newFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_web_page_favorite_icon);
117 assert newFavoriteIconImageView != null; // Remove the warning below that `newFavoriteIcon` might be null.
118 newFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon);
120 // Load the text for `edit_bookmark_name_edittext`.
121 EditText bookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_name_edittext);
122 assert bookmarkNameEditText != null; // Remove the warning below that `bookmarkNameEditText` might be null.
123 bookmarkNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)));
125 // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
126 bookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
127 public boolean onKey(View v, int keyCode, KeyEvent event) {
128 // If the event is a key-down on the "enter" button, select the PositiveButton `Save`.
129 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
130 // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
131 editBookmarkListener.onSaveEditBookmark(EditBookmark.this);
132 // Manually dismiss `alertDialog`.
133 alertDialog.dismiss();
134 // Consume the event.
136 } else { // If any other key was pressed, do not consume the event.
142 // Load the text for `edit_bookmark_url_edittext`.
143 EditText bookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_url_edittext);
144 assert bookmarkUrlEditText != null;// Remove the warning below that `bookmarkUrlEditText` might be null.
145 bookmarkUrlEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_URL)));
147 // Allow the "enter" key on the keyboard to save the bookmark from `edit_bookmark_url_edittext`.
148 bookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
149 public boolean onKey(View v, int keyCode, KeyEvent event) {
150 // If the event is a key-down on the `enter` button, select the PositiveButton `Save`.
151 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
152 // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
153 editBookmarkListener.onSaveEditBookmark(EditBookmark.this);
154 // Manually dismiss the `AlertDialog`.
155 alertDialog.dismiss();
156 // Consume the event.
158 } else { // If any other key was pressed, do not consume the event.
164 // `onCreateDialog` requires the return of an `AlertDialog`.