2 * Copyright © 2016-2018 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.view.WindowManager;
43 import android.widget.AdapterView;
44 import android.widget.Button;
45 import android.widget.CursorAdapter;
46 import android.widget.ImageView;
47 import android.widget.ListView;
48 import android.widget.TextView;
50 import com.stoutner.privacybrowser.R;
51 import com.stoutner.privacybrowser.activities.BookmarksActivity;
52 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
53 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
55 import java.io.ByteArrayOutputStream;
57 public class MoveToFolderDialog extends AppCompatDialogFragment {
58 // Instantiate class variables.
59 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
60 private StringBuilder exceptFolders;
62 // The public interface is used to send information back to the parent activity.
63 public interface MoveToFolderListener {
64 void onMoveToFolder(AppCompatDialogFragment dialogFragment);
67 // `moveToFolderListener` is used in `onAttach()` and `onCreateDialog`.
68 private MoveToFolderListener moveToFolderListener;
70 public void onAttach(Context context) {
71 super.onAttach(context);
73 // Get a handle for `MoveToFolderListener` from `parentActivity`.
75 moveToFolderListener = (MoveToFolderListener) context;
76 } catch(ClassCastException exception) {
77 throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
81 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
82 @SuppressLint("InflateParams")
85 public Dialog onCreateDialog(Bundle savedInstanceState) {
86 // 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`.
87 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(getContext(), null, null, 0);
89 // Use an alert dialog builder to create the alert dialog.
90 AlertDialog.Builder dialogBuilder;
92 // Set the style according to the theme.
93 if (MainWebViewActivity.darkTheme) {
94 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
96 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
100 dialogBuilder.setTitle(R.string.move_to_folder);
102 // Remove the incorrect lint warning that `getActivity()` might be null.
103 assert getActivity() != null;
105 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
106 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
108 // Set the listener for the negative button.
109 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
110 // Do nothing. The `AlertDialog` will close automatically.
113 // Set the listener fo the positive button.
114 dialogBuilder.setPositiveButton(R.string.move, (DialogInterface dialog, int which) -> {
115 // Return the `DialogFragment` to the parent activity on save.
116 moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this);
119 // Create an alert dialog from the alert dialog builder.
120 final AlertDialog alertDialog = dialogBuilder.create();
122 // Disable screenshots if not allowed.
123 if (!MainWebViewActivity.allowScreenshots) {
124 // Remove the warning below that `getWindow()` might be null.
125 assert alertDialog.getWindow() != null;
127 // Disable screenshots.
128 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
131 // Show the alert dialog so the items in the layout can be modified.
134 // Get a handle for the positive button.
135 final Button moveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
137 // Initially disable the positive button.
138 moveButton.setEnabled(false);
140 // Initialize the variables.
141 Cursor foldersCursor;
142 CursorAdapter foldersCursorAdapter;
143 exceptFolders = new StringBuilder();
145 // Check to see if we are in the `Home Folder`.
146 if (BookmarksActivity.currentFolder.isEmpty()) { // Don't display `Home Folder` at the top of the `ListView`.
147 // If a folder is selected, add it and all children to the list of folders not to display.
148 long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
149 for (long databaseIdLong : selectedBookmarksLongArray) {
150 // Get `databaseIdInt` for each selected bookmark.
151 int databaseIdInt = (int) databaseIdLong;
153 // If `databaseIdInt` is a folder.
154 if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
155 // Get the name of the selected folder.
156 String folderName = bookmarksDatabaseHelper.getFolderName(databaseIdInt);
158 // Populate the list of folders not to get.
159 if (exceptFolders.toString().isEmpty()){
160 // Add the selected folder to the list of folders not to display.
161 exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName));
163 // Add the selected folder to the end of the list of folders not to display.
164 exceptFolders.append(",");
165 exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName));
168 // Add the selected folder's subfolders to the list of folders not to display.
169 addSubfoldersToExceptFolders(folderName);
173 // Get a `Cursor` containing the folders to display.
174 foldersCursor = bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders.toString());
176 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
177 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
179 public View newView(Context context, Cursor cursor, ViewGroup parent) {
180 // Inflate the individual item layout. `false` does not attach it to the root.
181 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
185 public void bindView(View view, Context context, Cursor cursor) {
186 // Get the folder icon from `cursor`.
187 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
188 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
189 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
190 // Display `folderIconBitmap` in `move_to_folder_icon`.
191 ImageView folderIconImageView = view.findViewById(R.id.move_to_folder_icon);
192 folderIconImageView.setImageBitmap(folderIconBitmap);
194 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
195 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
196 TextView folderNameTextView = view.findViewById(R.id.move_to_folder_name_textview);
197 folderNameTextView.setText(folderName);
200 } else { // Display `Home Folder` at the top of the `ListView`.
201 // Get the home folder icon drawable and convert it to a `Bitmap`.
202 Drawable homeFolderIconDrawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.folder_gray_bitmap);
203 BitmapDrawable homeFolderIconBitmapDrawable = (BitmapDrawable) homeFolderIconDrawable;
204 assert homeFolderIconDrawable != null;
205 Bitmap homeFolderIconBitmap = homeFolderIconBitmapDrawable.getBitmap();
207 // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
208 ByteArrayOutputStream homeFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
209 homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream);
210 byte[] homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray();
212 // Setup a `MatrixCursor` for the `Home Folder`.
213 String[] homeFolderMatrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME, BookmarksDatabaseHelper.FAVORITE_ICON};
214 MatrixCursor homeFolderMatrixCursor = new MatrixCursor(homeFolderMatrixCursorColumnNames);
215 homeFolderMatrixCursor.addRow(new Object[]{0, getString(R.string.home_folder), homeFolderIconByteArray});
217 // Add the parent folder to the list of folders not to display.
218 exceptFolders.append(DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder));
220 // If a folder is selected, add it and all children to the list of folders not to display.
221 long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
222 for (long databaseIdLong : selectedBookmarksLongArray) {
223 // Get `databaseIdInt` for each selected bookmark.
224 int databaseIdInt = (int) databaseIdLong;
226 // If `databaseIdInt` is a folder.
227 if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
228 // Get the name of the selected folder.
229 String folderName = bookmarksDatabaseHelper.getFolderName(databaseIdInt);
231 // Add the selected folder to the end of the list of folders not to display.
232 exceptFolders.append(",");
233 exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName));
235 // Add the selected folder's subfolders to the list of folders not to display.
236 addSubfoldersToExceptFolders(folderName);
240 // Get a `Cursor` containing the folders to display.
241 foldersCursor = bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders.toString());
243 // Combine `homeFolderMatrixCursor` and `foldersCursor`.
244 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
246 // Setup `foldersCursorAdaptor`. `false` disables autoRequery.
247 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
249 public View newView(Context context, Cursor cursor, ViewGroup parent) {
250 // Inflate the individual item layout. `false` does not attach it to the root.
251 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
255 public void bindView(View view, Context context, Cursor cursor) {
256 // Get the folder icon from `cursor`.
257 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
258 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
259 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
260 // Display `folderIconBitmap` in `move_to_folder_icon`.
261 ImageView folderIconImageView = view.findViewById(R.id.move_to_folder_icon);
262 folderIconImageView.setImageBitmap(folderIconBitmap);
264 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
265 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
266 TextView folderNameTextView = view.findViewById(R.id.move_to_folder_name_textview);
267 folderNameTextView.setText(folderName);
272 // Display the ListView
273 ListView foldersListView = alertDialog.findViewById(R.id.move_to_folder_listview);
274 foldersListView.setAdapter(foldersCursorAdapter);
276 // Enable the move button when a folder is selected.
277 foldersListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
278 // Enable the move button.
279 moveButton.setEnabled(true);
282 // `onCreateDialog` requires the return of an `AlertDialog`.
286 private void addSubfoldersToExceptFolders(String folderName) {
287 // Get a `Cursor` will all the immediate subfolders.
288 Cursor subfoldersCursor = bookmarksDatabaseHelper.getSubfoldersCursor(folderName);
290 for (int i = 0; i < subfoldersCursor.getCount(); i++) {
291 // Move `subfolderCursor` to the current item.
292 subfoldersCursor.moveToPosition(i);
294 // Get the name of the subfolder.
295 String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
297 // Add the subfolder to `exceptFolders`.
298 exceptFolders.append(",");
299 exceptFolders.append(DatabaseUtils.sqlEscapeString(subfolderName));
301 // Run the same tasks for any subfolders of the subfolder.
302 addSubfoldersToExceptFolders(subfolderName);