2 * Copyright © 2016-2020 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.Dialog;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.content.SharedPreferences;
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.preference.PreferenceManager;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.view.WindowManager;
40 import android.widget.AdapterView;
41 import android.widget.Button;
42 import android.widget.CursorAdapter;
43 import android.widget.ImageView;
44 import android.widget.ListView;
45 import android.widget.TextView;
47 import androidx.annotation.NonNull;
48 import androidx.appcompat.app.AlertDialog;
49 import androidx.core.content.ContextCompat;
50 import androidx.fragment.app.DialogFragment;
52 import com.stoutner.privacybrowser.R;
53 import com.stoutner.privacybrowser.activities.BookmarksActivity;
54 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
56 import java.io.ByteArrayOutputStream;
58 public class MoveToFolderDialog extends DialogFragment {
59 // Instantiate the class variables.
60 private MoveToFolderListener moveToFolderListener;
61 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
62 private StringBuilder exceptFolders;
64 // The public interface is used to send information back to the parent activity.
65 public interface MoveToFolderListener {
66 void onMoveToFolder(DialogFragment dialogFragment);
69 public void onAttach(@NonNull Context context) {
70 // Run the default commands.
71 super.onAttach(context);
73 // Get a handle for `MoveToFolderListener` from the launching context.
74 moveToFolderListener = (MoveToFolderListener) context;
77 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
78 @SuppressLint("InflateParams")
81 public Dialog onCreateDialog(Bundle savedInstanceState) {
82 // Initialize the database helper. The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
83 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(getContext(), null, null, 0);
85 // Use an alert dialog builder to create the alert dialog.
86 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
89 dialogBuilder.setTitle(R.string.move_to_folder);
91 // Remove the incorrect lint warning that `getActivity()` might be null.
92 assert getActivity() != null;
94 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
95 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
97 // Set the listener for the negative button.
98 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
99 // Do nothing. The `AlertDialog` will close automatically.
102 // Set the listener fo the positive button.
103 dialogBuilder.setPositiveButton(R.string.move, (DialogInterface dialog, int which) -> {
104 // Return the `DialogFragment` to the parent activity on save.
105 moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this);
108 // Create an alert dialog from the alert dialog builder.
109 final AlertDialog alertDialog = dialogBuilder.create();
111 // Get a handle for the shared preferences.
112 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
114 // Get the screenshot preference.
115 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
117 // Disable screenshots if not allowed.
118 if (!allowScreenshots) {
119 // Remove the warning below that `getWindow()` might be null.
120 assert alertDialog.getWindow() != null;
122 // Disable screenshots.
123 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
126 // Show the alert dialog so the items in the layout can be modified.
129 // Get a handle for the positive button.
130 final Button moveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
132 // Initially disable the positive button.
133 moveButton.setEnabled(false);
135 // Initialize the variables.
136 Cursor foldersCursor;
137 CursorAdapter foldersCursorAdapter;
138 exceptFolders = new StringBuilder();
140 // Check to see if we are in the `Home Folder`.
141 if (BookmarksActivity.currentFolder.isEmpty()) { // Don't display `Home Folder` at the top of the `ListView`.
142 // If a folder is selected, add it and all children to the list of folders not to display.
143 long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
144 for (long databaseIdLong : selectedBookmarksLongArray) {
145 // Get `databaseIdInt` for each selected bookmark.
146 int databaseIdInt = (int) databaseIdLong;
148 // If `databaseIdInt` is a folder.
149 if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
150 // Get the name of the selected folder.
151 String folderName = bookmarksDatabaseHelper.getFolderName(databaseIdInt);
153 // Populate the list of folders not to get.
154 if (exceptFolders.toString().isEmpty()){
155 // Add the selected folder to the list of folders not to display.
156 exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName));
158 // Add the selected folder to the end of the list of folders not to display.
159 exceptFolders.append(",");
160 exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName));
163 // Add the selected folder's subfolders to the list of folders not to display.
164 addSubfoldersToExceptFolders(folderName);
168 // Get a cursor containing the folders to display.
169 foldersCursor = bookmarksDatabaseHelper.getFoldersExcept(exceptFolders.toString());
171 // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery.
172 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
174 public View newView(Context context, Cursor cursor, ViewGroup parent) {
175 // Remove the incorrect lint warning that `.getLayoutInflater()` might be false.
176 assert getActivity() != null;
178 // Inflate the individual item layout. `false` does not attach it to the root.
179 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
183 public void bindView(View view, Context context, Cursor cursor) {
184 // Get the folder icon from `cursor`.
185 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
186 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
187 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
188 // Display `folderIconBitmap` in `move_to_folder_icon`.
189 ImageView folderIconImageView = view.findViewById(R.id.move_to_folder_icon);
190 folderIconImageView.setImageBitmap(folderIconBitmap);
192 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
193 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
194 TextView folderNameTextView = view.findViewById(R.id.move_to_folder_name_textview);
195 folderNameTextView.setText(folderName);
198 } else { // Display `Home Folder` at the top of the `ListView`.
199 // Get the home folder icon drawable and convert it to a `Bitmap`.
200 Drawable homeFolderIconDrawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.folder_gray_bitmap);
201 BitmapDrawable homeFolderIconBitmapDrawable = (BitmapDrawable) homeFolderIconDrawable;
202 assert homeFolderIconDrawable != null;
203 Bitmap homeFolderIconBitmap = homeFolderIconBitmapDrawable.getBitmap();
205 // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
206 ByteArrayOutputStream homeFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
207 homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream);
208 byte[] homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray();
210 // Setup a `MatrixCursor` for the `Home Folder`.
211 String[] homeFolderMatrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME, BookmarksDatabaseHelper.FAVORITE_ICON};
212 MatrixCursor homeFolderMatrixCursor = new MatrixCursor(homeFolderMatrixCursorColumnNames);
213 homeFolderMatrixCursor.addRow(new Object[]{0, getString(R.string.home_folder), homeFolderIconByteArray});
215 // Add the parent folder to the list of folders not to display.
216 exceptFolders.append(DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder));
218 // If a folder is selected, add it and all children to the list of folders not to display.
219 long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
220 for (long databaseIdLong : selectedBookmarksLongArray) {
221 // Get `databaseIdInt` for each selected bookmark.
222 int databaseIdInt = (int) databaseIdLong;
224 // If `databaseIdInt` is a folder.
225 if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
226 // Get the name of the selected folder.
227 String folderName = bookmarksDatabaseHelper.getFolderName(databaseIdInt);
229 // Add the selected folder to the end of the list of folders not to display.
230 exceptFolders.append(",");
231 exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName));
233 // Add the selected folder's subfolders to the list of folders not to display.
234 addSubfoldersToExceptFolders(folderName);
238 // Get a `Cursor` containing the folders to display.
239 foldersCursor = bookmarksDatabaseHelper.getFoldersExcept(exceptFolders.toString());
241 // Combine `homeFolderMatrixCursor` and `foldersCursor`.
242 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
244 // Setup `foldersCursorAdaptor`. `false` disables autoRequery.
245 foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
247 public View newView(Context context, Cursor cursor, ViewGroup parent) {
248 // Remove the incorrect lint warning that `.getLayoutInflater()` might be false.
249 assert getActivity() != null;
251 // Inflate the individual item layout. `false` does not attach it to the root.
252 return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
256 public void bindView(View view, Context context, Cursor cursor) {
257 // Get the folder icon from `cursor`.
258 byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
259 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
260 Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length);
261 // Display `folderIconBitmap` in `move_to_folder_icon`.
262 ImageView folderIconImageView = view.findViewById(R.id.move_to_folder_icon);
263 folderIconImageView.setImageBitmap(folderIconBitmap);
265 // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`.
266 String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
267 TextView folderNameTextView = view.findViewById(R.id.move_to_folder_name_textview);
268 folderNameTextView.setText(folderName);
273 // Get a handle for the folders list view.
274 ListView foldersListView = alertDialog.findViewById(R.id.move_to_folder_listview);
276 // Remove the incorrect lint warning below that the view might be null.
277 assert foldersListView != null;
279 // Set the folder list view adapter.
280 foldersListView.setAdapter(foldersCursorAdapter);
282 // Enable the move button when a folder is selected.
283 foldersListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
284 // Enable the move button.
285 moveButton.setEnabled(true);
288 // `onCreateDialog` requires the return of an `AlertDialog`.
292 private void addSubfoldersToExceptFolders(String folderName) {
293 // Get a `Cursor` will all the immediate subfolders.
294 Cursor subfoldersCursor = bookmarksDatabaseHelper.getSubfolders(folderName);
296 for (int i = 0; i < subfoldersCursor.getCount(); i++) {
297 // Move `subfolderCursor` to the current item.
298 subfoldersCursor.moveToPosition(i);
300 // Get the name of the subfolder.
301 String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
303 // Add the subfolder to `exceptFolders`.
304 exceptFolders.append(",");
305 exceptFolders.append(DatabaseUtils.sqlEscapeString(subfolderName));
307 // Run the same tasks for any subfolders of the subfolder.
308 addSubfoldersToExceptFolders(subfolderName);