]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.java
Create a dark theme for the `AlertDialogs`.
[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 // We have to use `AppCompatDialogFragment` 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.CursorAdapter;
43 import android.widget.ImageView;
44 import android.widget.ListView;
45 import android.widget.TextView;
46
47 import com.stoutner.privacybrowser.R;
48 import com.stoutner.privacybrowser.activities.BookmarksActivity;
49 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
50 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
51
52 import java.io.ByteArrayOutputStream;
53
54 public class MoveToFolderDialog 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`.
83         AlertDialog.Builder dialogBuilder;
84
85         // Set the style according to the theme.
86         if (MainWebViewActivity.darkTheme) {
87             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
88         } else {
89             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
90         }
91
92         // Set the title.
93         dialogBuilder.setTitle(R.string.move_to_folder);
94
95         // Set the view.  The parent view is `null` because it will be assigned by `AlertDialog`.
96         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
97
98         // Set an `onClick()` listener for the negative button.
99         dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
100             @Override
101             public void onClick(DialogInterface dialog, int which) {
102                 // Do nothing.  The `AlertDialog` will close automatically.
103             }
104         });
105
106         // Set the `onClick()` listener fo the positive button.
107         dialogBuilder.setPositiveButton(R.string.move, new Dialog.OnClickListener() {
108             @Override
109             public void onClick(DialogInterface dialog, int which) {
110                 // Return the `DialogFragment` to the parent activity on save.
111                 moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this);
112             }
113         });
114
115         // Create an `AlertDialog` from the `AlertDialog.Builder`.
116         final AlertDialog alertDialog = dialogBuilder.create();
117
118         // We need to show the `AlertDialog` before we can modify items in the layout.
119         alertDialog.show();
120
121         // Initialize the `Cursor` and `CursorAdapter` variables.
122         Cursor foldersCursor;
123         CursorAdapter foldersCursorAdapter;
124
125         // Check to see if we are in the `Home Folder`.
126         if (BookmarksActivity.currentFolder.isEmpty()) {  // Don't display `Home Folder` at the top of the `ListView`.
127             // Initialize `exceptFolders`.
128             exceptFolders = "";
129
130             // If a folder is selected, add it and all children to the list of folders not to display.
131             long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
132             for (long databaseIdLong : selectedBookmarksLongArray) {
133                 // Get `databaseIdInt` for each selected bookmark.
134                 int databaseIdInt = (int) databaseIdLong;
135
136                 // If `databaseIdInt` is a folder.
137                 if (BookmarksActivity.bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
138                     // Get the name of the selected folder.
139                     String folderName = BookmarksActivity.bookmarksDatabaseHelper.getFolderName(databaseIdInt);
140
141                     if (exceptFolders.isEmpty()){
142                         // Add the selected folder to the list of folders not to display.
143                         exceptFolders = DatabaseUtils.sqlEscapeString(folderName);
144                     } else {
145                         // Add the selected folder to the end of the list of folders not to display.
146                         exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
147                     }
148
149                     // Add the selected folder's subfolders to the list of folders not to display.
150                     addSubfoldersToExceptFolders(folderName);
151                 }
152             }
153
154             // Get a `Cursor` containing the folders to display.
155             foldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders);
156
157             // Setup `foldersCursorAdaptor` with `this` context.  `false` disables autoRequery.
158             foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
159                 @Override
160                 public View newView(Context context, Cursor cursor, ViewGroup parent) {
161                     // Inflate the individual item layout.  `false` does not attach it to the root.
162                     return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
163                 }
164
165                 @Override
166                 public void bindView(View view, Context context, Cursor cursor) {
167                     // Get the folder icon from `cursor`.
168                     byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
169                     // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
170                     Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
171                     // Display `folderIconBitmap` in `move_to_folder_icon`.
172                     ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
173                     folderIconImageView.setImageBitmap(folderIconBitmap);
174
175                     // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
176                     String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
177                     TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
178                     folderNameTextView.setText(folderName);
179                 }
180             };
181         } else {  // Display `Home Folder` at the top of the `ListView`.
182             // Get the home folder icon drawable and convert it to a `Bitmap`.  `this` specifies the current context.
183             Drawable homeFolderIconDrawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.folder_gray_bitmap);
184             BitmapDrawable homeFolderIconBitmapDrawable = (BitmapDrawable) homeFolderIconDrawable;
185             Bitmap homeFolderIconBitmap = homeFolderIconBitmapDrawable.getBitmap();
186             // Convert the folder `Bitmap` to a byte array.  `0` is for lossless compression (the only option for a PNG).
187             ByteArrayOutputStream homeFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
188             homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream);
189             byte[] homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray();
190
191             // Setup a `MatrixCursor` for the `Home Folder`.
192             String[] homeFolderMatrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME, BookmarksDatabaseHelper.FAVORITE_ICON};
193             MatrixCursor homeFolderMatrixCursor = new MatrixCursor(homeFolderMatrixCursorColumnNames);
194             homeFolderMatrixCursor.addRow(new Object[]{0, getString(R.string.home_folder), homeFolderIconByteArray});
195
196             // Add the parent folder to the list of folders not to display.
197             exceptFolders = DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder);
198
199             // If a folder is selected, add it and all children to the list of folders not to display.
200             long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
201             for (long databaseIdLong : selectedBookmarksLongArray) {
202                 // Get `databaseIdInt` for each selected bookmark.
203                 int databaseIdInt = (int) databaseIdLong;
204
205                 // If `databaseIdInt` is a folder.
206                 if (BookmarksActivity.bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
207                     // Get the name of the selected folder.
208                     String folderName = BookmarksActivity.bookmarksDatabaseHelper.getFolderName(databaseIdInt);
209
210                     // Add the selected folder to the end of the list of folders not to display.
211                     exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
212
213                     // Add the selected folder's subfolders to the list of folders not to display.
214                     addSubfoldersToExceptFolders(folderName);
215                 }
216             }
217
218             // Get a `foldersCursor`.
219             foldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders);
220
221             // Combine `homeFolderMatrixCursor` and `foldersCursor`.
222             MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
223
224             // Setup `foldersCursorAdaptor` with `this` context.  `false` disables autoRequery.
225             foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
226                 @Override
227                 public View newView(Context context, Cursor cursor, ViewGroup parent) {
228                     // Inflate the individual item layout.  `false` does not attach it to the root.
229                     return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
230                 }
231
232                 @Override
233                 public void bindView(View view, Context context, Cursor cursor) {
234                     // Get the folder icon from `cursor`.
235                     byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
236                     // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
237                     Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
238                     // Display `folderIconBitmap` in `move_to_folder_icon`.
239                     ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
240                     folderIconImageView.setImageBitmap(folderIconBitmap);
241
242                     // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
243                     String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
244                     TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
245                     folderNameTextView.setText(folderName);
246                 }
247             };
248         }
249
250         // Display the ListView
251         ListView foldersListView = (ListView) alertDialog.findViewById(R.id.move_to_folder_listview);
252         foldersListView.setAdapter(foldersCursorAdapter);
253
254         // `onCreateDialog` requires the return of an `AlertDialog`.
255         return alertDialog;
256     }
257
258     private void addSubfoldersToExceptFolders(String folderName) {
259         // Get a `Cursor` will all the immediate subfolders.
260         Cursor subfoldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getSubfoldersCursor(folderName);
261
262         for (int i = 0; i < subfoldersCursor.getCount(); i++) {
263             // Move `subfolderCursor` to the current item.
264             subfoldersCursor.moveToPosition(i);
265
266             // Get the name of the subfolder.
267             String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
268
269             // Run the same tasks for any subfolders of the subfolder.
270             addSubfoldersToExceptFolders(subfolderName);
271
272             // Add the subfolder to `exceptFolders`.
273             subfolderName = DatabaseUtils.sqlEscapeString(subfolderName);
274             exceptFolders = exceptFolders + "," + subfolderName;
275         }
276
277     }
278 }