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 // 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;
47 import com.stoutner.privacybrowser.R;
48 import com.stoutner.privacybrowser.activities.BookmarksActivity;
49 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
51 import java.io.ByteArrayOutputStream;
53 public class MoveToFolderDialog extends AppCompatDialogFragment {
54 // The public interface is used to send information back to the parent activity.
55 public interface MoveToFolderListener {
56 void onMoveToFolder(AppCompatDialogFragment dialogFragment);
59 // `moveToFolderListener` is used in `onAttach()` and `onCreateDialog`.
60 private MoveToFolderListener moveToFolderListener;
62 public void onAttach(Context context) {
63 super.onAttach(context);
65 // Get a handle for `MoveToFolderListener` from `parentActivity`.
67 moveToFolderListener = (MoveToFolderListener) context;
68 } catch(ClassCastException exception) {
69 throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
73 // `exceptFolders` is used in `onCreateDialog()` and `addSubfoldersToExceptFolders()`.
74 private String exceptFolders;
76 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
77 @SuppressLint("InflateParams")
80 public Dialog onCreateDialog(Bundle savedInstanceState) {
81 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
82 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
83 dialogBuilder.setTitle(R.string.move_to_folder);
84 // The parent view is `null` because it will be assigned by `AlertDialog`.
85 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
87 // Set an `onClick()` listener for the negative button.
88 dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
90 public void onClick(DialogInterface dialog, int which) {
91 // Do nothing. The `AlertDialog` will close automatically.
95 // Set the `onClick()` listener fo the positive button.
96 dialogBuilder.setPositiveButton(R.string.move, new Dialog.OnClickListener() {
98 public void onClick(DialogInterface dialog, int which) {
99 // Return the `DialogFragment` to the parent activity on save.
100 moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this);
104 // Create an `AlertDialog` from the `AlertDialog.Builder`.
105 final AlertDialog alertDialog = dialogBuilder.create();
107 // We need to show the `AlertDialog` before we can modify items in the layout.
110 // Initialize the `Cursor` and `CursorAdapter` variables.
111 Cursor foldersCursor;
112 CursorAdapter foldersCursorAdapter;
114 // Check to see if we are in the `Home Folder`.
115 if (BookmarksActivity.currentFolder.isEmpty()) { // Don't display `Home Folder` at the top of the `ListView`.
116 // Initialize `exceptFolders`.
119 // If a folder is selected, add it and all children to the list of folders not to display.
120 long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
121 for (long databaseIdLong : selectedBookmarksLongArray) {
122 // Get `databaseIdInt` for each selected bookmark.
123 int databaseIdInt = (int) databaseIdLong;
125 // If `databaseIdInt` is a folder.
126 if (BookmarksActivity.bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
127 // Get the name of the selected folder.
128 String folderName = BookmarksActivity.bookmarksDatabaseHelper.getFolderName(databaseIdInt);
130 if (exceptFolders.isEmpty()){
131 // Add the selected folder to the list of folders not to display.
132 exceptFolders = DatabaseUtils.sqlEscapeString(folderName);
134 // Add the selected folder to the end of the list of folders not to display.
135 exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
138 // Add the selected folder's subfolders to the list of folders not to display.
139 addSubfoldersToExceptFolders(folderName);
143 // Get a `Cursor` containing the folders to display.
144 foldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders);
146 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
147 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
149 public View newView(Context context, Cursor cursor, ViewGroup parent) {
150 // Inflate the individual item layout. `false` does not attach it to the root.
151 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
155 public void bindView(View view, Context context, Cursor cursor) {
156 // Get the folder icon from `cursor`.
157 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
158 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
159 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
160 // Display `folderIconBitmap` in `move_to_folder_icon`.
161 ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
162 folderIconImageView.setImageBitmap(folderIconBitmap);
164 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
165 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
166 TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
167 folderNameTextView.setText(folderName);
170 } else { // Display `Home Folder` at the top of the `ListView`.
171 // Get the home folder icon drawable and convert it to a `Bitmap`. `this` specifies the current context.
172 Drawable homeFolderIconDrawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.folder_gray_bitmap);
173 BitmapDrawable homeFolderIconBitmapDrawable = (BitmapDrawable) homeFolderIconDrawable;
174 Bitmap homeFolderIconBitmap = homeFolderIconBitmapDrawable.getBitmap();
175 // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
176 ByteArrayOutputStream homeFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
177 homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream);
178 byte[] homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray();
180 // Setup a `MatrixCursor` for the `Home Folder`.
181 String[] homeFolderMatrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME, BookmarksDatabaseHelper.FAVORITE_ICON};
182 MatrixCursor homeFolderMatrixCursor = new MatrixCursor(homeFolderMatrixCursorColumnNames);
183 homeFolderMatrixCursor.addRow(new Object[]{0, getString(R.string.home_folder), homeFolderIconByteArray});
185 // Add the parent folder to the list of folders not to display.
186 exceptFolders = DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder);
188 // If a folder is selected, add it and all children to the list of folders not to display.
189 long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
190 for (long databaseIdLong : selectedBookmarksLongArray) {
191 // Get `databaseIdInt` for each selected bookmark.
192 int databaseIdInt = (int) databaseIdLong;
194 // If `databaseIdInt` is a folder.
195 if (BookmarksActivity.bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
196 // Get the name of the selected folder.
197 String folderName = BookmarksActivity.bookmarksDatabaseHelper.getFolderName(databaseIdInt);
199 // Add the selected folder to the end of the list of folders not to display.
200 exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
202 // Add the selected folder's subfolders to the list of folders not to display.
203 addSubfoldersToExceptFolders(folderName);
207 // Get a `foldersCursor`.
208 foldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders);
210 // Combine `homeFolderMatrixCursor` and `foldersCursor`.
211 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
213 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
214 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
216 public View newView(Context context, Cursor cursor, ViewGroup parent) {
217 // Inflate the individual item layout. `false` does not attach it to the root.
218 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
222 public void bindView(View view, Context context, Cursor cursor) {
223 // Get the folder icon from `cursor`.
224 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
225 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
226 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
227 // Display `folderIconBitmap` in `move_to_folder_icon`.
228 ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
229 folderIconImageView.setImageBitmap(folderIconBitmap);
231 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
232 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
233 TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
234 folderNameTextView.setText(folderName);
239 // Display the ListView
240 ListView foldersListView = (ListView) alertDialog.findViewById(R.id.move_to_folder_listview);
241 foldersListView.setAdapter(foldersCursorAdapter);
243 // `onCreateDialog` requires the return of an `AlertDialog`.
247 private void addSubfoldersToExceptFolders(String folderName) {
248 // Get a `Cursor` will all the immediate subfolders.
249 Cursor subfoldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getSubfoldersCursor(folderName);
251 for (int i = 0; i < subfoldersCursor.getCount(); i++) {
252 // Move `subfolderCursor` to the current item.
253 subfoldersCursor.moveToPosition(i);
255 // Get the name of the subfolder.
256 String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
258 // Run the same tasks for any subfolders of the subfolder.
259 addSubfoldersToExceptFolders(subfolderName);
261 // Add the subfolder to `exceptFolders`.
262 subfolderName = DatabaseUtils.sqlEscapeString(subfolderName);
263 exceptFolders = exceptFolders + "," + subfolderName;