2 * Copyright © 2016-2018 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.view.WindowManager;
43 import android.widget.AdapterView;
44 import android.widget.EditText;
45 import android.widget.ImageView;
46 import android.widget.ListView;
47 import android.widget.RadioButton;
48 import android.widget.Spinner;
49 import android.widget.TextView;
51 import com.stoutner.privacybrowser.R;
52 import com.stoutner.privacybrowser.dialogs.EditBookmarkDatabaseViewDialog;
53 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDatabaseViewDialog;
54 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
56 import java.io.ByteArrayOutputStream;
58 public class BookmarksDatabaseViewActivity extends AppCompatActivity implements EditBookmarkDatabaseViewDialog.EditBookmarkDatabaseViewListener, EditBookmarkFolderDatabaseViewDialog.EditBookmarkFolderDatabaseViewListener {
59 // Instantiate the constants.
60 private static final int ALL_FOLDERS_DATABASE_ID = -2;
61 private static final int HOME_FOLDER_DATABASE_ID = -1;
63 // `bookmarksDatabaseHelper` is used in `onCreate()` and `updateBookmarksListView()`.
64 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
66 // `bookmarksCursor` is used in `onCreate()`, `updateBookmarksListView()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
67 private Cursor bookmarksCursor;
69 // `bookmarksCursorAdapter` is used in `onCreate()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`.
70 private CursorAdapter bookmarksCursorAdapter;
72 // `oldFolderNameString` is used in `onCreate()` and `onSaveBookmarkFolder()`.
73 private String oldFolderNameString;
75 // `currentFolderDatabaseId` is used in `onCreate()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
76 private int currentFolderDatabaseId;
78 // `currentFolder` is used in `onCreate()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
79 private String currentFolderName;
82 public void onCreate(Bundle savedInstanceState) {
83 // Disable screenshots if not allowed.
84 if (!MainWebViewActivity.allowScreenshots) {
85 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
88 // Set the activity theme.
89 if (MainWebViewActivity.darkTheme) {
90 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
92 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
95 // Run the default commands.
96 super.onCreate(savedInstanceState);
98 // Set the content view.
99 setContentView(R.layout.bookmarks_databaseview_coordinatorlayout);
101 // The `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21.
102 final Toolbar bookmarksDatabaseViewAppBar = findViewById(R.id.bookmarks_databaseview_toolbar);
103 setSupportActionBar(bookmarksDatabaseViewAppBar);
105 // Get a handle for the `AppBar`.
106 final ActionBar appBar = getSupportActionBar();
108 // Remove the incorrect warning in Android Studio that `appBar` might be null.
109 assert appBar != null;
111 // Display the `Spinner` and the back arrow in the `AppBar`.
112 appBar.setCustomView(R.layout.bookmarks_databaseview_spinner);
113 appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
115 // 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`.
116 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
118 // Setup a `MatrixCursor` for "All Folders" and "Home Folder".
119 String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME};
120 MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
121 matrixCursor.addRow(new Object[]{ALL_FOLDERS_DATABASE_ID, getString(R.string.all_folders)});
122 matrixCursor.addRow(new Object[]{HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)});
124 // Get a `Cursor` with the list of all the folders.
125 Cursor foldersCursor = bookmarksDatabaseHelper.getAllFoldersCursor();
127 // Combine `matrixCursor` and `foldersCursor`.
128 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
130 // Create a `ResourceCursorAdapter` for the `Spinner` with `this` context. `0` specifies no flags.;
131 ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(this, R.layout.bookmarks_databaseview_spinner_item, foldersMergeCursor, 0) {
133 public void bindView(View view, Context context, Cursor cursor) {
134 // Get a handle for the `Spinner` item `TextView`.
135 TextView spinnerItemTextView = view.findViewById(R.id.spinner_item_textview);
137 // Set the `TextView` to display the folder name.
138 spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
142 // Set the `ResourceCursorAdapter` drop drown view resource.
143 foldersCursorAdapter.setDropDownViewResource(R.layout.bookmarks_databaseview_spinner_dropdown_item);
145 // Get a handle for the folder `Spinner`.
146 Spinner folderSpinner = findViewById(R.id.bookmarks_databaseview_spinner);
148 // Set the adapter for the folder `Spinner`.
149 folderSpinner.setAdapter(foldersCursorAdapter);
151 // Handle clicks on the `Spinner` dropdown.
152 folderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
154 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
155 // Convert the database ID to an `int`.
156 int databaseId = (int) id;
158 // Store the current folder database ID.
159 currentFolderDatabaseId = databaseId;
161 // Populate the bookmarks `ListView` based on the `Spinner` selection.
162 switch (databaseId) {
163 // Get a cursor with all the folders.
164 case ALL_FOLDERS_DATABASE_ID:
165 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
168 // Get a cursor for the home folder.
169 case HOME_FOLDER_DATABASE_ID:
170 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
173 // Display the selected folder.
175 // Get a handle for the selected view.
176 TextView selectedFolderTextView = view.findViewById(R.id.spinner_item_textview);
178 // Extract the name of the selected folder.
179 String folderName = selectedFolderTextView.getText().toString();
181 // Get a cursor for the selected folder.
182 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(folderName);
184 // Store the current folder name.
185 currentFolderName = folderName;
188 // Update the `ListView`.
189 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
193 public void onNothingSelected(AdapterView<?> parent) {
198 // Get a handle for the bookmarks `ListView`.
199 ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
201 // Get a `Cursor` with the current contents of the bookmarks database.
202 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
204 // Setup a `CursorAdapter` with `this` context. `false` disables autoRequery.
205 bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
207 public View newView(Context context, Cursor cursor, ViewGroup parent) {
208 // Inflate the individual item layout. `false` does not attach it to the root.
209 return getLayoutInflater().inflate(R.layout.bookmarks_databaseview_item_linearlayout, parent, false);
213 public void bindView(View view, Context context, Cursor cursor) {
214 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
216 // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`.
217 int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID));
218 TextView bookmarkDatabaseIdTextView = view.findViewById(R.id.bookmarks_databaseview_database_id);
219 bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId));
221 // Get the favorite icon byte array from the `Cursor`.
222 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
223 // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last.
224 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
225 // Display the bitmap in `bookmarkFavoriteIcon`.
226 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmarks_databaseview_favorite_icon);
227 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
229 // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`.
230 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
231 TextView bookmarkNameTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_name);
232 bookmarkNameTextView.setText(bookmarkNameString);
234 // Make the font bold for folders.
236 // The first argument is `null` prevent changing of the font.
237 bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
238 } else { // Reset the font to default.
239 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
242 // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`.
243 String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
244 TextView bookmarkUrlTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_url);
245 bookmarkUrlTextView.setText(bookmarkUrlString);
247 // Hide the URL if the bookmark is a folder.
249 bookmarkUrlTextView.setVisibility(View.GONE);
251 bookmarkUrlTextView.setVisibility(View.VISIBLE);
254 // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`.
255 int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER));
256 TextView bookmarkDisplayOrderTextView = view.findViewById(R.id.bookmarks_databaseview_display_order);
257 bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder));
259 // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`.
260 String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER));
261 ImageView parentFolderImageView = view.findViewById(R.id.bookmarks_databaseview_parent_folder_icon);
262 TextView bookmarkParentFolderTextView = view.findViewById(R.id.bookmarks_databaseview_parent_folder);
264 // Make the folder name gray if it is the home folder.
265 if (bookmarkParentFolder.isEmpty()) {
266 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_gray));
267 bookmarkParentFolderTextView.setText(R.string.home_folder);
268 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_500));
270 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
271 bookmarkParentFolderTextView.setText(bookmarkParentFolder);
273 // Set the text color according to the theme.
274 if (MainWebViewActivity.darkTheme) {
275 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
277 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
283 // Update the ListView.
284 bookmarksListView.setAdapter(bookmarksCursorAdapter);
286 // Set the current folder database ID.
287 currentFolderDatabaseId = ALL_FOLDERS_DATABASE_ID;
289 // Set a listener to edit a bookmark when it is tapped.
290 bookmarksListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
291 // Convert the database ID to an `int`.
292 int databaseId = (int) id;
294 // Show the edit bookmark or edit bookmark folder dialog.
295 if (bookmarksDatabaseHelper.isFolder(databaseId)) {
296 // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
297 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
299 // Show the edit bookmark folder dialog.
300 AppCompatDialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId);
301 editBookmarkFolderDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
303 // Show the edit bookmark dialog.
304 AppCompatDialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId);
305 editBookmarkDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
311 public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
312 // Get handles for the views from dialog fragment.
313 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
314 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
315 EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
316 Spinner folderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_folder_spinner);
317 EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_display_order_edittext);
319 // Extract the bookmark information.
320 String bookmarkNameString = editBookmarkNameEditText.getText().toString();
321 String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
322 int folderDatabaseId = (int) folderSpinner.getSelectedItemId();
323 int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
325 // Instantiate the parent folder name `String`.
326 String parentFolderNameString;
328 // Set the parent folder name.
329 if (folderDatabaseId == EditBookmarkDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
330 parentFolderNameString = "";
331 } else { // Get the parent folder name from the database.
332 parentFolderNameString = bookmarksDatabaseHelper.getFolderName(folderDatabaseId);
335 // Update the bookmark.
336 if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon.
337 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt);
338 } else { // Update the bookmark using the `WebView` favorite icon.
339 // Convert the favorite icon to a byte array. `0` is for lossless compression (the only option for a PNG).
340 ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
341 MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
342 byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
344 // Update the bookmark and the favorite icon.
345 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt, newFavoriteIconByteArray);
348 // Update `bookmarksCursor` with the contents of the current folder.
349 switch (currentFolderDatabaseId) {
350 case ALL_FOLDERS_DATABASE_ID:
351 // Get a cursor with all the bookmarks.
352 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
355 case HOME_FOLDER_DATABASE_ID:
356 // Get a cursor with all the bookmarks in the home folder.
357 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
361 // Get a cursor with all the bookmarks in the current folder.
362 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(currentFolderName);
365 // Update the `ListView`.
366 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
370 public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
371 // Get handles for the views from dialog fragment.
372 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
373 RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
374 ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
375 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
376 Spinner parentFolderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_folder_parent_folder_spinner);
377 EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_display_order_edittext);
379 // Extract the folder information.
380 String newFolderNameString = editBookmarkNameEditText.getText().toString();
381 int parentFolderDatabaseId = (int) parentFolderSpinner.getSelectedItemId();
382 int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
384 // Instantiate the parent folder name `String`.
385 String parentFolderNameString;
387 // Set the parent folder name.
388 if (parentFolderDatabaseId == EditBookmarkFolderDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
389 parentFolderNameString = "";
390 } else { // Get the parent folder name from the database.
391 parentFolderNameString = bookmarksDatabaseHelper.getFolderName(parentFolderDatabaseId);
394 // Update the folder.
395 if (currentBookmarkIconRadioButton.isChecked()) { // Update the folder without changing the favorite icon.
396 bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt);
397 } else { // Update the folder and the icon.
398 // Instantiate the new folder icon `Bitmap`.
399 Bitmap folderIconBitmap;
401 // Populate the new folder icon bitmap.
402 if (defaultFolderIconRadioButton.isChecked()) {
403 // Get the default folder icon and convert it to a `Bitmap`.
404 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
405 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
406 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
407 } else { // Use the `WebView` favorite icon.
408 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
411 // Convert the folder icon to a byte array. `0` is for lossless compression (the only option for a PNG).
412 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
413 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
414 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
416 // Update the folder and the icon.
417 bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt, newFolderIconByteArray);
420 // Update `bookmarksCursor` with the contents of the current folder.
421 switch (currentFolderDatabaseId) {
422 case ALL_FOLDERS_DATABASE_ID:
423 // Get a cursor with all the bookmarks.
424 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
427 case HOME_FOLDER_DATABASE_ID:
428 // Get a cursor with all the bookmarks in the home folder.
429 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
433 // Get a cursor with all the bookmarks in the current folder.
434 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(currentFolderName);
437 // Update the `ListView`.
438 bookmarksCursorAdapter.changeCursor(bookmarksCursor);