diff options
Diffstat (limited to 'core/java/android/hardware/location')
7 files changed, 526 insertions, 0 deletions
diff --git a/core/java/android/hardware/location/ActivityChangedEvent.aidl b/core/java/android/hardware/location/ActivityChangedEvent.aidl new file mode 100644 index 0000000..21f2445 --- /dev/null +++ b/core/java/android/hardware/location/ActivityChangedEvent.aidl @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package android.hardware.location; + +parcelable ActivityChangedEvent;
\ No newline at end of file diff --git a/core/java/android/hardware/location/ActivityChangedEvent.java b/core/java/android/hardware/location/ActivityChangedEvent.java new file mode 100644 index 0000000..0a89207 --- /dev/null +++ b/core/java/android/hardware/location/ActivityChangedEvent.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package android.hardware.location; + +import android.annotation.NonNull; +import android.os.Parcel; +import android.os.Parcelable; + +import java.security.InvalidParameterException; +import java.util.Arrays; +import java.util.List; + +/** + * A class representing an event for Activity changes. + * + * @hide + */ +public class ActivityChangedEvent implements Parcelable { + private final List<ActivityRecognitionEvent> mActivityRecognitionEvents; + + public ActivityChangedEvent(ActivityRecognitionEvent[] activityRecognitionEvents) { + if (activityRecognitionEvents == null) { + throw new InvalidParameterException( + "Parameter 'activityRecognitionEvents' must not be null."); + } + + mActivityRecognitionEvents = Arrays.asList(activityRecognitionEvents); + } + + @NonNull + public Iterable<ActivityRecognitionEvent> getActivityRecognitionEvents() { + return mActivityRecognitionEvents; + } + + public static final Creator<ActivityChangedEvent> CREATOR = + new Creator<ActivityChangedEvent>() { + @Override + public ActivityChangedEvent createFromParcel(Parcel source) { + int activityRecognitionEventsLength = source.readInt(); + ActivityRecognitionEvent[] activityRecognitionEvents = + new ActivityRecognitionEvent[activityRecognitionEventsLength]; + source.readTypedArray(activityRecognitionEvents, ActivityRecognitionEvent.CREATOR); + + return new ActivityChangedEvent(activityRecognitionEvents); + } + + @Override + public ActivityChangedEvent[] newArray(int size) { + return new ActivityChangedEvent[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int flags) { + ActivityRecognitionEvent[] activityRecognitionEventArray = + mActivityRecognitionEvents.toArray(new ActivityRecognitionEvent[0]); + parcel.writeInt(activityRecognitionEventArray.length); + parcel.writeTypedArray(activityRecognitionEventArray, flags); + } +} diff --git a/core/java/android/hardware/location/ActivityRecognitionEvent.java b/core/java/android/hardware/location/ActivityRecognitionEvent.java new file mode 100644 index 0000000..5aeb899 --- /dev/null +++ b/core/java/android/hardware/location/ActivityRecognitionEvent.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + +import android.os.Parcel; +import android.os.Parcelable; + +/** + * A class that represents an Activity Recognition Event. + * + * @hide + */ +public class ActivityRecognitionEvent implements Parcelable { + private final String mActivity; + private final int mEventType; + private final long mTimestampNs; + + public ActivityRecognitionEvent(String activity, int eventType, long timestampNs) { + mActivity = activity; + mEventType = eventType; + mTimestampNs = timestampNs; + } + + public String getActivity() { + return mActivity; + } + + public int getEventType() { + return mEventType; + } + + public long getTimestampNs() { + return mTimestampNs; + } + + public static final Creator<ActivityRecognitionEvent> CREATOR = + new Creator<ActivityRecognitionEvent>() { + @Override + public ActivityRecognitionEvent createFromParcel(Parcel source) { + String activity = source.readString(); + int eventType = source.readInt(); + long timestampNs = source.readLong(); + + return new ActivityRecognitionEvent(activity, eventType, timestampNs); + } + + @Override + public ActivityRecognitionEvent[] newArray(int size) { + return new ActivityRecognitionEvent[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int flags) { + parcel.writeString(mActivity); + parcel.writeInt(mEventType); + parcel.writeLong(mTimestampNs); + } +} diff --git a/core/java/android/hardware/location/ActivityRecognitionHardware.java b/core/java/android/hardware/location/ActivityRecognitionHardware.java new file mode 100644 index 0000000..a4ce4ac --- /dev/null +++ b/core/java/android/hardware/location/ActivityRecognitionHardware.java @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package android.hardware.location; + +import android.Manifest; +import android.content.Context; +import android.os.RemoteCallbackList; +import android.os.RemoteException; +import android.text.TextUtils; +import android.util.Log; + +/** + * A class that implements an {@link IActivityRecognitionHardware} backed up by the Activity + * Recognition HAL. + * + * @hide + */ +public class ActivityRecognitionHardware extends IActivityRecognitionHardware.Stub { + private static final String TAG = "ActivityRecognitionHardware"; + + private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE; + private static final int INVALID_ACTIVITY_TYPE = -1; + private static final int NATIVE_SUCCESS_RESULT = 0; + + private static ActivityRecognitionHardware sSingletonInstance = null; + private static final Object sSingletonInstanceLock = new Object(); + + private final Context mContext; + private final String[] mSupportedActivities; + + private final RemoteCallbackList<IActivityRecognitionHardwareSink> mSinks = + new RemoteCallbackList<IActivityRecognitionHardwareSink>(); + + private static class Event { + public int activity; + public int type; + public long timestamp; + } + + private ActivityRecognitionHardware(Context context) { + nativeInitialize(); + + mContext = context; + mSupportedActivities = fetchSupportedActivities(); + } + + public static ActivityRecognitionHardware getInstance(Context context) { + synchronized (sSingletonInstanceLock) { + if (sSingletonInstance == null) { + sSingletonInstance = new ActivityRecognitionHardware(context); + } + + return sSingletonInstance; + } + } + + public static boolean isSupported() { + return nativeIsSupported(); + } + + @Override + public String[] getSupportedActivities() { + checkPermissions(); + return mSupportedActivities; + } + + @Override + public boolean isActivitySupported(String activity) { + checkPermissions(); + int activityType = getActivityType(activity); + return activityType != INVALID_ACTIVITY_TYPE; + } + + @Override + public boolean registerSink(IActivityRecognitionHardwareSink sink) { + checkPermissions(); + return mSinks.register(sink); + } + + @Override + public boolean unregisterSink(IActivityRecognitionHardwareSink sink) { + checkPermissions(); + return mSinks.unregister(sink); + } + + @Override + public boolean enableActivityEvent(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; + } + + @Override + public boolean disableActivityEvent(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; + } + + @Override + public boolean flush() { + checkPermissions(); + int result = nativeFlush(); + return result == NATIVE_SUCCESS_RESULT; + } + + /** + * Called by the Activity-Recognition HAL. + */ + private void onActivityChanged(Event[] events) { + int size = mSinks.beginBroadcast(); + if (size == 0 || events == null || events.length == 0) { + 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); + + 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(); + + } + + private String getActivityName(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 int getActivityType(String activity) { + if (TextUtils.isEmpty(activity)) { + return INVALID_ACTIVITY_TYPE; + } + + int supporteActivitiesLength = mSupportedActivities.length; + for (int i = 0; i < supporteActivitiesLength; ++i) { + if (activity.equals(mSupportedActivities[i])) { + return i; + } + } + + return INVALID_ACTIVITY_TYPE; + } + + private void checkPermissions() { + String message = String.format( + "Permission '%s' not granted to access ActivityRecognitionHardware", + HARDWARE_PERMISSION); + mContext.enforceCallingPermission(HARDWARE_PERMISSION, message); + } + + private static String[] fetchSupportedActivities() { + String[] supportedActivities = nativeGetSupportedActivities(); + if (supportedActivities != null) { + return supportedActivities; + } + + return new String[0]; + } + + // native bindings + static { nativeClassInit(); } + + private static native void nativeClassInit(); + private static native void nativeInitialize(); + private static native void nativeRelease(); + private static native boolean nativeIsSupported(); + private static native String[] nativeGetSupportedActivities(); + private static native int nativeEnableActivityEvent( + int activityType, + int eventType, + long reportLatenceNs); + private static native int nativeDisableActivityEvent(int activityType, int eventType); + private static native int nativeFlush(); +} diff --git a/core/java/android/hardware/location/IActivityRecognitionHardware.aidl b/core/java/android/hardware/location/IActivityRecognitionHardware.aidl new file mode 100644 index 0000000..bc6b183 --- /dev/null +++ b/core/java/android/hardware/location/IActivityRecognitionHardware.aidl @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2014, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/license/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + +import android.hardware.location.IActivityRecognitionHardwareSink; + +/** + * Activity Recognition Hardware provider interface. + * This interface can be used to implement hardware based activity recognition. + * + * @hide + */ +interface IActivityRecognitionHardware { + /** + * Gets an array of supported activities by hardware. + */ + String[] getSupportedActivities(); + + /** + * Returns true if the given activity is supported, false otherwise. + */ + boolean isActivitySupported(in String activityType); + + /** + * Registers a sink with Hardware Activity-Recognition. + */ + boolean registerSink(in IActivityRecognitionHardwareSink sink); + + /** + * Unregisters a sink with Hardware Activity-Recognition. + */ + boolean unregisterSink(in IActivityRecognitionHardwareSink sink); + + /** + * Enables tracking of a given activity/event type, if the activity is supported. + */ + boolean enableActivityEvent(in String activityType, int eventType, long reportLatencyNs); + + /** + * Disables tracking of a given activity/eventy type. + */ + boolean disableActivityEvent(in String activityType, int eventType); + + /** + * Requests hardware for all the activity events detected up to the given point in time. + */ + boolean flush(); +}
\ No newline at end of file diff --git a/core/java/android/hardware/location/IActivityRecognitionHardwareSink.aidl b/core/java/android/hardware/location/IActivityRecognitionHardwareSink.aidl new file mode 100644 index 0000000..21c8e87 --- /dev/null +++ b/core/java/android/hardware/location/IActivityRecognitionHardwareSink.aidl @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2014, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/license/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + +import android.hardware.location.ActivityChangedEvent; + +/** + * Activity Recognition Hardware provider Sink interface. + * This interface can be used to implement sinks to receive activity notifications. + * + * @hide + */ +interface IActivityRecognitionHardwareSink { + /** + * Activity changed event. + */ + void onActivityChanged(in ActivityChangedEvent event); +}
\ No newline at end of file diff --git a/core/java/android/hardware/location/IActivityRecognitionHardwareWatcher.aidl b/core/java/android/hardware/location/IActivityRecognitionHardwareWatcher.aidl new file mode 100644 index 0000000..0507f52 --- /dev/null +++ b/core/java/android/hardware/location/IActivityRecognitionHardwareWatcher.aidl @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2014, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/license/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + +import android.hardware.location.IActivityRecognitionHardware; + +/** + * Activity Recognition Hardware watcher. This interface can be used to receive interfaces to + * implementations of {@link IActivityRecognitionHardware}. + * + * @hide + */ +interface IActivityRecognitionHardwareWatcher { + /** + * Hardware Activity-Recognition availability event. + */ + void onInstanceChanged(in IActivityRecognitionHardware instance); +}
\ No newline at end of file |
