]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java
Updates about_licenses, adding the full text of the Apache License 2.0 and the 3...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / BookmarksDatabaseViewActivity.java
1 /*
2  * Copyright © 2016-2017 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.activities;
21
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;
38
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
41
42 public class BookmarksDatabaseViewActivity extends AppCompatActivity {
43     // `bookmarksDatabaseHelper` is used in `onCreate()` and `updateBookmarksListView()`.
44     private BookmarksDatabaseHelper bookmarksDatabaseHelper;
45
46     // `bookmarksListView` is used in `onCreate()` and `updateBookmarksListView()`.
47     private ListView bookmarksListView;
48
49     @Override
50     public void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setContentView(R.layout.bookmarks_database_view_coordinatorlayout);
53
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);
57
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);
62
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);
68
69         // Display the bookmarks in the ListView.
70         updateBookmarksListView();
71
72     }
73
74     private void updateBookmarksListView() {
75         // Get a `Cursor` with the current contents of the bookmarks database.
76         final Cursor bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
77
78         // Setup `bookmarksCursorAdapter` with `this` context.  The `false` disables autoRequery.
79         CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
80             @Override
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);
84             }
85
86             @Override
87             public void bindView(View view, Context context, Cursor cursor) {
88                 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
89
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));
94
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);
102
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.
108                 if (isFolder) {
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);
113                 }
114
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));
119
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));
129                 } else {
130                     parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
131                     bookmarkParentFolderTextView.setText(bookmarkParentFolder);
132                     bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
133                 }
134
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);
139                 if (isFolder) {
140                     bookmarkUrlTextView.setVisibility(View.GONE);
141                 } else {
142                     bookmarkUrlTextView.setVisibility(View.VISIBLE);
143                 }
144             }
145         };
146
147         // Update the ListView.
148         bookmarksListView.setAdapter(bookmarksCursorAdapter);
149     }
150 }