]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java
1725cac46756d2c2c670061e7300a8ffe79633f0
[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.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;
43
44 import com.stoutner.privacybrowser.R;
45 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
46
47 public class BookmarksDatabaseViewActivity extends AppCompatActivity {
48     // `bookmarksDatabaseHelper` is used in `onCreate()` and `updateBookmarksListView()`.
49     private BookmarksDatabaseHelper bookmarksDatabaseHelper;
50
51     // `bookmarksListView` is used in `onCreate()` and `updateBookmarksListView()`.
52     private ListView bookmarksListView;
53
54     // `bookmarksCursorAdapter` is used in `onCreate()` and `updateBookmarksListView()`.
55     private CursorAdapter bookmarksCursorAdapter;
56
57     @Override
58     public void onCreate(Bundle savedInstanceState) {
59         // Set the activity theme.
60         if (MainWebViewActivity.darkTheme) {
61             setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
62         } else {
63             setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
64         }
65
66         super.onCreate(savedInstanceState);
67         setContentView(R.layout.bookmarks_databaseview_coordinatorlayout);
68
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);
72
73         // Get a handle for the `AppBar`.
74         final ActionBar appBar = getSupportActionBar();
75
76         // Remove the incorrect warning in Android Studio that `appBar` might be null.
77         assert appBar != null;
78
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);
82
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);
85
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)});
91
92         // Get a `Cursor` with the list of all the folders.
93         Cursor foldersCursor = bookmarksDatabaseHelper.getAllFoldersCursor();
94
95         // Combine `matrixCursor` and `foldersCursor`.
96         MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
97
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) {
100             @Override
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);
104
105                 // Set the `TextView` to display the folder name.
106                 spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
107             }
108         };
109
110         // Set the `ResourceCursorAdapter` drop drown view resource.
111         foldersCursorAdapter.setDropDownViewResource(R.layout.bookmarks_databaseview_spinner_dropdown_item);
112
113         // Get a handle for the folder `Spinner`.
114         Spinner folderSpinner = (Spinner) findViewById(R.id.bookmarks_database_view_spinner);
115
116         // Set the adapter for the folder `Spinner`.
117         folderSpinner.setAdapter(foldersCursorAdapter);
118
119         // Handle clicks on the `Spinner` dropdown.
120         folderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
121             @Override
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;
125
126                 // Instantiate the bookmarks `Cursor`.
127                 Cursor bookmarksCursor;
128
129                 // Populate the bookmarks `ListView` based on the `Spinner` selection.
130                 switch (databaseId) {
131                     // Display all the folders.
132                     case -2:
133                         // Get a cursor with all the folders.
134                         bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
135                         break;
136
137                     // Display the home folder.
138                     case -1:
139                         // Get a cursor for the home folder.
140                         bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
141                         break;
142
143                     // Display the selected folder.
144                     default:
145                         // Get a handle for the selected view.
146                         TextView selectedFolderTextView = (TextView) view.findViewById(R.id.spinner_item_textview);
147
148                         // Extract the name of the selected folder.
149                         String folderName = selectedFolderTextView.getText().toString();
150
151                         // Get a cursor for the selected folder.
152                         bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(folderName);
153                 }
154
155                 // Update the `ListView`.
156                 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
157             }
158
159             @Override
160             public void onNothingSelected(AdapterView<?> parent) {
161                 // Do nothing.
162             }
163         });
164
165         // Get a handle for the bookmarks `ListView`.
166         bookmarksListView = (ListView) findViewById(R.id.bookmarks_database_view_listview);
167
168         // Display the bookmarks in the `ListView`.
169         updateBookmarksListView();
170     }
171
172     private void updateBookmarksListView() {
173         // Get a `Cursor` with the current contents of the bookmarks database.
174         final Cursor bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
175
176         // Setup `bookmarksCursorAdapter` with `this` context.  The `false` disables autoRequery.
177         bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
178             @Override
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);
182             }
183
184             @Override
185             public void bindView(View view, Context context, Cursor cursor) {
186                 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
187
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));
192
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);
200
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.
206                 if (isFolder) {
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);
211                 }
212
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));
217
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));
227                 } else {
228                     parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
229                     bookmarkParentFolderTextView.setText(bookmarkParentFolder);
230
231                     // Set the text color according to the theme.
232                     if (MainWebViewActivity.darkTheme) {
233                         bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
234                     } else {
235                         bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
236                     }
237                 }
238
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);
243                 if (isFolder) {
244                     bookmarkUrlTextView.setVisibility(View.GONE);
245                 } else {
246                     bookmarkUrlTextView.setVisibility(View.VISIBLE);
247                 }
248             }
249         };
250
251         // Update the ListView.
252         bookmarksListView.setAdapter(bookmarksCursorAdapter);
253     }
254 }