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 onEditBookmarkCancel(DialogFragment editBookmarkDialogFragment);
45 void onEditBookmarkSave(DialogFragment editBookmarkDialogFragment);
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 the `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[] selectedBookmarksLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds();
66 int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[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 // Get the favorite icon byte array from the `Cursor`.
73 byte[] favoriteIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
74 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
75 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
76 // Convert the `Bitmap` to a `Drawable`.
77 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), favoriteIconBitmap);
79 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
80 AlertDialog.Builder editBookmarkDialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
81 editBookmarkDialogBuilder.setTitle(R.string.edit_bookmark);
82 editBookmarkDialogBuilder.setIcon(favoriteIconDrawable);
83 // The parent view is `null` because it will be assigned by `AlertDialog`.
84 editBookmarkDialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_dialog, null));
86 // Set an `onClick()` listener for the negative button.
87 editBookmarkDialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
89 public void onClick(DialogInterface dialog, int which) {
90 // Return the `DialogFragment` to the parent activity on cancel.
91 editBookmarkListener.onEditBookmarkCancel(EditBookmark.this);
95 // Set the `onClick()` listener fo the positive button.
96 editBookmarkDialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
98 public void onClick(DialogInterface dialog, int which) {
99 // Return the `DialogFragment` to the parent activity on save.
100 editBookmarkListener.onEditBookmarkSave(EditBookmark.this);
105 // Create an `AlertDialog` from the `AlertDialog.Builder`.
106 final AlertDialog editBookmarkDialog = editBookmarkDialogBuilder.create();
108 // Show the keyboard when the `Dialog` is displayed on the screen.
109 editBookmarkDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
111 // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below.
112 editBookmarkDialog.show();
114 // Get a `Drawable` of the favorite icon from `mainWebView` and display it in `edit_bookmark_new_favorite_icon`.
115 ImageView newFavoriteIcon = (ImageView) editBookmarkDialog.findViewById(R.id.edit_bookmark_new_favorite_icon);
116 assert newFavoriteIcon != null; // Remove the warning below that `newFavoriteIcon` might be null.
117 newFavoriteIcon.setImageBitmap(MainWebViewActivity.favoriteIcon);
119 // Load the text for `edit_bookmark_name_edittext`.
120 EditText bookmarkNameEditText = (EditText) editBookmarkDialog.findViewById(R.id.edit_bookmark_name_edittext);
121 assert bookmarkNameEditText != null; // Remove the warning below that `bookmarkNameEditText` might be null.
122 bookmarkNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)));
124 // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
125 bookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
126 public boolean onKey(View v, int keyCode, KeyEvent event) {
127 // If the event is a key-down on the "enter" button, select the PositiveButton `Save`.
128 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
129 // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
130 editBookmarkListener.onEditBookmarkSave(EditBookmark.this);
131 // Manually dismiss the `AlertDialog`.
132 editBookmarkDialog.dismiss();
133 // Consume the event.
135 } else { // If any other key was pressed, do not consume the event.
141 // Load the text for `create_bookmark_url_edittext`.
142 EditText bookmarkUrlEditText = (EditText) editBookmarkDialog.findViewById(R.id.edit_bookmark_url_edittext);
143 assert bookmarkUrlEditText != null;// Remove the warning below that `bookmarkUrlEditText` might be null.
144 bookmarkUrlEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_URL)));
146 // Allow the "enter" key on the keyboard to save the bookmark from `edit_bookmark_url_edittext`.
147 bookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
148 public boolean onKey(View v, int keyCode, KeyEvent event) {
149 // If the event is a key-down on the `enter` button, select the PositiveButton `Save`.
150 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
151 // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
152 editBookmarkListener.onEditBookmarkSave(EditBookmark.this);
153 // Manually dismiss the `AlertDialog`.
154 editBookmarkDialog.dismiss();
155 // Consume the event.
157 } else { // If any other key was pressed, do not consume the event.
163 // `onCreateDialog` requires the return of an `AlertDialog`.
164 return editBookmarkDialog;