Home | History | Annotate | Download | only in gui
      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 #define LOG_TAG "Sensors"
     18 
     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 
     26 #include <binder/IServiceManager.h>
     27 
     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 {
     43     const String16 name("sensorservice");
     44     while (getService(name, &mSensorServer) != NO_ERROR) {
     45         usleep(250000);
     46     }
     47 
     48     mSensors = mSensorServer->getSensorList();
     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     }
     54 }
     55 
     56 SensorManager::~SensorManager()
     57 {
     58     free(mSensorList);
     59 }
     60 
     61 ssize_t SensorManager::getSensorList(Sensor const* const** list) const
     62 {
     63     *list = mSensorList;
     64     return mSensors.size();
     65 }
     66 
     67 Sensor const* SensorManager::getDefaultSensor(int type)
     68 {
     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;
     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
     88