2 * Copyright © 2016-2017 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.dialogs;
22 import android.annotation.SuppressLint;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
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 import android.support.annotation.IdRes;
32 import android.support.annotation.NonNull;
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.text.Editable;
36 import android.text.TextWatcher;
37 import android.view.KeyEvent;
38 import android.view.View;
39 import android.view.WindowManager;
40 import android.widget.Button;
41 import android.widget.EditText;
42 import android.widget.ImageView;
43 import android.widget.RadioButton;
44 import android.widget.RadioGroup;
46 import com.stoutner.privacybrowser.activities.BookmarksActivity;
47 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
48 import com.stoutner.privacybrowser.R;
49 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
51 public class EditBookmarkDialog extends AppCompatDialogFragment {
52 // The public interface is used to send information back to the parent activity.
53 public interface EditBookmarkListener {
54 void onSaveEditBookmark(AppCompatDialogFragment dialogFragment);
57 // `editBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
58 private EditBookmarkListener editBookmarkListener;
60 public void onAttach(Context context) {
61 super.onAttach(context);
63 // Get a handle for `EditBookmarkListener` from `context`.
65 editBookmarkListener = (EditBookmarkListener) context;
66 } catch(ClassCastException exception) {
67 throw new ClassCastException(context.toString() + " must implement EditBookmarkListener.");
71 // Instantiate the class variables.
72 EditText nameEditText;
74 RadioButton newIconRadioButton;
79 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
80 @SuppressLint("InflateParams")
83 public Dialog onCreateDialog(Bundle savedInstanceState) {
84 // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
85 long[] selectedBookmarkLongArray = BookmarksActivity.checkedItemIds;
86 int selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
88 // Get a `Cursor` with the specified bookmark and move it to the first position.
89 Cursor bookmarkCursor = BookmarksActivity.bookmarksDatabaseHelper.getBookmarkCursor(selectedBookmarkDatabaseId);
90 bookmarkCursor.moveToFirst();
92 // Use `AlertDialog.Builder` to create the `AlertDialog`.
93 AlertDialog.Builder dialogBuilder;
95 // Set the style according to the theme.
96 if (MainWebViewActivity.darkTheme) {
97 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
99 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
103 dialogBuilder.setTitle(R.string.edit_bookmark);
105 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
106 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_dialog, null));
108 // Set an `onClick()` listener for the negative button.
109 dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
111 public void onClick(DialogInterface dialog, int which) {
112 // Do nothing. The `AlertDialog` will close automatically.
116 // Set the `onClick()` listener fo the positive button.
117 dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
119 public void onClick(DialogInterface dialog, int which) {
120 // Return the `DialogFragment` to the parent activity on save.
121 editBookmarkListener.onSaveEditBookmark(EditBookmarkDialog.this);
125 // Create an `AlertDialog` from the `AlertDialog.Builder`.
126 final AlertDialog alertDialog = dialogBuilder.create();
128 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
129 assert alertDialog.getWindow() != null;
131 // Show the keyboard when `alertDialog` is displayed on the screen.
132 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
134 // The `AlertDialog` must be shown before items in the layout can be modified.
137 // Get handles for the layout items.
138 RadioGroup iconRadioGroup = (RadioGroup) alertDialog.findViewById(R.id.edit_bookmark_icon_radiogroup);
139 ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_current_icon);
140 ImageView newFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_web_page_favorite_icon);
141 newIconRadioButton = (RadioButton) alertDialog.findViewById(R.id.edit_bookmark_web_page_favorite_icon_radiobutton);
142 nameEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_name_edittext);
143 urlEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_url_edittext);
144 editButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
146 // Get the current favorite icon byte array from the `Cursor`.
147 byte[] currentIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
149 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
150 Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length);
152 // Display `currentIconBitmap` in `edit_bookmark_current_icon`.
153 currentIconImageView.setImageBitmap(currentIconBitmap);
155 // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_bookmark_web_page_favorite_icon`.
156 newFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIconBitmap);
158 // Store the current bookmark name and URL.
159 currentName = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
160 currentUrl = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
162 // Populate the `EditTexts`.
163 nameEditText.setText(currentName);
164 urlEditText.setText(currentUrl);
166 // Initially disable the edit button.
167 editButton.setEnabled(false);
169 // Update the edit button if the icon selection changes.
170 iconRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
172 public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
173 // Update the edit button.
178 // Update the edit button if the bookmark name changes.
179 nameEditText.addTextChangedListener(new TextWatcher() {
181 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
186 public void onTextChanged(CharSequence s, int start, int before, int count) {
191 public void afterTextChanged(Editable s) {
192 // Update the edit button.
197 // Update the edit button if the URL changes.
198 urlEditText.addTextChangedListener(new TextWatcher() {
200 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
205 public void onTextChanged(CharSequence s, int start, int before, int count) {
210 public void afterTextChanged(Editable s) {
211 // Update the edit button.
216 // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
217 nameEditText.setOnKeyListener(new View.OnKeyListener() {
219 public boolean onKey(View v, int keyCode, KeyEvent event) {
220 // If the event is an `ACTION_DOWN` on the `enter` key, save the bookmark.
221 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER) && editButton.isEnabled()) { // The enter key was pressed and the edit button is enabled.
222 // Trigger `onSaveEditBookmark()` and return the `DialogFragment` to the parent activity.
223 editBookmarkListener.onSaveEditBookmark(EditBookmarkDialog.this);
224 // Manually dismiss `alertDialog`.
225 alertDialog.dismiss();
226 // Consume the event.
228 } else { // If any other key was pressed, or if the edit button is currently disabled, do not consume the event.
234 // Allow the "enter" key on the keyboard to save the bookmark from `edit_bookmark_url_edittext`.
235 urlEditText.setOnKeyListener(new View.OnKeyListener() {
236 public boolean onKey(View v, int keyCode, KeyEvent event) {
237 // If the event is a key-down on the `enter` button, select the PositiveButton `Save`.
238 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER) && editButton.isEnabled()) { // The enter key was pressed and the edit button is enabled.
239 // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
240 editBookmarkListener.onSaveEditBookmark(EditBookmarkDialog.this);
241 // Manually dismiss the `AlertDialog`.
242 alertDialog.dismiss();
243 // Consume the event.
245 } else { // If any other key was pressed, or if the edit button is currently disabled, do not consume the event.
251 // `onCreateDialog` requires the return of an `AlertDialog`.
255 private void updateEditButton() {
256 // Get the text from the `EditTexts`.
257 String newName = nameEditText.getText().toString();
258 String newUrl = urlEditText.getText().toString();
260 // Has the favorite icon changed?
261 boolean iconChanged = newIconRadioButton.isChecked();
263 // Has the name changed?
264 boolean nameChanged = !newName.equals(currentName);
266 // Has the URL changed?
267 boolean urlChanged = !newUrl.equals(currentUrl);
269 editButton.setEnabled(iconChanged || nameChanged || urlChanged);