diff --git a/app/src/main/java/protect/card_locker/cardview/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/cardview/LoyaltyCardViewActivity.java
index 029aec070..077b5029a 100644
--- a/app/src/main/java/protect/card_locker/cardview/LoyaltyCardViewActivity.java
+++ b/app/src/main/java/protect/card_locker/cardview/LoyaltyCardViewActivity.java
@@ -456,7 +456,7 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
Window window = getWindow();
applyWindowPreferences(window);
- enablePausedNfcIfConfigured();
+ configurePausedNfc(settings.getDisableNfcWhileViewingCard());
if (!loadCurrentCardFromDatabase()) {
finish();
@@ -476,6 +476,14 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
ShortcutHelper.updateShortcuts(this);
}
+ @Override
+ protected void onPause() {
+ // Some devices have broken NFC, which will cause a crash if reader mode is enabled
+ // So ensure we disable it explicitly before letting Android "save" the NFC state
+ configurePausedNfc(false);
+ super.onPause();
+ }
+
@Override
protected void onDestroy() {
if (database != null && database.isOpen()) {
@@ -513,28 +521,36 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
window.setAttributes(attributes);
}
- private void enablePausedNfcIfConfigured() {
+ private void configurePausedNfc(boolean pause) {
// Pause NFC to prevent NFC payments from triggering while showing a barcode
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
return;
}
- if (settings.getDisableNfcWhileViewingCard()) {
- nfcAdapter.enableReaderMode(this, tag -> {
- Snackbar snackbar = Snackbar.make(binding.container, R.string.nfc_blocked_while_viewing_card, Snackbar.LENGTH_LONG)
- .setAnchorView(binding.fabEdit)
- .setAction(R.string.change_settings, view -> {
- // Open settings activity
- Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
- startActivity(intent);
- });
- snackbar.show();
- }, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B
- | NfcAdapter.FLAG_READER_NFC_F | NfcAdapter.FLAG_READER_NFC_V
- | NfcAdapter.FLAG_READER_NFC_BARCODE
- | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
- | NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS, null);
+ if (pause) {
+ try {
+ nfcAdapter.enableReaderMode(this, tag -> {
+ Snackbar snackbar = Snackbar.make(binding.container, R.string.nfc_blocked_while_viewing_card, Snackbar.LENGTH_LONG)
+ .setAnchorView(binding.fabEdit)
+ .setAction(R.string.change_settings, view -> {
+ // Open settings activity
+ Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
+ startActivity(intent);
+ });
+ snackbar.show();
+ }, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B
+ | NfcAdapter.FLAG_READER_NFC_F | NfcAdapter.FLAG_READER_NFC_V
+ | NfcAdapter.FLAG_READER_NFC_BARCODE
+ | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
+ | NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS, null);
+ } catch (Exception e) {
+ // For some unknown reason, this can throw a DeadObjectException.
+ // Mostly got reports from FOSS users, which implies it may be more common with custom ROMs? Uncertain.
+ Toast.makeText(this, R.string.nfc_block_system_error, Toast.LENGTH_SHORT).show();
+ Log.e(TAG, "Failed to pause NFC: " + e);
+ e.printStackTrace();
+ }
} else {
nfcAdapter.disableReaderMode(this);
}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 6b3b8ce3b..3cbdea079 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -350,4 +350,5 @@
Back
NFC is paused
Change settings
+ Failed to pause NFC