blob: 37960673cc46de93642cf754e259af96b3f4c847 [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
17#ifndef ANDROID_GUI_SENSOR_MANAGER_H
18#define ANDROID_GUI_SENSOR_MANAGER_H
19
Svet Ganov5fa32d42015-05-07 10:50:59 -070020#include <map>
21
Mathias Agopian589ce852010-07-13 22:21:56 -070022#include <stdint.h>
23#include <sys/types.h>
24
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070025#include <binder/IBinder.h>
Svet Ganov5fa32d42015-05-07 10:50:59 -070026#include <binder/IPCThreadState.h>
27#include <binder/IServiceManager.h>
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070028
Mathias Agopian589ce852010-07-13 22:21:56 -070029#include <utils/Errors.h>
30#include <utils/RefBase.h>
31#include <utils/Singleton.h>
32#include <utils/Vector.h>
Aravind Akella78442202015-03-26 15:58:59 -070033#include <utils/String8.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070034
35#include <gui/SensorEventQueue.h>
36
37// ----------------------------------------------------------------------------
38// Concrete types for the NDK
39struct ASensorManager { };
40
41// ----------------------------------------------------------------------------
42namespace android {
43// ----------------------------------------------------------------------------
44
45class ISensorServer;
46class Sensor;
47class SensorEventQueue;
Mathias Agopian589ce852010-07-13 22:21:56 -070048// ----------------------------------------------------------------------------
49
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070050class SensorManager :
Svetoslavb412f6e2015-04-29 16:50:41 -070051 public ASensorManager
Mathias Agopian589ce852010-07-13 22:21:56 -070052{
53public:
Svet Ganov5fa32d42015-05-07 10:50:59 -070054 static SensorManager& getInstanceForPackage(const String16& packageName) {
55 Mutex::Autolock _l(sLock);
56
57 SensorManager* sensorManager;
58 std::map<String16, SensorManager*>::iterator iterator =
59 sPackageInstances.find(packageName);
60
61 if (iterator != sPackageInstances.end()) {
62 sensorManager = iterator->second;
63 } else {
64 String16 opPackageName = packageName;
65
66 // It is possible that the calling code has no access to the package name.
67 // In this case we will get the packages for the calling UID and pick the
68 // first one for attributing the app op. This will work correctly for
69 // runtime permissions as for legacy apps we will toggle the app op for
70 // all packages in the UID. The caveat is that the operation may be attributed
71 // to the wrong package and stats based on app ops may be slightly off.
72 if (opPackageName.size() <= 0) {
73 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
74 if (binder != 0) {
75 const uid_t uid = IPCThreadState::self()->getCallingUid();
76 Vector<String16> packages;
77 interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages);
78 if (!packages.isEmpty()) {
79 opPackageName = packages[0];
80 } else {
81 ALOGE("No packages for calling UID");
82 }
83 } else {
84 ALOGE("Cannot get permission service");
85 }
86 }
87
88 sensorManager = new SensorManager(opPackageName);
89
90 // If we had no package name, we looked it up from the UID and the sensor
91 // manager instance we created should also be mapped to the empty package
92 // name, to avoid looking up the packages for a UID and get the same result.
93 if (packageName.size() <= 0) {
94 sPackageInstances.insert(std::make_pair(String16(), sensorManager));
95 }
96
97 // Stash the per package sensor manager.
98 sPackageInstances.insert(std::make_pair(opPackageName, sensorManager));
99 }
100
101 return *sensorManager;
102 }
103
Narayan Kamath8034fc62015-07-29 09:36:05 +0000104 SensorManager(const String16& opPackageName);
Mathias Agopian589ce852010-07-13 22:21:56 -0700105 ~SensorManager();
106
Mathias Agopiana7352c92010-07-14 23:41:37 -0700107 ssize_t getSensorList(Sensor const* const** list) const;
108 Sensor const* getDefaultSensor(int type);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700109 sp<SensorEventQueue> createEventQueue(String8 packageName = String8(""), int mode = 0);
Aravind Akella841a5922015-06-29 12:37:48 -0700110 bool isDataInjectionEnabled();
Mathias Agopian589ce852010-07-13 22:21:56 -0700111
112private:
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700113 // DeathRecipient interface
114 void sensorManagerDied();
115
116 status_t assertStateLocked() const;
117
118private:
Svet Ganov5fa32d42015-05-07 10:50:59 -0700119 static Mutex sLock;
120 static std::map<String16, SensorManager*> sPackageInstances;
121
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700122 mutable Mutex mLock;
123 mutable sp<ISensorServer> mSensorServer;
124 mutable Sensor const** mSensorList;
125 mutable Vector<Sensor> mSensors;
126 mutable sp<IBinder::DeathRecipient> mDeathObserver;
Svetoslavb412f6e2015-04-29 16:50:41 -0700127 const String16 mOpPackageName;
Mathias Agopian589ce852010-07-13 22:21:56 -0700128};
129
Mathias Agopian589ce852010-07-13 22:21:56 -0700130// ----------------------------------------------------------------------------
131}; // namespace android
132
133#endif // ANDROID_GUI_SENSOR_MANAGER_H