]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java
Slight update to the Privacy Policy wording.
[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.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;
40
41 public class EditBookmarkFolder extends AppCompatDialogFragment {
42     // The public interface is used to send information back to the parent activity.
43     public interface EditBookmarkFolderListener {
44         void onSaveEditBookmarkFolder(AppCompatDialogFragment dialogFragment);
45     }
46
47     // `editFolderListener` is used in `onAttach()` and `onCreateDialog`.
48     private EditBookmarkFolderListener editBookmarkFolderListener;
49
50     public void onAttach(Context context) {
51         super.onAttach(context);
52
53         // Get a handle for `EditFolderListener` from `parentActivity`.
54         try {
55             editBookmarkFolderListener = (EditBookmarkFolderListener) context;
56         } catch(ClassCastException exception) {
57             throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
58         }
59     }
60
61     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
62     @SuppressLint("InflateParams")
63     @Override
64     @NonNull
65     public Dialog onCreateDialog(Bundle savedInstanceState) {
66         // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
67         long[] selectedBookmarkLongArray = BookmarksActivity.checkedItemIds;
68         int selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
69
70         // Get a `Cursor` with the specified bookmark and move it to the first position.
71         Cursor bookmarkCursor = BookmarksActivity.bookmarksDatabaseHandler.getBookmarkCursor(selectedBookmarkDatabaseId);
72         bookmarkCursor.moveToFirst();
73
74         // Use `AlertDialog.Builder` to create the `AlertDialog`.  The style formats the color of the button text.
75         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
76         dialogBuilder.setTitle(R.string.edit_folder);
77         // The parent view is `null` because it will be assigned by `AlertDialog`.
78         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_folder_dialog, null));
79
80         // Set an `onClick()` listener for the negative button.
81         dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
82             @Override
83             public void onClick(DialogInterface dialog, int which) {
84                 // Do nothing.  The `AlertDialog` will close automatically.
85             }
86         });
87
88         // Set the `onClick()` listener fo the positive button.
89         dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() {
90             @Override
91             public void onClick(DialogInterface dialog, int which) {
92                 // Return the `DialogFragment` to the parent activity on save.
93                 editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this);
94             }
95         });
96
97
98         // Create an `AlertDialog` from the `AlertDialog.Builder`.
99         final AlertDialog alertDialog = dialogBuilder.create();
100
101         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
102         assert alertDialog.getWindow() != null;
103
104         // Show the keyboard when the `Dialog` is displayed on the screen.
105         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
106
107         // We need to show the `AlertDialog` before we can modify items in the layout.
108         alertDialog.show();
109
110         // Get the current favorite icon byte array from the `Cursor`.
111         byte[] currentIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
112         // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
113         Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length);
114         // Display `currentIconBitmap` in `edit_folder_current_icon`.
115         ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_current_icon);
116         assert currentIconImageView != null;  // Remove the warning below that `currentIconImageView` might be null.
117         currentIconImageView.setImageBitmap(currentIconBitmap);
118
119         // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_folder_web_page_favorite_icon`.
120         ImageView webPageFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_web_page_favorite_icon);
121         assert webPageFavoriteIconImageView != null;  // Remove the warning below that `webPageFavoriteIcon` might be null.
122         webPageFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon);
123
124         // Load the text for `edit_folder_name_edittext`.
125         EditText folderNameEditText = (EditText) alertDialog.findViewById(R.id.edit_folder_name_edittext);
126         assert folderNameEditText != null;  // Remove the warning below that `bookmarkNameEditText` might be null.
127         folderNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)));
128
129         // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`.
130         folderNameEditText.setOnKeyListener(new View.OnKeyListener() {
131             public boolean onKey(View v, int keyCode, KeyEvent event) {
132                 // If the event is a key-down on the "enter" button, select the PositiveButton `Save`.
133                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
134                     // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity.
135                     editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this);
136                     // Manually dismiss the `AlertDialog`.
137                     alertDialog.dismiss();
138                     // Consume the event.
139                     return true;
140                 } else {  // If any other key was pressed, do not consume the event.
141                     return false;
142                 }
143             }
144         });
145
146         // `onCreateDialog` requires the return of an `AlertDialog`.
147         return alertDialog;
148     }
149 }