blob: d719efb5f11e94b0c45600ad938afc7fee4b0f51 [file] [log] [blame]
Mathias Agopianb957b9d2010-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 Agopian1bf79782010-07-14 23:41:37 -070017#define LOG_TAG "Sensors"
18
Mathias Agopianb957b9d2010-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 Agopian1bf79782010-07-14 23:41:37 -070026#include <binder/IServiceManager.h>
27
Mathias Agopianb957b9d2010-07-13 22:21:56 -070028#include <gui/ISensorServer.h>
29#include <gui/ISensorEventConnection.h>
30#include <gui/Sensor.h>
31#include <gui/SensorManager.h>
32#include <gui/SensorEventQueue.h>
33
34// ----------------------------------------------------------------------------
35namespace android {
36// ----------------------------------------------------------------------------
37
38ANDROID_SINGLETON_STATIC_INSTANCE(SensorManager)
39
40SensorManager::SensorManager()
41 : mSensorList(0)
42{
Mathias Agopian1bf79782010-07-14 23:41:37 -070043 const String16 name("sensorservice");
44 while (getService(name, &mSensorServer) != NO_ERROR) {
45 usleep(250000);
46 }
47
Mathias Agopianb957b9d2010-07-13 22:21:56 -070048 mSensors = mSensorServer->getSensorList();
Mathias Agopian1bf79782010-07-14 23:41:37 -070049 size_t count = mSensors.size();
50 mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*));
51 for (size_t i=0 ; i<count ; i++) {
52 mSensorList[i] = mSensors.array() + i;
53 }
Mathias Agopianb957b9d2010-07-13 22:21:56 -070054}
55
56SensorManager::~SensorManager()
57{
Mathias Agopian1bf79782010-07-14 23:41:37 -070058 free(mSensorList);
Mathias Agopianb957b9d2010-07-13 22:21:56 -070059}
60
Mathias Agopian1bf79782010-07-14 23:41:37 -070061ssize_t SensorManager::getSensorList(Sensor const* const** list) const
Mathias Agopianb957b9d2010-07-13 22:21:56 -070062{
63 *list = mSensorList;
64 return mSensors.size();
65}
66
Mathias Agopian1bf79782010-07-14 23:41:37 -070067Sensor const* SensorManager::getDefaultSensor(int type)
Mathias Agopianb957b9d2010-07-13 22:21:56 -070068{
Mathias Agopian1bf79782010-07-14 23:41:37 -070069 // For now we just return the first sensor of that type we find.
70 // in the future it will make sense to let the SensorService make
71 // that decision.
72 for (size_t i=0 ; i<mSensors.size() ; i++) {
73 if (mSensorList[i]->getType() == type)
74 return mSensorList[i];
75 }
76 return NULL;
Mathias Agopianb957b9d2010-07-13 22:21:56 -070077}
78
79sp<SensorEventQueue> SensorManager::createEventQueue()
80{
81 sp<SensorEventQueue> result = new SensorEventQueue(
82 mSensorServer->createSensorEventConnection());
83 return result;
84}
85
86// ----------------------------------------------------------------------------
87}; // namespace android