Code example for NfcAdapter

Methods: attemptDeadServiceRecovery, getNfcAdapterExtrasInterface

0
    private final NfcAdapter mAdapter;
    final String mPackageName;
 
    /** get service handles */ 
    private static void initService(NfcAdapter adapter) {
        final INfcAdapterExtras service = adapter.getNfcAdapterExtrasInterface();
        if (service != null) {
            // Leave stale rather than receive a null value. 
            sService = service;
        } 
    } 
 
    /** 
     * Get the {@link NfcAdapterExtras} for the given {@link NfcAdapter}. 
     * 
     * <p class="note"> 
     * Requires the {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. 
     * 
     * @param adapter a {@link NfcAdapter}, must not be null 
     * @return the {@link NfcAdapterExtras} object for the given {@link NfcAdapter} 
     */ 
    public static NfcAdapterExtras get(NfcAdapter adapter) {
        Context context = adapter.getContext();
        if (context == null) {
            throw new UnsupportedOperationException(
                    "You must pass a context to your NfcAdapter to use the NFC extras APIs"); 
        } 
 
        synchronized (NfcAdapterExtras.class) { 
            if (sService == null) {
                initService(adapter);
            } 
            NfcAdapterExtras extras = sNfcExtras.get(adapter);
            if (extras == null) {
                extras = new NfcAdapterExtras(adapter);
                sNfcExtras.put(adapter,  extras);
            } 
            return extras;
        } 
    } 
 
    private NfcAdapterExtras(NfcAdapter adapter) {
        mAdapter = adapter;
        mPackageName = adapter.getContext().getPackageName();
        mEmbeddedEe = new NfcExecutionEnvironment(this);
        mRouteOnWhenScreenOn = new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON,
                mEmbeddedEe);
    } 
 
    /** 
     * Immutable data class that describes a card emulation route. 
     */ 
    public final static class CardEmulationRoute { 
        /** 
         * Card Emulation is turned off on this NfcAdapter. 
         * <p>This is the default routing state after boot. 
         */ 
        public static final int ROUTE_OFF = 1;
 
        /** 
         * Card Emulation is routed to {@link #nfcEe} only when the screen is on, 
         * otherwise it is turned off. 
         */ 
        public static final int ROUTE_ON_WHEN_SCREEN_ON = 2;
 
        /** 
         * A route such as {@link #ROUTE_OFF} or {@link #ROUTE_ON_WHEN_SCREEN_ON}. 
         */ 
        public final int route;
 
        /** 
         * The {@link NFcExecutionEnvironment} that is Card Emulation is routed to. 
         * <p>null if {@link #route} is {@link #ROUTE_OFF}, otherwise not null. 
         */ 
        public final NfcExecutionEnvironment nfcEe;
 
        public CardEmulationRoute(int route, NfcExecutionEnvironment nfcEe) {
            if (route == ROUTE_OFF && nfcEe != null) {
                throw new IllegalArgumentException("must not specifiy a NFC-EE with ROUTE_OFF");
            } else if (route != ROUTE_OFF && nfcEe == null) {
                throw new IllegalArgumentException("must specifiy a NFC-EE for this route");
            } 
            this.route = route;
            this.nfcEe = nfcEe;
        } 
    } 
 
    /** 
     * NFC service dead - attempt best effort recovery 
     */ 
    void attemptDeadServiceRecovery(Exception e) {
        Log.e(TAG, "NFC Adapter Extras dead - attempting to recover");
        mAdapter.attemptDeadServiceRecovery(e);
        initService(mAdapter);
    } 
 
    INfcAdapterExtras getService() { 
        return sService;