Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7469ca0a authored by Roshan Pius's avatar Roshan Pius Committed by Automerger Merge Worker
Browse files

Merge "nfc(api): Separate out NfcExtras from nfc APIs" into main am:...

Merge "nfc(api): Separate out NfcExtras from nfc APIs" into main am: 16043abe am: d394d576 am: ebe3fcf8

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2798514



Change-Id: Iaf488ed591afedf48acb0573f5f12292c7c8d104
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 8a470ae1 ebe3fcf8
Loading
Loading
Loading
Loading
+31 −3
Original line number Diff line number Diff line
@@ -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);
    }