From 85ed2da5182609fb092bb5a1bde247520e904801 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Sun, 31 May 2020 16:19:33 +0100 Subject: [PATCH] fixes for Play Store crash reports --- .../java/io/lbry/browser/MainActivity.java | 51 +++++--- .../io/lbry/browser/VerificationActivity.java | 6 +- .../dialog/RepostClaimDialogFragment.java | 16 ++- .../browser/dialog/SendTipDialogFragment.java | 12 +- .../tasks/localdata/LoadGalleryItemsTask.java | 28 ++--- .../ui/channel/ChannelFormFragment.java | 8 +- .../ui/findcontent/FileViewFragment.java | 7 +- .../ui/publish/PublishFormFragment.java | 110 ++++++++++-------- .../EmailVerificationFragment.java | 36 ++++-- .../browser/ui/wallet/WalletFragment.java | 21 +++- .../java/io/lbry/browser/utils/LbryUri.java | 4 + app/src/main/res/values/strings.xml | 3 + 12 files changed, 193 insertions(+), 109 deletions(-) diff --git a/app/src/main/java/io/lbry/browser/MainActivity.java b/app/src/main/java/io/lbry/browser/MainActivity.java index 71b05652..0a33dc3b 100644 --- a/app/src/main/java/io/lbry/browser/MainActivity.java +++ b/app/src/main/java/io/lbry/browser/MainActivity.java @@ -353,7 +353,11 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener @Override protected void onCreate(Bundle savedInstanceState) { // workaround to fix dark theme because https://issuetracker.google.com/issues/37124582 - new WebView(this); + try { + new WebView(this); + } catch (Exception ex) { + // pass (don't fail initialization on some _weird_ device implementations) + } AppCompatDelegate.setDefaultNightMode(isDarkMode() ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO); initKeyStore(); @@ -365,17 +369,20 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener initSpecialRouteMap(); LbryAnalytics.init(this); - FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(Task task) { - if (!task.isSuccessful()) { - return; + try { + FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(Task task) { + if (!task.isSuccessful()) { + return; + } + // Get new Instance ID token + firebaseMessagingToken = task.getResult().getToken(); } - - // Get new Instance ID token - firebaseMessagingToken = task.getResult().getToken(); - } - }); + }); + } catch (IllegalStateException ex) { + // pass + } super.onCreate(savedInstanceState); dbHelper = new DatabaseHelper(this); @@ -739,8 +746,13 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener public void openPublishesOnSuccessfulPublish() { // close publish form - getSupportFragmentManager().popBackStack(); - openFragment(PublishesFragment.class, true, NavMenuItem.ID_ITEM_PUBLISHES); + try { + getSupportFragmentManager().popBackStack(); + openFragment(PublishesFragment.class, true, NavMenuItem.ID_ITEM_PUBLISHES); + } catch (IllegalStateException ex) { + // pass + onBackPressed(); + } } public void openPublishForm(Claim claim) { @@ -2486,8 +2498,14 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener !startingSignInFlowActivity) { enteringPIPMode = true; PictureInPictureParams params = new PictureInPictureParams.Builder().build(); - enterPictureInPictureMode(params); - return true; + + try { + enterPictureInPictureMode(params); + return true; + } catch (IllegalStateException ex) { + // pass + enteringPIPMode = false; + } } return false; @@ -2933,6 +2951,9 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener } public static boolean hasPermission(String permission, Context context) { + if (context == null) { + return false; + } return (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED); } diff --git a/app/src/main/java/io/lbry/browser/VerificationActivity.java b/app/src/main/java/io/lbry/browser/VerificationActivity.java index 32324e8d..ac489ce3 100644 --- a/app/src/main/java/io/lbry/browser/VerificationActivity.java +++ b/app/src/main/java/io/lbry/browser/VerificationActivity.java @@ -177,7 +177,7 @@ public class VerificationActivity extends FragmentActivity implements SignInList @Override public void onError(Exception error) { - showFetchUserError(error.getMessage()); + showFetchUserError(error != null ? error.getMessage() : getString(R.string.fetch_current_user_error)); hideLoading(); } }); @@ -213,7 +213,7 @@ public class VerificationActivity extends FragmentActivity implements SignInList } @Override public void onError(Exception error) { - showFetchUserError(error.getMessage()); + showFetchUserError(error != null ? error.getMessage() : getString(R.string.fetch_current_user_error)); hideLoading(); } }); @@ -250,7 +250,7 @@ public class VerificationActivity extends FragmentActivity implements SignInList @Override public void onError(Exception error) { - showFetchUserError(error.getMessage()); + showFetchUserError(error != null ? error.getMessage() : getString(R.string.fetch_current_user_error)); hideLoading(); } }); diff --git a/app/src/main/java/io/lbry/browser/dialog/RepostClaimDialogFragment.java b/app/src/main/java/io/lbry/browser/dialog/RepostClaimDialogFragment.java index f34099ce..4cc84758 100644 --- a/app/src/main/java/io/lbry/browser/dialog/RepostClaimDialogFragment.java +++ b/app/src/main/java/io/lbry/browser/dialog/RepostClaimDialogFragment.java @@ -231,6 +231,11 @@ public class RepostClaimDialogFragment extends BottomSheetDialogFragment impleme } Claim channel = (Claim) channelSpinner.getSelectedItem(); + if (channel == null) { + showError(getString(R.string.please_select_repost_channel)); + return; + } + StreamRepostTask task = new StreamRepostTask(name, bid, claim.getClaimId(), channel.getClaimId(), repostProgress, new ClaimResultHandler() { @Override public void beforeStart() { @@ -256,10 +261,13 @@ public class RepostClaimDialogFragment extends BottomSheetDialogFragment impleme } private void showError(String message) { - Snackbar.make(getView(), message, Snackbar.LENGTH_LONG). - setBackgroundTint(Color.RED). - setTextColor(Color.WHITE). - show(); + View view = getView(); + if (view != null && !Helper.isNullOrEmpty(message)) { + Snackbar.make(view, message, Snackbar.LENGTH_LONG). + setBackgroundTint(Color.RED). + setTextColor(Color.WHITE). + show(); + } } private void startLoading() { diff --git a/app/src/main/java/io/lbry/browser/dialog/SendTipDialogFragment.java b/app/src/main/java/io/lbry/browser/dialog/SendTipDialogFragment.java index 6be2d917..7c9b7a3b 100644 --- a/app/src/main/java/io/lbry/browser/dialog/SendTipDialogFragment.java +++ b/app/src/main/java/io/lbry/browser/dialog/SendTipDialogFragment.java @@ -91,11 +91,13 @@ public class SendTipDialogFragment extends BottomSheetDialogFragment implements TextView infoText = view.findViewById(R.id.tip_info); infoText.setMovementMethod(LinkMovementMethod.getInstance()); - infoText.setText(HtmlCompat.fromHtml( - Claim.TYPE_CHANNEL.equalsIgnoreCase(claim.getValueType()) ? - getString(R.string.send_tip_info_channel, claim.getTitleOrName()) : - getString(R.string.send_tip_info_content, claim.getTitleOrName()), - HtmlCompat.FROM_HTML_MODE_LEGACY)); + if (claim != null) { + infoText.setText(HtmlCompat.fromHtml( + Claim.TYPE_CHANNEL.equalsIgnoreCase(claim.getValueType()) ? + getString(R.string.send_tip_info_channel, claim.getTitleOrName()) : + getString(R.string.send_tip_info_content, claim.getTitleOrName()), + HtmlCompat.FROM_HTML_MODE_LEGACY)); + } sendButton.setOnClickListener(new View.OnClickListener() { @Override diff --git a/app/src/main/java/io/lbry/browser/tasks/localdata/LoadGalleryItemsTask.java b/app/src/main/java/io/lbry/browser/tasks/localdata/LoadGalleryItemsTask.java index 1b2e8307..845f5ec5 100644 --- a/app/src/main/java/io/lbry/browser/tasks/localdata/LoadGalleryItemsTask.java +++ b/app/src/main/java/io/lbry/browser/tasks/localdata/LoadGalleryItemsTask.java @@ -55,20 +55,22 @@ public class LoadGalleryItemsTask extends AsyncTask params = getParams(); - if (params.containsKey("claim")) { + if (params == null) { + Context context = getContext(); + if (context instanceof MainActivity) { + ((MainActivity) context).onBackPressed(); + return; + } + } else if (params.containsKey("claim")) { Claim claim = (Claim) params.get("claim"); if (claim != null && !claim.equals(this.currentClaim)) { this.currentClaim = claim; diff --git a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java index f4c1674d..9abec9c3 100644 --- a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java @@ -780,7 +780,10 @@ public class FileViewFragment extends BaseFragment implements dialog.setListener(new RepostClaimDialogFragment.RepostClaimListener() { @Override public void onClaimReposted(Claim claim) { - Snackbar.make(root.findViewById(R.id.file_view_claim_display_area), R.string.content_successfully_reposted, Snackbar.LENGTH_LONG).show(); + Context context = getContext(); + if (context instanceof MainActivity) { + ((MainActivity) context).showMessage(R.string.content_successfully_reposted); + } } }); Context context = getContext(); @@ -1352,7 +1355,7 @@ public class FileViewFragment extends BaseFragment implements root.findViewById(R.id.file_view_exoplayer_container).setVisibility(View.GONE); root.findViewById(R.id.file_view_unsupported_container).setVisibility(View.VISIBLE); String fileNameString = ""; - if (claim.getFile() != null) { + if (claim.getFile() != null && !Helper.isNullOrEmpty(claim.getFile().getDownloadPath())) { LbryFile lbryFile = claim.getFile(); File file = new File(lbryFile.getDownloadPath()); fileNameString = String.format("\"%s\" ", file.getName()); diff --git a/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java b/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java index 0c5a259e..d308afba 100644 --- a/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java @@ -581,58 +581,63 @@ public class PublishFormFragment extends BaseFragment implements private void updateFieldsFromCurrentClaim() { if (currentClaim != null && !editFieldsLoaded) { Context context = getContext(); - Claim.StreamMetadata metadata = (Claim.StreamMetadata) currentClaim.getValue(); - uploadedThumbnailUrl = currentClaim.getThumbnailUrl(); - if (context != null && !Helper.isNullOrEmpty(uploadedThumbnailUrl)) { - Glide.with(context.getApplicationContext()).load(uploadedThumbnailUrl).centerCrop().into(imageThumbnail); - } - - inputTitle.setText(currentClaim.getTitle()); - inputDescription.setText(currentClaim.getDescription()); - if (addedTagsAdapter != null && currentClaim.getTagObjects() != null) { - addedTagsAdapter.addTags(currentClaim.getTagObjects()); - updateSuggestedTags(currentFilter, SUGGESTED_LIMIT, true); - } - - if (metadata.getFee() != null) { - Fee fee = metadata.getFee(); - switchPrice.setChecked(true); - inputPrice.setText(fee.getAmount()); - priceCurrencySpinner.setSelection("lbc".equalsIgnoreCase(fee.getCurrency()) ? 0 : 1); - } - - inputAddress.setText(currentClaim.getName()); - inputDeposit.setText(currentClaim.getAmount()); - - if (metadata.getLanguages() != null && metadata.getLanguages().size() > 0) { - // get the first language - String langCode = metadata.getLanguages().get(0); - int langCodePosition = ((LanguageSpinnerAdapter) languageSpinner.getAdapter()).getItemPosition(langCode); - if (langCodePosition > -1) { - languageSpinner.setSelection(langCodePosition); - } - } - - if (!Helper.isNullOrEmpty(metadata.getLicense())) { - LicenseSpinnerAdapter adapter = (LicenseSpinnerAdapter) licenseSpinner.getAdapter(); - int licPosition = adapter.getItemPosition(metadata.getLicense()); - if (licPosition == -1) { - licPosition = adapter.getItemPosition(Predefined.LICENSE_OTHER); - } - if (licPosition > -1) { - licenseSpinner.setSelection(licPosition); + try { + Claim.StreamMetadata metadata = (Claim.StreamMetadata) currentClaim.getValue(); + uploadedThumbnailUrl = currentClaim.getThumbnailUrl(); + if (context != null && !Helper.isNullOrEmpty(uploadedThumbnailUrl)) { + Glide.with(context.getApplicationContext()).load(uploadedThumbnailUrl).centerCrop().into(imageThumbnail); } - License selectedLicense = (License) licenseSpinner.getSelectedItem(); - boolean otherLicense = Arrays.asList( - Predefined.LICENSE_COPYRIGHTED.toLowerCase(), - Predefined.LICENSE_OTHER.toLowerCase()).contains(selectedLicense.getName().toLowerCase()); - inputOtherLicenseDescription.setText(otherLicense ? metadata.getLicense() : null); - } + inputTitle.setText(currentClaim.getTitle()); + inputDescription.setText(currentClaim.getDescription()); + if (addedTagsAdapter != null && currentClaim.getTagObjects() != null) { + addedTagsAdapter.addTags(currentClaim.getTagObjects()); + updateSuggestedTags(currentFilter, SUGGESTED_LIMIT, true); + } - inputAddress.setEnabled(false); - editMode = true; - editFieldsLoaded = true; + if (metadata.getFee() != null) { + Fee fee = metadata.getFee(); + switchPrice.setChecked(true); + inputPrice.setText(fee.getAmount()); + priceCurrencySpinner.setSelection("lbc".equalsIgnoreCase(fee.getCurrency()) ? 0 : 1); + } + + inputAddress.setText(currentClaim.getName()); + inputDeposit.setText(currentClaim.getAmount()); + + if (metadata.getLanguages() != null && metadata.getLanguages().size() > 0) { + // get the first language + String langCode = metadata.getLanguages().get(0); + int langCodePosition = ((LanguageSpinnerAdapter) languageSpinner.getAdapter()).getItemPosition(langCode); + if (langCodePosition > -1) { + languageSpinner.setSelection(langCodePosition); + } + } + + if (!Helper.isNullOrEmpty(metadata.getLicense())) { + LicenseSpinnerAdapter adapter = (LicenseSpinnerAdapter) licenseSpinner.getAdapter(); + int licPosition = adapter.getItemPosition(metadata.getLicense()); + if (licPosition == -1) { + licPosition = adapter.getItemPosition(Predefined.LICENSE_OTHER); + } + if (licPosition > -1) { + licenseSpinner.setSelection(licPosition); + } + + License selectedLicense = (License) licenseSpinner.getSelectedItem(); + boolean otherLicense = Arrays.asList( + Predefined.LICENSE_COPYRIGHTED.toLowerCase(), + Predefined.LICENSE_OTHER.toLowerCase()).contains(selectedLicense.getName().toLowerCase()); + inputOtherLicenseDescription.setText(otherLicense ? metadata.getLicense() : null); + } + + inputAddress.setEnabled(false); + editMode = true; + editFieldsLoaded = true; + } catch (ClassCastException ex) { + // invalid claim value type + cancelOnFatalCondition(getString(R.string.publish_invalid_claim_type)); + } } } @@ -1415,8 +1420,11 @@ public class PublishFormFragment extends BaseFragment implements @Override public void onFilePicked(String filePath) { if (Helper.isNullOrEmpty(filePath)) { - Snackbar.make(getView(), R.string.undetermined_image_filepath, Snackbar.LENGTH_LONG).setBackgroundTint( - ContextCompat.getColor(getContext(), R.color.red)).show(); + View view = getView(); + if (view != null) { + Snackbar.make(view, R.string.undetermined_image_filepath, Snackbar.LENGTH_LONG). + setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + } return; } diff --git a/app/src/main/java/io/lbry/browser/ui/verification/EmailVerificationFragment.java b/app/src/main/java/io/lbry/browser/ui/verification/EmailVerificationFragment.java index 3bd5576f..f8ffeb3b 100644 --- a/app/src/main/java/io/lbry/browser/ui/verification/EmailVerificationFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/verification/EmailVerificationFragment.java @@ -70,8 +70,12 @@ public class EmailVerificationFragment extends Fragment { inputEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean hasFocus) { - String layoutHint = !hasFocus ? "" : getString(R.string.email); - inputLayoutEmail.setHint(layoutHint); + try { + String layoutHint = !hasFocus ? "" : getString(R.string.email); + inputLayoutEmail.setHint(layoutHint); + } catch (IllegalStateException ex) { + // pass + } } }); buttonContinue.setOnClickListener(new View.OnClickListener() { @@ -98,9 +102,12 @@ public class EmailVerificationFragment extends Fragment { private void addEmail() { currentEmail = Helper.getValue(inputEmail.getText()); - if (Helper.isNullOrEmpty(currentEmail) || currentEmail.indexOf("@") == -1) { - Snackbar.make(getView(), R.string.provide_valid_email, Snackbar.LENGTH_LONG). - setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + if (Helper.isNullOrEmpty(currentEmail) || !currentEmail.contains("@")) { + View view = getView(); + if (view != null) { + Snackbar.make(view, R.string.provide_valid_email, Snackbar.LENGTH_LONG). + setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + } return; } @@ -136,8 +143,11 @@ public class EmailVerificationFragment extends Fragment { @Override public void onError(Exception error) { - Snackbar.make(getView(), error.getMessage(), Snackbar.LENGTH_LONG). - setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + View view = getView(); + if (view != null && error != null) { + Snackbar.make(getView(), error.getMessage(), Snackbar.LENGTH_LONG). + setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + }; Helper.setViewVisibility(buttonContinue, View.VISIBLE); } }); @@ -194,14 +204,20 @@ public class EmailVerificationFragment extends Fragment { @Override public void onSuccess() { - Snackbar.make(getView(), R.string.please_follow_instructions, Snackbar.LENGTH_LONG).show(); + View view = getView(); + if (view != null) { + Snackbar.make(view, R.string.please_follow_instructions, Snackbar.LENGTH_LONG).show(); + } Helper.setViewEnabled(buttonResend, true); } @Override public void onError(Exception error) { - Snackbar.make(getView(), error.getMessage(), Snackbar.LENGTH_LONG). - setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + View view = getView(); + if (view != null && error != null) { + Snackbar.make(view, error.getMessage(), Snackbar.LENGTH_LONG). + setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + } Helper.setViewEnabled(buttonResend, true); } }); diff --git a/app/src/main/java/io/lbry/browser/ui/wallet/WalletFragment.java b/app/src/main/java/io/lbry/browser/ui/wallet/WalletFragment.java index 4da4ea99..31b9a687 100644 --- a/app/src/main/java/io/lbry/browser/ui/wallet/WalletFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/wallet/WalletFragment.java @@ -409,14 +409,22 @@ public class WalletFragment extends BaseFragment implements SdkStatusListener, W // wallet_send task String recipientAddress = Helper.getValue(inputSendAddress.getText()); String amountString = Helper.getValue(inputSendAmount.getText()); - String amount = new DecimalFormat(Helper.SDK_AMOUNT_FORMAT, new DecimalFormatSymbols(Locale.US)). - format(new BigDecimal(amountString).doubleValue()); + String amount = null; + try { + amount = new DecimalFormat(Helper.SDK_AMOUNT_FORMAT, new DecimalFormatSymbols(Locale.US)). + format(new BigDecimal(amountString).doubleValue()); + } catch (NumberFormatException ex) { + Snackbar.make(getView(), R.string.invalid_amount, Snackbar.LENGTH_LONG). + setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + return; + } disableSendControls(); + double actualSendAmount = Double.valueOf(amount); WalletSendTask task = new WalletSendTask(recipientAddress, amount, walletSendProgress, new WalletSendTask.WalletSendHandler() { @Override public void onSuccess() { - double sentAmount = Double.valueOf(amount); + double sentAmount = actualSendAmount; String message = getResources().getQuantityString( R.plurals.you_sent_credits, sentAmount == 1.0 ? 1 : 2, new DecimalFormat("#,###.##").format(sentAmount)); @@ -530,8 +538,11 @@ public class WalletFragment extends BaseFragment implements SdkStatusListener, W } @Override public void onSuccess(String newAddress) { - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext()); - sp.edit().putString(MainActivity.PREFERENCE_KEY_INTERNAL_WALLET_RECEIVE_ADDRESS, newAddress).apply(); + Context context = getContext(); + if (context != null) { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + sp.edit().putString(MainActivity.PREFERENCE_KEY_INTERNAL_WALLET_RECEIVE_ADDRESS, newAddress).apply(); + } Helper.setViewText(textWalletReceiveAddress, newAddress); Helper.setViewEnabled(buttonGetNewAddress, true); } diff --git a/app/src/main/java/io/lbry/browser/utils/LbryUri.java b/app/src/main/java/io/lbry/browser/utils/LbryUri.java index 5d9f815f..a7a5c3df 100644 --- a/app/src/main/java/io/lbry/browser/utils/LbryUri.java +++ b/app/src/main/java/io/lbry/browser/utils/LbryUri.java @@ -66,6 +66,10 @@ public class LbryUri { REGEX_PART_MODIFIER_SEPARATOR)); String cleanUrl = url, queryString = null; + if (Helper.isNullOrEmpty(url)) { + throw new LbryUriException("Invalid url parameter."); + } + Matcher qsMatcher = PATTERN_SEPARATE_QUERY_STRING.matcher(url); if (qsMatcher.matches()) { queryString = qsMatcher.group(2); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6d742a0d..7d5fe110 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -81,6 +81,7 @@ Are you sure you want to unpublish this content? No files will be removed from your device. The content was successfully deleted from the blockchain. The content could not be deleted at this time. Please try again later. + Please select a channel to repost on. %1$s view %1$s views @@ -123,6 +124,7 @@ Show extra fields Hide extra fields No file found to publish. + Invalid claim specified for editing. Video optimization A thumbnail could not be automatically created from your content file. Your video is being optimized for better support on a wide range of devices. You can fill out the remaining fields below while this is in progress. @@ -391,6 +393,7 @@ Verify Please enter a valid phone number. Please enter the verification code sent to your phone number. + User account could not be retrieved at this time. Please try again later. You have not added any tags yet. Add tags to improve discovery. -- 2.45.2