2 * Copyright 2016 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;
22 import android.annotation.SuppressLint;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
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 // 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 import android.support.v7.app.AlertDialog;
39 import android.view.View;
40 import android.view.ViewGroup;
41 import android.widget.CursorAdapter;
42 import android.widget.ImageView;
43 import android.widget.ListView;
44 import android.widget.TextView;
46 import java.io.ByteArrayOutputStream;
48 public class MoveToFolder extends DialogFragment {
49 // The public interface is used to send information back to the parent activity.
50 public interface MoveToFolderListener {
51 void onMoveToFolder(DialogFragment dialogFragment);
54 // `moveToFolderListener` is used in `onAttach()` and `onCreateDialog`.
55 private MoveToFolderListener moveToFolderListener;
57 public void onAttach(Context context) {
58 super.onAttach(context);
60 // Get a handle for `MoveToFolderListener` from `parentActivity`.
62 moveToFolderListener = (MoveToFolderListener) context;
63 } catch(ClassCastException exception) {
64 throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
68 // `exceptFolders` is used in `onCreateDialog()` and `addSubfoldersToExceptFolders()`.
69 private String exceptFolders;
71 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
72 @SuppressLint("InflateParams")
74 public Dialog onCreateDialog(Bundle savedInstanceState) {
75 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
76 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
77 dialogBuilder.setTitle(R.string.move_to_folder);
78 // The parent view is `null` because it will be assigned by `AlertDialog`.
79 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
81 // Set an `onClick()` listener for the negative button.
82 dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
84 public void onClick(DialogInterface dialog, int which) {
85 // Do nothing. The `AlertDialog` will close automatically.
89 // Set the `onClick()` listener fo the positive button.
90 dialogBuilder.setPositiveButton(R.string.move, new Dialog.OnClickListener() {
92 public void onClick(DialogInterface dialog, int which) {
93 // Return the `DialogFragment` to the parent activity on save.
94 moveToFolderListener.onMoveToFolder(MoveToFolder.this);
98 // Create an `AlertDialog` from the `AlertDialog.Builder`.
99 final AlertDialog alertDialog = dialogBuilder.create();
101 // We need to show the `AlertDialog` before we can modify items in the layout.
104 // Initialize the `Cursor` and `CursorAdapter` variables.
105 Cursor foldersCursor;
106 CursorAdapter foldersCursorAdapter;
108 // Check to see if we are in the `Home Folder`.
109 if (BookmarksActivity.currentFolder.isEmpty()) { // Don't display `Home Folder` at the top of the `ListView`.
110 // Initialize `exceptFolders`.
113 // If a folder is selected, add it and all children to the list of folders not to display.
114 long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
115 for (long databaseIdLong : selectedBookmarksLongArray) {
116 // Get `databaseIdInt` for each selected bookmark.
117 int databaseIdInt = (int) databaseIdLong;
119 // If `databaseIdInt` is a folder.
120 if (BookmarksActivity.bookmarksDatabaseHandler.isFolder(databaseIdInt)) {
121 // Get the name of the selected folder.
122 String folderName = BookmarksActivity.bookmarksDatabaseHandler.getFolderName(databaseIdInt);
124 if (exceptFolders.isEmpty()){
125 // Add the selected folder to the list of folders not to display.
126 exceptFolders = DatabaseUtils.sqlEscapeString(folderName);
128 // Add the selected folder to the end of the list of folders not to display.
129 exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
132 // Add the selected folder's subfolders to the list of folders not to display.
133 addSubfoldersToExceptFolders(folderName);
137 // Get a `Cursor` containing the folders to display.
138 foldersCursor = BookmarksActivity.bookmarksDatabaseHandler.getFoldersCursorExcept(exceptFolders);
140 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
141 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
143 public View newView(Context context, Cursor cursor, ViewGroup parent) {
144 // Inflate the individual item layout. `false` does not attach it to the root.
145 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
149 public void bindView(View view, Context context, Cursor cursor) {
150 // Get the folder icon from `cursor`.
151 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
152 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
153 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
154 // Display `folderIconBitmap` in `move_to_folder_icon`.
155 ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
156 assert folderIconImageView != null; // Remove the warning below that `currentIconImageView` might be null;
157 folderIconImageView.setImageBitmap(folderIconBitmap);
159 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
160 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
161 TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
162 folderNameTextView.setText(folderName);
165 } else { // Display `Home Folder` at the top of the `ListView`.
166 // Get the home folder icon drawable and convert it to a `Bitmap`. `this` specifies the current context.
167 Drawable homeFolderIconDrawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.folder_grey_bitmap);
168 BitmapDrawable homeFolderIconBitmapDrawable = (BitmapDrawable) homeFolderIconDrawable;
169 Bitmap homeFolderIconBitmap = homeFolderIconBitmapDrawable.getBitmap();
170 // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
171 ByteArrayOutputStream homeFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
172 homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream);
173 byte[] homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray();
175 // Setup a `MatrixCursor` for the `Home Folder`.
176 String[] homeFolderMatrixCursorColumnNames = {BookmarksDatabaseHandler._ID, BookmarksDatabaseHandler.BOOKMARK_NAME, BookmarksDatabaseHandler.FAVORITE_ICON};
177 MatrixCursor homeFolderMatrixCursor = new MatrixCursor(homeFolderMatrixCursorColumnNames);
178 homeFolderMatrixCursor.addRow(new Object[]{0, getString(R.string.home_folder), homeFolderIconByteArray});
180 // Add the parent folder to the list of folders not to display.
181 exceptFolders = DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder);
183 // If a folder is selected, add it and all children to the list of folders not to display.
184 long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
185 for (long databaseIdLong : selectedBookmarksLongArray) {
186 // Get `databaseIdInt` for each selected bookmark.
187 int databaseIdInt = (int) databaseIdLong;
189 // If `databaseIdInt` is a folder.
190 if (BookmarksActivity.bookmarksDatabaseHandler.isFolder(databaseIdInt)) {
191 // Get the name of the selected folder.
192 String folderName = BookmarksActivity.bookmarksDatabaseHandler.getFolderName(databaseIdInt);
194 // Add the selected folder to the end of the list of folders not to display.
195 exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
197 // Add the selected folder's subfolders to the list of folders not to display.
198 addSubfoldersToExceptFolders(folderName);
202 // Get a `foldersCursor`.
203 foldersCursor = BookmarksActivity.bookmarksDatabaseHandler.getFoldersCursorExcept(exceptFolders);
205 // Combine `homeFolderMatrixCursor` and `foldersCursor`.
206 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
208 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
209 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
211 public View newView(Context context, Cursor cursor, ViewGroup parent) {
212 // Inflate the individual item layout. `false` does not attach it to the root.
213 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
217 public void bindView(View view, Context context, Cursor cursor) {
218 // Get the folder icon from `cursor`.
219 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
220 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
221 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
222 // Display `folderIconBitmap` in `move_to_folder_icon`.
223 ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
224 assert folderIconImageView != null; // Remove the warning below that `currentIconImageView` might be null;
225 folderIconImageView.setImageBitmap(folderIconBitmap);
227 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
228 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
229 TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
230 folderNameTextView.setText(folderName);
235 // Display the ListView
236 ListView foldersListView = (ListView) alertDialog.findViewById(R.id.move_to_folder_listview);
237 assert foldersListView != null; // Remove the warning below that `foldersListView` might be null.
238 foldersListView.setAdapter(foldersCursorAdapter);
240 // `onCreateDialog` requires the return of an `AlertDialog`.
244 private void addSubfoldersToExceptFolders(String folderName) {
245 // Get a `Cursor` will all the immediate subfolders.
246 Cursor subfoldersCursor = BookmarksActivity.bookmarksDatabaseHandler.getSubfoldersCursor(folderName);
248 for (int i = 0; i < subfoldersCursor.getCount(); i++) {
249 // Move `subfolderCursor` to the current item.
250 subfoldersCursor.moveToPosition(i);
252 // Get the name of the subfolder.
253 String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
255 // Run the same tasks for any subfolders of the subfolder.
256 addSubfoldersToExceptFolders(subfolderName);
258 // Add the subfolder to `exceptFolders`.
259 subfolderName = DatabaseUtils.sqlEscapeString(subfolderName);
260 exceptFolders = exceptFolders + "," + subfolderName;