From c4ad9f457f41cbc86391e0099629cd94a235258a Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Fri, 15 Jul 2016 13:44:38 -0700 Subject: [PATCH] Add the ability to create and edit bookmark folders. --- .idea/dictionaries/soren.xml | 2 + app/src/main/AndroidManifest.xml | 54 +-- app/src/main/assets/about_licenses.html | 42 ++- .../assets/images/ic_create_new_folder.png | Bin 0 -> 911 bytes app/src/main/assets/images/ic_folder.png | Bin 0 -> 811 bytes .../main/assets/images/ic_folder_special.png | Bin 0 -> 1572 bytes .../privacybrowser/BookmarksActivity.java | 355 ++++++++++++++---- .../BookmarksDatabaseHandler.java | 195 ++++++++-- .../BookmarksDatabaseViewActivity.java | 144 +++++++ .../privacybrowser/CreateBookmark.java | 52 +-- .../privacybrowser/CreateBookmarkFolder.java | 120 ++++++ .../CreateHomeScreenShortcut.java | 36 +- .../stoutner/privacybrowser/EditBookmark.java | 75 ++-- .../privacybrowser/EditBookmarkFolder.java | 143 +++++++ .../privacybrowser/MainWebViewActivity.java | 14 +- .../stoutner/privacybrowser/MoveToFolder.java | 25 ++ .../res/drawable-hdpi/folder_blue_bitmap.png | Bin 0 -> 613 bytes .../res/drawable-mdpi/folder_blue_bitmap.png | Bin 0 -> 480 bytes .../res/drawable-xhdpi/folder_blue_bitmap.png | Bin 0 -> 895 bytes .../drawable-xxhdpi/folder_blue_bitmap.png | Bin 0 -> 1168 bytes app/src/main/res/drawable/back.xml | 4 +- app/src/main/res/drawable/create_folder.xml | 15 + app/src/main/res/drawable/edit.xml | 4 +- app/src/main/res/drawable/exit.xml | 4 +- app/src/main/res/drawable/folder.xml | 14 + app/src/main/res/drawable/forward.xml | 4 +- .../drawable/move_bookmark_down_disabled.xml | 2 +- .../drawable/move_bookmark_down_enabled.xml | 2 +- .../drawable/move_bookmark_up_disabled.xml | 2 +- .../res/drawable/move_bookmark_up_enabled.xml | 2 +- app/src/main/res/drawable/move_to_folder.xml | 15 + .../layout/bookmarks_coordinatorlayout.xml | 14 +- ...kmarks_database_view_coordinatorlayout.xml | 58 +++ .../bookmarks_database_view_linearlayout.xml | 110 ++++++ .../layout/bookmarks_item_linearlayout.xml | 4 +- .../res/layout/create_bookmark_dialog.xml | 8 +- .../layout/create_bookmark_folder_dialog.xml | 113 ++++++ .../create_home_screen_shortcut_dialog.xml | 38 +- .../main/res/layout/edit_bookmark_dialog.xml | 84 +++-- .../layout/edit_bookmark_folder_dialog.xml | 130 +++++++ .../main/res/menu/bookmarks_context_menu.xml | 13 +- .../main/res/menu/bookmarks_options_menu.xml | 15 +- app/src/main/res/values/colors.xml | 4 +- app/src/main/res/values/strings.xml | 20 +- 44 files changed, 1622 insertions(+), 314 deletions(-) create mode 100644 app/src/main/assets/images/ic_create_new_folder.png create mode 100644 app/src/main/assets/images/ic_folder.png create mode 100644 app/src/main/assets/images/ic_folder_special.png create mode 100644 app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseViewActivity.java create mode 100644 app/src/main/java/com/stoutner/privacybrowser/CreateBookmarkFolder.java create mode 100644 app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java create mode 100644 app/src/main/java/com/stoutner/privacybrowser/MoveToFolder.java create mode 100644 app/src/main/res/drawable-hdpi/folder_blue_bitmap.png create mode 100644 app/src/main/res/drawable-mdpi/folder_blue_bitmap.png create mode 100644 app/src/main/res/drawable-xhdpi/folder_blue_bitmap.png create mode 100644 app/src/main/res/drawable-xxhdpi/folder_blue_bitmap.png create mode 100644 app/src/main/res/drawable/create_folder.xml create mode 100644 app/src/main/res/drawable/folder.xml create mode 100644 app/src/main/res/drawable/move_to_folder.xml create mode 100644 app/src/main/res/layout/bookmarks_database_view_coordinatorlayout.xml create mode 100644 app/src/main/res/layout/bookmarks_database_view_linearlayout.xml create mode 100644 app/src/main/res/layout/create_bookmark_folder_dialog.xml create mode 100644 app/src/main/res/layout/edit_bookmark_folder_dialog.xml diff --git a/.idea/dictionaries/soren.xml b/.idea/dictionaries/soren.xml index 2d91de18..f6e91ca9 100644 --- a/.idea/dictionaries/soren.xml +++ b/.idea/dictionaries/soren.xml @@ -35,6 +35,8 @@ orbot panopticlick parentfolder + radiobutton + radiogroup redmine referer relativelayout diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 17fb93ab..60d1f3aa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,7 +28,7 @@ - + - + - + - + @@ -69,48 +69,54 @@ - + - + android:persistableMode="persistNever" /> + + + - + - + android:persistableMode="persistNever" /> - + - + android:persistableMode="persistNever" /> - + - + android:persistableMode="persistNever" /> diff --git a/app/src/main/assets/about_licenses.html b/app/src/main/assets/about_licenses.html index 63e79966..f09fbb42 100644 --- a/app/src/main/assets/about_licenses.html +++ b/app/src/main/assets/about_licenses.html @@ -23,8 +23,10 @@ color: 0D4781; } - img.center { + img.icon { vertical-align: middle; + height: 32; + width: 32; } img.left { @@ -55,37 +57,43 @@ are derived from ic_security and ic_language. Modifications were made by Soren Stoutner in 2016.

-

ic_language.

+

ic_language.

-

ic_home.

+

ic_home.

-

ic_arrow_back.

+

ic_arrow_back.

-

ic_arrow_forward.

+

ic_arrow_forward.

-

ic_bookmark_border.

+

ic_bookmark_border.

-

ic_file_download.

+

ic_file_download.

-

ic_settings.

+

ic_settings.

-

ic_import_contacts.

+

ic_import_contacts.

-

ic_info_outline.

+

ic_info_outline.

-

ic_exit_to_app.

+

ic_exit_to_app.

-

ic_add.

+

ic_add.

-

ic_vertical_align_top

+

ic_vertical_align_top

-

ic_vertical_align_bottom

+

ic_vertical_align_bottom

-

ic_edit.

+

ic_folder

-

ic_download.

+

ic_create_new_folder

-

ic_select_all

+

ic_folder_special

+ +

ic_edit.

+ +

ic_download.

+ +

ic_select_all


GNU General Public License

diff --git a/app/src/main/assets/images/ic_create_new_folder.png b/app/src/main/assets/images/ic_create_new_folder.png new file mode 100644 index 0000000000000000000000000000000000000000..db0234d7b9e35cae4e5e159ebd095d6679b87741 GIT binary patch literal 911 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSEX7WqAsj$Z!;#Vf4nJ zsJsMW#sHP)KY)UgC9V-A!TD(=<%vb94CUqJdYO6I#mR{Use1WE>9gP2NHH)l^LV;A zhE&XXdv|wMNT5jjM}4=W++{+_5&|x7_9QHx(Xc2Xiko>~6MsU|bfdZZ8tfb{-H7zi zGR>@Ez39ZF=6JDUrAs%bN5PCYw$C@IZ{8=b<576-%)Fcb*Q_}+$H!rkiYGFvzUSe) z-I&XVaqYf0vNcQ|<>i2b{<~m7g+o*80DaoM{t3&CXEQc*5{u>L+OsgXgLF zIlq7lDo@SN3S7~7zVt!ngUVp;z)7VEycMiAkMHNFtevs!Px+dyo9{80SLNQy`<5yb zeO=mp)%jGb*n~#A@9FhxPX}@;A9(Zadht&6=Oqlh+ae#tz4QFXA)(jbm~5&3gk{0j z%-~vS=6BEMB(O`^8C$YjGxN;%KG1oknfJg4$%Vyd-GrD8<{S&=FJY7LV-M!v!pxG+ z&}7VT&=SaC_t@_8e73^UX2~TRU+Yy!J_Z77aiK! zyVLon|1q7&ZprhZFZ-|h^X;-{BFcWC>+~*tWe9y5<8LtG{qyob~W^_YY>D`aiYr>fW%-hpfAc4@^JAf92PHJLdH2H_>bJ z81GBhIfPZ^GMZPvi(uAcdatE-XH{H&4nJ zsJsMW#sHP)KY)UgC9V-A!TD(=<%vb94CUqJdYO6I#mR{Use1WE>9gP2NHH)lt?+bl z45^s&_O7*mNT5jDL-9`YEd53|-l?}{&3LeMIrS*=hgJkrUBLc>Z3~*XeV(1T zzkVLyr^y^&r~TqB6$&Y8|J7^4BD!};oiKm&Kg$;{EZoyS77OttO#IbbEEG~Q`IoOT zi>-F0c7-qbx%zu4= z{XyY%bq&r2waYqZ^)_F6dXO!lY7IlVxBQd&4KEFuZP=fA$%P0tT-@ecx{cwEer1Dj z!mm5pmT&kBYS%HQ-tWqNSQ+EVP{&us{WW*@+k=xBYTgzIUcI<0 zTy^$enGLLS^!D`~+rm7Dv2Lqnj1gm5LwRi8lEA$SQ$H`P;j9U)hl_VxY;o-$XN z&X|0_`N3^F@hxpDc^@#c-WU02Ut4>qoF`(Dq%-${pY!I0@6Nrn=j3L#HO%{8$GM{< e9~DpGhx&)LocV9>Oj!&}*bJVoelF{r5}E+ecuL3s literal 0 HcmV?d00001 diff --git a/app/src/main/assets/images/ic_folder_special.png b/app/src/main/assets/images/ic_folder_special.png new file mode 100644 index 0000000000000000000000000000000000000000..2eef308a3714d5ba413493e557191d82a2f71f08 GIT binary patch literal 1572 zcmV+<2HW|GP)`S1Ch);qbiXwtO)<@qo#G+Px5k%CAiufP}FKLWgm5LWEUJ-0!Vo~BHjZs@4{u_5! zH`#M$=FFL$`F-HfWIH?O%${G)&di>f1rZSu5fKp)5fKp)5fKp)5fKp)5$SrhjMmcw zYyq|b1Hk%>)|=Ejz=M(7Pzk31_X0D(iGr2;!)>+zTn2na{Ek)#BG?A}Li`R^2qL%; zm?eJ4`jiKPyb={PPW%qlND#?R;&-G%5Ya2d@5Ho4ZwCqW0rSM~K#c^ETtWPf(?}4} zb;R#9g&tQGOxNBj`_lplaYz$kDn*`3OFvMZyFP|2>m3tY2Or*+~p0!Z;} z2HwI(vP2Ys?$?9u%j{0_WCfr+b^{~6Pf`HNqXvv&=WCoP0Oc|W+~)HH1)zLx@L5;^ zD4$J0zw5#ZK>5_LIU7zCfOHn?rDz6R7gPY!c@B6!b|cz2*>%B_Lbjp;#;_sxrq~Th zN&!gjL*QWj=Yzmi5!;fO-U5<)sOjg2qqQO>1t6JOU|-Yk&tf0db{z#EnMZ+zrr#HV z$0M~R6$QZGpTOQWk3UlXzpkPH_}d2@Y4i9r@IuaQNlgLpHGQ ze^-0xl0!lTz|D)m_a4hn0WSw?OQsY6H%Sify5f*fp9VJkj-BJ5!FKqLV(0gd00$DR zZydM-I2YIeoQ+M@ItROp+M4uDPOr=XSj0MV7VF5P_1|aek1cibbp7A?0Bu<5kgyL6 z#Q=?Ptj7Y>v{jq*LCV7O?ZPx}a05=ACCGs_bjZQw>zRPvxW!?wq1)iq6Zw7G} zHs#FgVvkU|Z3Jj8PXMd4e@TMr!6um`-Cm6V|8WcODsWothB#OYyae2yM`cwB0Kg7l z0ysZ*Lsl?|UG9B-UKLg;005U`pXLq6Zb%zZ+E%pyK!m?C2RpEH4Y`j_RSW>+{65>k z(*DWmF)FWW0HCp_JxZL{p@tozMm<8QasZaxi5+X7nq!?y=O_~0)+I{{fEfYaAfInK zISbfNLC#Vt01cb5IekL`>lgyw1GWaN!;1pY>LTFEfa@Qw*HIV>!2eAKTz8U>m4i|M zT784x%pYG#_>aHasXQUEG$q9k=VQve!%20qWBp3i`va;V3N0?;r4 z9M7Si6NEj|@S*@TB%~`L_5&1v2A^Z}0pKyLBPX%j#hvHz{6g&qxa#IPft|_% z_C?s8ti~p4&b3*`Z`h}My$<1Q?>>h(my5Hkh&fC2%B)yZZ>jZb_89O z^)28yaAwnX*6Q2(U50f`0k_in2X_YWcc5+3K7dF0xM>@Qv7tFsUGLL+*!W4HdbAJV zVFDWgMzMSTrurVf4mM$jjZa}0fkxjCDD&09u7-fGfpt06R|9Sac2a;liYWlLvlhd; zh;{!Lnr>` zu{(?%b6MDD1enJ@;gVu|4ad$0|DC|D{9c@j3EoF<68{4nmGfc zt>q{=UU<5d_VeOY;*>ceTylOX@I}gotE4Z7fr~rzRWQB4J=o2dW4{npE7QQ;q`Zl= z&v}5AtOB-R!_Xi$*+H^5gPr6V$L=YZ$CXSXA|fIpA|fIpA|fIpA|fIpA|fJEbpHU3 WuhL&|k-W?R0000= 21. + // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21. final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar); setSupportActionBar(bookmarksAppBar); - // Display the home arrow on supportAppBar. - final ActionBar appBar = getSupportActionBar(); + // Display the home arrow on `SupportActionBar`. + appBar = getSupportActionBar(); assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that appBar might be null. appBar.setDisplayHomeAsUpEnabled(true); + // Initialize the database handler and the ListView. // `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 BookmarksDatabaseHandler. + // The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHandler`. bookmarksDatabaseHandler = new BookmarksDatabaseHandler(this, null, null, 0); bookmarksListView = (ListView) findViewById(R.id.bookmarks_listview); + // Set currentFolder to the home folder, which is null in the database. + currentFolder = ""; + // Display the bookmarks in the ListView. - updateBookmarksListView(); + updateBookmarksListView(currentFolder); // Set a listener so that tapping a list item loads the URL. We need to store the activity // in a variable so that we can return to the parent activity after loading the URL. @@ -98,12 +121,28 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Convert the id from long to int to match the format of the bookmarks database. int databaseID = (int) id; - // Get the bookmark URL and assign it to formattedUrlString. - MainWebViewActivity.formattedUrlString = bookmarksDatabaseHandler.getBookmarkURL(databaseID); + // Get the bookmark `Cursor` and move it to the first row. + Cursor bookmarkCursor = bookmarksDatabaseHandler.getBookmarkCursor(databaseID); + bookmarkCursor.moveToFirst(); + + // If the bookmark is a folder load its contents into the ListView. + if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.IS_FOLDER)) == 1) { + // Update `currentFolder`. + currentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)); + + // Reload the ListView with `currentFolder`. + updateBookmarksListView(currentFolder); + } else { // Load the URL into `mainWebView`. + // Get the bookmark URL and assign it to formattedUrlString. + MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_URL)); + + // Load formattedUrlString and return to the main activity. + MainWebViewActivity.mainWebView.loadUrl(MainWebViewActivity.formattedUrlString); + NavUtils.navigateUpFromSameTask(bookmarksActivity); + } - // Load formattedUrlString and return to the main activity. - MainWebViewActivity.mainWebView.loadUrl(MainWebViewActivity.formattedUrlString); - NavUtils.navigateUpFromSameTask(bookmarksActivity); + // Close the `Cursor`. + bookmarkCursor.close(); } }); @@ -125,7 +164,14 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma public boolean onCreateActionMode(ActionMode mode, Menu menu) { // Inflate the menu for the contextual app bar and set the title. getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu); - mode.setTitle(R.string.bookmarks); + + // Set the title. + if (currentFolder.isEmpty()) { + // Use `R.string.bookmarks` if we are in the home folder. + mode.setTitle(R.string.bookmarks); + } else { // Use the current folder name as the title. + mode.setTitle(currentFolder); + } // Get a handle for MenuItems we need to selectively disable. moveBookmarkUpMenuItem = menu.findItem(R.id.move_bookmark_up); @@ -202,12 +248,14 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // `numberOfBookmarks` is used in `R.id.move_bookmark_up_enabled`, `R.id.move_bookmark_down_enabled`, and `R.id.context_menu_select_all_bookmarks`. int numberOfBookmarks; - // `selectedBookmarkLongArray` is used in `R.id.move_bookmark_up` and `R.id.move_bookmark_down`. + // `selectedBookmarkLongArray` is used in `R.id.move_bookmark_up`, `R.id.move_bookmark_down`, and `R.id.edit_bookmark`. long[]selectedBookmarkLongArray; - // `selectedBookmarkDatabaseId` is used in `R.id.move_bookmark_up` and `R.id.move_bookmark_down`. + // `selectedBookmarkDatabaseId` is used in `R.id.move_bookmark_up`, `R.id.move_bookmark_down`, and `R.id.edit_bookmark`. int selectedBookmarkDatabaseId; // `selectedBookmarkNewPosition` is used in `R.id.move_bookmark_up` and `R.id.move_bookmark_down`. int selectedBookmarkNewPosition; + // `bookmarkPositionSparseBooleanArray` is used in `R.id.edit_bookmark` and `R.id.delete_bookmark`. + SparseBooleanArray bookmarkPositionSparseBooleanArray; switch (menuItemId) { case R.id.move_bookmark_up: @@ -238,7 +286,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma } // Refresh the ListView. - updateBookmarksListView(); + updateBookmarksListView(currentFolder); // Select the previously selected bookmark in the new location. bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true); @@ -275,7 +323,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma } // Refresh the ListView. - updateBookmarksListView(); + updateBookmarksListView(currentFolder); // Select the previously selected bookmark in the new location. bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true); @@ -284,22 +332,44 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma break; case R.id.edit_bookmark: + // Get a handle for `contextualActionMode` so we can close it when `editBookmarkDialog` is finished. + contextualActionMode = mode; + // Get a handle for `selectedBookmarkPosition` so we can scroll to it after refreshing the ListView. - SparseBooleanArray bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); + bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(0); - // Get a handle for `contextualActionMode` so we can close it when `editBookmarkDialog` is finished. - contextualActionMode = mode; + // Move to the selected database ID and find out if it is a folder. + bookmarksCursor.moveToPosition(selectedBookmarkPosition); + boolean isFolder = (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHandler.IS_FOLDER)) == 1); - // Show the `EditBookmark` `AlertDialog` and name the instance `@string/edit_bookmark`. - DialogFragment editBookmarkDialog = new EditBookmark(); - editBookmarkDialog.show(getFragmentManager(), "@string/edit_bookmark"); + if (isFolder) { + // Save the current folder name. + oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)); + + // Show the `EditBookmarkFolder` `AlertDialog` and name the instance `@string/edit_folder`. + DialogFragment editFolderDialog = new EditBookmarkFolder(); + editFolderDialog.show(getFragmentManager(), getResources().getString(R.string.edit_folder)); + } else { + // Show the `EditBookmark` `AlertDialog` and name the instance `@string/edit_bookmark`. + DialogFragment editBookmarkDialog = new EditBookmark(); + editBookmarkDialog.show(getFragmentManager(), getResources().getString(R.string.edit_bookmark)); + } break; case R.id.delete_bookmark: // Get an array of the selected rows. final long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); + // Get a handle for `selectedBookmarkPosition` so we can scroll to it after refreshing the ListView. + bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); + selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(0); + + updateBookmarksListViewExcept(selectedBookmarksLongArray, currentFolder); + + // Scroll to where the deleted bookmark was located. + bookmarksListView.setSelection(selectedBookmarkPosition); + String snackbarMessage; // Determine how many items are in the array and prepare an appropriate Snackbar message. @@ -309,8 +379,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma snackbarMessage = selectedBookmarksLongArray.length + " " + getString(R.string.bookmarks_deleted); } - updateBookmarksListViewExcept(selectedBookmarksLongArray); - // Show a SnackBar. Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG) .setAction(R.string.undo, new View.OnClickListener() { @@ -326,7 +394,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // The user pushed the "Undo" button. case Snackbar.Callback.DISMISS_EVENT_ACTION: // Refresh the ListView to show the rows again. - updateBookmarksListView(); + updateBookmarksListView(currentFolder); break; @@ -376,7 +444,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma public void onClick(View view) { // Show the `CreateBookmark` `AlertDialog` and name the instance `@string/create_bookmark`. DialogFragment createBookmarkDialog = new CreateBookmark(); - createBookmarkDialog.show(getFragmentManager(), "@string/create_bookmark"); + createBookmarkDialog.show(getFragmentManager(), getResources().getString(R.string.create_bookmark)); } }); } @@ -402,7 +470,21 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma switch (menuItemId) { case android.R.id.home: - NavUtils.navigateUpFromSameTask(this); + // Exit BookmarksActivity if currently at the home folder. + if (currentFolder.isEmpty()) { + NavUtils.navigateUpFromSameTask(this); + } else { // Navigate up one folder. + // Place the former parent folder in `currentFolder`. + currentFolder = bookmarksDatabaseHandler.getParentFolder(currentFolder); + + updateBookmarksListView(currentFolder); + } + break; + + case R.id.create_folder: + // Show the `CreateBookmarkFolder` `AlertDialog` and name the instance `@string/create_folder`. + DialogFragment createBookmarkFolderDialog = new CreateBookmarkFolder(); + createBookmarkFolderDialog.show(getFragmentManager(), getResources().getString(R.string.create_folder)); break; case R.id.options_menu_select_all_bookmarks: @@ -412,21 +494,27 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma bookmarksListView.setItemChecked(i, true); } break; + + case R.id.bookmarks_database_view: + // Launch `BookmarksDatabaseViewActivity`. + Intent bookmarksDatabaseViewIntent = new Intent(this, BookmarksDatabaseViewActivity.class); + startActivity(bookmarksDatabaseViewIntent); + break; } return true; } @Override - public void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment) { + public void onCancelCreateBookmark(DialogFragment dialogFragment) { // Do nothing because the user selected `Cancel`. } @Override - public void onCreateBookmarkCreate(DialogFragment createBookmarkDialogFragment) { + public void onCreateBookmark(DialogFragment dialogFragment) { // Get the `EditText`s from the `createBookmarkDialogFragment` and extract the strings. - EditText createBookmarkNameEditText = (EditText) createBookmarkDialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext); + EditText createBookmarkNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext); String bookmarkNameString = createBookmarkNameEditText.getText().toString(); - EditText createBookmarkUrlEditText = (EditText) createBookmarkDialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext); + EditText createBookmarkUrlEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext); String bookmarkUrlString = createBookmarkUrlEditText.getText().toString(); // Convert the favoriteIcon Bitmap to a byte array. `0` is for lossless compression (the only option for a PNG). @@ -438,63 +526,175 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma int newBookmarkDisplayOrder = bookmarksListView.getCount(); // Create the bookmark. - bookmarksDatabaseHandler.createBookmark(bookmarkNameString, bookmarkUrlString, newBookmarkDisplayOrder, favoriteIconByteArray); + bookmarksDatabaseHandler.createBookmark(bookmarkNameString, bookmarkUrlString, newBookmarkDisplayOrder, currentFolder, favoriteIconByteArray); // Refresh the ListView. `setSelection` scrolls to the bottom of the list. - updateBookmarksListView(); - bookmarksListView.setSelection(bookmarksListView.getCount()); + updateBookmarksListView(currentFolder); + bookmarksListView.setSelection(newBookmarkDisplayOrder); } @Override - public void onEditBookmarkCancel(DialogFragment editBookmarkDialogFragment) { + public void onCancelCreateBookmarkFolder(DialogFragment dialogFragment) { // Do nothing because the user selected `Cancel`. } @Override - public void onEditBookmarkSave(DialogFragment editBookmarkDialogFragment) { - // Get the `EditText`s from the `editBookmarkDialogFragment` and extract the strings. - EditText editBookmarkNameEditText = (EditText) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext); - String bookmarkNameString = editBookmarkNameEditText.getText().toString(); - EditText editBookmarkUrlEditText = (EditText) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext); - String bookmarkUrlString = editBookmarkUrlEditText.getText().toString(); + public void onCreateBookmarkFolder(DialogFragment dialogFragment) { + // Get `create_folder_name_edit_text` and extract the string. + EditText createFolderNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext); + String folderNameString = createFolderNameEditText.getText().toString(); + + // Check to see if the folder already exists. + Cursor bookmarkFolderCursor = bookmarksDatabaseHandler.getFolderCursor(folderNameString); + int existingFoldersWithNewName = bookmarkFolderCursor.getCount(); + bookmarkFolderCursor.close(); + if (folderNameString.isEmpty() || (existingFoldersWithNewName > 0)) { + String cannotCreateFolder = getResources().getString(R.string.cannot_create_folder) + " \"" + folderNameString + "\""; + Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), cannotCreateFolder, Snackbar.LENGTH_INDEFINITE).show(); + } else { // Create the folder. + // Get the new folder icon. + RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobuttion); + Bitmap folderIconBitmap; + if (defaultFolderIconRadioButton.isChecked()) { + // Get the default folder icon drawable and convert it to a `Bitmap`. `this` specifies the current context. + Drawable folderIconDrawable = ContextCompat.getDrawable(this, R.drawable.folder_blue_bitmap); + BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable; + folderIconBitmap = folderIconBitmapDrawable.getBitmap(); + } else { + folderIconBitmap = MainWebViewActivity.favoriteIcon; + } - CheckBox useNewFavoriteIconBitmap = (CheckBox) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_use_new_favorite_icon_checkbox); - byte[] favoriteIconByteArray; + // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG). + ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream(); + folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream); + byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray(); + + // Move all the bookmarks down one in the display order. + for (int i = 0; i < bookmarksListView.getCount(); i++) { + int databaseId = (int) bookmarksListView.getItemIdAtPosition(i); + bookmarksDatabaseHandler.updateBookmarkDisplayOrder(databaseId, i + 1); + } + // Create the folder, placing it at the top of the ListView + bookmarksDatabaseHandler.createFolder(folderNameString, 0, currentFolder, folderIconByteArray); + + // Refresh the ListView. + updateBookmarksListView(currentFolder); + } + } + + @Override + public void onCancelEditBookmark(DialogFragment dialogFragment) { + // Do nothing because the user selected `Cancel`. + } + + @Override + public void onSaveEditBookmark(DialogFragment dialogFragment) { // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`. long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0]; - if (useNewFavoriteIconBitmap.isChecked()) { - // Get the new favorite icon from the Dialog and convert it into a Bitmap. - ImageView newFavoriteIconImageView = (ImageView) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_new_favorite_icon); - Drawable favoriteIconDrawable = newFavoriteIconImageView.getDrawable(); - Bitmap favoriteIconBitmap = ((BitmapDrawable) favoriteIconDrawable).getBitmap(); + // Get the `EditText`s from the `editBookmarkDialogFragment` and extract the strings. + EditText editBookmarkNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext); + String bookmarkNameString = editBookmarkNameEditText.getText().toString(); + EditText editBookmarkUrlEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext); + String bookmarkUrlString = editBookmarkUrlEditText.getText().toString(); - // Convert the new `favoriteIconBitmap` into a Byte Array. - ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream(); - favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream); - favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray(); + // Get `edit_bookmark_current_icon_radiobutton`. + RadioButton currentBookmarkIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton); - // Update the bookmark and the favorite icon. - bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, favoriteIconByteArray); - } else { // Update the bookmark without changing the favorite icon. + if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon. bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString); + } else { // Update the bookmark and the favorite icon. + // Get the new favorite icon from the `Dialog` and convert it into a `Bitmap`. + ImageView newFavoriteIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_web_page_favorite_icon); + Drawable newFavoriteIconDrawable = newFavoriteIconImageView.getDrawable(); + Bitmap newFavoriteIconBitmap = ((BitmapDrawable) newFavoriteIconDrawable).getBitmap(); + + // Convert `newFavoriteIconBitmap` into a Byte Array. + ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream(); + newFavoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream); + byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray(); + + // Update the bookmark and the favorite icon. + bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, newFavoriteIconByteArray); } // Close the contextual action mode. contextualActionMode.finish(); - // Refresh the `ListView`. `setSelection` scrolls to that position. - updateBookmarksListView(); + // Refresh the `ListView`. `setSelection` scrolls to the position of the bookmark that was edited. + updateBookmarksListView(currentFolder); bookmarksListView.setSelection(selectedBookmarkPosition); } - private void updateBookmarksListView() { - // Get a Cursor with the current contents of the bookmarks database. - final Cursor bookmarksCursor = bookmarksDatabaseHandler.getAllBookmarksCursor(); + @Override + public void onCancelEditBookmarkFolder(DialogFragment dialogFragment) { + // Do nothing because the user selected `Cancel`. + } + + @Override + public void onSaveEditBookmarkFolder(DialogFragment dialogFragment) { + // Get the new folder name. + EditText editFolderNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext); + String newFolderNameString = editFolderNameEditText.getText().toString(); + + // Check to see if the new folder name is unique. + Cursor bookmarkFolderCursor = bookmarksDatabaseHandler.getFolderCursor(newFolderNameString); + int existingFoldersWithNewName = bookmarkFolderCursor.getCount(); + bookmarkFolderCursor.close(); + if ( ((existingFoldersWithNewName == 0) || newFolderNameString.equals(oldFolderNameString)) && !newFolderNameString.isEmpty()) { + // Get a long array with the the databaseId of the selected folder and convert it to an `int`. + long[] selectedFolderLongArray = bookmarksListView.getCheckedItemIds(); + int selectedFolderDatabaseId = (int) selectedFolderLongArray[0]; + + // Get the `RadioButtons` from the `Dialog`. + RadioButton currentFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton); + RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton); + Bitmap folderIconBitmap; + + // Prepare the favorite icon. + if (currentFolderIconRadioButton.isChecked()) { + // Update the folder name if it has changed. + if (!newFolderNameString.equals(oldFolderNameString)) { + bookmarksDatabaseHandler.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString); + + // Refresh the `ListView`. `setSelection` scrolls to the position of the folder that was edited. + updateBookmarksListView(currentFolder); + bookmarksListView.setSelection(selectedBookmarkPosition); + } + } else { // Prepare the new favorite icon. + if (defaultFolderIconRadioButton.isChecked()) { + // Get the default folder icon drawable and convert it to a `Bitmap`. `this` specifies the current context. + Drawable folderIconDrawable = ContextCompat.getDrawable(this, R.drawable.folder_blue_bitmap); + BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable; + folderIconBitmap = folderIconBitmapDrawable.getBitmap(); + } else { // Use the web page favorite icon. + folderIconBitmap = MainWebViewActivity.favoriteIcon; + } + + // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG). + ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream(); + folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream); + byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray(); + + bookmarksDatabaseHandler.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, folderIconByteArray); + + // Refresh the `ListView`. `setSelection` scrolls to the position of the folder that was edited. + updateBookmarksListView(currentFolder); + bookmarksListView.setSelection(selectedBookmarkPosition); + } + } else { // Don't edit the folder because the new name is not unique. + String cannot_rename_folder = getResources().getString(R.string.cannot_rename_folder) + " \"" + newFolderNameString + "\""; + Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), cannot_rename_folder, Snackbar.LENGTH_INDEFINITE).show(); + } + } + + private void updateBookmarksListView(String folderName) { + // Get a `Cursor` with the current contents of the bookmarks database. + bookmarksCursor = bookmarksDatabaseHandler.getAllBookmarksCursorByDisplayOrder(folderName); - // Setup bookmarksCursorAdapter with `this` context. The `false` disables autoRequery. + // Setup `bookmarksCursorAdapter` with `this` context. The `false` disables autoRequery. CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { @@ -518,20 +718,34 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`. String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)); TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name); - assert bookmarkNameTextView != null; // This assert removes the warning that bookmarkNameTextView might be null. bookmarkNameTextView.setText(bookmarkNameString); + + // Make the font bold for folders. + if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHandler.IS_FOLDER)) == 1) { + // The first argument is `null` because we don't want to chage the font. + bookmarkNameTextView.setTypeface(null, Typeface.BOLD); + } else { // Reset the font to default. + bookmarkNameTextView.setTypeface(Typeface.DEFAULT); + } } }; // Update the ListView. bookmarksListView.setAdapter(bookmarksCursorAdapter); + + // Set the AppBar title. + if (currentFolder.isEmpty()) { + appBar.setTitle(R.string.bookmarks); + } else { + appBar.setTitle(currentFolder); + } } - private void updateBookmarksListViewExcept(long[] exceptIdLongArray) { - // Get a Cursor with the current contents of the bookmarks database except for the specified database IDs. - final Cursor bookmarksCursor = bookmarksDatabaseHandler.getBookmarksCursorExcept(exceptIdLongArray); + private void updateBookmarksListViewExcept(long[] exceptIdLongArray, String folderName) { + // Get a `Cursor` with the current contents of the bookmarks database except for the specified database IDs. + bookmarksCursor = bookmarksDatabaseHandler.getBookmarksCursorExcept(exceptIdLongArray, folderName); - // Setup bookmarksCursorAdapter with `this` context. The `false` disables autoRequery. + // Setup `bookmarksCursorAdapter` with `this` context. The `false` disables autoRequery. CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { @@ -555,8 +769,15 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`. String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)); TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name); - assert bookmarkNameTextView != null; // This assert removes the warning that bookmarkNameTextView might be null. bookmarkNameTextView.setText(bookmarkNameString); + + // Make the font bold for folders. + if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHandler.IS_FOLDER)) == 1) { + // The first argument is `null` because we don't want to chage the font. + bookmarkNameTextView.setTypeface(null, Typeface.BOLD); + } else { // Reset the font to default. + bookmarkNameTextView.setTypeface(Typeface.DEFAULT); + } } }; diff --git a/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java b/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java index 5a5a025a..bc17bc7d 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java +++ b/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java @@ -22,8 +22,10 @@ package com.stoutner.privacybrowser; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.provider.ContactsContract; public class BookmarksDatabaseHandler extends SQLiteOpenHelper { private static final int SCHEMA_VERSION = 1; @@ -62,21 +64,41 @@ public class BookmarksDatabaseHandler extends SQLiteOpenHelper { // Code for upgrading the database will be added here when the schema version > 1. } - public void createBookmark(String bookmarkName, String bookmarkURL, int displayOrder, byte[] favoriteIcon) { + public void createBookmark(String bookmarkName, String bookmarkURL, int displayOrder, String parentFolder, byte[] favoriteIcon) { ContentValues bookmarkContentValues = new ContentValues(); // ID is created automatically. bookmarkContentValues.put(DISPLAY_ORDER, displayOrder); bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName); bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL); - bookmarkContentValues.put(PARENT_FOLDER, ""); + bookmarkContentValues.put(PARENT_FOLDER, parentFolder); bookmarkContentValues.put(IS_FOLDER, false); bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon); // Get a writable database handle. SQLiteDatabase bookmarksDatabase = this.getWritableDatabase(); - // The second argument is "null", which makes it so that completely null rows cannot be created. Not a problem in our case. + // The second argument is `null`, which makes it so that completely null rows cannot be created. Not a problem in our case. + bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues); + + // Close the database handle. + bookmarksDatabase.close(); + } + + public void createFolder(String folderName, int displayOrder, String parentFolder, byte[] favoriteIcon) { + ContentValues bookmarkContentValues = new ContentValues(); + + // ID is created automatically. + bookmarkContentValues.put(DISPLAY_ORDER, displayOrder); + bookmarkContentValues.put(BOOKMARK_NAME, folderName); + bookmarkContentValues.put(PARENT_FOLDER, parentFolder); + bookmarkContentValues.put(IS_FOLDER, true); + bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon); + + // Get a writable database handle. + SQLiteDatabase bookmarksDatabase = this.getWritableDatabase(); + + // The second argument is `null`, which makes it so that completely null rows cannot be created. Not a problem in our case. bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues); // Close the database handle. @@ -87,38 +109,54 @@ public class BookmarksDatabaseHandler extends SQLiteOpenHelper { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // Prepare the SQL statement to get the cursor for `databaseId` + // Prepare the SQL statement to get the `Cursor` for `databaseId` final String GET_ONE_BOOKMARK = "Select * FROM " + BOOKMARKS_TABLE + " WHERE " + _ID + " = " + databaseId; - // Return the results as a `Cursor`. The second argument is `null` because there are no selectionArgs. + // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`. // We can't close the `Cursor` because we need to use it in the parent activity. return bookmarksDatabase.rawQuery(GET_ONE_BOOKMARK, null); } - public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray) { + public Cursor getFolderCursor(String folderName) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // Prepare a string that contains the comma-separated list of IDs not to get. - String doNotGetIdsString = ""; - // Extract the array to `doNotGetIdsString`. - for (long databaseIdLong : exceptIdLongArray) { - // If this is the first number, only add the number. - if (doNotGetIdsString.isEmpty()) { - doNotGetIdsString = String.valueOf(databaseIdLong); - } else { // If there already is a number in the string, place a `,` before the number. - doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong; - } - } + // SQL escape `folderName`. + folderName = DatabaseUtils.sqlEscapeString(folderName); - // Prepare the SQL statement to select all items except those with the specified IDs. - final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE + - " WHERE " + _ID + " NOT IN (" + doNotGetIdsString + ") ORDER BY " + DISPLAY_ORDER + " ASC"; + // Prepare the SQL statement to get the `Cursor` for the folder. + final String GET_FOLDER = "Select * FROM " + BOOKMARKS_TABLE + + " WHERE " + BOOKMARK_NAME + " = " + folderName + + " AND " + IS_FOLDER + " = " + 1; - // Return the results as a `Cursor`. The second argument is `null` because there are no selectionArgs. + // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`. // We can't close the `Cursor` because we need to use it in the parent activity. - return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null); + return bookmarksDatabase.rawQuery(GET_FOLDER, null); + } + + public String getParentFolder(String currentFolder) { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); + + // SQL escape `currentFolder`. + currentFolder = DatabaseUtils.sqlEscapeString(currentFolder); + + // Prepare the SQL statement to get the parent folder. + final String GET_PARENT_FOLDER = "Select * FROM " + BOOKMARKS_TABLE + + " WHERE " + IS_FOLDER + " = " + 1 + " AND " + BOOKMARK_NAME + " = " + currentFolder; + + // The second argument is `null` because there are no `selectionArgs`. + Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_PARENT_FOLDER, null); + bookmarkCursor.moveToFirst(); + + // Store the name of the parent folder. + String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER)); + + // Close the `Cursor`. + bookmarkCursor.close(); + + return parentFolder; } public Cursor getAllBookmarksCursor() { @@ -126,35 +164,55 @@ public class BookmarksDatabaseHandler extends SQLiteOpenHelper { SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); // Get everything in the BOOKMARKS_TABLE. - final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE + " ORDER BY " + DISPLAY_ORDER + " ASC"; + final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE; // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs. // We can't close the Cursor because we need to use it in the parent activity. return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null); } - public String getBookmarkURL(int databaseId) { + public Cursor getAllBookmarksCursorByDisplayOrder(String folderName) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // Prepare the SQL statement to get the row for the selected databaseId. - final String GET_BOOKMARK_URL = "Select * FROM " + BOOKMARKS_TABLE + - " WHERE " + _ID + " = " + databaseId; + // SQL escape `folderName`. + folderName = DatabaseUtils.sqlEscapeString(folderName); - // Save the results as Cursor and move it to the first (only) row. The second argument is "null" because there are no selectionArgs. - Cursor bookmarksCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK_URL, null); - bookmarksCursor.moveToFirst(); + // Get everything in the BOOKMARKS_TABLE. + final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE + + " WHERE " + PARENT_FOLDER + " = " + folderName + " ORDER BY " + DISPLAY_ORDER + " ASC"; - // Get the int that identifies the "BOOKMARK_URL" column and save the string as bookmarkURL. - int urlColumnInt = bookmarksCursor.getColumnIndex(BOOKMARK_URL); - String bookmarkURLString = bookmarksCursor.getString(urlColumnInt); + // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs. + // We can't close the Cursor because we need to use it in the parent activity. + return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null); + } - // Close the Cursor and the database handle. - bookmarksCursor.close(); - bookmarksDatabase.close(); + public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray, String folderName) { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // Return the bookmarkURLString. - return bookmarkURLString; + // Prepare a string that contains the comma-separated list of IDs not to get. + String doNotGetIdsString = ""; + // Extract the array to `doNotGetIdsString`. + for (long databaseIdLong : exceptIdLongArray) { + // If this is the first number, only add the number. + if (doNotGetIdsString.isEmpty()) { + doNotGetIdsString = String.valueOf(databaseIdLong); + } else { // If there already is a number in the string, place a `,` before the number. + doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong; + } + } + + // SQL escape `folderName`. + folderName = DatabaseUtils.sqlEscapeString(folderName); + + // Prepare the SQL statement to select all items except those with the specified IDs. + final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE + + " WHERE " + PARENT_FOLDER + " = " + folderName + " AND " + _ID + " NOT IN (" + doNotGetIdsString + ") ORDER BY " + DISPLAY_ORDER + " ASC"; + + // Return the results as a `Cursor`. The second argument is `null` because there are no selectionArgs. + // We can't close the `Cursor` because we need to use it in the parent activity. + return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null); } public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) { @@ -167,7 +225,7 @@ public class BookmarksDatabaseHandler extends SQLiteOpenHelper { // Get a writable database handle. SQLiteDatabase bookmarksDatabase = this.getWritableDatabase(); - // Update the database. The last argument is `null` because there are no `whereArgs`. + // Update the bookmark. The last argument is `null` because there are no `whereArgs`. bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null); // Close the database handle. @@ -185,13 +243,70 @@ public class BookmarksDatabaseHandler extends SQLiteOpenHelper { // Get a writable database handle. SQLiteDatabase bookmarksDatabase = this.getWritableDatabase(); - // Update the database. The last argument is `null` because there are no `whereArgs`. + // Update the bookmark. The last argument is `null` because there are no `whereArgs`. bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null); // Close the database handle. bookmarksDatabase.close(); } + public void updateFolder(int databaseId, String oldFolderName, String newFolderName) { + // Get a writable database handle. + SQLiteDatabase bookmarksDatabase = this.getWritableDatabase(); + + // Update the folder first. Store the updated values in `folderContentValues`. + ContentValues folderContentValues = new ContentValues(); + + folderContentValues.put(BOOKMARK_NAME, newFolderName); + + // Run the update on the folder. The last argument is `null` because there are no `whereArgs`. + bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null); + + + // Update the bookmarks inside the folder with the new parent folder name. + ContentValues bookmarkContentValues = new ContentValues(); + + bookmarkContentValues.put(PARENT_FOLDER, newFolderName); + + // SQL escape `oldFolderName`. + oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName); + + // Run the update on the bookmarks. The last argument is `null` because there are no `whereArgs`. + bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null); + + // Close the database handle. + bookmarksDatabase.close(); + } + + public void updateFolder(int databaseId, String oldFolderName, String newFolderName, byte[] folderIcon) { + // Get a writable database handle. + SQLiteDatabase bookmarksDatabase = this.getWritableDatabase(); + + // Update the folder first. Store the updated values in `folderContentValues`. + ContentValues folderContentValues = new ContentValues(); + + folderContentValues.put(BOOKMARK_NAME, newFolderName); + folderContentValues.put(FAVORITE_ICON, folderIcon); + + // Run the update on the folder. The last argument is `null` because there are no `whereArgs`. + bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, _ID + " = " + databaseId, null); + + + // Update the bookmarks inside the folder with the new parent folder name. + ContentValues bookmarkContentValues = new ContentValues(); + + bookmarkContentValues.put(PARENT_FOLDER, newFolderName); + + // SQL escape `oldFolderName`. + oldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName); + + // Run the update on the bookmarks. The last argument is `null` because there are no `whereArgs`. + bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, PARENT_FOLDER + " = " + oldFolderName, null); + + // Close the database handle. + bookmarksDatabase.close(); + } + public void updateBookmarkDisplayOrder(int databaseId, int displayOrder) { // Store the updated values in `bookmarkContentValues`. ContentValues bookmarkContentValues = new ContentValues(); diff --git a/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseViewActivity.java new file mode 100644 index 00000000..00bad900 --- /dev/null +++ b/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseViewActivity.java @@ -0,0 +1,144 @@ +/** + * Copyright 2016 Soren Stoutner . + * + * This file is part of Privacy Browser . + * + * Privacy Browser is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser. If not, see . + */ + +package com.stoutner.privacybrowser; + +import android.content.Context; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Typeface; +import android.os.Bundle; +import android.support.v4.content.ContextCompat; +import android.support.v4.widget.CursorAdapter; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; + +public class BookmarksDatabaseViewActivity extends AppCompatActivity { + // `bookmarksDatabaseHandler` is used in `onCreate()` and `updateBookmarksListView()`. + BookmarksDatabaseHandler bookmarksDatabaseHandler; + + // `bookmarksListView` is used in `onCreate()` and `updateBookmarksListView()`. + ListView bookmarksListView; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.bookmarks_database_view_coordinatorlayout); + + // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21. + final Toolbar bookmarksDatabaseViewAppBar = (Toolbar) findViewById(R.id.bookmarks_database_view_toolbar); + setSupportActionBar(bookmarksDatabaseViewAppBar); + + // Display the home arrow on `SupportActionBar`. + final ActionBar appBar = getSupportActionBar(); + assert appBar != null; // This assert removes the incorrect warning in Android Studio on the following line that appBar might be null. + appBar.setDisplayHomeAsUpEnabled(true); + + // Initialize the database handler and the ListView. + // `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 `BookmarksDatabaseHandler`. + bookmarksDatabaseHandler = new BookmarksDatabaseHandler(this, null, null, 0); + bookmarksListView = (ListView) findViewById(R.id.bookmarks_database_view_listview); + + // Display the bookmarks in the ListView. + updateBookmarksListView(); + + } + + private void updateBookmarksListView() { + // Get a `Cursor` with the current contents of the bookmarks database. + final Cursor bookmarksCursor = bookmarksDatabaseHandler.getAllBookmarksCursor(); + + // Setup `bookmarksCursorAdapter` with `this` context. The `false` disables autoRequery. + CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + // Inflate the individual item layout. `false` does not attach it to the root. + return getLayoutInflater().inflate(R.layout.bookmarks_database_view_linearlayout, parent, false); + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHandler.IS_FOLDER)) == 1); + + // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`. + int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHandler._ID)); + TextView bookmarkDatabaseIdTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_database_id); + bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId)); + + // Get the favorite icon byte array from the `Cursor`. + byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON)); + // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last. + Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); + // Display the bitmap in `bookmarkFavoriteIcon`. + ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmarks_database_view_favorite_icon); + bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap); + + // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`. + String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)); + TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_bookmark_name); + bookmarkNameTextView.setText(bookmarkNameString); + // Make the font bold for folders. + if (isFolder) { + // The first argument is `null` because we don't want to chage the font. + bookmarkNameTextView.setTypeface(null, Typeface.BOLD); + } else { // Reset the font to default. + bookmarkNameTextView.setTypeface(Typeface.DEFAULT); + } + + // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`. + int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHandler.DISPLAY_ORDER)); + TextView bookmarkDisplayOrderTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_display_order); + bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder)); + + // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`. + String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.PARENT_FOLDER)); + TextView bookmarkParentFolderTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_parent_folder); + // Make the folder name gray if it is the home folder. + if (bookmarkParentFolder.isEmpty()) { + bookmarkParentFolderTextView.setText(R.string.home_folder); + bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.grey)); + } else { + bookmarkParentFolderTextView.setText(bookmarkParentFolder); + bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black)); + } + + // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`. + String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_URL)); + TextView bookmarkUrlTextView = (TextView) view.findViewById(R.id.bookmarks_database_view_bookmark_url); + bookmarkUrlTextView.setText(bookmarkUrlString); + if (isFolder) { + bookmarkUrlTextView.setVisibility(View.GONE); + } else { + bookmarkUrlTextView.setVisibility(View.VISIBLE); + } + } + }; + + // Update the ListView. + bookmarksListView.setAdapter(bookmarksCursorAdapter); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacybrowser/CreateBookmark.java b/app/src/main/java/com/stoutner/privacybrowser/CreateBookmark.java index c5acd035..b779709c 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/CreateBookmark.java +++ b/app/src/main/java/com/stoutner/privacybrowser/CreateBookmark.java @@ -36,9 +36,9 @@ import android.widget.EditText; public class CreateBookmark extends DialogFragment { // The public interface is used to send information back to the parent activity. public interface CreateBookmarkListener { - void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment); + void onCancelCreateBookmark(DialogFragment dialogFragment); - void onCreateBookmarkCreate(DialogFragment createBookmarkDialogFragment); + void onCreateBookmark(DialogFragment dialogFragment); } // `createBookmarkListener` is used in `onAttach()` and `onCreateDialog()` @@ -48,7 +48,7 @@ public class CreateBookmark extends DialogFragment { public void onAttach(Activity parentActivity) { super.onAttach(parentActivity); - // Get a handle for `CreateBookmarkListener` from the `parentActivity`. + // Get a handle for `CreateBookmarkListener` from `parentActivity`. try { createBookmarkListener = (CreateBookmarkListener) parentActivity; } catch(ClassCastException exception) { @@ -62,51 +62,51 @@ public class CreateBookmark extends DialogFragment { Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon); // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text. - AlertDialog.Builder createBookmarkDialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); - createBookmarkDialogBuilder.setTitle(R.string.create_bookmark); - createBookmarkDialogBuilder.setIcon(favoriteIconDrawable); - // The parent view is `null` because it will be assigned by `AlertDialog`. - createBookmarkDialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_dialog, null)); + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); + dialogBuilder.setTitle(R.string.create_bookmark); + dialogBuilder.setIcon(favoriteIconDrawable); + // The parent view is `null` because it will be assigned by the `AlertDialog`. + dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_dialog, null)); // Set an `onClick()` listener for the negative button. - createBookmarkDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Return the `DialogFragment` to the parent activity on cancel. - createBookmarkListener.onCreateBookmarkCancel(CreateBookmark.this); + createBookmarkListener.onCancelCreateBookmark(CreateBookmark.this); } }); // Set an `onClick()` listener for the positive button. - createBookmarkDialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() { + dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Return the `DialogFragment` to the parent activity on create. - createBookmarkListener.onCreateBookmarkCreate(CreateBookmark.this); + createBookmarkListener.onCreateBookmark(CreateBookmark.this); } }); // Create an `AlertDialog` from the `AlertDialog.Builder`. - final AlertDialog createBookmarkDialog = createBookmarkDialogBuilder.create(); + final AlertDialog alertDialog = dialogBuilder.create(); // Show the keyboard when the `Dialog` is displayed on the screen. - createBookmarkDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below. - createBookmarkDialog.show(); + alertDialog.show(); // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_name_edittext`. - EditText createBookmarkNameEditText = (EditText) createBookmarkDialog.findViewById(R.id.create_bookmark_name_edittext); - assert createBookmarkNameEditText != null; // Remove the warning below that createBookmarkNameEditText might be null. + EditText createBookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_name_edittext); + assert createBookmarkNameEditText != null; // Remove the warning below that `createBookmarkNameEditText` might be `null`. createBookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { - // If the event is a key-down on the `enter` button, select the PositiveButton `Create`. + // If the event is a key-down on the `enter` button, select the `PositiveButton` `Create`. if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { - // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity. - createBookmarkListener.onCreateBookmarkCreate(CreateBookmark.this); + // Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity. + createBookmarkListener.onCreateBookmark(CreateBookmark.this); // Manually dismiss the `AlertDialog`. - createBookmarkDialog.dismiss(); + alertDialog.dismiss(); // Consume the event. return true; } else { // If any other key was pressed, do not consume the event. @@ -116,8 +116,8 @@ public class CreateBookmark extends DialogFragment { }); // Set the formattedUrlString as the initial text of `create_bookmark_url_edittext`. - EditText createBookmarkUrlEditText = (EditText) createBookmarkDialog.findViewById(R.id.create_bookmark_url_edittext); - assert createBookmarkUrlEditText != null;// Remove the warning below that `createBookmarkUrlEditText` might be null. + EditText createBookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_url_edittext); + assert createBookmarkUrlEditText != null;// Remove the warning below that `createBookmarkUrlEditText` might be `null`. createBookmarkUrlEditText.setText(MainWebViewActivity.formattedUrlString); // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`. @@ -126,9 +126,9 @@ public class CreateBookmark extends DialogFragment { // If the event is a key-down on the "enter" button, select the PositiveButton "Create". if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity. - createBookmarkListener.onCreateBookmarkCreate(CreateBookmark.this); + createBookmarkListener.onCreateBookmark(CreateBookmark.this); // Manually dismiss the `AlertDialog`. - createBookmarkDialog.dismiss(); + alertDialog.dismiss(); // Consume the event. return true; } else { // If any other key was pressed, do not consume the event. @@ -138,6 +138,6 @@ public class CreateBookmark extends DialogFragment { }); // `onCreateDialog()` requires the return of an `AlertDialog`. - return createBookmarkDialog; + return alertDialog; } } \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacybrowser/CreateBookmarkFolder.java b/app/src/main/java/com/stoutner/privacybrowser/CreateBookmarkFolder.java new file mode 100644 index 00000000..0847ce3f --- /dev/null +++ b/app/src/main/java/com/stoutner/privacybrowser/CreateBookmarkFolder.java @@ -0,0 +1,120 @@ +/** + * Copyright 2016 Soren Stoutner . + * + * This file is part of Privacy Browser . + * + * Privacy Browser is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser. If not, see . + */ + +package com.stoutner.privacybrowser; + +import android.app.Activity; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.os.Bundle; +// If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard. +import android.support.v7.app.AlertDialog; +import android.view.KeyEvent; +import android.view.View; +import android.view.WindowManager; +import android.widget.EditText; +import android.widget.ImageView; + +public class CreateBookmarkFolder extends DialogFragment { + // The public interface is used to send information back to the parent activity. + public interface CreateBookmarkFolderListener { + void onCancelCreateBookmarkFolder(DialogFragment dialogFragment); + + void onCreateBookmarkFolder(DialogFragment dialogFragment); + } + + // `createBookmarkFolderListener` is used in `onAttach()` and `onCreateDialog`. + private CreateBookmarkFolderListener createBookmarkFolderListener; + + public void onAttach(Activity parentActivity) { + super.onAttach(parentActivity); + + // Get a handle for `createBookmarkFolderListener` from `parentActivity`. + try { + createBookmarkFolderListener = (CreateBookmarkFolderListener) parentActivity; + } catch(ClassCastException exception) { + throw new ClassCastException(parentActivity.toString() + " must implement CreateBookmarkFolderListener."); + } + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text. + final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); + dialogBuilder.setTitle(R.string.create_folder); + // The parent view is `null` because it will be assigned by the `AlertDialog`. + dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_folder_dialog, null)); + + // Set an `onClick()` listener for the negative button. + dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Return the `DialogFragment` to the parent activity on cancel. + createBookmarkFolderListener.onCancelCreateBookmarkFolder(CreateBookmarkFolder.this); + } + }); + + // Set an `onClick()` listener fo the positive button. + dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Return the `DialogFragment` to the parent activity on create. + createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolder.this); + } + }); + + + // Create an `AlertDialog` from the `AlertDialog.Builder`. + final AlertDialog alertDialog = dialogBuilder.create(); + + // Show the keyboard when the `Dialog` is displayed on the screen. + alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + + // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below. + alertDialog.show(); + + // Allow the `enter` key on the keyboard to create the folder from `create_folder_name_edittext`. + EditText createFolderNameEditText = (EditText) alertDialog.findViewById(R.id.create_folder_name_edittext); + assert createFolderNameEditText != null; // Remove the warning below that `createFolderNameEditText` might be `null`. + createFolderNameEditText.setOnKeyListener(new View.OnKeyListener() { + public boolean onKey(View v, int keyCode, KeyEvent event) { + // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`. + if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { + // Trigger `createBookmarkFolderListener` and return the `DialogFragment` to the parent activity. + createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolder.this); + // Manually dismiss the `AlertDialog`. + alertDialog.dismiss(); + // Consume the event. + return true; + } else { // If any other key was pressed do not consume the event. + return false; + } + } + }); + + // Display the current favorite icon. + ImageView webPageIconImageView = (ImageView) alertDialog.findViewById(R.id.create_folder_web_page_icon); + assert webPageIconImageView != null; // Remove the warning that `webPageIconImageView` may be null. + webPageIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon); + + // `onCreateDialog()` requires the return of an `AlertDialog`. + return alertDialog; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacybrowser/CreateHomeScreenShortcut.java b/app/src/main/java/com/stoutner/privacybrowser/CreateHomeScreenShortcut.java index 71e84ab3..4a6b2fa7 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/CreateHomeScreenShortcut.java +++ b/app/src/main/java/com/stoutner/privacybrowser/CreateHomeScreenShortcut.java @@ -37,9 +37,9 @@ import android.widget.EditText; public class CreateHomeScreenShortcut extends DialogFragment { // The public interface is used to send information back to the parent activity. public interface CreateHomeScreenSchortcutListener { - void onCreateHomeScreenShortcutCancel(DialogFragment dialogFragment); + void onCancelCreateHomeScreenShortcut(DialogFragment dialogFragment); - void onCreateHomeScreenShortcutCreate(DialogFragment dialogFragment); + void onCreateHomeScreenShortcut(DialogFragment dialogFragment); } //createHomeScreenShortcutListener is used in onAttach and and onCreateDialog. @@ -64,50 +64,50 @@ public class CreateHomeScreenShortcut extends DialogFragment { LayoutInflater customDialogInflater = getActivity().getLayoutInflater(); // Use AlertDialog.Builder to create the AlertDialog. The style formats the color of the button text. - AlertDialog.Builder createHomeScreenShorcutDialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); - createHomeScreenShorcutDialogBuilder.setTitle(R.string.create_shortcut); - createHomeScreenShorcutDialogBuilder.setIcon(favoriteIconDrawable); + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); + dialogBuilder.setTitle(R.string.create_shortcut); + dialogBuilder.setIcon(favoriteIconDrawable); // The parent view is "null" because it will be assigned by AlertDialog. - createHomeScreenShorcutDialogBuilder.setView(customDialogInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null)); + dialogBuilder.setView(customDialogInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null)); // Set an onClick listener on the negative button. - createHomeScreenShorcutDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - createHomeScreenShortcutListener.onCreateHomeScreenShortcutCancel(CreateHomeScreenShortcut.this); + createHomeScreenShortcutListener.onCancelCreateHomeScreenShortcut(CreateHomeScreenShortcut.this); } }); // Set an onClick listener on the positive button. - createHomeScreenShorcutDialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() { + dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - createHomeScreenShortcutListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this); + createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcut.this); } }); // Create an AlertDialog from the AlertDialogBuilder. - final AlertDialog createHomeScreenShortcutAlertDialog = createHomeScreenShorcutDialogBuilder.create(); + final AlertDialog alertDialog = dialogBuilder.create(); // Show the keyboard when the Dialog is displayed on the screen. - createHomeScreenShortcutAlertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); // We need to show the AlertDialog before we can call setOnKeyListener() below. - createHomeScreenShortcutAlertDialog.show(); + alertDialog.show(); // Allow the "enter" key on the keyboard to create the shortcut. - EditText shortcutNameEditText = (EditText) createHomeScreenShortcutAlertDialog.findViewById(R.id.shortcut_name_edittext); + EditText shortcutNameEditText = (EditText) alertDialog.findViewById(R.id.shortcut_name_edittext); assert shortcutNameEditText != null; // Remove the warning below that shortcutNameEditText might be null. shortcutNameEditText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down on the "enter" button, select the PositiveButton "Create". if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Trigger the create listener. - createHomeScreenShortcutListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this); + createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcut.this); - // Manually dismiss the AlertDialog. - createHomeScreenShortcutAlertDialog.dismiss(); + // Manually dismiss `alertDialog`. + alertDialog.dismiss(); // Consume the event. return true; @@ -118,6 +118,6 @@ public class CreateHomeScreenShortcut extends DialogFragment { }); // onCreateDialog requires the return of an AlertDialog. - return createHomeScreenShortcutAlertDialog; + return alertDialog; } } \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacybrowser/EditBookmark.java b/app/src/main/java/com/stoutner/privacybrowser/EditBookmark.java index 8eee58d6..8343ace2 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/EditBookmark.java +++ b/app/src/main/java/com/stoutner/privacybrowser/EditBookmark.java @@ -40,9 +40,9 @@ import android.widget.ImageView; public class EditBookmark extends DialogFragment { // The public interface is used to send information back to the parent activity. public interface EditBookmarkListener { - void onEditBookmarkCancel(DialogFragment editBookmarkDialogFragment); + void onCancelEditBookmark(DialogFragment dialogFragment); - void onEditBookmarkSave(DialogFragment editBookmarkDialogFragment); + void onSaveEditBookmark(DialogFragment dialogFragment); } // `editBookmarkListener` is used in `onAttach()` and `onCreateDialog()` @@ -51,7 +51,7 @@ public class EditBookmark extends DialogFragment { public void onAttach(Activity parentActivity) { super.onAttach(parentActivity); - // Get a handle for `EditBookmarkListener` from the `parentActivity`. + // Get a handle for `EditBookmarkListener` from `parentActivity`. try { editBookmarkListener = (EditBookmarkListener) parentActivity; } catch(ClassCastException exception) { @@ -62,62 +62,63 @@ public class EditBookmark extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`. - long[] selectedBookmarksLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds(); - int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0]; + long[] selectedBookmarkLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds(); + int selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0]; // Get a `Cursor` with the specified bookmark and move it to the first position. Cursor bookmarkCursor = BookmarksActivity.bookmarksDatabaseHandler.getBookmarkCursor(selectedBookmarkDatabaseId); bookmarkCursor.moveToFirst(); - // Get the favorite icon byte array from the `Cursor`. - byte[] favoriteIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON)); - // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last. - Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); - // Convert the `Bitmap` to a `Drawable`. - Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), favoriteIconBitmap); - // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text. - AlertDialog.Builder editBookmarkDialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); - editBookmarkDialogBuilder.setTitle(R.string.edit_bookmark); - editBookmarkDialogBuilder.setIcon(favoriteIconDrawable); + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); + dialogBuilder.setTitle(R.string.edit_bookmark); // The parent view is `null` because it will be assigned by `AlertDialog`. - editBookmarkDialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_dialog, null)); + dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_dialog, null)); // Set an `onClick()` listener for the negative button. - editBookmarkDialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() { + dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Return the `DialogFragment` to the parent activity on cancel. - editBookmarkListener.onEditBookmarkCancel(EditBookmark.this); + editBookmarkListener.onCancelEditBookmark(EditBookmark.this); } }); // Set the `onClick()` listener fo the positive button. - editBookmarkDialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() { + dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Return the `DialogFragment` to the parent activity on save. - editBookmarkListener.onEditBookmarkSave(EditBookmark.this); + editBookmarkListener.onSaveEditBookmark(EditBookmark.this); } }); // Create an `AlertDialog` from the `AlertDialog.Builder`. - final AlertDialog editBookmarkDialog = editBookmarkDialogBuilder.create(); + final AlertDialog alertDialog = dialogBuilder.create(); // Show the keyboard when the `Dialog` is displayed on the screen. - editBookmarkDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); - // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below. - editBookmarkDialog.show(); + // We need to show the `AlertDialog` before we can modify items in the layout. + alertDialog.show(); - // Get a `Drawable` of the favorite icon from `mainWebView` and display it in `edit_bookmark_new_favorite_icon`. - ImageView newFavoriteIcon = (ImageView) editBookmarkDialog.findViewById(R.id.edit_bookmark_new_favorite_icon); - assert newFavoriteIcon != null; // Remove the warning below that `newFavoriteIcon` might be null. - newFavoriteIcon.setImageBitmap(MainWebViewActivity.favoriteIcon); + // Get the current favorite icon byte array from the `Cursor`. + byte[] currentIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON)); + // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last. + Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length); + // Display `currentIconBitmap` in `edit_bookmark_current_icon`. + ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_current_icon); + assert currentIconImageView != null; // Remove the warning below that `currentIconImageView` might be null; + currentIconImageView.setImageBitmap(currentIconBitmap); + + // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_bookmark_web_page_favorite_icon`. + ImageView newFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_bookmark_web_page_favorite_icon); + assert newFavoriteIconImageView != null; // Remove the warning below that `newFavoriteIcon` might be null. + newFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon); // Load the text for `edit_bookmark_name_edittext`. - EditText bookmarkNameEditText = (EditText) editBookmarkDialog.findViewById(R.id.edit_bookmark_name_edittext); + EditText bookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_name_edittext); assert bookmarkNameEditText != null; // Remove the warning below that `bookmarkNameEditText` might be null. bookmarkNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME))); @@ -127,9 +128,9 @@ public class EditBookmark extends DialogFragment { // If the event is a key-down on the "enter" button, select the PositiveButton `Save`. if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity. - editBookmarkListener.onEditBookmarkSave(EditBookmark.this); - // Manually dismiss the `AlertDialog`. - editBookmarkDialog.dismiss(); + editBookmarkListener.onSaveEditBookmark(EditBookmark.this); + // Manually dismiss `alertDialog`. + alertDialog.dismiss(); // Consume the event. return true; } else { // If any other key was pressed, do not consume the event. @@ -138,8 +139,8 @@ public class EditBookmark extends DialogFragment { } }); - // Load the text for `create_bookmark_url_edittext`. - EditText bookmarkUrlEditText = (EditText) editBookmarkDialog.findViewById(R.id.edit_bookmark_url_edittext); + // Load the text for `edit_bookmark_url_edittext`. + EditText bookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.edit_bookmark_url_edittext); assert bookmarkUrlEditText != null;// Remove the warning below that `bookmarkUrlEditText` might be null. bookmarkUrlEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_URL))); @@ -149,9 +150,9 @@ public class EditBookmark extends DialogFragment { // If the event is a key-down on the `enter` button, select the PositiveButton `Save`. if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity. - editBookmarkListener.onEditBookmarkSave(EditBookmark.this); + editBookmarkListener.onSaveEditBookmark(EditBookmark.this); // Manually dismiss the `AlertDialog`. - editBookmarkDialog.dismiss(); + alertDialog.dismiss(); // Consume the event. return true; } else { // If any other key was pressed, do not consume the event. @@ -161,6 +162,6 @@ public class EditBookmark extends DialogFragment { }); // `onCreateDialog` requires the return of an `AlertDialog`. - return editBookmarkDialog; + return alertDialog; } } \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java b/app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java new file mode 100644 index 00000000..c27b963d --- /dev/null +++ b/app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java @@ -0,0 +1,143 @@ +/** + * Copyright 2016 Soren Stoutner . + * + * This file is part of Privacy Browser . + * + * Privacy Browser is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser. If not, see . + */ + +package com.stoutner.privacybrowser; + +import android.app.Activity; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Bundle; +// If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard. +import android.support.v7.app.AlertDialog; +import android.view.KeyEvent; +import android.view.View; +import android.view.WindowManager; +import android.widget.EditText; +import android.widget.ImageView; + +public class EditBookmarkFolder extends DialogFragment { + // The public interface is used to send information back to the parent activity. + public interface EditBookmarkFolderListener { + void onCancelEditBookmarkFolder(DialogFragment dialogFragment); + + void onSaveEditBookmarkFolder(DialogFragment dialogFragment); + } + + // `editFolderListener` is used in `onAttach()` and `onCreateDialog`. + private EditBookmarkFolderListener editBookmarkFolderListener; + + public void onAttach(Activity parentActivity) { + super.onAttach(parentActivity); + + // Get a handle for `EditFolderListener` from `parentActivity`. + try { + editBookmarkFolderListener = (EditBookmarkFolderListener) parentActivity; + } catch(ClassCastException exception) { + throw new ClassCastException(parentActivity.toString() + " must implement EditBookmarkFolderListener."); + } + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`. + long[] selectedBookmarkLongArray = BookmarksActivity.bookmarksListView.getCheckedItemIds(); + int selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0]; + + // Get a `Cursor` with the specified bookmark and move it to the first position. + Cursor bookmarkCursor = BookmarksActivity.bookmarksDatabaseHandler.getBookmarkCursor(selectedBookmarkDatabaseId); + bookmarkCursor.moveToFirst(); + + // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text. + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); + dialogBuilder.setTitle(R.string.edit_folder); + // The parent view is `null` because it will be assigned by `AlertDialog`. + dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.edit_bookmark_folder_dialog, null)); + + // Set an `onClick()` listener for the negative button. + dialogBuilder.setNegativeButton(R.string.cancel, new Dialog.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Return the `DialogFragment` to the parent activity on cancel. + editBookmarkFolderListener.onCancelEditBookmarkFolder(EditBookmarkFolder.this); + } + }); + + // Set the `onClick()` listener fo the positive button. + dialogBuilder.setPositiveButton(R.string.save, new Dialog.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Return the `DialogFragment` to the parent activity on save. + editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this); + } + }); + + + // Create an `AlertDialog` from the `AlertDialog.Builder`. + final AlertDialog alertDialog = dialogBuilder.create(); + + // Show the keyboard when the `Dialog` is displayed on the screen. + alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + + // We need to show the `AlertDialog` before we can modify items in the layout. + alertDialog.show(); + + // Get the current favorite icon byte array from the `Cursor`. + byte[] currentIconByteArray = bookmarkCursor.getBlob(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON)); + // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last. + Bitmap currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.length); + // Display `currentIconBitmap` in `edit_folder_current_icon`. + ImageView currentIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_current_icon); + assert currentIconImageView != null; // Remove the warning below that `currentIconImageView` might be null. + currentIconImageView.setImageBitmap(currentIconBitmap); + + // Get a `Bitmap` of the favorite icon from `MainWebViewActivity` and display it in `edit_folder_web_page_favorite_icon`. + ImageView webPageFavoriteIcon = (ImageView) alertDialog.findViewById(R.id.edit_folder_web_page_favorite_icon); + assert webPageFavoriteIcon != null; // Remove the warning below that `webPageFavoriteIcon` might be null. + webPageFavoriteIcon.setImageBitmap(MainWebViewActivity.favoriteIcon); + + // Load the text for `edit_folder_name_edittext`. + EditText folderNameEditText = (EditText) alertDialog.findViewById(R.id.edit_folder_name_edittext); + assert folderNameEditText != null; // Remove the warning below that `bookmarkNameEditText` might be null. + folderNameEditText.setText(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME))); + + // Allow the `enter` key on the keyboard to save the bookmark from `edit_bookmark_name_edittext`. + folderNameEditText.setOnKeyListener(new View.OnKeyListener() { + public boolean onKey(View v, int keyCode, KeyEvent event) { + // If the event is a key-down on the "enter" button, select the PositiveButton `Save`. + if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { + // Trigger `editBookmarkListener` and return the DialogFragment to the parent activity. + editBookmarkFolderListener.onSaveEditBookmarkFolder(EditBookmarkFolder.this); + // Manually dismiss the `AlertDialog`. + alertDialog.dismiss(); + // Consume the event. + return true; + } else { // If any other key was pressed, do not consume the event. + return false; + } + } + }); + + // `onCreateDialog` requires the return of an `AlertDialog`. + return alertDialog; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java index e59168fe..f10c2e45 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java @@ -65,7 +65,7 @@ import java.net.URLEncoder; // We need to use AppCompatActivity from android.support.v7.app.AppCompatActivity to have access to the SupportActionBar until the minimum API is >= 21. public class MainWebViewActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener { - // `favoriteIcon` is public static so it can be accessed from `CreateHomeScreenShortcut`, `BookmarksActivity`, and `EditBookmark`. + // `favoriteIcon` is public static so it can be accessed from `CreateHomeScreenShortcut`, `BookmarksActivity`, `CreateBookmark`, `CreateBookmarkFolder`, and `EditBookmark`. // It is also used in `onCreate()` and `onCreateHomeScreenShortcutCreate()`. public static Bitmap favoriteIcon; @@ -629,7 +629,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation case R.id.addToHomescreen: // Show the CreateHomeScreenShortcut AlertDialog and name this instance "@string/create_shortcut". DialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcut(); - createHomeScreenShortcutDialogFragment.show(getFragmentManager(), "@string/create_shortcut"); + createHomeScreenShortcutDialogFragment.show(getFragmentManager(), getResources().getString(R.string.create_shortcut)); //Everything else will be handled by CreateHomeScreenShortcut and the associated listeners below. return true; @@ -684,19 +684,19 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation break; case R.id.settings: - // Launch SettingsActivity. + // Launch `SettingsActivity`. Intent settingsIntent = new Intent(this, SettingsActivity.class); startActivity(settingsIntent); break; case R.id.guide: - // Launch GuideActivity. + // Launch `GuideActivity`. Intent guideIntent = new Intent(this, GuideActivity.class); startActivity(guideIntent); break; case R.id.about: - // Launch AboutActivity. + // Launch `AboutActivity`. Intent aboutIntent = new Intent(this, AboutActivity.class); startActivity(aboutIntent); break; @@ -765,12 +765,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation } @Override - public void onCreateHomeScreenShortcutCancel(DialogFragment dialogFragment) { + public void onCancelCreateHomeScreenShortcut(DialogFragment dialogFragment) { // Do nothing because the user selected "Cancel". } @Override - public void onCreateHomeScreenShortcutCreate(DialogFragment dialogFragment) { + public void onCreateHomeScreenShortcut(DialogFragment dialogFragment) { // Get shortcutNameEditText from the alert dialog. EditText shortcutNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.shortcut_name_edittext); diff --git a/app/src/main/java/com/stoutner/privacybrowser/MoveToFolder.java b/app/src/main/java/com/stoutner/privacybrowser/MoveToFolder.java new file mode 100644 index 00000000..2ae2cc65 --- /dev/null +++ b/app/src/main/java/com/stoutner/privacybrowser/MoveToFolder.java @@ -0,0 +1,25 @@ +/** + * Copyright 2016 Soren Stoutner . + * + * This file is part of Privacy Browser . + * + * Privacy Browser is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser. If not, see . + */ + +package com.stoutner.privacybrowser; + +import android.app.DialogFragment; + +public class MoveToFolder extends DialogFragment { +} diff --git a/app/src/main/res/drawable-hdpi/folder_blue_bitmap.png b/app/src/main/res/drawable-hdpi/folder_blue_bitmap.png new file mode 100644 index 0000000000000000000000000000000000000000..0b5f491180076a3715a20bda96e0f6111f9aaac3 GIT binary patch literal 613 zcmeAS@N?(olHy`uVBq!ia0vp^4nSywFr#OX$_JpJWQl7;NpOBzNqJ&XDnogBxn5>oc5!lIL8@MUQTpt6Hc|`>jE_8B z978f1-%j7}9~>xh-2SktPLCQ?EoKO~xrm3Sn+1!pv(M^V=kcGR;L?U& z-U5f1&TKjHp~r>r)4UU}A;q3vGH`xQ(5?G&u89$HbWZnV?-_l`TedS*Xn=(B#! zy|%WJq4vSm^?YaE^lUSGp5~Fi(dOZvHEJ^|ADEc#v^L)}HQ`x(p{D-N7UTETmeH@Z z>#AoJyxVHgYS?@y?_-^2^qs1t3ALOr^+U28j%oZC_%X+2f|k2q$>X?XxtY)Jewcr( z6e#z~!HTmp`Vhb4r>oz0uK(~}TR?(e^zWr`jr~pgg$tNUB)lU7yyyOMSM+me*1NBl z@2tlVS}9q=$kE)l?C*y=o|h8+86K#-;pF%vQ_vaFu(iQ-BSWvZK~56mHKuDvm^uhB zWfOE0e02^)9heot@L;Vf&qux=xw(la<~iTkvdtjnZs+vMY;Nr%n;)zy-kEyAHGAd~ z!5<;Iy=yEQ)>S<#^-%5jU>N-V%;BlY1=}|Kjrn+4;jzQ}tG~-7M1L$*nq9Q)^VLfy dydEF3+#685aguD>PhgT@@O1TaS?83{1OWFa2a5mz literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-mdpi/folder_blue_bitmap.png b/app/src/main/res/drawable-mdpi/folder_blue_bitmap.png new file mode 100644 index 0000000000000000000000000000000000000000..2e8e77df5ddc3539eb897d6d15859ee8be445c3c GIT binary patch literal 480 zcmeAS@N?(olHy`uVBq!ia0vp^20*OF!3HFodL(yEr+qAXP8FD1G)j8z}|`##m1m z$B>BDw==E1nH@#k&d)Wys9^ST(y2qITn=8luW)1K0tGD&O_td@)2?-8h-UjHE=_v= z{>14+$DA&Bxpt_jRq57DX`1cZ##?-<@#iz^GjZF14xC!DZ_6?z%LEw-C5}UtMh|sE zwj1X+eq*;1ni{gNre}kQ*P^0{GT)w6sU}{kOEul{iQ&+j$KJn7avoP1Y&m0Z!1X$@ zUCSugf6u(HPEJ8}FC`^j2K`%Fvq3y;hJ)?3y8XK>XH@-)_5Qh3O@ROP1i1@rwjZOI zXK(*Kvt5JhPe7*Nd^4t`-JTAc3^Q{jZ|hn)hCEl+U0{%9vTMFTgZN8nh61K-hA!1? zzaFzG^$Kt3l}@^C!FYRz{{nh5r{G-97i@(|3O#)w%1}tjORyA@pV9`rjr2r?^^w-|bWu_{$Pr7IyD% TclQopY%qAb`njxgN@xNAyvf1f literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/folder_blue_bitmap.png b/app/src/main/res/drawable-xhdpi/folder_blue_bitmap.png new file mode 100644 index 0000000000000000000000000000000000000000..ab7a3083fe349b8b3d70e4f75e7e87ced7006a7c GIT binary patch literal 895 zcmeAS@N?(olHy`uVBq!ia0vp^2|(=2!3HEFS0!EmQY^(zo*^7SP{WbZ0pxQQctjR6 zFeqApFr(`oc5!lIL8@MUQTpt6Hc|`>On*FG z978JRyuEwcJETyA{loj8y~ZM`DR#%>wg_w%i}F}qwySg2Qm237sXsEVo@BbJuw>-q4E`SW?_^Pa`c(EomVXM44|@`**C z&SbO}X9!Q7VRfZzkyB&Aw&G&fM#Ja&CaS-r(m(y&{ZnnV-PSu?uHUbgF<#8Y-m^|AJ<@$RVh85~2}6G~*4QRyhB?9EOYVjKsTY@Lcx5V58FlyHo*r2h zaT$h?rSWsbE=pLRk-w6y@h2tyyNjmviK@xAvB|1&Z0i}OY&g8e*F8q#(w}9^-!WUC ztSXFS)4d@8G-iq7<7(&D66+!^ne_G_-e0A64)9H!yF_)dXNyyC=e_3j_0?fpl5_YDT(eQ*R*+@993ZYs1LhqSyb|qdv-%ja87XP_Qh}Y zzcZOJ>O3vz7H?dt`bYABSkVH9pyz!zSOhHZ=WmsDSfb<`#xV8GHl`I@u9PuMzrivg zk})NWVdh%~quC5b+6`y86gJB`qzOBuu|j2Z6A>~@C+6G`TDFN(VKdW-HDKi{brZGV z8ktVKDK(kt%P>gcyqV(DCY@~n&@p`&A zhE&XXd&f2>x>SVqL-Bup^^MK}qKZfTlo^}!mK;3PHRI6qD+>iZmMFaFkVr^&@V$KC ziJ-KEfHaQ}kH&;YidibtrZwt02(xc)U=b?3)1mmveNO+eeLNbfy1ovF>depD?$@k4 z?{5{wd8U8b!k!uPIxk)mq9 zBU3hI|L&y>-}iqGR@=DMl=(s7Vw<&#HNKX)uiwB@y*sj@ar-`BGa=jOjJ1ah{8!I? z9p^po!SRFd4tZ&V=P#4_NGRhuYRTWG{xWmt!Q1|A*0GN4 z{Y?DbZQUqzZ z4{p9VXz z1KTwk&NIb4W+~`n1taT*;yDcGHo{OLXGx#%0ZTC$(rY|d$YgOGj#MuIje8yeLpxZk zj&nYc;Rd5}hUXTHbCO}`alflILX!JIOn_eFIuyf>A4D=-_5f~^f#y$lKrx(~!EBB5 zgqghz3)1E`oe5!H5c*mqWto)1!=_TMO^OG8F1V!;$*w3Z6(HoGYP_+4)8U$xz){cV zSx1i-F$%Q4T2d<0HC@4nVG$!k*89|S#<|=0Tt5mNyb@ISB0J%D?}D{gY}acAZ0mM4 zoSAR(W$RmUbM6oBbswr&YWWWA@zm*RJjeRt^Xd2L;oJM~6lw2>I-{4jbdexK{E>OR zx4ah4H=ixBQfza#b^rTcu?_z|elq0MU^ubWR(9*o3d-yKb}AGcnx%G%23{T^O+RVDNPH Kb6Mw<&;$Tx8uKOq literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable/back.xml b/app/src/main/res/drawable/back.xml index e69b5ad5..a49efd8b 100644 --- a/app/src/main/res/drawable/back.xml +++ b/app/src/main/res/drawable/back.xml @@ -3,11 +3,11 @@ + android:viewportWidth="24.0" + android:autoMirrored="true" > . --> + + + + + diff --git a/app/src/main/res/drawable/edit.xml b/app/src/main/res/drawable/edit.xml index d5164881..89a86c32 100644 --- a/app/src/main/res/drawable/edit.xml +++ b/app/src/main/res/drawable/edit.xml @@ -3,11 +3,11 @@ + android:viewportWidth="24.0" + android:autoMirrored="true" > + android:viewportWidth="24.0" + android:autoMirrored="true" > . --> + + + + + diff --git a/app/src/main/res/drawable/forward.xml b/app/src/main/res/drawable/forward.xml index e8ccd370..dfa9e066 100644 --- a/app/src/main/res/drawable/forward.xml +++ b/app/src/main/res/drawable/forward.xml @@ -3,11 +3,11 @@ + android:viewportWidth="24.0" + android:autoMirrored="true" > . --> . --> . --> . --> . --> + + + + + diff --git a/app/src/main/res/layout/bookmarks_coordinatorlayout.xml b/app/src/main/res/layout/bookmarks_coordinatorlayout.xml index 783a19bc..4ebdaaa5 100644 --- a/app/src/main/res/layout/bookmarks_coordinatorlayout.xml +++ b/app/src/main/res/layout/bookmarks_coordinatorlayout.xml @@ -18,9 +18,9 @@ You should have received a copy of the GNU General Public License along with Privacy Browser. If not, see . --> - + - + - + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/bookmarks_database_view_linearlayout.xml b/app/src/main/res/layout/bookmarks_database_view_linearlayout.xml new file mode 100644 index 00000000..f1949e89 --- /dev/null +++ b/app/src/main/res/layout/bookmarks_database_view_linearlayout.xml @@ -0,0 +1,110 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/bookmarks_item_linearlayout.xml b/app/src/main/res/layout/bookmarks_item_linearlayout.xml index 408844fe..cbb12038 100644 --- a/app/src/main/res/layout/bookmarks_item_linearlayout.xml +++ b/app/src/main/res/layout/bookmarks_item_linearlayout.xml @@ -21,11 +21,11 @@ + android:background="@drawable/bookmarks_list_selector" > + android:layout_marginEnd="4dp" > + `android:inputType="textUri"` disables spell check in the EditText. --> + `android:inputType="textUri"` disables spell check in the EditText.--> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/create_home_screen_shortcut_dialog.xml b/app/src/main/res/layout/create_home_screen_shortcut_dialog.xml index 5e924111..11469aff 100644 --- a/app/src/main/res/layout/create_home_screen_shortcut_dialog.xml +++ b/app/src/main/res/layout/create_home_screen_shortcut_dialog.xml @@ -18,25 +18,29 @@ You should have received a copy of the GNU General Public License along with Privacy Browser. If not, see . --> - - - - + + - \ No newline at end of file + android:layout_marginBottom="10dp" + android:layout_marginStart="4dp" + android:layout_marginEnd="4dp" > + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/edit_bookmark_dialog.xml b/app/src/main/res/layout/edit_bookmark_dialog.xml index 1768ca08..7bb2e454 100644 --- a/app/src/main/res/layout/edit_bookmark_dialog.xml +++ b/app/src/main/res/layout/edit_bookmark_dialog.xml @@ -20,40 +20,66 @@ + android:orientation="vertical"> + - - - - + + + - - + + + + + + + + + android:layout_width="match_parent" + android:checkedButton="@+id/edit_bookmark_current_icon_radiobutton" > + + + + + @@ -61,7 +87,9 @@ android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginTop="12dp" - android:layout_marginBottom="6dp" > + android:layout_marginBottom="6dp" + android:layout_marginStart="4dp" + android:layout_marginEnd="4dp" > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/bookmarks_context_menu.xml b/app/src/main/res/menu/bookmarks_context_menu.xml index ba1139ee..1f880e39 100644 --- a/app/src/main/res/menu/bookmarks_context_menu.xml +++ b/app/src/main/res/menu/bookmarks_context_menu.xml @@ -36,24 +36,31 @@ android:icon="@drawable/move_bookmark_down_enabled" app:showAsAction="ifRoom" /> + + \ No newline at end of file diff --git a/app/src/main/res/menu/bookmarks_options_menu.xml b/app/src/main/res/menu/bookmarks_options_menu.xml index 856594ce..41b7b86f 100644 --- a/app/src/main/res/menu/bookmarks_options_menu.xml +++ b/app/src/main/res/menu/bookmarks_options_menu.xml @@ -22,10 +22,23 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 02fa590f..598c51b7 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -23,9 +23,11 @@ #FF000000 #FF1976D2 #FF0D47A1 - #FF607d8b + #FF607D8B + #FF9E9E9E #FF64DD17 #FFBBDEFB + #FF42A5F5 #FFD50000 #FFFFFFFF #FFFFD600 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a4a030e0..42396d59 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -70,18 +70,28 @@ Create + Database View Create bookmark + Create folder + Current folder icon + Default folder icon + Web page favorite icon + Folder name + Folder names must be unique + Cannot create the folder because the name is not unique: + Cannot rename the folder because the new name is not unique: Edit bookmark + Edit folder + Move to Folder Save Bookmark name Bookmark URL - New favorite icon - Use new icon + Current bookmark icon Selected Move Up - Move down + Move Down Edit Delete Select All @@ -89,6 +99,10 @@ Bookmarks Deleted Undo + + Bookmarks Database View + Home folder + Privacy Browser Guide Overview -- 2.45.2