2010-07-14 05:21:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
#define LOG_TAG "Sensors"
|
|
|
|
|
2010-07-14 05:21:56 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <utils/Errors.h>
|
|
|
|
#include <utils/RefBase.h>
|
|
|
|
#include <utils/Singleton.h>
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
#include <binder/IServiceManager.h>
|
|
|
|
|
2010-07-14 05:21:56 +00:00
|
|
|
#include <gui/ISensorServer.h>
|
|
|
|
#include <gui/ISensorEventConnection.h>
|
|
|
|
#include <gui/Sensor.h>
|
|
|
|
#include <gui/SensorManager.h>
|
|
|
|
#include <gui/SensorEventQueue.h>
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
ANDROID_SINGLETON_STATIC_INSTANCE(SensorManager)
|
|
|
|
|
|
|
|
SensorManager::SensorManager()
|
|
|
|
: mSensorList(0)
|
|
|
|
{
|
2010-07-15 06:41:37 +00:00
|
|
|
const String16 name("sensorservice");
|
|
|
|
while (getService(name, &mSensorServer) != NO_ERROR) {
|
|
|
|
usleep(250000);
|
|
|
|
}
|
|
|
|
|
2010-07-14 05:21:56 +00:00
|
|
|
mSensors = mSensorServer->getSensorList();
|
2010-07-15 06:41:37 +00:00
|
|
|
size_t count = mSensors.size();
|
|
|
|
mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*));
|
|
|
|
for (size_t i=0 ; i<count ; i++) {
|
|
|
|
mSensorList[i] = mSensors.array() + i;
|
|
|
|
}
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SensorManager::~SensorManager()
|
|
|
|
{
|
2010-07-15 06:41:37 +00:00
|
|
|
free(mSensorList);
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
ssize_t SensorManager::getSensorList(Sensor const* const** list) const
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
|
|
|
*list = mSensorList;
|
|
|
|
return mSensors.size();
|
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
Sensor const* SensorManager::getDefaultSensor(int type)
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
2010-07-15 06:41:37 +00:00
|
|
|
// For now we just return the first sensor of that type we find.
|
|
|
|
// in the future it will make sense to let the SensorService make
|
|
|
|
// that decision.
|
|
|
|
for (size_t i=0 ; i<mSensors.size() ; i++) {
|
|
|
|
if (mSensorList[i]->getType() == type)
|
|
|
|
return mSensorList[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sp<SensorEventQueue> SensorManager::createEventQueue()
|
|
|
|
{
|
2011-10-16 07:38:30 +00:00
|
|
|
sp<SensorEventQueue> queue;
|
|
|
|
|
|
|
|
if (mSensorServer == NULL) {
|
|
|
|
LOGE("createEventQueue: mSensorSever is NULL");
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<ISensorEventConnection> connection =
|
|
|
|
mSensorServer->createSensorEventConnection();
|
|
|
|
if (connection == NULL) {
|
|
|
|
LOGE("createEventQueue: connection is NULL");
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue = new SensorEventQueue(connection);
|
|
|
|
|
|
|
|
return queue;
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|