Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 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 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 17 | #define LOG_TAG "Sensors" |
| 18 | |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 19 | #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 Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 26 | #include <binder/IServiceManager.h> |
| 27 | |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 28 | #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 | // ---------------------------------------------------------------------------- |
| 35 | namespace android { |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorManager) |
| 39 | |
| 40 | SensorManager::SensorManager() |
| 41 | : mSensorList(0) |
| 42 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 43 | const String16 name("sensorservice"); |
| 44 | while (getService(name, &mSensorServer) != NO_ERROR) { |
| 45 | usleep(250000); |
| 46 | } |
| 47 | |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 48 | mSensors = mSensorServer->getSensorList(); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 49 | 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 Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | SensorManager::~SensorManager() |
| 57 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 58 | free(mSensorList); |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 61 | ssize_t SensorManager::getSensorList(Sensor const* const** list) const |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 62 | { |
| 63 | *list = mSensorList; |
| 64 | return mSensors.size(); |
| 65 | } |
| 66 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 67 | Sensor const* SensorManager::getDefaultSensor(int type) |
Mathias Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 68 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 69 | // 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 Agopian | b957b9d | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | sp<SensorEventQueue> SensorManager::createEventQueue() |
| 80 | { |
| 81 | sp<SensorEventQueue> result = new SensorEventQueue( |
| 82 | mSensorServer->createSensorEventConnection()); |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | // ---------------------------------------------------------------------------- |
| 87 | }; // namespace android |