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.graphics.Bitmap;
25 import android.graphics.BitmapFactory;
26 import android.graphics.Typeface;
27 import android.os.Bundle;
28 import android.support.v4.content.ContextCompat;
29 import android.support.v4.widget.CursorAdapter;
30 import android.support.v7.app.ActionBar;
31 import android.support.v7.app.AppCompatActivity;
32 import android.support.v7.widget.Toolbar;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.ImageView;
36 import android.widget.ListView;
37 import android.widget.TextView;
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
42 public class BookmarksDatabaseViewActivity extends AppCompatActivity {
43 // `bookmarksDatabaseHelper` is used in `onCreate()` and `updateBookmarksListView()`.
44 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
46 // `bookmarksListView` is used in `onCreate()` and `updateBookmarksListView()`.
47 private ListView bookmarksListView;
50 public void onCreate(Bundle savedInstanceState) {
51 // Set the activity theme.
52 if (MainWebViewActivity.darkTheme) {
53 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
55 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
58 super.onCreate(savedInstanceState);
59 setContentView(R.layout.bookmarks_database_view_coordinatorlayout);
61 // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
62 final Toolbar bookmarksDatabaseViewAppBar = (Toolbar) findViewById(R.id.bookmarks_database_view_toolbar);
63 setSupportActionBar(bookmarksDatabaseViewAppBar);
65 // Display the home arrow on `SupportActionBar`.
66 final ActionBar appBar = getSupportActionBar();
67 assert appBar != null; // This assert removes the incorrect warning in Android Studio on the following line that appBar might be null.
68 appBar.setDisplayHomeAsUpEnabled(true);
70 // Initialize the database handler and the ListView.
71 // `this` specifies the context. The two `null`s do not specify the database name or a `CursorFactory`.
72 // The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`.
73 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
74 bookmarksListView = (ListView) findViewById(R.id.bookmarks_database_view_listview);
76 // Display the bookmarks in the ListView.
77 updateBookmarksListView();
81 private void updateBookmarksListView() {
82 // Get a `Cursor` with the current contents of the bookmarks database.
83 final Cursor bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
85 // Setup `bookmarksCursorAdapter` with `this` context. The `false` disables autoRequery.
86 CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
88 public View newView(Context context, Cursor cursor, ViewGroup parent) {
89 // Inflate the individual item layout. `false` does not attach it to the root.
90 return getLayoutInflater().inflate(R.layout.bookmarks_database_view_item_linearlayout, parent, false);
94 public void bindView(View view, Context context, Cursor cursor) {
95 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
97 // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`.
98 int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID));
99 TextView bookmarkDatabaseIdTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_database_id);
100 bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId));
102 // Get the favorite icon byte array from the `Cursor`.
103 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
104 // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last.
105 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
106 // Display the bitmap in `bookmarkFavoriteIcon`.
107 ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmarks_database_view_favorite_icon);
108 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
110 // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`.
111 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
112 TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_bookmark_name);
113 bookmarkNameTextView.setText(bookmarkNameString);
114 // Make the font bold for folders.
116 // The first argument is `null` because we don't want to change the font.
117 bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
118 } else { // Reset the font to default.
119 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
122 // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`.
123 int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER));
124 TextView bookmarkDisplayOrderTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_display_order);
125 bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder));
127 // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`.
128 String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER));
129 ImageView parentFolderImageView = (ImageView) view.findViewById(R.id.bookmarks_database_view_parent_folder_icon);
130 TextView bookmarkParentFolderTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_parent_folder);
131 // Make the folder name gray if it is the home folder.
132 if (bookmarkParentFolder.isEmpty()) {
133 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_gray));
134 bookmarkParentFolderTextView.setText(R.string.home_folder);
135 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_500));
137 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
138 bookmarkParentFolderTextView.setText(bookmarkParentFolder);
140 // Set the text color according to the theme.
141 if (MainWebViewActivity.darkTheme) {
142 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
144 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
148 // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`.
149 String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
150 TextView bookmarkUrlTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_bookmark_url);
151 bookmarkUrlTextView.setText(bookmarkUrlString);
153 bookmarkUrlTextView.setVisibility(View.GONE);
155 bookmarkUrlTextView.setVisibility(View.VISIBLE);
160 // Update the ListView.
161 bookmarksListView.setAdapter(bookmarksCursorAdapter);