SensorManager.h revision 8034fc63a06768678a7afe0bfcc7070a65eda1ab
1/*
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
20#include <map>
21
22#include <stdint.h>
23#include <sys/types.h>
24
25#include <binder/IBinder.h>
26#include <binder/IPCThreadState.h>
27#include <binder/IServiceManager.h>
28
29#include <utils/Errors.h>
30#include <utils/RefBase.h>
31#include <utils/Singleton.h>
32#include <utils/Vector.h>
33#include <utils/String8.h>
34
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;
48// ----------------------------------------------------------------------------
49
50class SensorManager :
51    public ASensorManager
52{
53public:
54    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
104    SensorManager(const String16& opPackageName);
105    ~SensorManager();
106
107    ssize_t getSensorList(Sensor const* const** list) const;
108    Sensor const* getDefaultSensor(int type);
109    sp<SensorEventQueue> createEventQueue(String8 packageName = String8(""), int mode = 0);
110    bool isDataInjectionEnabled();
111
112private:
113    // DeathRecipient interface
114    void sensorManagerDied();
115
116    status_t assertStateLocked() const;
117
118private:
119    static Mutex sLock;
120    static std::map<String16, SensorManager*> sPackageInstances;
121
122    mutable Mutex mLock;
123    mutable sp<ISensorServer> mSensorServer;
124    mutable Sensor const** mSensorList;
125    mutable Vector<Sensor> mSensors;
126    mutable sp<IBinder::DeathRecipient> mDeathObserver;
127    const String16 mOpPackageName;
128};
129
130// ----------------------------------------------------------------------------
131}; // namespace android
132
133#endif // ANDROID_GUI_SENSOR_MANAGER_H
134