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.app.Activity;
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.design.widget.Snackbar;
38 import android.support.v4.content.ContextCompat;
39 import android.support.v7.app.AlertDialog;
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 java.io.ByteArrayOutputStream;
49 public class MoveToFolder extends DialogFragment {
50 // The public interface is used to send information back to the parent activity.
51 public interface MoveToFolderListener {
52 void onCancelMoveToFolder(DialogFragment dialogFragment);
54 void onMoveToFolder(DialogFragment dialogFragment);
57 // `moveToFolderListener` is used in `onAttach()` and `onCreateDialog`.
58 private MoveToFolderListener moveToFolderListener;
60 public void onAttach(Activity parentActivity) {
61 super.onAttach(parentActivity);
63 // Get a handle for `MoveToFolderListener` from `parentActivity`.
65 moveToFolderListener = (MoveToFolderListener) parentActivity;
66 } catch(ClassCastException exception) {
67 throw new ClassCastException(parentActivity.toString() + " must implement EditBookmarkFolderListener.");
71 // `exceptFolders` is used in `onCreateDialog()` and `addSubfoldersToExceptFolders()`.
72 private String exceptFolders;
75 public Dialog onCreateDialog(Bundle savedInstanceState) {
76 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
77 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
78 dialogBuilder.setTitle(R.string.move_to_folder);
79 // The parent view is `null` because it will be assigned by `AlertDialog`.
80 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
82 // Set an `onClick()` listener for the negative button.
83 dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() {
85 public void onClick(DialogInterface dialog, int which) {
86 // Return the `DialogFragment` to the parent activity on cancel.
87 moveToFolderListener.onCancelMoveToFolder(MoveToFolder.this);
91 // Set the `onClick()` listener fo the positive button.
92 dialogBuilder.setPositiveButton(R.string.move, new Dialog.OnClickListener() {
94 public void onClick(DialogInterface dialog, int which) {
95 // Return the `DialogFragment` to the parent activity on save.
96 moveToFolderListener.onMoveToFolder(MoveToFolder.this);
100 // Create an `AlertDialog` from the `AlertDialog.Builder`.
101 final AlertDialog alertDialog = dialogBuilder.create();
103 // We need to show the `AlertDialog` before we can modify items in the layout.
106 // Initialize the `Cursor` and `CursorAdapter` variables.
107 Cursor foldersCursor;
108 CursorAdapter foldersCursorAdapter;
110 // Check to see if we are in the `Home Folder`.
111 if (BookmarksActivity.currentFolder.isEmpty()) { // Don't display `Home Folder` at the top of the `ListView`.
112 // Initialize `exceptFolders`.
115 // If a folder is selected, add it and all children to the list of folders not to display.
116 long[] selectedBookmarksLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds();
117 for (long databaseIdLong : selectedBookmarksLongArray) {
118 // Get `databaseIdInt` for each selected bookmark.
119 int databaseIdInt = (int) databaseIdLong;
121 // If `databaseIdInt` is a folder.
122 if (BookmarksActivity.bookmarksDatabaseHandler.isFolder(databaseIdInt)) {
123 // Get the name of the selected folder.
124 String folderName = BookmarksActivity.bookmarksDatabaseHandler.getFolderName(databaseIdInt);
126 if (exceptFolders.isEmpty()){
127 // Add the selected folder to the list of folders not to display.
128 exceptFolders = DatabaseUtils.sqlEscapeString(folderName);
130 // Add the selected folder to the end of the list of folders not to display.
131 exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
134 // Add the selected folder's subfolders to the list of folders not to display.
135 addSubfoldersToExceptFolders(folderName);
139 // Get a `Cursor` containing the folders to display.
140 foldersCursor = BookmarksActivity.bookmarksDatabaseHandler.getFoldersCursorExcept(exceptFolders);
142 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
143 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
145 public View newView(Context context, Cursor cursor, ViewGroup parent) {
146 // Inflate the individual item layout. `false` does not attach it to the root.
147 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
151 public void bindView(View view, Context context, Cursor cursor) {
152 // Get the folder icon from `cursor`.
153 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
154 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
155 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
156 // Display `folderIconBitmap` in `move_to_folder_icon`.
157 ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
158 assert folderIconImageView != null; // Remove the warning below that `currentIconImageView` might be null;
159 folderIconImageView.setImageBitmap(folderIconBitmap);
161 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
162 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
163 TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
164 folderNameTextView.setText(folderName);
167 } else { // Display `Home Folder` at the top of the `ListView`.
168 // Get the home folder icon drawable and convert it to a `Bitmap`. `this` specifies the current context.
169 Drawable homeFolderIconDrawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.folder_grey_bitmap);
170 BitmapDrawable homeFolderIconBitmapDrawable = (BitmapDrawable) homeFolderIconDrawable;
171 Bitmap homeFolderIconBitmap = homeFolderIconBitmapDrawable.getBitmap();
172 // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
173 ByteArrayOutputStream homeFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
174 homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream);
175 byte[] homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray();
177 // Setup a `MatrixCursor` for the `Home Folder`.
178 String[] homeFolderMatrixCursorColumnNames = {BookmarksDatabaseHandler._ID, BookmarksDatabaseHandler.BOOKMARK_NAME, BookmarksDatabaseHandler.FAVORITE_ICON};
179 MatrixCursor homeFolderMatrixCursor = new MatrixCursor(homeFolderMatrixCursorColumnNames);
180 homeFolderMatrixCursor.addRow(new Object[]{0, getString(R.string.home_folder), homeFolderIconByteArray});
182 // Add the parent folder to the list of folders not to display.
183 exceptFolders = DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder);
185 // If a folder is selected, add it and all children to the list of folders not to display.
186 long[] selectedBookmarksLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds();
187 for (long databaseIdLong : selectedBookmarksLongArray) {
188 // Get `databaseIdInt` for each selected bookmark.
189 int databaseIdInt = (int) databaseIdLong;
191 // If `databaseIdInt` is a folder.
192 if (BookmarksActivity.bookmarksDatabaseHandler.isFolder(databaseIdInt)) {
193 // Get the name of the selected folder.
194 String folderName = BookmarksActivity.bookmarksDatabaseHandler.getFolderName(databaseIdInt);
196 // Add the selected folder to the end of the list of folders not to display.
197 exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName);
199 // Add the selected folder's subfolders to the list of folders not to display.
200 addSubfoldersToExceptFolders(folderName);
204 // Get a `foldersCursor`.
205 foldersCursor = BookmarksActivity.bookmarksDatabaseHandler.getFoldersCursorExcept(exceptFolders);
207 // Combine `homeFolderMatrixCursor` and `foldersCursor`.
208 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
210 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
211 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
213 public View newView(Context context, Cursor cursor, ViewGroup parent) {
214 // Inflate the individual item layout. `false` does not attach it to the root.
215 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
219 public void bindView(View view, Context context, Cursor cursor) {
220 // Get the folder icon from `cursor`.
221 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
222 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
223 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
224 // Display `folderIconBitmap` in `move_to_folder_icon`.
225 ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon);
226 assert folderIconImageView != null; // Remove the warning below that `currentIconImageView` might be null;
227 folderIconImageView.setImageBitmap(folderIconBitmap);
229 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
230 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
231 TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview);
232 folderNameTextView.setText(folderName);
237 // Display the ListView
238 ListView foldersListView = (ListView) alertDialog.findViewById(R.id.move_to_folder_listview);
239 assert foldersListView != null; // Remove the warning below that `foldersListView` might be null.
240 foldersListView.setAdapter(foldersCursorAdapter);
242 // `onCreateDialog` requires the return of an `AlertDialog`.
246 public void addSubfoldersToExceptFolders(String folderName) {
247 // Get a `Cursor` will all the immediate subfolders.
248 Cursor subfoldersCursor = BookmarksActivity.bookmarksDatabaseHandler.getSubfoldersCursor(folderName);
250 for (int i = 0; i < subfoldersCursor.getCount(); i++) {
251 // Move `subfolderCursor` to the current item.
252 subfoldersCursor.moveToPosition(i);
254 // Get the name of the subfolder.
255 String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
257 // Run the same tasks for any subfolders of the subfolder.
258 addSubfoldersToExceptFolders(subfolderName);
260 // Add the subfolder to `exceptFolders`.
261 subfolderName = DatabaseUtils.sqlEscapeString(subfolderName);
262 exceptFolders = exceptFolders + "," + subfolderName;