2 * Copyright © 2016-2017 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.activities;
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.database.MergeCursor;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.graphics.Typeface;
29 import android.os.Bundle;
30 import android.support.v4.content.ContextCompat;
31 import android.support.v4.widget.CursorAdapter;
32 import android.support.v4.widget.ResourceCursorAdapter;
33 import android.support.v7.app.ActionBar;
34 import android.support.v7.app.AppCompatActivity;
35 import android.support.v7.widget.Toolbar;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.AdapterView;
39 import android.widget.ImageView;
40 import android.widget.ListView;
41 import android.widget.Spinner;
42 import android.widget.TextView;
44 import com.stoutner.privacybrowser.R;
45 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
47 public class BookmarksDatabaseViewActivity extends AppCompatActivity {
48 // `bookmarksDatabaseHelper` is used in `onCreate()` and `updateBookmarksListView()`.
49 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
51 // `bookmarksListView` is used in `onCreate()` and `updateBookmarksListView()`.
52 private ListView bookmarksListView;
54 // `bookmarksCursorAdapter` is used in `onCreate()` and `updateBookmarksListView()`.
55 private CursorAdapter bookmarksCursorAdapter;
58 public void onCreate(Bundle savedInstanceState) {
59 // Set the activity theme.
60 if (MainWebViewActivity.darkTheme) {
61 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
63 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
66 super.onCreate(savedInstanceState);
67 setContentView(R.layout.bookmarks_databaseview_coordinatorlayout);
69 // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
70 final Toolbar bookmarksDatabaseViewAppBar = (Toolbar) findViewById(R.id.bookmarks_database_view_toolbar);
71 setSupportActionBar(bookmarksDatabaseViewAppBar);
73 // Get a handle for the `AppBar`.
74 final ActionBar appBar = getSupportActionBar();
76 // Remove the incorrect warning in Android Studio that `appBar` might be null.
77 assert appBar != null;
79 // Display the `Spinner` and the back arrow in the `AppBar`.
80 appBar.setCustomView(R.layout.bookmarks_databaseview_spinner);
81 appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
83 // 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`.
84 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
86 // Setup a `MatrixCursor` for "All Folders" and "Home Folder".
87 String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME};
88 MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
89 matrixCursor.addRow(new Object[]{-2, getString(R.string.all_folders)});
90 matrixCursor.addRow(new Object[]{-1, getString(R.string.home_folder)});
92 // Get a `Cursor` with the list of all the folders.
93 Cursor foldersCursor = bookmarksDatabaseHelper.getAllFoldersCursor();
95 // Combine `matrixCursor` and `foldersCursor`.
96 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
98 // Create a `ResourceCursorAdapter` for the spinner with `this` context. `0` specifies no flags.;
99 ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(this, R.layout.bookmarks_databaseview_spinner_item, foldersMergeCursor, 0) {
101 public void bindView(View view, Context context, Cursor cursor) {
102 // Get a handle for the spinner item `TextView`.
103 TextView spinnerItemTextView = (TextView) view.findViewById(R.id.spinner_item_textview);
105 // Set the `TextView` to display the folder name.
106 spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
110 // Set the `ResourceCursorAdapter` drop drown view resource.
111 foldersCursorAdapter.setDropDownViewResource(R.layout.bookmarks_databaseview_spinner_dropdown_item);
113 // Get a handle for the folder `Spinner`.
114 Spinner folderSpinner = (Spinner) findViewById(R.id.bookmarks_database_view_spinner);
116 // Set the adapter for the folder `Spinner`.
117 folderSpinner.setAdapter(foldersCursorAdapter);
119 // Handle clicks on the `Spinner` dropdown.
120 folderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
122 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
123 // Convert the database ID to an `int`.
124 int databaseId = (int) id;
126 // Instantiate the bookmarks `Cursor`.
127 Cursor bookmarksCursor;
129 // Populate the bookmarks `ListView` based on the `Spinner` selection.
130 switch (databaseId) {
131 // Display all the folders.
133 // Get a cursor with all the folders.
134 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
137 // Display the home folder.
139 // Get a cursor for the home folder.
140 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
143 // Display the selected folder.
145 // Get a handle for the selected view.
146 TextView selectedFolderTextView = (TextView) view.findViewById(R.id.spinner_item_textview);
148 // Extract the name of the selected folder.
149 String folderName = selectedFolderTextView.getText().toString();
151 // Get a cursor for the selected folder.
152 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(folderName);
155 // Update the `ListView`.
156 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
160 public void onNothingSelected(AdapterView<?> parent) {
165 // Get a handle for the bookmarks `ListView`.
166 bookmarksListView = (ListView) findViewById(R.id.bookmarks_database_view_listview);
168 // Display the bookmarks in the `ListView`.
169 updateBookmarksListView();
172 private void updateBookmarksListView() {
173 // Get a `Cursor` with the current contents of the bookmarks database.
174 final Cursor bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
176 // Setup `bookmarksCursorAdapter` with `this` context. The `false` disables autoRequery.
177 bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, 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 getLayoutInflater().inflate(R.layout.bookmarks_databaseview_item_linearlayout, parent, false);
185 public void bindView(View view, Context context, Cursor cursor) {
186 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
188 // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`.
189 int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID));
190 TextView bookmarkDatabaseIdTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_database_id);
191 bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId));
193 // Get the favorite icon byte array from the `Cursor`.
194 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
195 // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last.
196 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
197 // Display the bitmap in `bookmarkFavoriteIcon`.
198 ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmarks_database_view_favorite_icon);
199 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
201 // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`.
202 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
203 TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_bookmark_name);
204 bookmarkNameTextView.setText(bookmarkNameString);
205 // Make the font bold for folders.
207 // The first argument is `null` because we don't want to change the font.
208 bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
209 } else { // Reset the font to default.
210 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
213 // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`.
214 int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER));
215 TextView bookmarkDisplayOrderTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_display_order);
216 bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder));
218 // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`.
219 String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER));
220 ImageView parentFolderImageView = (ImageView) view.findViewById(R.id.bookmarks_database_view_parent_folder_icon);
221 TextView bookmarkParentFolderTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_parent_folder);
222 // Make the folder name gray if it is the home folder.
223 if (bookmarkParentFolder.isEmpty()) {
224 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_gray));
225 bookmarkParentFolderTextView.setText(R.string.home_folder);
226 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_500));
228 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
229 bookmarkParentFolderTextView.setText(bookmarkParentFolder);
231 // Set the text color according to the theme.
232 if (MainWebViewActivity.darkTheme) {
233 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
235 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
239 // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`.
240 String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
241 TextView bookmarkUrlTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_bookmark_url);
242 bookmarkUrlTextView.setText(bookmarkUrlString);
244 bookmarkUrlTextView.setVisibility(View.GONE);
246 bookmarkUrlTextView.setVisibility(View.VISIBLE);
251 // Update the ListView.
252 bookmarksListView.setAdapter(bookmarksCursorAdapter);