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 // The public interface is used to send information back to the parent activity.
58 public interface MoveToFolderListener {
59 void onMoveToFolder(AppCompatDialogFragment dialogFragment);
62 // `moveToFolderListener` is used in `onAttach()` and `onCreateDialog`.
63 private MoveToFolderListener moveToFolderListener;
65 public void onAttach(Context context) {
66 super.onAttach(context);
68 // Get a handle for `MoveToFolderListener` from `parentActivity`.
70 moveToFolderListener = (MoveToFolderListener) context;
71 } catch(ClassCastException exception) {
72 throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
76 // `exceptFolders` is used in `onCreateDialog()` and `addSubfoldersToExceptFolders()`.
77 private String exceptFolders;
79 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
80 @SuppressLint("InflateParams")
83 public Dialog onCreateDialog(Bundle savedInstanceState) {
84 // Use `AlertDialog.Builder` to create the `AlertDialog`.
85 AlertDialog.Builder dialogBuilder;
87 // Set the style according to the theme.
88 if (MainWebViewActivity.darkTheme) {
89 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
91 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
95 dialogBuilder.setTitle(R.string.move_to_folder);
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));
100 // Set an `onClick()` listener for the negative button.
101 dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
103 public void onClick(DialogInterface dialog, int which) {
104 // Do nothing. The `AlertDialog` will close automatically.
108 // Set the `onClick()` listener fo the positive button.
109 dialogBuilder.setPositiveButton(R.string.move, new Dialog.OnClickListener() {
111 public void onClick(DialogInterface dialog, int which) {
112 // Return the `DialogFragment` to the parent activity on save.
113 moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this);
117 // Create an `AlertDialog` from the `AlertDialog.Builder`.
118 final AlertDialog alertDialog = dialogBuilder.create();
120 // Show the `AlertDialog` so the items in the layout can be modified.
123 // Get a handle for the positive button.
124 final Button moveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
126 // Initially disable the positive button.
127 moveButton.setEnabled(false);
129 // Initialize the `Cursor` and `CursorAdapter` variables.
130 Cursor foldersCursor;
131 CursorAdapter foldersCursorAdapter;
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`.
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;
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);
149 if (exceptFolders.isEmpty()){
150 // Add the selected folder to the list of folders not to display.
151 exceptFolders = DatabaseUtils.sqlEscapeString(folderName);
153 // Add the selected folder to the end of the list of folders not to display.
154 exceptFolders = exceptFolders + "," + 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 = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders);
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 = (ImageView) 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 = (TextView) 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 = 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 (BookmarksActivity.bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
215 // Get the name of the selected folder.
216 String folderName = BookmarksActivity.bookmarksDatabaseHelper.getFolderName(databaseIdInt);
218 // Add the selected folder to the end of the list of folders not to display.
219 exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
221 // Add the selected folder's subfolders to the list of folders not to display.
222 addSubfoldersToExceptFolders(folderName);
226 // Get a `foldersCursor`.
227 foldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders);
229 // Combine `homeFolderMatrixCursor` and `foldersCursor`.
230 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
232 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
233 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
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);
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);
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);
258 // Display the ListView
259 ListView foldersListView = (ListView) alertDialog.findViewById(R.id.move_to_folder_listview);
260 foldersListView.setAdapter(foldersCursorAdapter);
262 // Enable the move button when a folder is selected.
263 foldersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
265 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
266 // Enable the move button.
267 moveButton.setEnabled(true);
271 // `onCreateDialog` requires the return of an `AlertDialog`.
275 private void addSubfoldersToExceptFolders(String folderName) {
276 // Get a `Cursor` will all the immediate subfolders.
277 Cursor subfoldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getSubfoldersCursor(folderName);
279 for (int i = 0; i < subfoldersCursor.getCount(); i++) {
280 // Move `subfolderCursor` to the current item.
281 subfoldersCursor.moveToPosition(i);
283 // Get the name of the subfolder.
284 String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
286 // Run the same tasks for any subfolders of the subfolder.
287 addSubfoldersToExceptFolders(subfolderName);
289 // Add the subfolder to `exceptFolders`.
290 subfolderName = DatabaseUtils.sqlEscapeString(subfolderName);
291 exceptFolders = exceptFolders + "," + subfolderName;