diff options
author | 2023-10-20 11:47:12 -0700 | |
---|---|---|
committer | 2023-10-20 11:47:12 -0700 | |
commit | 0091ec1a638ec960351af2358e1a43380705c819 (patch) | |
tree | f928bc80ab46cb1ac3462caea7b841e529cfe835 /nfc-extras/java/com | |
parent | a1a4b4ab2f3c3bb0393ef3a090608b17eae8a28f (diff) |
nfc(api): Separate out NfcExtras from nfc APIs
This is an API that has been unsupported for almost 10 years (see ag/443092).
But, this API has not been formally deprecated.
Use reflection to access @hide APIs from NfcAdapter class.
Bug: 303286040
Test: Compiles
Change-Id: I7cac9a4e1bca3fea220b5ac3d7d1cb2d63652485
Diffstat (limited to 'nfc-extras/java/com')
-rw-r--r-- | nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java index ffed80479662..a7c6c690f852 100644 --- a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java +++ b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java @@ -16,6 +16,8 @@ package com.android.nfc_extras; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.HashMap; import android.content.Context; @@ -30,6 +32,8 @@ import android.util.Log; * * There is a 1-1 relationship between an {@link NfcAdapterExtras} object and * a {@link NfcAdapter} object. + * + * TODO(b/303286040): Deprecate this API surface since this is no longer supported (see ag/443092) */ public final class NfcAdapterExtras { private static final String TAG = "NfcAdapterExtras"; @@ -81,6 +85,18 @@ public final class NfcAdapterExtras { } } + private static Context getContextFromNfcAdapter(NfcAdapter adapter) { + try { + Method method = NfcAdapter.class.getDeclaredMethod("getContext"); + method.setAccessible(true); + return (Context) method.invoke(adapter); + } catch (SecurityException | NoSuchMethodException | IllegalArgumentException + | IllegalAccessException | IllegalAccessError | InvocationTargetException e) { + Log.e(TAG, "Unable to get context from NfcAdapter"); + } + return null; + } + /** * Get the {@link NfcAdapterExtras} for the given {@link NfcAdapter}. * @@ -91,7 +107,7 @@ public final class NfcAdapterExtras { * @return the {@link NfcAdapterExtras} object for the given {@link NfcAdapter} */ public static NfcAdapterExtras get(NfcAdapter adapter) { - Context context = adapter.getContext(); + Context context = getContextFromNfcAdapter(adapter); if (context == null) { throw new UnsupportedOperationException( "You must pass a context to your NfcAdapter to use the NFC extras APIs"); @@ -112,7 +128,7 @@ public final class NfcAdapterExtras { private NfcAdapterExtras(NfcAdapter adapter) { mAdapter = adapter; - mPackageName = adapter.getContext().getPackageName(); + mPackageName = getContextFromNfcAdapter(adapter).getPackageName(); mEmbeddedEe = new NfcExecutionEnvironment(this); mRouteOnWhenScreenOn = new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, mEmbeddedEe); @@ -156,12 +172,24 @@ public final class NfcAdapterExtras { } } + private static void attemptDeadServiceRecoveryOnNfcAdapter(NfcAdapter adapter, Exception e) { + try { + Method method = NfcAdapter.class.getDeclaredMethod( + "attemptDeadServiceRecovery", Exception.class); + method.setAccessible(true); + method.invoke(adapter, e); + } catch (SecurityException | NoSuchMethodException | IllegalArgumentException + | IllegalAccessException | IllegalAccessError | InvocationTargetException ex) { + Log.e(TAG, "Unable to attempt dead service recovery on NfcAdapter"); + } + } + /** * NFC service dead - attempt best effort recovery */ void attemptDeadServiceRecovery(Exception e) { Log.e(TAG, "NFC Adapter Extras dead - attempting to recover"); - mAdapter.attemptDeadServiceRecovery(e); + attemptDeadServiceRecoveryOnNfcAdapter(mAdapter, e); initService(mAdapter); } |