FileDocCategorySizeDatePackage
NfcActivityManager.javaAPI DocAndroid 5.1 API17916Thu Mar 12 22:22:10 GMT 2015android.nfc

NfcActivityManager

public final class NfcActivityManager extends IAppCallback.Stub implements Application.ActivityLifecycleCallbacks
Manages NFC API's that are coupled to the life-cycle of an Activity.

Uses {@link Application#registerActivityLifecycleCallbacks} to hook into activity life-cycle events such as onPause() and onResume().

hide

Fields Summary
static final String
TAG
static final Boolean
DBG
final NfcAdapter
mAdapter
final NfcEvent
mDefaultEvent
final List
mApps
final List
mActivities
Constructors Summary
public NfcActivityManager(NfcAdapter adapter)

        mAdapter = adapter;
        mActivities = new LinkedList<NfcActivityState>();
        mApps = new ArrayList<NfcApplicationState>(1);  // Android VM usually has 1 app
        mDefaultEvent = new NfcEvent(mAdapter);
    
Methods Summary
public BeamShareDatacreateBeamShareData()
Callback from NFC service, usually on binder thread

        NfcAdapter.CreateNdefMessageCallback ndefCallback;
        NfcAdapter.CreateBeamUrisCallback urisCallback;
        NdefMessage message;
        Activity activity;
        Uri[] uris;
        int flags;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = findResumedActivityState();
            if (state == null) return null;

            ndefCallback = state.ndefMessageCallback;
            urisCallback = state.uriCallback;
            message = state.ndefMessage;
            uris = state.uris;
            flags = state.flags;
            activity = state.activity;
        }

        // Make callbacks without lock
        if (ndefCallback != null) {
            message  = ndefCallback.createNdefMessage(mDefaultEvent);
        }
        if (urisCallback != null) {
            uris = urisCallback.createBeamUris(mDefaultEvent);
            if (uris != null) {
                ArrayList<Uri> validUris = new ArrayList<Uri>();
                for (Uri uri : uris) {
                    if (uri == null) {
                        Log.e(TAG, "Uri not allowed to be null.");
                        continue;
                    }
                    String scheme = uri.getScheme();
                    if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
                            !scheme.equalsIgnoreCase("content"))) {
                        Log.e(TAG, "Uri needs to have " +
                                "either scheme file or scheme content");
                        continue;
                    }
                    uri = ContentProvider.maybeAddUserId(uri, UserHandle.myUserId());
                    validUris.add(uri);
                }

                uris = validUris.toArray(new Uri[validUris.size()]);
            }
        }
        if (uris != null && uris.length > 0) {
            for (Uri uri : uris) {
                // Grant the NFC process permission to read these URIs
                activity.grantUriPermission("com.android.nfc", uri,
                        Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
        }
        return new BeamShareData(message, uris, UserHandle.CURRENT, flags);
    
synchronized voiddestroyActivityState(android.app.Activity activity)

        NfcActivityState activityState = findActivityState(activity);
        if (activityState != null) {
            activityState.destroy();
            mActivities.remove(activityState);
        }
    
public voiddisableReaderMode(android.app.Activity activity)

        boolean isResumed;
        Binder token;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = getActivityState(activity);
            state.readerCallback = null;
            state.readerModeFlags = 0;
            state.readerModeExtras = null;
            token = state.token;
            isResumed = state.resumed;
        }
        if (isResumed) {
            setReaderMode(token, 0, null);
        }

    
public voidenableReaderMode(android.app.Activity activity, android.nfc.NfcAdapter.ReaderCallback callback, int flags, android.os.Bundle extras)

        boolean isResumed;
        Binder token;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = getActivityState(activity);
            state.readerCallback = callback;
            state.readerModeFlags = flags;
            state.readerModeExtras = extras;
            token = state.token;
            isResumed = state.resumed;
        }
        if (isResumed) {
            setReaderMode(token, flags, extras);
        }
    
synchronized android.nfc.NfcActivityManager$NfcActivityStatefindActivityState(android.app.Activity activity)
find activity state from mActivities

        for (NfcActivityState state : mActivities) {
            if (state.activity == activity) {
                return state;
            }
        }
        return null;
    
android.nfc.NfcActivityManager$NfcApplicationStatefindAppState(android.app.Application app)

        for (NfcApplicationState appState : mApps) {
            if (appState.app == app) {
                return appState;
            }
        }
        return null;
    
synchronized android.nfc.NfcActivityManager$NfcActivityStatefindResumedActivityState()

        for (NfcActivityState state : mActivities) {
            if (state.resumed) {
                return state;
            }
        }
        return null;
    
synchronized android.nfc.NfcActivityManager$NfcActivityStategetActivityState(android.app.Activity activity)
find or create activity state from mActivities

        NfcActivityState state = findActivityState(activity);
        if (state == null) {
            state = new NfcActivityState(activity);
            mActivities.add(state);
        }
        return state;
    
public voidonActivityCreated(android.app.Activity activity, android.os.Bundle savedInstanceState)
Callback from Activity life-cycle, on main thread

 /* NO-OP */ 
public voidonActivityDestroyed(android.app.Activity activity)
Callback from Activity life-cycle, on main thread

        synchronized (NfcActivityManager.this) {
            NfcActivityState state = findActivityState(activity);
            if (DBG) Log.d(TAG, "onDestroy() for " + activity + " " + state);
            if (state != null) {
                // release all associated references
                destroyActivityState(activity);
            }
        }
    
public voidonActivityPaused(android.app.Activity activity)
Callback from Activity life-cycle, on main thread

        boolean readerModeFlagsSet;
        Binder token;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = findActivityState(activity);
            if (DBG) Log.d(TAG, "onPause() for " + activity + " " + state);
            if (state == null) return;
            state.resumed = false;
            token = state.token;
            readerModeFlagsSet = state.readerModeFlags != 0;
        }
        if (readerModeFlagsSet) {
            // Restore default p2p modes
            setReaderMode(token, 0, null);
        }
    
public voidonActivityResumed(android.app.Activity activity)
Callback from Activity life-cycle, on main thread

        int readerModeFlags = 0;
        Bundle readerModeExtras = null;
        Binder token;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = findActivityState(activity);
            if (DBG) Log.d(TAG, "onResume() for " + activity + " " + state);
            if (state == null) return;
            state.resumed = true;
            token = state.token;
            readerModeFlags = state.readerModeFlags;
            readerModeExtras = state.readerModeExtras;
        }
        if (readerModeFlags != 0) {
            setReaderMode(token, readerModeFlags, readerModeExtras);
        }
        requestNfcServiceCallback();
    
public voidonActivitySaveInstanceState(android.app.Activity activity, android.os.Bundle outState)
Callback from Activity life-cycle, on main thread

 /* NO-OP */ 
public voidonActivityStarted(android.app.Activity activity)
Callback from Activity life-cycle, on main thread

 /* NO-OP */ 
public voidonActivityStopped(android.app.Activity activity)
Callback from Activity life-cycle, on main thread

 /* NO-OP */ 
public voidonNdefPushComplete()
Callback from NFC service, usually on binder thread

        NfcAdapter.OnNdefPushCompleteCallback callback;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = findResumedActivityState();
            if (state == null) return;

            callback = state.onNdefPushCompleteCallback;
        }

        // Make callback without lock
        if (callback != null) {
            callback.onNdefPushComplete(mDefaultEvent);
        }
    
public voidonTagDiscovered(Tag tag)

        NfcAdapter.ReaderCallback callback;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = findResumedActivityState();
            if (state == null) return;

            callback = state.readerCallback;
        }

        // Make callback without lock
        if (callback != null) {
            callback.onTagDiscovered(tag);
        }

    
voidregisterApplication(android.app.Application app)

        NfcApplicationState appState = findAppState(app);
        if (appState == null) {
            appState = new NfcApplicationState(app);
            mApps.add(appState);
        }
        appState.register();
    
voidrequestNfcServiceCallback()
Request or unrequest NFC service callbacks. Makes IPC call - do not hold lock.

        try {
            NfcAdapter.sService.setAppCallback(this);
        } catch (RemoteException e) {
            mAdapter.attemptDeadServiceRecovery(e);
        }
    
public voidsetNdefPushContentUri(android.app.Activity activity, android.net.Uri[] uris)

        boolean isResumed;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = getActivityState(activity);
            state.uris = uris;
            isResumed = state.resumed;
        }
        if (isResumed) {
            // requestNfcServiceCallback() verifies permission also
            requestNfcServiceCallback();
        } else {
            // Crash API calls early in case NFC permission is missing
            verifyNfcPermission();
        }
    
public voidsetNdefPushContentUriCallback(android.app.Activity activity, NfcAdapter.CreateBeamUrisCallback callback)

        boolean isResumed;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = getActivityState(activity);
            state.uriCallback = callback;
            isResumed = state.resumed;
        }
        if (isResumed) {
            // requestNfcServiceCallback() verifies permission also
            requestNfcServiceCallback();
        } else {
            // Crash API calls early in case NFC permission is missing
            verifyNfcPermission();
        }
    
public voidsetNdefPushMessage(android.app.Activity activity, NdefMessage message, int flags)

        boolean isResumed;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = getActivityState(activity);
            state.ndefMessage = message;
            state.flags = flags;
            isResumed = state.resumed;
        }
        if (isResumed) {
            // requestNfcServiceCallback() verifies permission also
            requestNfcServiceCallback();
        } else {
            // Crash API calls early in case NFC permission is missing
            verifyNfcPermission();
        }
    
public voidsetNdefPushMessageCallback(android.app.Activity activity, NfcAdapter.CreateNdefMessageCallback callback, int flags)

        boolean isResumed;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = getActivityState(activity);
            state.ndefMessageCallback = callback;
            state.flags = flags;
            isResumed = state.resumed;
        }
        if (isResumed) {
            // requestNfcServiceCallback() verifies permission also
            requestNfcServiceCallback();
        } else {
            // Crash API calls early in case NFC permission is missing
            verifyNfcPermission();
        }
    
public voidsetOnNdefPushCompleteCallback(android.app.Activity activity, NfcAdapter.OnNdefPushCompleteCallback callback)

        boolean isResumed;
        synchronized (NfcActivityManager.this) {
            NfcActivityState state = getActivityState(activity);
            state.onNdefPushCompleteCallback = callback;
            isResumed = state.resumed;
        }
        if (isResumed) {
            // requestNfcServiceCallback() verifies permission also
            requestNfcServiceCallback();
        } else {
            // Crash API calls early in case NFC permission is missing
            verifyNfcPermission();
        }
    
public voidsetReaderMode(android.os.Binder token, int flags, android.os.Bundle extras)

        if (DBG) Log.d(TAG, "Setting reader mode");
        try {
            NfcAdapter.sService.setReaderMode(token, this, flags, extras);
        } catch (RemoteException e) {
            mAdapter.attemptDeadServiceRecovery(e);
        }
    
voidunregisterApplication(android.app.Application app)

        NfcApplicationState appState = findAppState(app);
        if (appState == null) {
            Log.e(TAG, "app was not registered " + app);
            return;
        }
        appState.unregister();
    
voidverifyNfcPermission()

        try {
            NfcAdapter.sService.verifyNfcPermission();
        } catch (RemoteException e) {
            mAdapter.attemptDeadServiceRecovery(e);
        }