FileDocCategorySizeDatePackage
ActivityRecognitionHardware.javaAPI DocAndroid 5.1 API7389Thu Mar 12 22:22:10 GMT 2015android.hardware.location

ActivityRecognitionHardware

public class ActivityRecognitionHardware extends IActivityRecognitionHardware.Stub
A class that implements an {@link IActivityRecognitionHardware} backed up by the Activity Recognition HAL.
hide

Fields Summary
private static final String
TAG
private static final String
HARDWARE_PERMISSION
private static final int
INVALID_ACTIVITY_TYPE
private static final int
NATIVE_SUCCESS_RESULT
private static ActivityRecognitionHardware
sSingletonInstance
private static final Object
sSingletonInstanceLock
private final android.content.Context
mContext
private final String[]
mSupportedActivities
private final android.os.RemoteCallbackList
mSinks
Constructors Summary
private ActivityRecognitionHardware(android.content.Context context)

    

       
        nativeInitialize();

        mContext = context;
        mSupportedActivities = fetchSupportedActivities();
    
Methods Summary
private voidcheckPermissions()

        String message = String.format(
                "Permission '%s' not granted to access ActivityRecognitionHardware",
                HARDWARE_PERMISSION);
        mContext.enforceCallingPermission(HARDWARE_PERMISSION, message);
    
public booleandisableActivityEvent(java.lang.String activity, int eventType)

        checkPermissions();

        int activityType = getActivityType(activity);
        if (activityType == INVALID_ACTIVITY_TYPE) {
            return false;
        }

        int result = nativeDisableActivityEvent(activityType, eventType);
        return result == NATIVE_SUCCESS_RESULT;
    
public booleanenableActivityEvent(java.lang.String activity, int eventType, long reportLatencyNs)

        checkPermissions();

        int activityType = getActivityType(activity);
        if (activityType == INVALID_ACTIVITY_TYPE) {
            return false;
        }

        int result = nativeEnableActivityEvent(activityType, eventType, reportLatencyNs);
        return result == NATIVE_SUCCESS_RESULT;
    
private java.lang.String[]fetchSupportedActivities()

        String[] supportedActivities = nativeGetSupportedActivities();
        if (supportedActivities != null) {
            return supportedActivities;
        }

        return new String[0];
    
public booleanflush()

        checkPermissions();
        int result = nativeFlush();
        return result == NATIVE_SUCCESS_RESULT;
    
private java.lang.StringgetActivityName(int activityType)

        if (activityType < 0 || activityType >= mSupportedActivities.length) {
            String message = String.format(
                    "Invalid ActivityType: %d, SupportedActivities: %d",
                    activityType,
                    mSupportedActivities.length);
            Log.e(TAG, message);
            return null;
        }

        return mSupportedActivities[activityType];
    
private intgetActivityType(java.lang.String activity)

        if (TextUtils.isEmpty(activity)) {
            return INVALID_ACTIVITY_TYPE;
        }

        int supportedActivitiesLength = mSupportedActivities.length;
        for (int i = 0; i < supportedActivitiesLength; ++i) {
            if (activity.equals(mSupportedActivities[i])) {
                return i;
            }
        }

        return INVALID_ACTIVITY_TYPE;
    
public static android.hardware.location.ActivityRecognitionHardwaregetInstance(android.content.Context context)

        synchronized (sSingletonInstanceLock) {
            if (sSingletonInstance == null) {
                sSingletonInstance = new ActivityRecognitionHardware(context);
            }

            return sSingletonInstance;
        }
    
public java.lang.String[]getSupportedActivities()

        checkPermissions();
        return mSupportedActivities;
    
public booleanisActivitySupported(java.lang.String activity)

        checkPermissions();
        int activityType = getActivityType(activity);
        return activityType != INVALID_ACTIVITY_TYPE;
    
public static booleanisSupported()

        return nativeIsSupported();
    
private static native voidnativeClassInit()

private native intnativeDisableActivityEvent(int activityType, int eventType)

private native intnativeEnableActivityEvent(int activityType, int eventType, long reportLatenceNs)

private native intnativeFlush()

private native java.lang.String[]nativeGetSupportedActivities()

private native voidnativeInitialize()

private static native booleannativeIsSupported()

private native voidnativeRelease()

private voidonActivityChanged(android.hardware.location.ActivityRecognitionHardware$Event[] events)
Called by the Activity-Recognition HAL.

        if (events == null || events.length == 0) {
            Log.d(TAG, "No events to broadcast for onActivityChanged.");
            return;
        }

        int eventsLength = events.length;
        ActivityRecognitionEvent activityRecognitionEventArray[] =
                new ActivityRecognitionEvent[eventsLength];
        for (int i = 0; i < eventsLength; ++i) {
            Event event = events[i];
            String activityName = getActivityName(event.activity);
            activityRecognitionEventArray[i] =
                    new ActivityRecognitionEvent(activityName, event.type, event.timestamp);
        }
        ActivityChangedEvent activityChangedEvent =
                new ActivityChangedEvent(activityRecognitionEventArray);

        int size = mSinks.beginBroadcast();
        for (int i = 0; i < size; ++i) {
            IActivityRecognitionHardwareSink sink = mSinks.getBroadcastItem(i);
            try {
                sink.onActivityChanged(activityChangedEvent);
            } catch (RemoteException e) {
                Log.e(TAG, "Error delivering activity changed event.", e);
            }
        }
        mSinks.finishBroadcast();

    
public booleanregisterSink(IActivityRecognitionHardwareSink sink)

        checkPermissions();
        return mSinks.register(sink);
    
public booleanunregisterSink(IActivityRecognitionHardwareSink sink)

        checkPermissions();
        return mSinks.unregister(sink);