diff options
| author | 2023-08-22 13:33:40 -0700 | |
|---|---|---|
| committer | 2023-08-23 13:51:54 -0700 | |
| commit | ad5e3c17768c9302a9528f624f3b942049b03839 (patch) | |
| tree | b9a39a7400498871b97b4c55e1ad22a4608f8c97 | |
| parent | f61798c69d2da2314cdd5dcde1f48709c10c52de (diff) | |
nfc(api): Restore behavior from Android T (follow-up)
Previous bug fix in ag/23755172 had a bug, we should always return null
if the service is not available regardless of |sIsInitialized| state.
Bug: 296358872
Test: Manual tests
Change-Id: I97a3db94189cc3b7bbecd8f25adb42c691fbdaa8
| -rw-r--r-- | core/java/android/nfc/NfcAdapter.java | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java index 36199e522520..a9eb672c4e4d 100644 --- a/core/java/android/nfc/NfcAdapter.java +++ b/core/java/android/nfc/NfcAdapter.java @@ -600,6 +600,17 @@ public final class NfcAdapter { return offHostSE; } + private static void retrieveServiceRegisterer() { + if (sServiceRegisterer == null) { + NfcServiceManager manager = NfcFrameworkInitializer.getNfcServiceManager(); + if (manager == null) { + Log.e(TAG, "NfcServiceManager is null"); + throw new UnsupportedOperationException(); + } + sServiceRegisterer = manager.getNfcManagerServiceRegisterer(); + } + } + /** * Returns the NfcAdapter for application context, * or throws if NFC is not available. @@ -627,12 +638,7 @@ public final class NfcAdapter { Log.v(TAG, "this device does not have NFC support"); throw new UnsupportedOperationException(); } - NfcServiceManager manager = NfcFrameworkInitializer.getNfcServiceManager(); - if (manager == null) { - Log.e(TAG, "NfcServiceManager is null"); - throw new UnsupportedOperationException(); - } - sServiceRegisterer = manager.getNfcManagerServiceRegisterer(); + retrieveServiceRegisterer(); sService = getServiceInterface(); if (sService == null) { Log.e(TAG, "could not retrieve NFC service"); @@ -706,11 +712,13 @@ public final class NfcAdapter { throw new IllegalArgumentException( "context not associated with any application (using a mock context?)"); } - - if (sIsInitialized && sServiceRegisterer.tryGet() == null) { - synchronized (NfcAdapter.class) { - /* Stale sService pointer */ - if (sIsInitialized) sIsInitialized = false; + retrieveServiceRegisterer(); + if (sServiceRegisterer.tryGet() == null) { + if (sIsInitialized) { + synchronized (NfcAdapter.class) { + /* Stale sService pointer */ + if (sIsInitialized) sIsInitialized = false; + } } return null; } |