1 /* 2 * Copyright (C) 2015 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 #ifndef ACTIVITY_H_ 18 19 #define ACTIVITY_H_ 20 21 #include <hardware/activity_recognition.h> 22 #include <media/stagefright/foundation/ABase.h> 23 #include <utils/KeyedVector.h> 24 #include <utils/Vector.h> 25 26 #include "activityeventhandler.h" 27 #include "hubconnection.h" 28 29 namespace android { 30 31 class ActivityContext : public ActivityEventHandler { 32 public: 33 activity_recognition_device_t device; 34 35 explicit ActivityContext(const struct hw_module_t *module); 36 ~ActivityContext(); 37 38 bool getHubAlive(); 39 40 void registerActivityCallback( 41 const activity_recognition_callback_procs_t *callback); 42 43 int enableActivityEvent(uint32_t activity_handle, 44 uint32_t event_type, int64_t max_report_latency_ns); 45 46 int disableActivityEvent(uint32_t activity_handle, uint32_t event_type); 47 48 int flush(); 49 50 // ActivityEventHandler interface. 51 virtual void OnActivityEvent(int sensorIndex, uint8_t eventIndex, 52 uint64_t whenNs) override; 53 virtual void OnFlush() override; 54 virtual void OnSensorHubReset() override; 55 56 private: 57 android::sp<android::HubConnection> mHubConnection; 58 59 android::Mutex mCallbackLock; 60 const activity_recognition_callback_procs_t *mCallback; 61 62 struct ActivityEvent { 63 uint8_t eventIndex; 64 int sensorIndex; 65 uint64_t whenNs; 66 }; 67 68 // Whether or not the newest published event index is known. When the AR HAL 69 // is initially started this is set to false to allow any event index from 70 // the sensor hub. It is also set to false when a hub reset occurs. 71 bool mNewestPublishedEventIndexIsKnown; 72 73 // The index of the newest published event. The next event from the sensor 74 // hub must follow this event or else it will be pushed into a list of 75 // events to be published once the gap in events has been received. 76 uint8_t mNewestPublishedEventIndex; 77 78 // The timestamp of the most recently published event. If the absolute value 79 // of the delta of the next timestamp to the current timestamp is below some 80 // threshold, this timestamp will be reused. This is used to ensure that 81 // activity transitions share the same timestamp and works around agressive 82 // AP->ContextHub time synchronization mechansims. 83 uint64_t mNewestPublishedTimestamp; 84 85 // The list of unpublished events. These are published once the next 86 // event arrives and is greater than mNewestPublishedEventIndex by 1 87 // (wrapping across 255). 88 Vector<ActivityEvent> mUnpublishedEvents; 89 90 // Track the number of flush events sent to the sensor hub. 91 int mOutstandingFlushEvents; 92 93 // Publishes remaining unpublished events. 94 void PublishUnpublishedEvents(); 95 96 // Publishes an AR event to the AR HAL client. 97 void PublishEvent(const ActivityEvent& event); 98 99 // Searches for very old AR events, discards them and publishes EVENT_EXIT 100 // transitions for all activities. 101 void DiscardExpiredUnpublishedEvents(uint64_t whenNs); 102 103 DISALLOW_EVIL_CONSTRUCTORS(ActivityContext); 104 }; 105 106 } // namespace android 107 108 #endif // ACTIVITY_H_ 109