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.os.Bundle;
30 // If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
31 import android.support.v7.app.AlertDialog;
32 import android.view.KeyEvent;
33 import android.view.View;
34 import android.view.WindowManager;
35 import android.widget.EditText;
36 import android.widget.ImageView;
38 public class EditBookmarkFolder extends DialogFragment {
39 // The public interface is used to send information back to the parent activity.
40 public interface EditBookmarkFolderListener {
41 void onSaveEditBookmarkFolder(DialogFragment dialogFragment);
44 // `editFolderListener` is used in `onAttach()` and `onCreateDialog`.
45 private EditBookmarkFolderListener editBookmarkFolderListener;
47 public void onAttach(Activity parentActivity) {
48 super.onAttach(parentActivity);
50 // Get a handle for `EditFolderListener` from `parentActivity`.
52 editBookmarkFolderListener = (EditBookmarkFolderListener) parentActivity;
53 } catch(ClassCastException exception) {
54 throw new ClassCastException(parentActivity.toString() + " must implement EditBookmarkFolderListener.");
59 public Dialog onCreateDialog(Bundle savedInstanceState) {
60 // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
61 long[] selectedBookmarkLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds();
62 int selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
64 // Get a `Cursor` with the specified bookmark and move it to the first position.
65 Cursor bookmarkCursor = BookmarksActivity.bookmarksDatabaseHandler.getBookmarkCursor(selectedBookmarkDatabaseId);
66 bookmarkCursor.moveToFirst();
68 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
69 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
70 dialogBuilder.setTitle(R.string.edit_folder);
71 // The parent view is `null` because it will be assigned by `AlertDialog`.
72 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_folder_dialog, null));
74 // Set an `onClick()` listener for the negative button.
75 dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
77 public void onClick(DialogInterface dialog, int which) {
78 // Do nothing. The `AlertDialog` will close automatically.
82 // Set the `onClick()` listener fo the positive button.
83 dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
85 public void onClick(DialogInterface dialog, int which) {
86 // Return the `DialogFragment` to the parent activity on save.
87 editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this);
92 // Create an `AlertDialog` from the `AlertDialog.Builder`.
93 final AlertDialog alertDialog = dialogBuilder.create();
95 // Show the keyboard when the `Dialog` is displayed on the screen.
96 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
98 // We need to show the `AlertDialog` before we can modify items in the layout.
101 // Get the current favorite icon byte array from the `Cursor`.
102 byte[] currentIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
103 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
104 Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length);
105 // Display `currentIconBitmap` in `edit_folder_current_icon`.
106 ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_current_icon);
107 assert currentIconImageView != null; // Remove the warning below that `currentIconImageView` might be null.
108 currentIconImageView.setImageBitmap(currentIconBitmap);
110 // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_folder_web_page_favorite_icon`.
111 ImageView webPageFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_web_page_favorite_icon);
112 assert webPageFavoriteIconImageView != null; // Remove the warning below that `webPageFavoriteIcon` might be null.
113 webPageFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon);
115 // Load the text for `edit_folder_name_edittext`.
116 EditText folderNameEditText = (EditText) alertDialog.findViewById(R.id.edit_folder_name_edittext);
117 assert folderNameEditText != null; // Remove the warning below that `bookmarkNameEditText` might be null.
118 folderNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)));
120 // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
121 folderNameEditText.setOnKeyListener(new View.OnKeyListener() {
122 public boolean onKey(View v, int keyCode, KeyEvent event) {
123 // If the event is a key-down on the "enter" button, select the PositiveButton `Save`.
124 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
125 // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
126 editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this);
127 // Manually dismiss the `AlertDialog`.
128 alertDialog.dismiss();
129 // Consume the event.
131 } else { // If any other key was pressed, do not consume the event.
137 // `onCreateDialog` requires the return of an `AlertDialog`.