2 * Copyright © 2016-2017 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.dialogs;
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;
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;
54 import java.io.ByteArrayOutputStream;
56 public class MoveToFolderDialog extends AppCompatDialogFragment {
57 // Instantiate class variables.
58 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
59 private StringBuilder exceptFolders;
61 // The public interface is used to send information back to the parent activity.
62 public interface MoveToFolderListener {
63 void onMoveToFolder(AppCompatDialogFragment dialogFragment);
66 // `moveToFolderListener` is used in `onAttach()` and `onCreateDialog`.
67 private MoveToFolderListener moveToFolderListener;
69 public void onAttach(Context context) {
70 super.onAttach(context);
72 // Get a handle for `MoveToFolderListener` from `parentActivity`.
74 moveToFolderListener = (MoveToFolderListener) context;
75 } catch(ClassCastException exception) {
76 throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
80 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
81 @SuppressLint("InflateParams")
84 public Dialog onCreateDialog(Bundle savedInstanceState) {
85 // Initialize the database helper. The two `nulls` do not specify the database name or a `CursorFactory`. The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
86 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(getContext(), null, null, 0);
88 // Use `AlertDialog.Builder` to create the `AlertDialog`.
89 AlertDialog.Builder dialogBuilder;
91 // Set the style according to the theme.
92 if (MainWebViewActivity.darkTheme) {
93 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
95 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
99 dialogBuilder.setTitle(R.string.move_to_folder);
101 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
102 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
104 // Set an `onClick()` listener for the negative button.
105 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
106 // Do nothing. The `AlertDialog` will close automatically.
109 // Set the `onClick()` listener fo the positive button.
110 dialogBuilder.setPositiveButton(R.string.move, (DialogInterface dialog, int which) -> {
111 // Return the `DialogFragment` to the parent activity on save.
112 moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this);
115 // Create an `AlertDialog` from the `AlertDialog.Builder`.
116 final AlertDialog alertDialog = dialogBuilder.create();
118 // Show the `AlertDialog` so the items in the layout can be modified.
121 // Get a handle for the positive button.
122 final Button moveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
124 // Initially disable the positive button.
125 moveButton.setEnabled(false);
127 // Initialize the `Cursor` and `CursorAdapter` variables.
128 Cursor foldersCursor;
129 CursorAdapter foldersCursorAdapter;
131 // Check to see if we are in the `Home Folder`.
132 if (BookmarksActivity.currentFolder.isEmpty()) { // Don't display `Home Folder` at the top of the `ListView`.
133 // Initialize `exceptFolders`.
134 exceptFolders = new StringBuilder();
136 // If a folder is selected, add it and all children to the list of folders not to display.
137 long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
138 for (long databaseIdLong : selectedBookmarksLongArray) {
139 // Get `databaseIdInt` for each selected bookmark.
140 int databaseIdInt = (int) databaseIdLong;
142 // If `databaseIdInt` is a folder.
143 if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
144 // Get the name of the selected folder.
145 String folderName = bookmarksDatabaseHelper.getFolderName(databaseIdInt);
147 // Populate the list of folders not to get.
148 if (exceptFolders.toString().isEmpty()){
149 // Add the selected folder to the list of folders not to display.
150 exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName));
152 // Add the selected folder to the end of the list of folders not to display.
153 exceptFolders.append(",");
154 exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName));
157 // Add the selected folder's subfolders to the list of folders not to display.
158 addSubfoldersToExceptFolders(folderName);
162 // Get a `Cursor` containing the folders to display.
163 foldersCursor = bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders.toString());
165 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
166 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
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);
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 = view.findViewById(R.id.move_to_folder_icon);
181 folderIconImageView.setImageBitmap(folderIconBitmap);
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 = view.findViewById(R.id.move_to_folder_name_textview);
186 folderNameTextView.setText(folderName);
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();
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});
204 // Add the parent folder to the list of folders not to display.
205 exceptFolders.append(DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder));
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;
213 // If `databaseIdInt` is a folder.
214 if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
215 // Get the name of the selected folder.
216 String folderName = bookmarksDatabaseHelper.getFolderName(databaseIdInt);
218 // Add the selected folder to the end of the list of folders not to display.
219 exceptFolders.append(",");
220 exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName));
222 // Add the selected folder's subfolders to the list of folders not to display.
223 addSubfoldersToExceptFolders(folderName);
227 // Get a `Cursor` containing the folders to display.
228 foldersCursor = bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders.toString());
230 // Combine `homeFolderMatrixCursor` and `foldersCursor`.
231 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
233 // Setup `foldersCursorAdaptor`. `false` disables autoRequery.
234 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
236 public View newView(Context context, Cursor cursor, ViewGroup parent) {
237 // Inflate the individual item layout. `false` does not attach it to the root.
238 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
242 public void bindView(View view, Context context, Cursor cursor) {
243 // Get the folder icon from `cursor`.
244 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
245 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
246 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
247 // Display `folderIconBitmap` in `move_to_folder_icon`.
248 ImageView folderIconImageView = view.findViewById(R.id.move_to_folder_icon);
249 folderIconImageView.setImageBitmap(folderIconBitmap);
251 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
252 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
253 TextView folderNameTextView = view.findViewById(R.id.move_to_folder_name_textview);
254 folderNameTextView.setText(folderName);
259 // Display the ListView
260 ListView foldersListView = alertDialog.findViewById(R.id.move_to_folder_listview);
261 foldersListView.setAdapter(foldersCursorAdapter);
263 // Enable the move button when a folder is selected.
264 foldersListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
265 // Enable the move button.
266 moveButton.setEnabled(true);
269 // `onCreateDialog` requires the return of an `AlertDialog`.
273 private void addSubfoldersToExceptFolders(String folderName) {
274 // Get a `Cursor` will all the immediate subfolders.
275 Cursor subfoldersCursor = bookmarksDatabaseHelper.getSubfoldersCursor(folderName);
277 for (int i = 0; i < subfoldersCursor.getCount(); i++) {
278 // Move `subfolderCursor` to the current item.
279 subfoldersCursor.moveToPosition(i);
281 // Get the name of the subfolder.
282 String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
284 // Add the subfolder to `exceptFolders`.
285 exceptFolders.append(",");
286 exceptFolders.append(DatabaseUtils.sqlEscapeString(subfolderName));
288 // Run the same tasks for any subfolders of the subfolder.
289 addSubfoldersToExceptFolders(subfolderName);