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.annotation.SuppressLint;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.database.Cursor;
28 import android.graphics.Bitmap;
29 import android.graphics.BitmapFactory;
30 import android.os.Bundle;
31 // If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
32 import android.support.v7.app.AlertDialog;
33 import android.view.KeyEvent;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.EditText;
37 import android.widget.ImageView;
39 public class EditBookmark extends DialogFragment {
40 // The public interface is used to send information back to the parent activity.
41 public interface EditBookmarkListener {
42 void onSaveEditBookmark(DialogFragment dialogFragment);
45 // `editBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
46 private EditBookmarkListener editBookmarkListener;
48 public void onAttach(Context context) {
49 super.onAttach(context);
51 // Get a handle for `EditBookmarkListener` from `context`.
53 editBookmarkListener = (EditBookmarkListener) context;
54 } catch(ClassCastException exception) {
55 throw new ClassCastException(context.toString() + " must implement EditBookmarkListener.");
59 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
60 @SuppressLint("InflateParams")
62 public Dialog onCreateDialog(Bundle savedInstanceState) {
63 // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
64 long[] selectedBookmarkLongArray = BookmarksActivity.checkedItemIds;
65 int selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
67 // Get a `Cursor` with the specified bookmark and move it to the first position.
68 Cursor bookmarkCursor = BookmarksActivity.bookmarksDatabaseHandler.getBookmarkCursor(selectedBookmarkDatabaseId);
69 bookmarkCursor.moveToFirst();
71 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
72 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
73 dialogBuilder.setTitle(R.string.edit_bookmark);
74 // The parent view is `null` because it will be assigned by `AlertDialog`.
75 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_dialog, null));
77 // Set an `onClick()` listener for the negative button.
78 dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
80 public void onClick(DialogInterface dialog, int which) {
81 // Do nothing. The `AlertDialog` will close automatically.
85 // Set the `onClick()` listener fo the positive button.
86 dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
88 public void onClick(DialogInterface dialog, int which) {
89 // Return the `DialogFragment` to the parent activity on save.
90 editBookmarkListener.onSaveEditBookmark(EditBookmark.this);
95 // Create an `AlertDialog` from the `AlertDialog.Builder`.
96 final AlertDialog alertDialog = dialogBuilder.create();
98 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
99 assert alertDialog.getWindow() != null;
101 // Show the keyboard when `alertDialog` is displayed on the screen.
102 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
104 // We need to show the `AlertDialog` before we can modify items in the layout.
107 // Get the current favorite icon byte array from the `Cursor`.
108 byte[] currentIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
109 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
110 Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length);
111 // Display `currentIconBitmap` in `edit_bookmark_current_icon`.
112 ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_current_icon);
113 assert currentIconImageView != null; // Remove the warning below that `currentIconImageView` might be null;
114 currentIconImageView.setImageBitmap(currentIconBitmap);
116 // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_bookmark_web_page_favorite_icon`.
117 ImageView newFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_web_page_favorite_icon);
118 assert newFavoriteIconImageView != null; // Remove the warning below that `newFavoriteIcon` might be null.
119 newFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon);
121 // Load the text for `edit_bookmark_name_edittext`.
122 EditText bookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_name_edittext);
123 assert bookmarkNameEditText != null; // Remove the warning below that `bookmarkNameEditText` might be null.
124 bookmarkNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)));
126 // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
127 bookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
129 public boolean onKey(View v, int keyCode, KeyEvent event) {
130 // If the event is an `ACTION_DOWN` on the `enter` key, save the bookmark.
131 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
132 // Trigger `onSaveEditBookmark()` and return the `DialogFragment` to the parent activity.
133 editBookmarkListener.onSaveEditBookmark(EditBookmark.this);
134 // Manually dismiss `alertDialog`.
135 alertDialog.dismiss();
136 // Consume the event.
138 } else { // If any other key was pressed, do not consume the event.
144 // Load the text for `edit_bookmark_url_edittext`.
145 EditText bookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_url_edittext);
146 assert bookmarkUrlEditText != null;// Remove the warning below that `bookmarkUrlEditText` might be null.
147 bookmarkUrlEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_URL)));
149 // Allow the "enter" key on the keyboard to save the bookmark from `edit_bookmark_url_edittext`.
150 bookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
151 public boolean onKey(View v, int keyCode, KeyEvent event) {
152 // If the event is a key-down on the `enter` button, select the PositiveButton `Save`.
153 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
154 // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
155 editBookmarkListener.onSaveEditBookmark(EditBookmark.this);
156 // Manually dismiss the `AlertDialog`.
157 alertDialog.dismiss();
158 // Consume the event.
160 } else { // If any other key was pressed, do not consume the event.
166 // `onCreateDialog` requires the return of an `AlertDialog`.