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.dialogs;
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 import android.support.annotation.NonNull;
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 // 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;
41 import com.stoutner.privacybrowser.activities.MainWebView;
42 import com.stoutner.privacybrowser.R;
43 import com.stoutner.privacybrowser.activities.Bookmarks;
44 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
46 public class EditBookmarkFolder extends AppCompatDialogFragment {
47 // The public interface is used to send information back to the parent activity.
48 public interface EditBookmarkFolderListener {
49 void onSaveEditBookmarkFolder(AppCompatDialogFragment dialogFragment);
52 // `editFolderListener` is used in `onAttach()` and `onCreateDialog`.
53 private EditBookmarkFolderListener editBookmarkFolderListener;
55 public void onAttach(Context context) {
56 super.onAttach(context);
58 // Get a handle for `EditFolderListener` from `parentActivity`.
60 editBookmarkFolderListener = (EditBookmarkFolderListener) context;
61 } catch(ClassCastException exception) {
62 throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
66 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
67 @SuppressLint("InflateParams")
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];
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();
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_folder);
82 // The parent view is `null` because it will be assigned by `AlertDialog`.
83 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_folder_dialog, null));
85 // Set an `onClick()` listener for the negative button.
86 dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
88 public void onClick(DialogInterface dialog, int which) {
89 // Do nothing. The `AlertDialog` will close automatically.
93 // Set the `onClick()` listener fo the positive button.
94 dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
96 public void onClick(DialogInterface dialog, int which) {
97 // Return the `DialogFragment` to the parent activity on save.
98 editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this);
103 // Create an `AlertDialog` from the `AlertDialog.Builder`.
104 final AlertDialog alertDialog = dialogBuilder.create();
106 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
107 assert alertDialog.getWindow() != null;
109 // Show the keyboard when the `Dialog` is displayed on the screen.
110 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
112 // We need to show the `AlertDialog` before we can modify items in the layout.
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_folder_current_icon`.
120 ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_current_icon);
121 assert currentIconImageView != null; // Remove the warning below that `currentIconImageView` might be null.
122 currentIconImageView.setImageBitmap(currentIconBitmap);
124 // Get a `Bitmap` of the favorite icon from `MainWebView` and display it in `edit_folder_web_page_favorite_icon`.
125 ImageView webPageFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_web_page_favorite_icon);
126 assert webPageFavoriteIconImageView != null; // Remove the warning below that `webPageFavoriteIcon` might be null.
127 webPageFavoriteIconImageView.setImageBitmap(MainWebView.favoriteIcon);
129 // Load the text for `edit_folder_name_edittext`.
130 EditText folderNameEditText = (EditText) alertDialog.findViewById(R.id.edit_folder_name_edittext);
131 assert folderNameEditText != null; // Remove the warning below that `bookmarkNameEditText` might be null.
132 folderNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
134 // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
135 folderNameEditText.setOnKeyListener(new View.OnKeyListener() {
136 public boolean onKey(View v, int keyCode, KeyEvent event) {
137 // If the event is a key-down on the "enter" button, select the PositiveButton `Save`.
138 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
139 // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
140 editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this);
141 // Manually dismiss the `AlertDialog`.
142 alertDialog.dismiss();
143 // Consume the event.
145 } else { // If any other key was pressed, do not consume the event.
151 // `onCreateDialog` requires the return of an `AlertDialog`.