From: Soren Stoutner Date: Wed, 17 Aug 2016 17:34:23 +0000 (-0700) Subject: Fix crashing when creating or editing bookmarks with no favorite icon. Fixes https... X-Git-Tag: v1.9~12 X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=commitdiff_plain;h=88279fa8a750a4ff24b093649159dc833cbce1fd Fix crashing when creating or editing bookmarks with no favorite icon. Fixes https://redmine.stoutner.com/issues/47 --- diff --git a/app/src/main/assets/about_licenses.html b/app/src/main/assets/about_licenses.html index f09fbb42..9bd93d46 100644 --- a/app/src/main/assets/about_licenses.html +++ b/app/src/main/assets/about_licenses.html @@ -57,6 +57,8 @@ are derived from ic_security and ic_language. Modifications were made by Soren Stoutner in 2016.

+

The following icons are unchanged except for layout information like color and size. Some of them have been renamed to match their use in the code. The original icons and names are shown below.

+

ic_language.

ic_home.

diff --git a/app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java b/app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java index 9b9bfb71..834889cb 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java @@ -33,7 +33,6 @@ import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.app.NavUtils; -import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; @@ -542,8 +541,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma 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). + // Convert the favoriteIcon Bitmap to a byte array. ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream(); + // `0` is for lossless compression (the only option for a PNG). MainWebViewActivity.favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream); byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray(); @@ -577,19 +577,20 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma 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. + // Get the new folder icon `Bitmap`. 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); + // Get the default folder icon `ImageView` from the `Dialog` and convert it to a `Bitmap`. + ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon); + Drawable folderIconDrawable = folderIconImageView.getDrawable(); BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable; folderIconBitmap = folderIconBitmapDrawable.getBitmap(); - } else { + } else { // Assign `favoriteIcon` from the `WebView`. folderIconBitmap = MainWebViewActivity.favoriteIcon; } - // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG). + // Convert `folderIconBitmap` 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(); @@ -630,15 +631,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma 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. + } else { // Update the bookmark using the `WebView` favorite icon. ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream(); - newFavoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream); + MainWebViewActivity.favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream); byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray(); // Update the bookmark and the favorite icon. @@ -676,11 +671,10 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // 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. + // Check if the favorite icon has changed. if (currentFolderIconRadioButton.isChecked()) { - // Update the folder name if it has changed. + // Update the folder name if it has changed without modifying the favorite icon. if (!newFolderNameString.equals(oldFolderNameString)) { bookmarksDatabaseHandler.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString); @@ -688,13 +682,16 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma updateBookmarksListView(currentFolder); bookmarksListView.setSelection(selectedBookmarkPosition); } - } else { // Prepare the new favorite icon. + } else { // Update the folder icon. + // Get the new folder icon `Bitmap`. + 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); + // Get the default folder icon `ImageView` from the `Drawable` and convert it to a `Bitmap`. + ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon); + Drawable folderIconDrawable = folderIconImageView.getDrawable(); BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable; folderIconBitmap = folderIconBitmapDrawable.getBitmap(); - } else { // Use the web page favorite icon. + } else { // Get the web page icon `ImageView` from the `Dialog`. folderIconBitmap = MainWebViewActivity.favoriteIcon; } diff --git a/app/src/main/java/com/stoutner/privacybrowser/EditBookmark.java b/app/src/main/java/com/stoutner/privacybrowser/EditBookmark.java index 8343ace2..61074703 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/EditBookmark.java +++ b/app/src/main/java/com/stoutner/privacybrowser/EditBookmark.java @@ -26,8 +26,6 @@ import android.content.DialogInterface; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; 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; diff --git a/app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java b/app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java index c27b963d..2becd6a1 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java +++ b/app/src/main/java/com/stoutner/privacybrowser/EditBookmarkFolder.java @@ -111,9 +111,9 @@ public class EditBookmarkFolder extends DialogFragment { 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); + ImageView webPageFavoriteIconImageView = (ImageView) alertDialog.findViewById(R.id.edit_folder_web_page_favorite_icon); + assert webPageFavoriteIconImageView != null; // Remove the warning below that `webPageFavoriteIcon` might be null. + webPageFavoriteIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon); // Load the text for `edit_folder_name_edittext`. EditText folderNameEditText = (EditText) alertDialog.findViewById(R.id.edit_folder_name_edittext); diff --git a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java index f10c2e45..bdafa3c9 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java @@ -27,12 +27,15 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; +import android.support.v4.content.ContextCompat; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.SwipeRefreshLayout; @@ -413,6 +416,14 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Load the initial website. mainWebView.loadUrl(formattedUrlString); + // Load the default favorite icon if it is null. + if (favoriteIcon == null) { + // We have to use `ContextCompat` until API >= 21. + Drawable favoriteIconDrawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.world); + BitmapDrawable favoriteIconBitmapDrawable = (BitmapDrawable) favoriteIconDrawable; + favoriteIcon = favoriteIconBitmapDrawable.getBitmap(); + } + // Initialize AdView for the free flavor and request an ad. If this is not the free flavor BannerAd.requestAd() does nothing. adView = findViewById(R.id.adView); BannerAd.requestAd(adView); diff --git a/app/src/main/res/drawable-hdpi/world.png b/app/src/main/res/drawable-hdpi/world.png new file mode 100644 index 00000000..fcfe815f Binary files /dev/null and b/app/src/main/res/drawable-hdpi/world.png differ diff --git a/app/src/main/res/drawable-mdpi/folder_dark_blue.xml b/app/src/main/res/drawable-mdpi/folder_dark_blue.xml deleted file mode 100644 index ffa94773..00000000 --- a/app/src/main/res/drawable-mdpi/folder_dark_blue.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - diff --git a/app/src/main/res/drawable-mdpi/world.png b/app/src/main/res/drawable-mdpi/world.png new file mode 100644 index 00000000..21b4cb22 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/world.png differ diff --git a/app/src/main/res/drawable-xhdpi/world.png b/app/src/main/res/drawable-xhdpi/world.png new file mode 100644 index 00000000..6ba09e64 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/world.png differ diff --git a/app/src/main/res/drawable-xxhdpi/world.png b/app/src/main/res/drawable-xxhdpi/world.png new file mode 100644 index 00000000..f6faee52 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/world.png differ diff --git a/app/src/main/res/drawable/folder_dark_blue.xml b/app/src/main/res/drawable/folder_dark_blue.xml new file mode 100644 index 00000000..ffa94773 --- /dev/null +++ b/app/src/main/res/drawable/folder_dark_blue.xml @@ -0,0 +1,14 @@ + + + + + + diff --git a/app/src/main/res/drawable/world.xml b/app/src/main/res/drawable/world.xml deleted file mode 100644 index a472dca2..00000000 --- a/app/src/main/res/drawable/world.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - \ No newline at end of file