]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.java
Disable the move bookmark to folder button until a folder is selected. https://redmi...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / MoveToFolderDialog.java
1 /*
2  * Copyright © 2016-2017 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.dialogs;
21
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.database.DatabaseUtils;
29 import android.database.MatrixCursor;
30 import android.database.MergeCursor;
31 import android.graphics.Bitmap;
32 import android.graphics.BitmapFactory;
33 import android.graphics.drawable.BitmapDrawable;
34 import android.graphics.drawable.Drawable;
35 import android.os.Bundle;
36 import android.support.annotation.NonNull;
37 import android.support.v4.content.ContextCompat;
38 // `AppCompatDialogFragment` must be used instead of `DialogFragment` or an error is produced on API <=22.
39 import android.support.v7.app.AppCompatDialogFragment;
40 import android.view.View;
41 import android.view.ViewGroup;
42 import android.widget.AdapterView;
43 import android.widget.Button;
44 import android.widget.CursorAdapter;
45 import android.widget.ImageView;
46 import android.widget.ListView;
47 import android.widget.TextView;
48
49 import com.stoutner.privacybrowser.R;
50 import com.stoutner.privacybrowser.activities.BookmarksActivity;
51 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
52 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
53
54 import java.io.ByteArrayOutputStream;
55
56 public class MoveToFolderDialog extends AppCompatDialogFragment {
57     // The public interface is used to send information back to the parent activity.
58     public interface MoveToFolderListener {
59         void onMoveToFolder(AppCompatDialogFragment dialogFragment);
60     }
61
62     // `moveToFolderListener` is used in `onAttach()` and `onCreateDialog`.
63     private MoveToFolderListener moveToFolderListener;
64
65     public void onAttach(Context context) {
66         super.onAttach(context);
67
68         // Get a handle for `MoveToFolderListener` from `parentActivity`.
69         try {
70             moveToFolderListener = (MoveToFolderListener) context;
71         } catch(ClassCastException exception) {
72             throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
73         }
74     }
75
76     // `exceptFolders` is used in `onCreateDialog()` and `addSubfoldersToExceptFolders()`.
77     private String exceptFolders;
78
79     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
80     @SuppressLint("InflateParams")
81     @Override
82     @NonNull
83     public Dialog onCreateDialog(Bundle savedInstanceState) {
84         // Use `AlertDialog.Builder` to create the `AlertDialog`.
85         AlertDialog.Builder dialogBuilder;
86
87         // Set the style according to the theme.
88         if (MainWebViewActivity.darkTheme) {
89             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
90         } else {
91             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
92         }
93
94         // Set the title.
95         dialogBuilder.setTitle(R.string.move_to_folder);
96
97         // Set the view.  The parent view is `null` because it will be assigned by `AlertDialog`.
98         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
99
100         // Set an `onClick()` listener for the negative button.
101         dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
102             @Override
103             public void onClick(DialogInterface dialog, int which) {
104                 // Do nothing.  The `AlertDialog` will close automatically.
105             }
106         });
107
108         // Set the `onClick()` listener fo the positive button.
109         dialogBuilder.setPositiveButton(R.string.move, new Dialog.OnClickListener() {
110             @Override
111             public void onClick(DialogInterface dialog, int which) {
112                 // Return the `DialogFragment` to the parent activity on save.
113                 moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this);
114             }
115         });
116
117         // Create an `AlertDialog` from the `AlertDialog.Builder`.
118         final AlertDialog alertDialog = dialogBuilder.create();
119
120         // Show the `AlertDialog` so the items in the layout can be modified.
121         alertDialog.show();
122
123         // Get a handle for the positive button.
124         final Button moveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
125
126         // Initially disable the positive button.
127         moveButton.setEnabled(false);
128
129         // Initialize the `Cursor` and `CursorAdapter` variables.
130         Cursor foldersCursor;
131         CursorAdapter foldersCursorAdapter;
132
133         // Check to see if we are in the `Home Folder`.
134         if (BookmarksActivity.currentFolder.isEmpty()) {  // Don't display `Home Folder` at the top of the `ListView`.
135             // Initialize `exceptFolders`.
136             exceptFolders = "";
137
138             // If a folder is selected, add it and all children to the list of folders not to display.
139             long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
140             for (long databaseIdLong : selectedBookmarksLongArray) {
141                 // Get `databaseIdInt` for each selected bookmark.
142                 int databaseIdInt = (int) databaseIdLong;
143
144                 // If `databaseIdInt` is a folder.
145                 if (BookmarksActivity.bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
146                     // Get the name of the selected folder.
147                     String folderName = BookmarksActivity.bookmarksDatabaseHelper.getFolderName(databaseIdInt);
148
149                     if (exceptFolders.isEmpty()){
150                         // Add the selected folder to the list of folders not to display.
151                         exceptFolders = DatabaseUtils.sqlEscapeString(folderName);
152                     } else {
153                         // Add the selected folder to the end of the list of folders not to display.
154                         exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
155                     }
156
157                     // Add the selected folder's subfolders to the list of folders not to display.
158                     addSubfoldersToExceptFolders(folderName);
159                 }
160             }
161
162             // Get a `Cursor` containing the folders to display.
163             foldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders);
164
165             // Setup `foldersCursorAdaptor` with `this` context.  `false` disables autoRequery.
166             foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
167                 @Override
168                 public View newView(Context context, Cursor cursor, ViewGroup parent) {
169                     // Inflate the individual item layout.  `false` does not attach it to the root.
170                     return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
171                 }
172
173                 @Override
174                 public void bindView(View view, Context context, Cursor cursor) {
175                     // Get the folder icon from `cursor`.
176                     byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
177                     // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
178                     Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
179                     // Display `folderIconBitmap` in `move_to_folder_icon`.
180                     ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
181                     folderIconImageView.setImageBitmap(folderIconBitmap);
182
183                     // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
184                     String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
185                     TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
186                     folderNameTextView.setText(folderName);
187                 }
188             };
189         } else {  // Display `Home Folder` at the top of the `ListView`.
190             // Get the home folder icon drawable and convert it to a `Bitmap`.  `this` specifies the current context.
191             Drawable homeFolderIconDrawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.folder_gray_bitmap);
192             BitmapDrawable homeFolderIconBitmapDrawable = (BitmapDrawable) homeFolderIconDrawable;
193             Bitmap homeFolderIconBitmap = homeFolderIconBitmapDrawable.getBitmap();
194             // Convert the folder `Bitmap` to a byte array.  `0` is for lossless compression (the only option for a PNG).
195             ByteArrayOutputStream homeFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
196             homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream);
197             byte[] homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray();
198
199             // Setup a `MatrixCursor` for the `Home Folder`.
200             String[] homeFolderMatrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME, BookmarksDatabaseHelper.FAVORITE_ICON};
201             MatrixCursor homeFolderMatrixCursor = new MatrixCursor(homeFolderMatrixCursorColumnNames);
202             homeFolderMatrixCursor.addRow(new Object[]{0, getString(R.string.home_folder), homeFolderIconByteArray});
203
204             // Add the parent folder to the list of folders not to display.
205             exceptFolders = DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder);
206
207             // If a folder is selected, add it and all children to the list of folders not to display.
208             long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
209             for (long databaseIdLong : selectedBookmarksLongArray) {
210                 // Get `databaseIdInt` for each selected bookmark.
211                 int databaseIdInt = (int) databaseIdLong;
212
213                 // If `databaseIdInt` is a folder.
214                 if (BookmarksActivity.bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
215                     // Get the name of the selected folder.
216                     String folderName = BookmarksActivity.bookmarksDatabaseHelper.getFolderName(databaseIdInt);
217
218                     // Add the selected folder to the end of the list of folders not to display.
219                     exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
220
221                     // Add the selected folder's subfolders to the list of folders not to display.
222                     addSubfoldersToExceptFolders(folderName);
223                 }
224             }
225
226             // Get a `foldersCursor`.
227             foldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders);
228
229             // Combine `homeFolderMatrixCursor` and `foldersCursor`.
230             MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
231
232             // Setup `foldersCursorAdaptor` with `this` context.  `false` disables autoRequery.
233             foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
234                 @Override
235                 public View newView(Context context, Cursor cursor, ViewGroup parent) {
236                     // Inflate the individual item layout.  `false` does not attach it to the root.
237                     return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
238                 }
239
240                 @Override
241                 public void bindView(View view, Context context, Cursor cursor) {
242                     // Get the folder icon from `cursor`.
243                     byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
244                     // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
245                     Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
246                     // Display `folderIconBitmap` in `move_to_folder_icon`.
247                     ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
248                     folderIconImageView.setImageBitmap(folderIconBitmap);
249
250                     // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
251                     String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
252                     TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
253                     folderNameTextView.setText(folderName);
254                 }
255             };
256         }
257
258         // Display the ListView
259         ListView foldersListView = (ListView) alertDialog.findViewById(R.id.move_to_folder_listview);
260         foldersListView.setAdapter(foldersCursorAdapter);
261
262         // Enable the move button when a folder is selected.
263         foldersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
264             @Override
265             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
266                 // Enable the move button.
267                 moveButton.setEnabled(true);
268             }
269         });
270
271         // `onCreateDialog` requires the return of an `AlertDialog`.
272         return alertDialog;
273     }
274
275     private void addSubfoldersToExceptFolders(String folderName) {
276         // Get a `Cursor` will all the immediate subfolders.
277         Cursor subfoldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getSubfoldersCursor(folderName);
278
279         for (int i = 0; i < subfoldersCursor.getCount(); i++) {
280             // Move `subfolderCursor` to the current item.
281             subfoldersCursor.moveToPosition(i);
282
283             // Get the name of the subfolder.
284             String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
285
286             // Run the same tasks for any subfolders of the subfolder.
287             addSubfoldersToExceptFolders(subfolderName);
288
289             // Add the subfolder to `exceptFolders`.
290             subfolderName = DatabaseUtils.sqlEscapeString(subfolderName);
291             exceptFolders = exceptFolders + "," + subfolderName;
292         }
293
294     }
295 }