X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FMoveToFolderDialog.java;h=2a767d969cfaad375151882d11f34c66ed63e6c2;hb=9f551f25b53a30cca7b19b6e6bfc2d2520d9aa1b;hp=a8012a4ecb8658042bc8367c7ed1f1326f17b8eb;hpb=e923db59fff5ad31f08679c84678cdfb1ca87d63;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.java b/app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.java index a8012a4e..2a767d96 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2017 Soren Stoutner . + * Copyright © 2016-2017 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -35,10 +35,12 @@ import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; -// We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22. +// `AppCompatDialogFragment` must be used instead of `DialogFragment` or an error is produced on API <=22. import android.support.v7.app.AppCompatDialogFragment; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.Button; import android.widget.CursorAdapter; import android.widget.ImageView; import android.widget.ListView; @@ -46,11 +48,16 @@ import android.widget.TextView; import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.activities.BookmarksActivity; +import com.stoutner.privacybrowser.activities.MainWebViewActivity; import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper; import java.io.ByteArrayOutputStream; public class MoveToFolderDialog extends AppCompatDialogFragment { + // Instantiate class variables. + private BookmarksDatabaseHelper bookmarksDatabaseHelper; + private StringBuilder exceptFolders; + // The public interface is used to send information back to the parent activity. public interface MoveToFolderListener { void onMoveToFolder(AppCompatDialogFragment dialogFragment); @@ -70,43 +77,53 @@ public class MoveToFolderDialog extends AppCompatDialogFragment { } } - // `exceptFolders` is used in `onCreateDialog()` and `addSubfoldersToExceptFolders()`. - private String exceptFolders; - // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`. @SuppressLint("InflateParams") @Override @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { - // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text. - AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); + // 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`. + bookmarksDatabaseHelper = new BookmarksDatabaseHelper(getContext(), null, null, 0); + + // Use `AlertDialog.Builder` to create the `AlertDialog`. + AlertDialog.Builder dialogBuilder; + + // Set the style according to the theme. + if (MainWebViewActivity.darkTheme) { + dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark); + } else { + dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight); + } + + // Set the title. dialogBuilder.setTitle(R.string.move_to_folder); - // The parent view is `null` because it will be assigned by `AlertDialog`. + + // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`. dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null)); // Set an `onClick()` listener for the negative button. - dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Do nothing. The `AlertDialog` will close automatically. - } + dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> { + // Do nothing. The `AlertDialog` will close automatically. }); // Set the `onClick()` listener fo the positive button. - dialogBuilder.setPositiveButton(R.string.move, new Dialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Return the `DialogFragment` to the parent activity on save. - moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this); - } + dialogBuilder.setPositiveButton(R.string.move, (DialogInterface dialog, int which) -> { + // Return the `DialogFragment` to the parent activity on save. + moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this); }); // Create an `AlertDialog` from the `AlertDialog.Builder`. final AlertDialog alertDialog = dialogBuilder.create(); - // We need to show the `AlertDialog` before we can modify items in the layout. + // Show the `AlertDialog` so the items in the layout can be modified. alertDialog.show(); + // Get a handle for the positive button. + final Button moveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); + + // Initially disable the positive button. + moveButton.setEnabled(false); + // Initialize the `Cursor` and `CursorAdapter` variables. Cursor foldersCursor; CursorAdapter foldersCursorAdapter; @@ -114,7 +131,7 @@ public class MoveToFolderDialog extends AppCompatDialogFragment { // Check to see if we are in the `Home Folder`. if (BookmarksActivity.currentFolder.isEmpty()) { // Don't display `Home Folder` at the top of the `ListView`. // Initialize `exceptFolders`. - exceptFolders = ""; + exceptFolders = new StringBuilder(); // If a folder is selected, add it and all children to the list of folders not to display. long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds; @@ -123,16 +140,18 @@ public class MoveToFolderDialog extends AppCompatDialogFragment { int databaseIdInt = (int) databaseIdLong; // If `databaseIdInt` is a folder. - if (BookmarksActivity.bookmarksDatabaseHelper.isFolder(databaseIdInt)) { + if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) { // Get the name of the selected folder. - String folderName = BookmarksActivity.bookmarksDatabaseHelper.getFolderName(databaseIdInt); + String folderName = bookmarksDatabaseHelper.getFolderName(databaseIdInt); - if (exceptFolders.isEmpty()){ + // Populate the list of folders not to get. + if (exceptFolders.toString().isEmpty()){ // Add the selected folder to the list of folders not to display. - exceptFolders = DatabaseUtils.sqlEscapeString(folderName); + exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName)); } else { // Add the selected folder to the end of the list of folders not to display. - exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName); + exceptFolders.append(","); + exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName)); } // Add the selected folder's subfolders to the list of folders not to display. @@ -141,7 +160,7 @@ public class MoveToFolderDialog extends AppCompatDialogFragment { } // Get a `Cursor` containing the folders to display. - foldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders); + foldersCursor = bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders.toString()); // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery. foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) { @@ -158,12 +177,12 @@ public class MoveToFolderDialog extends AppCompatDialogFragment { // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last. Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length); // Display `folderIconBitmap` in `move_to_folder_icon`. - ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon); + ImageView folderIconImageView = view.findViewById(R.id.move_to_folder_icon); folderIconImageView.setImageBitmap(folderIconBitmap); // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`. String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); - TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview); + TextView folderNameTextView = view.findViewById(R.id.move_to_folder_name_textview); folderNameTextView.setText(folderName); } }; @@ -183,7 +202,7 @@ public class MoveToFolderDialog extends AppCompatDialogFragment { homeFolderMatrixCursor.addRow(new Object[]{0, getString(R.string.home_folder), homeFolderIconByteArray}); // Add the parent folder to the list of folders not to display. - exceptFolders = DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder); + exceptFolders.append(DatabaseUtils.sqlEscapeString(BookmarksActivity.currentFolder)); // If a folder is selected, add it and all children to the list of folders not to display. long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds; @@ -192,25 +211,26 @@ public class MoveToFolderDialog extends AppCompatDialogFragment { int databaseIdInt = (int) databaseIdLong; // If `databaseIdInt` is a folder. - if (BookmarksActivity.bookmarksDatabaseHelper.isFolder(databaseIdInt)) { + if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) { // Get the name of the selected folder. - String folderName = BookmarksActivity.bookmarksDatabaseHelper.getFolderName(databaseIdInt); + String folderName = bookmarksDatabaseHelper.getFolderName(databaseIdInt); // Add the selected folder to the end of the list of folders not to display. - exceptFolders = exceptFolders + "," + DatabaseUtils.sqlEscapeString(folderName); + exceptFolders.append(","); + exceptFolders.append(DatabaseUtils.sqlEscapeString(folderName)); // Add the selected folder's subfolders to the list of folders not to display. addSubfoldersToExceptFolders(folderName); } } - // Get a `foldersCursor`. - foldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders); + // Get a `Cursor` containing the folders to display. + foldersCursor = bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders.toString()); // Combine `homeFolderMatrixCursor` and `foldersCursor`. MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor}); - // Setup `foldersCursorAdaptor` with `this` context. `false` disables autoRequery. + // Setup `foldersCursorAdaptor`. `false` disables autoRequery. foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) { @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { @@ -225,28 +245,34 @@ public class MoveToFolderDialog extends AppCompatDialogFragment { // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last. Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length); // Display `folderIconBitmap` in `move_to_folder_icon`. - ImageView folderIconImageView = (ImageView) view.findViewById(R.id.move_to_folder_icon); + ImageView folderIconImageView = view.findViewById(R.id.move_to_folder_icon); folderIconImageView.setImageBitmap(folderIconBitmap); // Get the folder name from `cursor` and display it in `move_to_folder_name_textview`. String folderName = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); - TextView folderNameTextView = (TextView) view.findViewById(R.id.move_to_folder_name_textview); + TextView folderNameTextView = view.findViewById(R.id.move_to_folder_name_textview); folderNameTextView.setText(folderName); } }; } // Display the ListView - ListView foldersListView = (ListView) alertDialog.findViewById(R.id.move_to_folder_listview); + ListView foldersListView = alertDialog.findViewById(R.id.move_to_folder_listview); foldersListView.setAdapter(foldersCursorAdapter); + // Enable the move button when a folder is selected. + foldersListView.setOnItemClickListener((AdapterView parent, View view, int position, long id) -> { + // Enable the move button. + moveButton.setEnabled(true); + }); + // `onCreateDialog` requires the return of an `AlertDialog`. return alertDialog; } private void addSubfoldersToExceptFolders(String folderName) { // Get a `Cursor` will all the immediate subfolders. - Cursor subfoldersCursor = BookmarksActivity.bookmarksDatabaseHelper.getSubfoldersCursor(folderName); + Cursor subfoldersCursor = bookmarksDatabaseHelper.getSubfoldersCursor(folderName); for (int i = 0; i < subfoldersCursor.getCount(); i++) { // Move `subfolderCursor` to the current item. @@ -255,13 +281,12 @@ public class MoveToFolderDialog extends AppCompatDialogFragment { // Get the name of the subfolder. String subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); + // Add the subfolder to `exceptFolders`. + exceptFolders.append(","); + exceptFolders.append(DatabaseUtils.sqlEscapeString(subfolderName)); + // Run the same tasks for any subfolders of the subfolder. addSubfoldersToExceptFolders(subfolderName); - - // Add the subfolder to `exceptFolders`. - subfolderName = DatabaseUtils.sqlEscapeString(subfolderName); - exceptFolders = exceptFolders + "," + subfolderName; } - } }