]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java
218023fd9dff1a15d7f8122f3fb136eab3af7afa
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / EditBookmarkFolder.java
1 /**
2  * Copyright 2016 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser;
21
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;
38
39 public class EditBookmarkFolder extends DialogFragment {
40     // The public interface is used to send information back to the parent activity.
41     public interface EditBookmarkFolderListener {
42         void onSaveEditBookmarkFolder(DialogFragment dialogFragment);
43     }
44
45     // `editFolderListener` is used in `onAttach()` and `onCreateDialog`.
46     private EditBookmarkFolderListener editBookmarkFolderListener;
47
48     public void onAttach(Context context) {
49         super.onAttach(context);
50
51         // Get a handle for `EditFolderListener` from `parentActivity`.
52         try {
53             editBookmarkFolderListener = (EditBookmarkFolderListener) context;
54         } catch(ClassCastException exception) {
55             throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
56         }
57     }
58
59     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
60     @SuppressLint("InflateParams")
61     @Override
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];
66
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();
70
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_folder);
74         // The parent view is `null` because it will be assigned by `AlertDialog`.
75         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_folder_dialog, null));
76
77         // Set an `onClick()` listener for the negative button.
78         dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
79             @Override
80             public void onClick(DialogInterface dialog, int which) {
81                 // Do nothing.  The `AlertDialog` will close automatically.
82             }
83         });
84
85         // Set the `onClick()` listener fo the positive button.
86         dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
87             @Override
88             public void onClick(DialogInterface dialog, int which) {
89                 // Return the `DialogFragment` to the parent activity on save.
90                 editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this);
91             }
92         });
93
94
95         // Create an `AlertDialog` from the `AlertDialog.Builder`.
96         final AlertDialog alertDialog = dialogBuilder.create();
97
98         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
99         assert alertDialog.getWindow() != null;
100
101         // Show the keyboard when the `Dialog` is displayed on the screen.
102         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
103
104         // We need to show the `AlertDialog` before we can modify items in the layout.
105         alertDialog.show();
106
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_folder_current_icon`.
112         ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_current_icon);
113         assert currentIconImageView != null;  // Remove the warning below that `currentIconImageView` might be null.
114         currentIconImageView.setImageBitmap(currentIconBitmap);
115
116         // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_folder_web_page_favorite_icon`.
117         ImageView webPageFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_web_page_favorite_icon);
118         assert webPageFavoriteIconImageView != null;  // Remove the warning below that `webPageFavoriteIcon` might be null.
119         webPageFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon);
120
121         // Load the text for `edit_folder_name_edittext`.
122         EditText folderNameEditText = (EditText) alertDialog.findViewById(R.id.edit_folder_name_edittext);
123         assert folderNameEditText != null;  // Remove the warning below that `bookmarkNameEditText` might be null.
124         folderNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)));
125
126         // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
127         folderNameEditText.setOnKeyListener(new View.OnKeyListener() {
128             public boolean onKey(View v, int keyCode, KeyEvent event) {
129                 // If the event is a key-down on the "enter" button, select the PositiveButton `Save`.
130                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
131                     // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
132                     editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this);
133                     // Manually dismiss the `AlertDialog`.
134                     alertDialog.dismiss();
135                     // Consume the event.
136                     return true;
137                 } else {  // If any other key was pressed, do not consume the event.
138                     return false;
139                 }
140             }
141         });
142
143         // `onCreateDialog` requires the return of an `AlertDialog`.
144         return alertDialog;
145     }
146 }