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 super.onCreate(savedInstanceState);
52 setContentView(R.layout.bookmarks_database_view_coordinatorlayout);
54 // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
55 final Toolbar bookmarksDatabaseViewAppBar = (Toolbar) findViewById(R.id.bookmarks_database_view_toolbar);
56 setSupportActionBar(bookmarksDatabaseViewAppBar);
58 // Display the home arrow on `SupportActionBar`.
59 final ActionBar appBar = getSupportActionBar();
60 assert appBar != null; // This assert removes the incorrect warning in Android Studio on the following line that appBar might be null.
61 appBar.setDisplayHomeAsUpEnabled(true);
63 // Initialize the database handler and the ListView.
64 // `this` specifies the context. The two `null`s do not specify the database name or a `CursorFactory`.
65 // The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`.
66 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
67 bookmarksListView = (ListView) findViewById(R.id.bookmarks_database_view_listview);
69 // Display the bookmarks in the ListView.
70 updateBookmarksListView();
74 private void updateBookmarksListView() {
75 // Get a `Cursor` with the current contents of the bookmarks database.
76 final Cursor bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
78 // Setup `bookmarksCursorAdapter` with `this` context. The `false` disables autoRequery.
79 CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
81 public View newView(Context context, Cursor cursor, ViewGroup parent) {
82 // Inflate the individual item layout. `false` does not attach it to the root.
83 return getLayoutInflater().inflate(R.layout.bookmarks_database_view_item_linearlayout, parent, false);
87 public void bindView(View view, Context context, Cursor cursor) {
88 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
90 // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`.
91 int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID));
92 TextView bookmarkDatabaseIdTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_database_id);
93 bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId));
95 // Get the favorite icon byte array from the `Cursor`.
96 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
97 // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last.
98 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
99 // Display the bitmap in `bookmarkFavoriteIcon`.
100 ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmarks_database_view_favorite_icon);
101 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
103 // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`.
104 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
105 TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_bookmark_name);
106 bookmarkNameTextView.setText(bookmarkNameString);
107 // Make the font bold for folders.
109 // The first argument is `null` because we don't want to change the font.
110 bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
111 } else { // Reset the font to default.
112 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
115 // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`.
116 int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER));
117 TextView bookmarkDisplayOrderTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_display_order);
118 bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder));
120 // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`.
121 String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER));
122 ImageView parentFolderImageView = (ImageView) view.findViewById(R.id.bookmarks_database_view_parent_folder_icon);
123 TextView bookmarkParentFolderTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_parent_folder);
124 // Make the folder name gray if it is the home folder.
125 if (bookmarkParentFolder.isEmpty()) {
126 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_gray));
127 bookmarkParentFolderTextView.setText(R.string.home_folder);
128 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_500));
130 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
131 bookmarkParentFolderTextView.setText(bookmarkParentFolder);
132 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
135 // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`.
136 String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
137 TextView bookmarkUrlTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_bookmark_url);
138 bookmarkUrlTextView.setText(bookmarkUrlString);
140 bookmarkUrlTextView.setVisibility(View.GONE);
142 bookmarkUrlTextView.setVisibility(View.VISIBLE);
147 // Update the ListView.
148 bookmarksListView.setAdapter(bookmarksCursorAdapter);