X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FBookmarksDatabaseViewActivity.java;h=1725cac46756d2c2c670061e7300a8ffe79633f0;hp=53fb657d4cebe31d25cf67027635ebe4a7e9a132;hb=e12908eb00d9c54c0a3c9f56312a31b9e5dfd094;hpb=a8669fa252bd4a278decc411912d6f8674ff483d diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java index 53fb657d..1725cac4 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java @@ -21,19 +21,24 @@ package com.stoutner.privacybrowser.activities; import android.content.Context; import android.database.Cursor; +import android.database.MatrixCursor; +import android.database.MergeCursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Typeface; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v4.widget.CursorAdapter; +import android.support.v4.widget.ResourceCursorAdapter; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.ImageView; import android.widget.ListView; +import android.widget.Spinner; import android.widget.TextView; import com.stoutner.privacybrowser.R; @@ -46,6 +51,9 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity { // `bookmarksListView` is used in `onCreate()` and `updateBookmarksListView()`. private ListView bookmarksListView; + // `bookmarksCursorAdapter` is used in `onCreate()` and `updateBookmarksListView()`. + private CursorAdapter bookmarksCursorAdapter; + @Override public void onCreate(Bundle savedInstanceState) { // Set the activity theme. @@ -56,26 +64,109 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity { } super.onCreate(savedInstanceState); - setContentView(R.layout.bookmarks_database_view_coordinatorlayout); + setContentView(R.layout.bookmarks_databaseview_coordinatorlayout); // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21. final Toolbar bookmarksDatabaseViewAppBar = (Toolbar) findViewById(R.id.bookmarks_database_view_toolbar); setSupportActionBar(bookmarksDatabaseViewAppBar); - // Display the home arrow on `SupportActionBar`. + // Get a handle for the `AppBar`. final ActionBar appBar = getSupportActionBar(); - assert appBar != null; // This assert removes the incorrect warning in Android Studio on the following line that appBar might be null. - appBar.setDisplayHomeAsUpEnabled(true); - // Initialize the database handler and the ListView. - // `this` specifies the context. The two `null`s do not specify the database name or a `CursorFactory`. - // The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`. + // Remove the incorrect warning in Android Studio that `appBar` might be null. + assert appBar != null; + + // Display the `Spinner` and the back arrow in the `AppBar`. + appBar.setCustomView(R.layout.bookmarks_databaseview_spinner); + appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP); + + // Initialize the database handler. `this` specifies the context. The two `null`s do not specify the database name or a `CursorFactory`. The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`. bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0); + + // Setup a `MatrixCursor` for "All Folders" and "Home Folder". + String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME}; + MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames); + matrixCursor.addRow(new Object[]{-2, getString(R.string.all_folders)}); + matrixCursor.addRow(new Object[]{-1, getString(R.string.home_folder)}); + + // Get a `Cursor` with the list of all the folders. + Cursor foldersCursor = bookmarksDatabaseHelper.getAllFoldersCursor(); + + // Combine `matrixCursor` and `foldersCursor`. + MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor}); + + // Create a `ResourceCursorAdapter` for the spinner with `this` context. `0` specifies no flags.; + ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(this, R.layout.bookmarks_databaseview_spinner_item, foldersMergeCursor, 0) { + @Override + public void bindView(View view, Context context, Cursor cursor) { + // Get a handle for the spinner item `TextView`. + TextView spinnerItemTextView = (TextView) view.findViewById(R.id.spinner_item_textview); + + // Set the `TextView` to display the folder name. + spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME))); + } + }; + + // Set the `ResourceCursorAdapter` drop drown view resource. + foldersCursorAdapter.setDropDownViewResource(R.layout.bookmarks_databaseview_spinner_dropdown_item); + + // Get a handle for the folder `Spinner`. + Spinner folderSpinner = (Spinner) findViewById(R.id.bookmarks_database_view_spinner); + + // Set the adapter for the folder `Spinner`. + folderSpinner.setAdapter(foldersCursorAdapter); + + // Handle clicks on the `Spinner` dropdown. + folderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + // Convert the database ID to an `int`. + int databaseId = (int) id; + + // Instantiate the bookmarks `Cursor`. + Cursor bookmarksCursor; + + // Populate the bookmarks `ListView` based on the `Spinner` selection. + switch (databaseId) { + // Display all the folders. + case -2: + // Get a cursor with all the folders. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(); + break; + + // Display the home folder. + case -1: + // Get a cursor for the home folder. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(""); + break; + + // Display the selected folder. + default: + // Get a handle for the selected view. + TextView selectedFolderTextView = (TextView) view.findViewById(R.id.spinner_item_textview); + + // Extract the name of the selected folder. + String folderName = selectedFolderTextView.getText().toString(); + + // Get a cursor for the selected folder. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(folderName); + } + + // Update the `ListView`. + bookmarksCursorAdapter.changeCursor(bookmarksCursor); + } + + @Override + public void onNothingSelected(AdapterView parent) { + // Do nothing. + } + }); + + // Get a handle for the bookmarks `ListView`. bookmarksListView = (ListView) findViewById(R.id.bookmarks_database_view_listview); - // Display the bookmarks in the ListView. + // Display the bookmarks in the `ListView`. updateBookmarksListView(); - } private void updateBookmarksListView() { @@ -83,11 +174,11 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity { final Cursor bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(); // Setup `bookmarksCursorAdapter` with `this` context. The `false` disables autoRequery. - CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { + bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { // Inflate the individual item layout. `false` does not attach it to the root. - return getLayoutInflater().inflate(R.layout.bookmarks_database_view_item_linearlayout, parent, false); + return getLayoutInflater().inflate(R.layout.bookmarks_databaseview_item_linearlayout, parent, false); } @Override