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