• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.location.provider;
18 
19 import com.android.internal.util.Preconditions;
20 
21 import android.hardware.location.IActivityRecognitionHardware;
22 import android.hardware.location.IActivityRecognitionHardwareSink;
23 import android.os.RemoteException;
24 
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashSet;
28 
29 /**
30  * A class that exposes {@link IActivityRecognitionHardware} functionality to unbundled services.
31  * @hide
32  */
33 public final class ActivityRecognitionProvider {
34     private final IActivityRecognitionHardware mService;
35     private final HashSet<Sink> mSinkSet = new HashSet<>();
36 
37     // the following constants must remain in sync with activity_recognition.h
38 
39     public static final String ACTIVITY_IN_VEHICLE = "android.activity_recognition.in_vehicle";
40     public static final String ACTIVITY_ON_BICYCLE = "android.activity_recognition.on_bicycle";
41     public static final String ACTIVITY_WALKING = "android.activity_recognition.walking";
42     public static final String ACTIVITY_RUNNING = "android.activity_recognition.running";
43     public static final String ACTIVITY_STILL = "android.activity_recognition.still";
44     public static final String ACTIVITY_TILTING = "android.activity_recognition.tilting";
45 
46     // NOTE: when adding an additional EVENT_TYPE_, EVENT_TYPE_COUNT needs to be updated in
47     // android.hardware.location.ActivityRecognitionHardware
48     public static final int EVENT_TYPE_FLUSH_COMPLETE = 0;
49     public static final int EVENT_TYPE_ENTER = 1;
50     public static final int EVENT_TYPE_EXIT = 2;
51 
52     // end constants activity_recognition.h
53 
54     /**
55      * Used to receive Activity-Recognition events.
56      */
57     public interface Sink {
onActivityChanged(ActivityChangedEvent event)58         void onActivityChanged(ActivityChangedEvent event);
59     }
60 
ActivityRecognitionProvider(IActivityRecognitionHardware service)61     public ActivityRecognitionProvider(IActivityRecognitionHardware service)
62             throws RemoteException {
63         Preconditions.checkNotNull(service);
64         mService = service;
65         mService.registerSink(new SinkTransport());
66     }
67 
getSupportedActivities()68     public String[] getSupportedActivities() throws RemoteException {
69         return mService.getSupportedActivities();
70     }
71 
isActivitySupported(String activity)72     public boolean isActivitySupported(String activity) throws RemoteException {
73         return mService.isActivitySupported(activity);
74     }
75 
registerSink(Sink sink)76     public void registerSink(Sink sink) {
77         Preconditions.checkNotNull(sink);
78         synchronized (mSinkSet) {
79             mSinkSet.add(sink);
80         }
81     }
82 
83     // TODO: if this functionality is exposed to 3rd party developers, handle unregistration (here
84     // and in the service) of all sinks while failing to disable all events
unregisterSink(Sink sink)85     public void unregisterSink(Sink sink) {
86         Preconditions.checkNotNull(sink);
87         synchronized (mSinkSet) {
88             mSinkSet.remove(sink);
89         }
90     }
91 
enableActivityEvent(String activity, int eventType, long reportLatencyNs)92     public boolean enableActivityEvent(String activity, int eventType, long reportLatencyNs)
93             throws RemoteException {
94         return mService.enableActivityEvent(activity, eventType, reportLatencyNs);
95     }
96 
disableActivityEvent(String activity, int eventType)97     public boolean disableActivityEvent(String activity, int eventType) throws RemoteException {
98         return mService.disableActivityEvent(activity, eventType);
99     }
100 
flush()101     public boolean flush() throws RemoteException {
102         return mService.flush();
103     }
104 
105     private final class SinkTransport extends IActivityRecognitionHardwareSink.Stub {
106         @Override
onActivityChanged(android.hardware.location.ActivityChangedEvent event)107         public void onActivityChanged(android.hardware.location.ActivityChangedEvent event) {
108             Collection<Sink> sinks;
109             synchronized (mSinkSet) {
110                 if (mSinkSet.isEmpty()) {
111                     return;
112                 }
113                 sinks = new ArrayList<>(mSinkSet);
114             }
115 
116             // translate the event from platform internal and GmsCore types
117             ArrayList<ActivityRecognitionEvent> gmsEvents = new ArrayList<>();
118             for (android.hardware.location.ActivityRecognitionEvent reportingEvent
119                     : event.getActivityRecognitionEvents()) {
120                 ActivityRecognitionEvent gmsEvent = new ActivityRecognitionEvent(
121                         reportingEvent.getActivity(),
122                         reportingEvent.getEventType(),
123                         reportingEvent.getTimestampNs());
124                 gmsEvents.add(gmsEvent);
125             }
126             ActivityChangedEvent gmsEvent = new ActivityChangedEvent(gmsEvents);
127 
128             for (Sink sink : sinks) {
129                 sink.onActivityChanged(gmsEvent);
130             }
131         }
132     }
133 }
134