]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/MoveToFolder.java
Add the ability to move bookmarks to folders.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / MoveToFolder.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.app.Activity;
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.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 // If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
37 import android.support.design.widget.Snackbar;
38 import android.support.v4.content.ContextCompat;
39 import android.support.v7.app.AlertDialog;
40 import android.view.View;
41 import android.view.ViewGroup;
42 import android.widget.CursorAdapter;
43 import android.widget.ImageView;
44 import android.widget.ListView;
45 import android.widget.TextView;
46
47 import java.io.ByteArrayOutputStream;
48
49 public class MoveToFolder extends DialogFragment {
50     // The public interface is used to send information back to the parent activity.
51     public interface MoveToFolderListener {
52         void onCancelMoveToFolder(DialogFragment dialogFragment);
53
54         void onMoveToFolder(DialogFragment dialogFragment);
55     }
56
57     // `moveToFolderListener` is used in `onAttach()` and `onCreateDialog`.
58     private MoveToFolderListener moveToFolderListener;
59
60     public void onAttach(Activity parentActivity) {
61         super.onAttach(parentActivity);
62
63         // Get a handle for `MoveToFolderListener` from `parentActivity`.
64         try {
65             moveToFolderListener = (MoveToFolderListener) parentActivity;
66         } catch(ClassCastException exception) {
67             throw new ClassCastException(parentActivity.toString() + " must implement EditBookmarkFolderListener.");
68         }
69     }
70
71     // `exceptFolders` is used in `onCreateDialog()` and `addSubfoldersToExceptFolders()`.
72     private String exceptFolders;
73
74     @Override
75     public Dialog onCreateDialog(Bundle savedInstanceState) {
76         // Use `AlertDialog.Builder` to create the `AlertDialog`.  The style formats the color of the button text.
77         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
78         dialogBuilder.setTitle(R.string.move_to_folder);
79         // The parent view is `null` because it will be assigned by `AlertDialog`.
80         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
81
82         // Set an `onClick()` listener for the negative button.
83         dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
84             @Override
85             public void onClick(DialogInterface dialog, int which) {
86                 // Return the `DialogFragment` to the parent activity on cancel.
87                 moveToFolderListener.onCancelMoveToFolder(MoveToFolder.this);
88             }
89         });
90
91         // Set the `onClick()` listener fo the positive button.
92         dialogBuilder.setPositiveButton(R.string.move, new Dialog.OnClickListener() {
93             @Override
94             public void onClick(DialogInterface dialog, int which) {
95                 // Return the `DialogFragment` to the parent activity on save.
96                 moveToFolderListener.onMoveToFolder(MoveToFolder.this);
97             }
98         });
99
100         // Create an `AlertDialog` from the `AlertDialog.Builder`.
101         final AlertDialog alertDialog = dialogBuilder.create();
102
103         // We need to show the `AlertDialog` before we can modify items in the layout.
104         alertDialog.show();
105
106         // Initialize the `Cursor` and `CursorAdapter` variables.
107         Cursor foldersCursor;
108         CursorAdapter foldersCursorAdapter;
109
110         // Check to see if we are in the `Home Folder`.
111         if (BookmarksActivity.currentFolder.isEmpty()) {  // Don't display `Home Folder` at the top of the `ListView`.
112             // Initialize `exceptFolders`.
113             exceptFolders = "";
114
115             // If a folder is selected, add it and all children to the list of folders not to display.
116             long[] selectedBookmarksLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds();
117             for (long databaseIdLong : selectedBookmarksLongArray) {
118                 // Get `databaseIdInt` for each selected bookmark.
119                 int databaseIdInt = (int) databaseIdLong;
120
121                 // If `databaseIdInt` is a folder.
122                 if (BookmarksActivity.bookmarksDatabaseHandler.isFolder(databaseIdInt)) {
123                     // Get the name of the selected folder.
124                     String folderName = BookmarksActivity.bookmarksDatabaseHandler.getFolderName(databaseIdInt);
125
126                     if (exceptFolders.isEmpty()){
127                         // Add the selected folder to the list of folders not to display.
128                         exceptFolders = DatabaseUtils.sqlEscapeString(folderName);
129                     } else {
130                         // Add the selected folder to the end of the list of folders not to display.
131                         exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
132                     }
133
134                     // Add the selected folder's subfolders to the list of folders not to display.
135                     addSubfoldersToExceptFolders(folderName);
136                 }
137             }
138
139             // Get a `Cursor` containing the folders to display.
140             foldersCursor = BookmarksActivity.bookmarksDatabaseHandler.getFoldersCursorExcept(exceptFolders);
141
142             // Setup `foldersCursorAdaptor` with `this` context.  `false` disables autoRequery.
143             foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
144                 @Override
145                 public View newView(Context context, Cursor cursor, ViewGroup parent) {
146                     // Inflate the individual item layout.  `false` does not attach it to the root.
147                     return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
148                 }
149
150                 @Override
151                 public void bindView(View view, Context context, Cursor cursor) {
152                     // Get the folder icon from `cursor`.
153                     byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
154                     // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
155                     Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
156                     // Display `folderIconBitmap` in `move_to_folder_icon`.
157                     ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
158                     assert folderIconImageView != null;  // Remove the warning below that `currentIconImageView` might be null;
159                     folderIconImageView.setImageBitmap(folderIconBitmap);
160
161                     // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
162                     String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
163                     TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
164                     folderNameTextView.setText(folderName);
165                 }
166             };
167         } else {  // Display `Home Folder` at the top of the `ListView`.
168             // Get the home folder icon drawable and convert it to a `Bitmap`.  `this` specifies the current context.
169             Drawable homeFolderIconDrawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.folder_grey_bitmap);
170             BitmapDrawable homeFolderIconBitmapDrawable = (BitmapDrawable) homeFolderIconDrawable;
171             Bitmap homeFolderIconBitmap = homeFolderIconBitmapDrawable.getBitmap();
172             // Convert the folder `Bitmap` to a byte array.  `0` is for lossless compression (the only option for a PNG).
173             ByteArrayOutputStream homeFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
174             homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream);
175             byte[] homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray();
176
177             // Setup a `MatrixCursor` for the `Home Folder`.
178             String[] homeFolderMatrixCursorColumnNames = {BookmarksDatabaseHandler._ID, BookmarksDatabaseHandler.BOOKMARK_NAME, BookmarksDatabaseHandler.FAVORITE_ICON};
179             MatrixCursor homeFolderMatrixCursor = new MatrixCursor(homeFolderMatrixCursorColumnNames);
180             homeFolderMatrixCursor.addRow(new Object[]{0, getString(R.string.home_folder), homeFolderIconByteArray});
181
182             // Add the parent folder to the list of folders not to display.
183             exceptFolders = DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder);
184
185             // If a folder is selected, add it and all children to the list of folders not to display.
186             long[] selectedBookmarksLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds();
187             for (long databaseIdLong : selectedBookmarksLongArray) {
188                 // Get `databaseIdInt` for each selected bookmark.
189                 int databaseIdInt = (int) databaseIdLong;
190
191                 // If `databaseIdInt` is a folder.
192                 if (BookmarksActivity.bookmarksDatabaseHandler.isFolder(databaseIdInt)) {
193                     // Get the name of the selected folder.
194                     String folderName = BookmarksActivity.bookmarksDatabaseHandler.getFolderName(databaseIdInt);
195
196                     // Add the selected folder to the end of the list of folders not to display.
197                     exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
198
199                     // Add the selected folder's subfolders to the list of folders not to display.
200                     addSubfoldersToExceptFolders(folderName);
201                 }
202             }
203
204             // Get a `foldersCursor`.
205             foldersCursor = BookmarksActivity.bookmarksDatabaseHandler.getFoldersCursorExcept(exceptFolders);
206
207             // Combine `homeFolderMatrixCursor` and `foldersCursor`.
208             MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
209
210             // Setup `foldersCursorAdaptor` with `this` context.  `false` disables autoRequery.
211             foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
212                 @Override
213                 public View newView(Context context, Cursor cursor, ViewGroup parent) {
214                     // Inflate the individual item layout.  `false` does not attach it to the root.
215                     return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
216                 }
217
218                 @Override
219                 public void bindView(View view, Context context, Cursor cursor) {
220                     // Get the folder icon from `cursor`.
221                     byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
222                     // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
223                     Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
224                     // Display `folderIconBitmap` in `move_to_folder_icon`.
225                     ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
226                     assert folderIconImageView != null;  // Remove the warning below that `currentIconImageView` might be null;
227                     folderIconImageView.setImageBitmap(folderIconBitmap);
228
229                     // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
230                     String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
231                     TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
232                     folderNameTextView.setText(folderName);
233                 }
234             };
235         }
236
237         // Display the ListView
238         ListView foldersListView = (ListView) alertDialog.findViewById(R.id.move_to_folder_listview);
239         assert foldersListView != null;  // Remove the warning below that `foldersListView` might be null.
240         foldersListView.setAdapter(foldersCursorAdapter);
241
242         // `onCreateDialog` requires the return of an `AlertDialog`.
243         return alertDialog;
244     }
245
246     public void addSubfoldersToExceptFolders(String folderName) {
247         // Get a `Cursor` will all the immediate subfolders.
248         Cursor subfoldersCursor = BookmarksActivity.bookmarksDatabaseHandler.getSubfoldersCursor(folderName);
249
250         for (int i = 0; i < subfoldersCursor.getCount(); i++) {
251             // Move `subfolderCursor` to the current item.
252             subfoldersCursor.moveToPosition(i);
253
254             // Get the name of the subfolder.
255             String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
256
257             // Run the same tasks for any subfolders of the subfolder.
258             addSubfoldersToExceptFolders(subfolderName);
259
260             // Add the subfolder to `exceptFolders`.
261             subfolderName = DatabaseUtils.sqlEscapeString(subfolderName);
262             exceptFolders = exceptFolders + "," + subfolderName;
263         }
264
265     }
266 }