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.graphics.drawable.BitmapDrawable;
30 import android.graphics.drawable.Drawable;
31 import android.os.Bundle;
32 import android.support.v4.content.ContextCompat;
33 import android.support.v4.widget.CursorAdapter;
34 import android.support.v4.widget.ResourceCursorAdapter;
35 import android.support.v7.app.ActionBar;
36 import android.support.v7.app.AppCompatActivity;
37 // `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22.
38 import android.support.v7.app.AppCompatDialogFragment;
39 import android.support.v7.widget.Toolbar;
40 import android.view.View;
41 import android.view.ViewGroup;
42 import android.widget.AdapterView;
43 import android.widget.EditText;
44 import android.widget.ImageView;
45 import android.widget.ListView;
46 import android.widget.RadioButton;
47 import android.widget.Spinner;
48 import android.widget.TextView;
50 import com.stoutner.privacybrowser.R;
51 import com.stoutner.privacybrowser.dialogs.EditBookmarkDatabaseViewDialog;
52 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDatabaseViewDialog;
53 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
55 import java.io.ByteArrayOutputStream;
57 public class BookmarksDatabaseViewActivity extends AppCompatActivity implements EditBookmarkDatabaseViewDialog.EditBookmarkDatabaseViewListener, EditBookmarkFolderDatabaseViewDialog.EditBookmarkFolderDatabaseViewListener {
58 // Instantiate the constants.
59 private static final int ALL_FOLDERS_DATABASE_ID = -2;
60 private static final int HOME_FOLDER_DATABASE_ID = -1;
62 // `bookmarksDatabaseHelper` is used in `onCreate()` and `updateBookmarksListView()`.
63 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
65 // `bookmarksCursor` is used in `onCreate()`, `updateBookmarksListView()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
66 private Cursor bookmarksCursor;
68 // `bookmarksCursorAdapter` is used in `onCreate()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`.
69 private CursorAdapter bookmarksCursorAdapter;
71 // `oldFolderNameString` is used in `onCreate()` and `onSaveBookmarkFolder()`.
72 private String oldFolderNameString;
74 // `currentFolderDatabaseId` is used in `onCreate()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
75 private int currentFolderDatabaseId;
77 // `currentFolder` is used in `onCreate()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
78 private String currentFolderName;
81 public void onCreate(Bundle savedInstanceState) {
82 // Set the activity theme.
83 if (MainWebViewActivity.darkTheme) {
84 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
86 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
89 // Run the default commands.
90 super.onCreate(savedInstanceState);
92 // Set the content view.
93 setContentView(R.layout.bookmarks_databaseview_coordinatorlayout);
95 // The `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21.
96 final Toolbar bookmarksDatabaseViewAppBar = findViewById(R.id.bookmarks_databaseview_toolbar);
97 setSupportActionBar(bookmarksDatabaseViewAppBar);
99 // Get a handle for the `AppBar`.
100 final ActionBar appBar = getSupportActionBar();
102 // Remove the incorrect warning in Android Studio that `appBar` might be null.
103 assert appBar != null;
105 // Display the `Spinner` and the back arrow in the `AppBar`.
106 appBar.setCustomView(R.layout.bookmarks_databaseview_spinner);
107 appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
109 // 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`.
110 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
112 // Setup a `MatrixCursor` for "All Folders" and "Home Folder".
113 String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME};
114 MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
115 matrixCursor.addRow(new Object[]{ALL_FOLDERS_DATABASE_ID, getString(R.string.all_folders)});
116 matrixCursor.addRow(new Object[]{HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)});
118 // Get a `Cursor` with the list of all the folders.
119 Cursor foldersCursor = bookmarksDatabaseHelper.getAllFoldersCursor();
121 // Combine `matrixCursor` and `foldersCursor`.
122 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
124 // Create a `ResourceCursorAdapter` for the `Spinner` with `this` context. `0` specifies no flags.;
125 ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(this, R.layout.bookmarks_databaseview_spinner_item, foldersMergeCursor, 0) {
127 public void bindView(View view, Context context, Cursor cursor) {
128 // Get a handle for the `Spinner` item `TextView`.
129 TextView spinnerItemTextView = view.findViewById(R.id.spinner_item_textview);
131 // Set the `TextView` to display the folder name.
132 spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
136 // Set the `ResourceCursorAdapter` drop drown view resource.
137 foldersCursorAdapter.setDropDownViewResource(R.layout.bookmarks_databaseview_spinner_dropdown_item);
139 // Get a handle for the folder `Spinner`.
140 Spinner folderSpinner = findViewById(R.id.bookmarks_databaseview_spinner);
142 // Set the adapter for the folder `Spinner`.
143 folderSpinner.setAdapter(foldersCursorAdapter);
145 // Handle clicks on the `Spinner` dropdown.
146 folderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
148 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
149 // Convert the database ID to an `int`.
150 int databaseId = (int) id;
152 // Store the current folder database ID.
153 currentFolderDatabaseId = databaseId;
155 // Populate the bookmarks `ListView` based on the `Spinner` selection.
156 switch (databaseId) {
157 // Get a cursor with all the folders.
158 case ALL_FOLDERS_DATABASE_ID:
159 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
162 // Get a cursor for the home folder.
163 case HOME_FOLDER_DATABASE_ID:
164 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
167 // Display the selected folder.
169 // Get a handle for the selected view.
170 TextView selectedFolderTextView = view.findViewById(R.id.spinner_item_textview);
172 // Extract the name of the selected folder.
173 String folderName = selectedFolderTextView.getText().toString();
175 // Get a cursor for the selected folder.
176 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(folderName);
178 // Store the current folder name.
179 currentFolderName = folderName;
182 // Update the `ListView`.
183 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
187 public void onNothingSelected(AdapterView<?> parent) {
192 // Get a handle for the bookmarks `ListView`.
193 ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
195 // Get a `Cursor` with the current contents of the bookmarks database.
196 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
198 // Setup a `CursorAdapter` with `this` context. `false` disables autoRequery.
199 bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
201 public View newView(Context context, Cursor cursor, ViewGroup parent) {
202 // Inflate the individual item layout. `false` does not attach it to the root.
203 return getLayoutInflater().inflate(R.layout.bookmarks_databaseview_item_linearlayout, parent, false);
207 public void bindView(View view, Context context, Cursor cursor) {
208 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
210 // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`.
211 int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID));
212 TextView bookmarkDatabaseIdTextView = view.findViewById(R.id.bookmarks_databaseview_database_id);
213 bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId));
215 // Get the favorite icon byte array from the `Cursor`.
216 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
217 // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last.
218 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
219 // Display the bitmap in `bookmarkFavoriteIcon`.
220 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmarks_databaseview_favorite_icon);
221 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
223 // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`.
224 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
225 TextView bookmarkNameTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_name);
226 bookmarkNameTextView.setText(bookmarkNameString);
228 // Make the font bold for folders.
230 // The first argument is `null` prevent changing of the font.
231 bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
232 } else { // Reset the font to default.
233 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
236 // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`.
237 String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
238 TextView bookmarkUrlTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_url);
239 bookmarkUrlTextView.setText(bookmarkUrlString);
241 // Hide the URL if the bookmark is a folder.
243 bookmarkUrlTextView.setVisibility(View.GONE);
245 bookmarkUrlTextView.setVisibility(View.VISIBLE);
248 // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`.
249 int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER));
250 TextView bookmarkDisplayOrderTextView = view.findViewById(R.id.bookmarks_databaseview_display_order);
251 bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder));
253 // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`.
254 String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER));
255 ImageView parentFolderImageView = view.findViewById(R.id.bookmarks_databaseview_parent_folder_icon);
256 TextView bookmarkParentFolderTextView = view.findViewById(R.id.bookmarks_databaseview_parent_folder);
258 // Make the folder name gray if it is the home folder.
259 if (bookmarkParentFolder.isEmpty()) {
260 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_gray));
261 bookmarkParentFolderTextView.setText(R.string.home_folder);
262 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_500));
264 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
265 bookmarkParentFolderTextView.setText(bookmarkParentFolder);
267 // Set the text color according to the theme.
268 if (MainWebViewActivity.darkTheme) {
269 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
271 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
277 // Update the ListView.
278 bookmarksListView.setAdapter(bookmarksCursorAdapter);
280 // Set the current folder database ID.
281 currentFolderDatabaseId = ALL_FOLDERS_DATABASE_ID;
283 // Set a listener to edit a bookmark when it is tapped.
284 bookmarksListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
285 // Convert the database ID to an `int`.
286 int databaseId = (int) id;
288 // Show the edit bookmark or edit bookmark folder dialog.
289 if (bookmarksDatabaseHelper.isFolder(databaseId)) {
290 // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
291 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
293 // Show the edit bookmark folder dialog.
294 AppCompatDialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId);
295 editBookmarkFolderDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
297 // Show the edit bookmark dialog.
298 AppCompatDialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId);
299 editBookmarkDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
305 public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
306 // Get handles for the views from dialog fragment.
307 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
308 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
309 EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
310 Spinner folderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_folder_spinner);
311 EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_display_order_edittext);
313 // Extract the bookmark information.
314 String bookmarkNameString = editBookmarkNameEditText.getText().toString();
315 String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
316 int folderDatabaseId = (int) folderSpinner.getSelectedItemId();
317 int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
319 // Instantiate the parent folder name `String`.
320 String parentFolderNameString;
322 // Set the parent folder name.
323 if (folderDatabaseId == EditBookmarkDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
324 parentFolderNameString = "";
325 } else { // Get the parent folder name from the database.
326 parentFolderNameString = bookmarksDatabaseHelper.getFolderName(folderDatabaseId);
329 // Update the bookmark.
330 if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon.
331 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt);
332 } else { // Update the bookmark using the `WebView` favorite icon.
333 // Convert the favorite icon to a byte array. `0` is for lossless compression (the only option for a PNG).
334 ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
335 MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
336 byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
338 // Update the bookmark and the favorite icon.
339 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt, newFavoriteIconByteArray);
342 // Update `bookmarksCursor` with the contents of the current folder.
343 switch (currentFolderDatabaseId) {
344 case ALL_FOLDERS_DATABASE_ID:
345 // Get a cursor with all the bookmarks.
346 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
349 case HOME_FOLDER_DATABASE_ID:
350 // Get a cursor with all the bookmarks in the home folder.
351 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
355 // Get a cursor with all the bookmarks in the current folder.
356 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(currentFolderName);
359 // Update the `ListView`.
360 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
364 public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
365 // Get handles for the views from dialog fragment.
366 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
367 RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
368 ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
369 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
370 Spinner parentFolderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_folder_parent_folder_spinner);
371 EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_display_order_edittext);
373 // Extract the folder information.
374 String newFolderNameString = editBookmarkNameEditText.getText().toString();
375 int parentFolderDatabaseId = (int) parentFolderSpinner.getSelectedItemId();
376 int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
378 // Instantiate the parent folder name `String`.
379 String parentFolderNameString;
381 // Set the parent folder name.
382 if (parentFolderDatabaseId == EditBookmarkFolderDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
383 parentFolderNameString = "";
384 } else { // Get the parent folder name from the database.
385 parentFolderNameString = bookmarksDatabaseHelper.getFolderName(parentFolderDatabaseId);
388 // Update the folder.
389 if (currentBookmarkIconRadioButton.isChecked()) { // Update the folder without changing the favorite icon.
390 bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt);
391 } else { // Update the folder and the icon.
392 // Instantiate the new folder icon `Bitmap`.
393 Bitmap folderIconBitmap;
395 // Populate the new folder icon bitmap.
396 if (defaultFolderIconRadioButton.isChecked()) {
397 // Get the default folder icon and convert it to a `Bitmap`.
398 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
399 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
400 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
401 } else { // Use the `WebView` favorite icon.
402 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
405 // Convert the folder icon to a byte array. `0` is for lossless compression (the only option for a PNG).
406 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
407 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
408 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
410 // Update the folder and the icon.
411 bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt, newFolderIconByteArray);
414 // Update `bookmarksCursor` with the contents of the current folder.
415 switch (currentFolderDatabaseId) {
416 case ALL_FOLDERS_DATABASE_ID:
417 // Get a cursor with all the bookmarks.
418 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
421 case HOME_FOLDER_DATABASE_ID:
422 // Get a cursor with all the bookmarks in the home folder.
423 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
427 // Get a cursor with all the bookmarks in the current folder.
428 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(currentFolderName);
431 // Update the `ListView`.
432 bookmarksCursorAdapter.changeCursor(bookmarksCursor);