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()`, `updateBookmarksListView()`, and `onDestroy()`.
64 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
66 // `bookmarksCursor` is used in `onCreate()`, `updateBookmarksListView()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, and `onDestroy()`.
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 app bar.
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 matrix cursor 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 resource cursor adapter for the spinner.
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 text view.
135 TextView spinnerItemTextView = view.findViewById(R.id.spinner_item_textview);
137 // Set the text view to display the folder name.
138 spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
142 // Set the resource cursor adapter drop drown view resource.
143 foldersCursorAdapter.setDropDownViewResource(R.layout.bookmarks_databaseview_spinner_dropdown_item);
145 // Get a handle for the folder spinner and set the adapter.
146 Spinner folderSpinner = findViewById(R.id.bookmarks_databaseview_spinner);
147 folderSpinner.setAdapter(foldersCursorAdapter);
149 // Handle clicks on the spinner dropdown.
150 folderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
152 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
153 // Convert the database ID to an `int`.
154 int databaseId = (int) id;
156 // Store the current folder database ID.
157 currentFolderDatabaseId = databaseId;
159 // Populate the bookmarks list view based on the spinner selection.
160 switch (databaseId) {
161 // Get a cursor with all the folders.
162 case ALL_FOLDERS_DATABASE_ID:
163 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
166 // Get a cursor for the home folder.
167 case HOME_FOLDER_DATABASE_ID:
168 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
171 // Display the selected folder.
173 // Get a handle for the selected view.
174 TextView selectedFolderTextView = view.findViewById(R.id.spinner_item_textview);
176 // Extract the name of the selected folder.
177 String folderName = selectedFolderTextView.getText().toString();
179 // Get a cursor for the selected folder.
180 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(folderName);
182 // Store the current folder name.
183 currentFolderName = folderName;
186 // Update the list view.
187 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
191 public void onNothingSelected(AdapterView<?> parent) {
196 // Get a handle for the bookmarks `ListView`.
197 ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
199 // Get a `Cursor` with the current contents of the bookmarks database.
200 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
202 // Setup a `CursorAdapter` with `this` context. `false` disables autoRequery.
203 bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
205 public View newView(Context context, Cursor cursor, ViewGroup parent) {
206 // Inflate the individual item layout. `false` does not attach it to the root.
207 return getLayoutInflater().inflate(R.layout.bookmarks_databaseview_item_linearlayout, parent, false);
211 public void bindView(View view, Context context, Cursor cursor) {
212 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
214 // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`.
215 int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID));
216 TextView bookmarkDatabaseIdTextView = view.findViewById(R.id.bookmarks_databaseview_database_id);
217 bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId));
219 // Get the favorite icon byte array from the `Cursor`.
220 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
221 // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last.
222 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
223 // Display the bitmap in `bookmarkFavoriteIcon`.
224 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmarks_databaseview_favorite_icon);
225 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
227 // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`.
228 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
229 TextView bookmarkNameTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_name);
230 bookmarkNameTextView.setText(bookmarkNameString);
232 // Make the font bold for folders.
234 // The first argument is `null` prevent changing of the font.
235 bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
236 } else { // Reset the font to default.
237 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
240 // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`.
241 String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
242 TextView bookmarkUrlTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_url);
243 bookmarkUrlTextView.setText(bookmarkUrlString);
245 // Hide the URL if the bookmark is a folder.
247 bookmarkUrlTextView.setVisibility(View.GONE);
249 bookmarkUrlTextView.setVisibility(View.VISIBLE);
252 // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`.
253 int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER));
254 TextView bookmarkDisplayOrderTextView = view.findViewById(R.id.bookmarks_databaseview_display_order);
255 bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder));
257 // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`.
258 String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER));
259 ImageView parentFolderImageView = view.findViewById(R.id.bookmarks_databaseview_parent_folder_icon);
260 TextView bookmarkParentFolderTextView = view.findViewById(R.id.bookmarks_databaseview_parent_folder);
262 // Make the folder name gray if it is the home folder.
263 if (bookmarkParentFolder.isEmpty()) {
264 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_gray));
265 bookmarkParentFolderTextView.setText(R.string.home_folder);
266 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_500));
268 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
269 bookmarkParentFolderTextView.setText(bookmarkParentFolder);
271 // Set the text color according to the theme.
272 if (MainWebViewActivity.darkTheme) {
273 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
275 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
281 // Update the ListView.
282 bookmarksListView.setAdapter(bookmarksCursorAdapter);
284 // Set the current folder database ID.
285 currentFolderDatabaseId = ALL_FOLDERS_DATABASE_ID;
287 // Set a listener to edit a bookmark when it is tapped.
288 bookmarksListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
289 // Convert the database ID to an `int`.
290 int databaseId = (int) id;
292 // Show the edit bookmark or edit bookmark folder dialog.
293 if (bookmarksDatabaseHelper.isFolder(databaseId)) {
294 // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
295 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
297 // Show the edit bookmark folder dialog.
298 AppCompatDialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId);
299 editBookmarkFolderDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
301 // Show the edit bookmark dialog.
302 AppCompatDialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId);
303 editBookmarkDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
309 public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
310 // Get handles for the views from dialog fragment.
311 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
312 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
313 EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
314 Spinner folderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_folder_spinner);
315 EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_display_order_edittext);
317 // Extract the bookmark information.
318 String bookmarkNameString = editBookmarkNameEditText.getText().toString();
319 String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
320 int folderDatabaseId = (int) folderSpinner.getSelectedItemId();
321 int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
323 // Instantiate the parent folder name `String`.
324 String parentFolderNameString;
326 // Set the parent folder name.
327 if (folderDatabaseId == EditBookmarkDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
328 parentFolderNameString = "";
329 } else { // Get the parent folder name from the database.
330 parentFolderNameString = bookmarksDatabaseHelper.getFolderName(folderDatabaseId);
333 // Update the bookmark.
334 if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon.
335 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt);
336 } else { // Update the bookmark using the `WebView` favorite icon.
337 // Convert the favorite icon to a byte array. `0` is for lossless compression (the only option for a PNG).
338 ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
339 MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
340 byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
342 // Update the bookmark and the favorite icon.
343 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt, newFavoriteIconByteArray);
346 // Update `bookmarksCursor` with the contents of the current folder.
347 switch (currentFolderDatabaseId) {
348 case ALL_FOLDERS_DATABASE_ID:
349 // Get a cursor with all the bookmarks.
350 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
353 case HOME_FOLDER_DATABASE_ID:
354 // Get a cursor with all the bookmarks in the home folder.
355 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
359 // Get a cursor with all the bookmarks in the current folder.
360 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(currentFolderName);
363 // Update the `ListView`.
364 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
368 public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
369 // Get handles for the views from dialog fragment.
370 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
371 RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
372 ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
373 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
374 Spinner parentFolderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_folder_parent_folder_spinner);
375 EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_display_order_edittext);
377 // Extract the folder information.
378 String newFolderNameString = editBookmarkNameEditText.getText().toString();
379 int parentFolderDatabaseId = (int) parentFolderSpinner.getSelectedItemId();
380 int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
382 // Instantiate the parent folder name `String`.
383 String parentFolderNameString;
385 // Set the parent folder name.
386 if (parentFolderDatabaseId == EditBookmarkFolderDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
387 parentFolderNameString = "";
388 } else { // Get the parent folder name from the database.
389 parentFolderNameString = bookmarksDatabaseHelper.getFolderName(parentFolderDatabaseId);
392 // Update the folder.
393 if (currentBookmarkIconRadioButton.isChecked()) { // Update the folder without changing the favorite icon.
394 bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt);
395 } else { // Update the folder and the icon.
396 // Instantiate the new folder icon `Bitmap`.
397 Bitmap folderIconBitmap;
399 // Populate the new folder icon bitmap.
400 if (defaultFolderIconRadioButton.isChecked()) {
401 // Get the default folder icon and convert it to a `Bitmap`.
402 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
403 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
404 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
405 } else { // Use the `WebView` favorite icon.
406 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
409 // Convert the folder icon to a byte array. `0` is for lossless compression (the only option for a PNG).
410 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
411 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
412 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
414 // Update the folder and the icon.
415 bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt, newFolderIconByteArray);
418 // Update `bookmarksCursor` with the contents of the current folder.
419 switch (currentFolderDatabaseId) {
420 case ALL_FOLDERS_DATABASE_ID:
421 // Get a cursor with all the bookmarks.
422 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
425 case HOME_FOLDER_DATABASE_ID:
426 // Get a cursor with all the bookmarks in the home folder.
427 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
431 // Get a cursor with all the bookmarks in the current folder.
432 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(currentFolderName);
435 // Update the `ListView`.
436 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
440 public void onDestroy() {
441 // Close the bookmarks cursor and database.
442 bookmarksCursor.close();
443 bookmarksDatabaseHelper.close();
445 // Run the default commands.