blob: 993415126e6c50a9059592ac4f1f4ef15ce663de [file] [log] [blame]
Mathias Agopian589ce852010-07-13 22:21:56 -07001/*
2 * Copyright (C) 2010 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
Mathias Agopiana7352c92010-07-14 23:41:37 -070017#define LOG_TAG "Sensors"
18
Mathias Agopian589ce852010-07-13 22:21:56 -070019#include <stdint.h>
20#include <sys/types.h>
21
22#include <utils/Errors.h>
23#include <utils/RefBase.h>
24#include <utils/Singleton.h>
25
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070026#include <binder/IBinder.h>
Mathias Agopiana7352c92010-07-14 23:41:37 -070027#include <binder/IServiceManager.h>
28
Mathias Agopian589ce852010-07-13 22:21:56 -070029#include <gui/ISensorServer.h>
30#include <gui/ISensorEventConnection.h>
31#include <gui/Sensor.h>
32#include <gui/SensorManager.h>
33#include <gui/SensorEventQueue.h>
34
35// ----------------------------------------------------------------------------
36namespace android {
37// ----------------------------------------------------------------------------
38
Aravind Akellae2806cb2015-07-29 18:03:48 -070039android::Mutex android::SensorManager::sLock;
40std::map<String16, SensorManager*> android::SensorManager::sPackageInstances;
41
42SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) {
43 Mutex::Autolock _l(sLock);
44 SensorManager* sensorManager;
45 std::map<String16, SensorManager*>::iterator iterator =
46 sPackageInstances.find(packageName);
47
48 if (iterator != sPackageInstances.end()) {
49 sensorManager = iterator->second;
50 } else {
51 String16 opPackageName = packageName;
52
53 // It is possible that the calling code has no access to the package name.
54 // In this case we will get the packages for the calling UID and pick the
55 // first one for attributing the app op. This will work correctly for
56 // runtime permissions as for legacy apps we will toggle the app op for
57 // all packages in the UID. The caveat is that the operation may be attributed
58 // to the wrong package and stats based on app ops may be slightly off.
59 if (opPackageName.size() <= 0) {
60 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
61 if (binder != 0) {
62 const uid_t uid = IPCThreadState::self()->getCallingUid();
63 Vector<String16> packages;
64 interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages);
65 if (!packages.isEmpty()) {
66 opPackageName = packages[0];
67 } else {
68 ALOGE("No packages for calling UID");
69 }
70 } else {
71 ALOGE("Cannot get permission service");
72 }
73 }
74
75 sensorManager = new SensorManager(opPackageName);
76
77 // If we had no package name, we looked it up from the UID and the sensor
78 // manager instance we created should also be mapped to the empty package
79 // name, to avoid looking up the packages for a UID and get the same result.
80 if (packageName.size() <= 0) {
81 sPackageInstances.insert(std::make_pair(String16(), sensorManager));
82 }
83
84 // Stash the per package sensor manager.
85 sPackageInstances.insert(std::make_pair(opPackageName, sensorManager));
86 }
87
88 return *sensorManager;
89}
90
Svetoslavb412f6e2015-04-29 16:50:41 -070091SensorManager::SensorManager(const String16& opPackageName)
92 : mSensorList(0), mOpPackageName(opPackageName)
Mathias Agopian589ce852010-07-13 22:21:56 -070093{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070094 // okay we're not locked here, but it's not needed during construction
95 assertStateLocked();
Mathias Agopian589ce852010-07-13 22:21:56 -070096}
97
98SensorManager::~SensorManager()
99{
Mathias Agopiana7352c92010-07-14 23:41:37 -0700100 free(mSensorList);
Mathias Agopian589ce852010-07-13 22:21:56 -0700101}
102
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700103void SensorManager::sensorManagerDied()
104{
105 Mutex::Autolock _l(mLock);
106 mSensorServer.clear();
107 free(mSensorList);
108 mSensorList = NULL;
109 mSensors.clear();
110}
111
112status_t SensorManager::assertStateLocked() const {
113 if (mSensorServer == NULL) {
Aravind Akellae2806cb2015-07-29 18:03:48 -0700114 // try for 10 seconds before giving up ...
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700115 const String16 name("sensorservice");
Aravind Akellae2806cb2015-07-29 18:03:48 -0700116 for (int i = 0;i < 10; i++) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700117 status_t err = getService(name, &mSensorServer);
118 if (err == NAME_NOT_FOUND) {
Aravind Akellae2806cb2015-07-29 18:03:48 -0700119 sleep(1);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700120 continue;
121 }
122 if (err != NO_ERROR) {
123 return err;
124 }
125 break;
126 }
127
128 class DeathObserver : public IBinder::DeathRecipient {
129 SensorManager& mSensorManger;
130 virtual void binderDied(const wp<IBinder>& who) {
Steve Block32397c12012-01-05 23:22:43 +0000131 ALOGW("sensorservice died [%p]", who.unsafe_get());
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700132 mSensorManger.sensorManagerDied();
133 }
134 public:
135 DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { }
136 };
137
Aravind Akellae2806cb2015-07-29 18:03:48 -0700138 if (mSensorServer == NULL) {
139 ALOGE("FATAL getsensorservice returned NULL");
140 }
141
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700142 mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800143 IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700144
Svetoslavb412f6e2015-04-29 16:50:41 -0700145 mSensors = mSensorServer->getSensorList(mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700146 size_t count = mSensors.size();
Dan Stozad723bd72014-11-18 10:24:03 -0800147 mSensorList =
148 static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700149 for (size_t i=0 ; i<count ; i++) {
150 mSensorList[i] = mSensors.array() + i;
151 }
152 }
153
154 return NO_ERROR;
155}
156
Mathias Agopiana7352c92010-07-14 23:41:37 -0700157ssize_t SensorManager::getSensorList(Sensor const* const** list) const
Mathias Agopian589ce852010-07-13 22:21:56 -0700158{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700159 Mutex::Autolock _l(mLock);
160 status_t err = assertStateLocked();
161 if (err < 0) {
Dan Stozad723bd72014-11-18 10:24:03 -0800162 return static_cast<ssize_t>(err);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700163 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700164 *list = mSensorList;
Dan Stozad723bd72014-11-18 10:24:03 -0800165 return static_cast<ssize_t>(mSensors.size());
Mathias Agopian589ce852010-07-13 22:21:56 -0700166}
167
Mathias Agopiana7352c92010-07-14 23:41:37 -0700168Sensor const* SensorManager::getDefaultSensor(int type)
Mathias Agopian589ce852010-07-13 22:21:56 -0700169{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700170 Mutex::Autolock _l(mLock);
171 if (assertStateLocked() == NO_ERROR) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700172 bool wakeUpSensor = false;
173 // For the following sensor types, return a wake-up sensor. These types are by default
174 // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
175 // a non_wake-up version.
176 if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
177 type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE ||
178 type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE) {
179 wakeUpSensor = true;
180 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700181 // For now we just return the first sensor of that type we find.
182 // in the future it will make sense to let the SensorService make
183 // that decision.
184 for (size_t i=0 ; i<mSensors.size() ; i++) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700185 if (mSensorList[i]->getType() == type &&
186 mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700187 return mSensorList[i];
Aravind Akellab37ba392014-08-05 14:53:07 -0700188 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700189 }
Mathias Agopiana7352c92010-07-14 23:41:37 -0700190 }
191 return NULL;
Mathias Agopian589ce852010-07-13 22:21:56 -0700192}
193
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700194sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) {
Mathias Agopianbe58de02011-10-16 00:38:30 -0700195 sp<SensorEventQueue> queue;
196
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700197 Mutex::Autolock _l(mLock);
198 while (assertStateLocked() == NO_ERROR) {
199 sp<ISensorEventConnection> connection =
Svetoslavb412f6e2015-04-29 16:50:41 -0700200 mSensorServer->createSensorEventConnection(packageName, mode, mOpPackageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700201 if (connection == NULL) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700202 // SensorService just died or the app doesn't have required permissions.
203 ALOGE("createEventQueue: connection is NULL.");
204 return NULL;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700205 }
206 queue = new SensorEventQueue(connection);
207 break;
Mathias Agopianbe58de02011-10-16 00:38:30 -0700208 }
Mathias Agopianbe58de02011-10-16 00:38:30 -0700209 return queue;
Mathias Agopian589ce852010-07-13 22:21:56 -0700210}
211
Aravind Akella841a5922015-06-29 12:37:48 -0700212bool SensorManager::isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700213 Mutex::Autolock _l(mLock);
214 if (assertStateLocked() == NO_ERROR) {
Aravind Akella841a5922015-06-29 12:37:48 -0700215 return mSensorServer->isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700216 }
Aravind Akella841a5922015-06-29 12:37:48 -0700217 return false;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700218}
219
Mathias Agopian589ce852010-07-13 22:21:56 -0700220// ----------------------------------------------------------------------------
221}; // namespace android