Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge "Add separate checks for Tag Reading and Card Emulation is enab…
Browse files Browse the repository at this point in the history
…led." into main
  • Loading branch information
Alisher Alikhodjaev authored and Gerrit Code Review committed Aug 9, 2023
2 parents bbc6fc3 + acc995c commit 24c9102
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions core/java/android/nfc/NfcAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ public static synchronized NfcAdapter getNfcAdapter(Context context) {
try {
sTagService = sService.getNfcTagInterface();
} catch (RemoteException e) {
sTagService = null;
Log.e(TAG, "could not retrieve NFC Tag service");
throw new UnsupportedOperationException();
}
Expand All @@ -650,12 +651,14 @@ public static synchronized NfcAdapter getNfcAdapter(Context context) {
try {
sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface();
} catch (RemoteException e) {
sNfcFCardEmulationService = null;
Log.e(TAG, "could not retrieve NFC-F card emulation service");
throw new UnsupportedOperationException();
}
try {
sCardEmulationService = sService.getNfcCardEmulationInterface();
} catch (RemoteException e) {
sCardEmulationService = null;
Log.e(TAG, "could not retrieve card emulation service");
throw new UnsupportedOperationException();
}
Expand Down Expand Up @@ -838,30 +841,54 @@ public void attemptDeadServiceRecovery(Exception e) {
// assigning to sService is not thread-safe, but this is best-effort code
// and on a well-behaved system should never happen
sService = service;
try {
sTagService = service.getNfcTagInterface();
} catch (RemoteException ee) {
Log.e(TAG, "could not retrieve NFC tag service during service recovery");
// nothing more can be done now, sService is still stale, we'll hit
// this recovery path again later
return;
if (sHasNfcFeature) {
try {
sTagService = service.getNfcTagInterface();
} catch (RemoteException ee) {
sTagService = null;
Log.e(TAG, "could not retrieve NFC tag service during service recovery");
// nothing more can be done now, sService is still stale, we'll hit
// this recovery path again later
return;
}
}

try {
sCardEmulationService = service.getNfcCardEmulationInterface();
} catch (RemoteException ee) {
Log.e(TAG, "could not retrieve NFC card emulation service during service recovery");
}
if (sHasCeFeature) {
try {
sCardEmulationService = service.getNfcCardEmulationInterface();
} catch (RemoteException ee) {
sCardEmulationService = null;
Log.e(TAG,
"could not retrieve NFC card emulation service during service recovery");
}

try {
sNfcFCardEmulationService = service.getNfcFCardEmulationInterface();
} catch (RemoteException ee) {
Log.e(TAG, "could not retrieve NFC-F card emulation service during service recovery");
try {
sNfcFCardEmulationService = service.getNfcFCardEmulationInterface();
} catch (RemoteException ee) {
sNfcFCardEmulationService = null;
Log.e(TAG,
"could not retrieve NFC-F card emulation service during service recovery");
}
}

return;
}

private boolean isCardEmulationEnabled() {
if (sHasCeFeature) {
return (sCardEmulationService != null || sNfcFCardEmulationService != null);
}
return false;
}

private boolean isTagReadingEnabled() {
if (sHasNfcFeature) {
return sTagService != null;
}
return false;
}


/**
* Return true if this NFC Adapter has any features enabled.
*
Expand All @@ -875,8 +902,9 @@ public void attemptDeadServiceRecovery(Exception e) {
* @return true if this NFC Adapter has any features enabled
*/
public boolean isEnabled() {
boolean serviceState = false;
try {
return sService.getState() == STATE_ON;
serviceState = sService.getState() == STATE_ON;
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
// Try one more time
Expand All @@ -885,12 +913,12 @@ public boolean isEnabled() {
return false;
}
try {
return sService.getState() == STATE_ON;
serviceState = sService.getState() == STATE_ON;
} catch (RemoteException ee) {
Log.e(TAG, "Failed to recover NFC Service.");
}
return false;
}
return serviceState && (isTagReadingEnabled() || isCardEmulationEnabled());
}

/**
Expand Down

0 comments on commit 24c9102

Please sign in to comment.