让我们来看看SensorManager的代码
SensorManager框架层代码
@frameworks/base/core/Java/Android/hardware/SensorManager.java
- public SensorManager(Looper mainLooper) {
- mMainLooper = mainLooper;
-
- synchronized(sListeners) {
- if(!sSensorModuleInitialized) {
- sSensorModuleInitialized = true;
- nativeClassInit();
- sWindowManager = IWindowManager.Stub.asInterface(
- ServiceManager.getService("window"));
- if (sWindowManager != null) {
-
-
- try {
- sRotation = sWindowManager.watchRotation(
- newIRotationWatcher.Stub() {
- public voidonRotationChanged(int rotation) {
- SensorManager.this.onRotationChanged(rotation);
- }
- }
- );
- } catch (RemoteException e) {
- }
- }
-
-
- sensors_module_init();
- final ArrayList<Sensor> fullList = sFullSensorsList;
- int i = 0;
- do {
- Sensor sensor = new Sensor();
-
- i = sensors_module_get_next_sensor(sensor, i);
- if (i>=0) {
-
-
- sensor.setLegacyType(getLegacySensorType(sensor.getType()));
- fullList.add(sensor);
- sHandleToSensor.append(sensor.getHandle(), sensor);
- }
- }while (i>0);
-
- sPool= new SensorEventPool( sFullSensorsList.size()*2 );
- sSensorThread = new SensorThread();
- }
- }
- }
很明显nativeClassInit(),sensors_module_init(),sensors_module_get_next_sensor()都是本地实现的方法。
- private static native void nativeClassInit();
- private static native int sensors_module_init();
- private static native intsensors_module_get_next_sensor(Sensor sensor, int next);
根据之前看代码的经验可知,很可能在frameworks/base/core/对应一个jni目录下的存在其对应的本地代码:
- frameworks/base/core/java/android/hardware/SensorManager.java
- frameworks/base/core/jni/android_hardware_SensorManager.cpp
果不其然,在jni存在其本地代码,让我们来看下nativeClassInit函数:
@frameworks/base/core/jni/android_hardware_SensorManager.cpp
- static void
- nativeClassInit (JNIEnv *_env, jclass _this)
- {
- jclasssensorClass = _env->FindClass("android/hardware/Sensor");
- SensorOffsets& sensorOffsets = gSensorOffsets;
- sensorOffsets.name =_env->GetFieldID(sensorClass, "mName", "Ljava/lang/String;");
- sensorOffsets.vendor =_env->GetFieldID(sensorClass, "mVendor", "Ljava/lang/String;");
- sensorOffsets.version =_env->GetFieldID(sensorClass, "mVersion", "I");
- sensorOffsets.handle =_env->GetFieldID(sensorClass, "mHandle", "I");
- sensorOffsets.type = _env->GetFieldID(sensorClass,"mType", "I");
- sensorOffsets.range =_env->GetFieldID(sensorClass, "mMaxRange", "F");
- sensorOffsets.resolution =_env->GetFieldID(sensorClass, "mResolution","F");
- sensorOffsets.power =_env->GetFieldID(sensorClass, "mPower", "F");
- sensorOffsets.minDelay =_env->GetFieldID(sensorClass, "mMinDelay", "I");
- }
其代码比较简单,将Java框架层的Sensor类中的成员保存在本地代码中的gSensorOffsets 结构体中将来使用。
sensors_module_init()本地方法的实现:
- static jint
- sensors_module_ini(JNIEnv *env, jclass clazz)
- {
- SensorManager::getInstance();
- return 0;
- }
在本地代码中调用了SensorManager的getInstance方法,这又是一个典型的单例模式获得类的对象,注意这儿的SensorManager是本地的类,而不是Java层的SensorManager类。
本地SensorManager的定义
@frameworks/base/include/gui/SensorManager.h
- class SensorManager :
- publicASensorManager,
- publicSingleton<SensorManager>
- {
- public:
- SensorManager();
- ~SensorManager();
-
- ssize_tgetSensorList(Sensor const* const** list) const;
-
- Sensor const*getDefaultSensor(int type);
- sp<SensorEventQueue> createEventQueue();
- private:
-
- voidsensorManagerDied();
- status_tassertStateLocked() const;
- private:
- mutable MutexmLock;
- mutablesp<ISensorServer> mSensorServer;
- mutableSensor const** mSensorList;
- mutableVector<Sensor> mSensors;
- mutablesp<IBinder::DeathRecipient> mDeathObserver;
- };
注意SensorManager又继承了ASensorManager和泛型类Singleton<SensorManager>,而SensorManager类定义里没有getInstance所以其定义肯定是在ASensorManager或Singleton中。
@frameworks/base/include/utils/Singleton.h
- template <typename TYPE>
- class ANDROID_API Singleton
- {
- public:
-
- staticTYPE& getInstance() {
- Mutex::Autolock _l(sLock);
- TYPE*instance = sInstance;
- if(instance == 0) {
- instance = new TYPE();
- sInstance = instance;
- }
-
- return*instance;
- }
-
- static boolhasInstance() {
- Mutex::Autolock _l(sLock);
- returnsInstance != 0;
- }
-
- protected:
- ~Singleton(){ };
- Singleton() {};
-
- private:
- Singleton(const Singleton&);
- Singleton& operator = (const Singleton&);
- static MutexsLock;
- static TYPE*sInstance;
- };
-
- };
第一次调用getInstance方法时,创建泛型对象即:SensorManager,随后再调用该方法时返回第一次创建的泛型对象。
1) 本地SensorManager的创建
本地SensorManager是一个单例模式,其构造方法相对比较简单,它的主要工作交给了assertStateLocked方法:
@frameworks/base/libs/gui/SensorManager.cpp
- SensorManager::SensorManager()
- :mSensorList(0)
- {
-
- assertStateLocked();
- }
-
- status_t SensorManager::assertStateLocked() const {
- if(mSensorServer == NULL) {
-
- constString16 name("sensorservice");
- for (inti=0 ; i<4 ; i++) {
- status_t err = getService(name,&mSensorServer);
- if(err == NAME_NOT_FOUND) {
- usleep(250000);
- continue;
- }
-
- if(err != NO_ERROR) {
- return err;
- }
- break;
- }
-
- classDeathObserver : public IBinder::DeathRecipient {
- SensorManager& mSensorManger;
- virtual void binderDied(const wp<IBinder>& who) {
- LOGW("sensorservice died [%p]", who.unsafe_get());
- mSensorManger.sensorManagerDied();
- }
- public:
- DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { }
- };
-
- mDeathObserver = new DeathObserver(*const_cast<SensorManager*>(this));
- mSensorServer->asBinder()->linkToDeath(mDeathObserver);
-
- mSensors= mSensorServer->getSensorList();
- size_tcount = mSensors.size();
- mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*));
- for(size_t i=0 ; i<count ; i++) {
- mSensorList[i] = mSensors.array() + i;
- }
- }
-
- returnNO_ERROR;
- }
在assertStateLocked方法里,先通过getService获得SensorService对象,然后注册了对SensorService的死亡监听器,SensorManager与SensorService不求同年同月同日,只求同年同月同日死。拜完了兄弟之后,调用getSensorList得到所有传感器的对象,存放到mSensorList中,保存在本地空间里。
2) 本地SensorManager中列表的获取
在上面函数调用中首先调用getService来获得SensorService服务,然后执行mSensorServer->getSensorList来获得服务提供的传感器列表:
- Vector<Sensor> SensorService::getSensorList()
- {
- return mUserSensorList;
- }
大家要注意啊,上面的getSensorList函数只是返回了mUserSensorList,而这个变量是在什么时候初始化的呢?
根据2.1节可知,SensorService在本地被初始化时,构造函数里并没有对mUserSensorList进行初始化,而SensorService里有一个onFirstRef方法,这个方法当SensorService第一次被强引用时被自动调用。那SensorService第一次被强引用是在什么时候呢?
在SensorManager::assertStateLocked方法里调用getService获得SensorService保存到mSensorServer成员变量中。
mSensorServer的定义在frameworks/base/include/gui/SensorManager.h中:
- class SensorManager :
- public ASensorManager,
- public Singleton<SensorManager>
- {
- mutable sp<ISensorServer>mSensorServer;
- mutable Sensorconst** mSensorList;
- mutable Vector<Sensor> mSensors;
- };
可以看出mSensroServer为强引用类型。所以在创建本地中的SensorManager类对象时,自动强引用SensorService,自动调用onFirstRef方法:
@frameworks/base/services/sensorservice/SensorService.cpp的onFirstRef简化方法如下:
- void SensorService::onFirstRef()
- {
- LOGD("nuSensorService starting...");
- SensorDevice& dev(SensorDevice::getInstance());
-
-
- if(dev.initCheck() == NO_ERROR) {
- sensor_tconst* list;
- ssize_tcount = dev.getSensorList(&list);
- if (count> 0) {
- …
- for(ssize_t i=0 ; i<count ; i++) {
- registerSensor( new HardwareSensor(list[i]) );
- …
- }
- constSensorFusion& fusion(SensorFusion::getInstance());
-
- if(hasGyro) {
- registerVirtualSensor( newRotationVectorSensor() );
- registerVirtualSensor( new GravitySensor(list, count) );
- registerVirtualSensor( new LinearAccelerationSensor(list, count) );
-
-
- registerVirtualSensor( new OrientationSensor() );
- registerVirtualSensor( new CorrectedGyroSensor(list, count) );
-
-
- char value[PROPERTY_VALUE_MAX];
- property_get("debug.sensors", value, "0");
- if (atoi(value)) {
- registerVirtualSensor( new GyroDriftSensor() );
- }
- }
-
-
- mUserSensorList = mSensorList;
- if(hasGyro &&
- (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) {
-
-
-
- if (orientationIndex >= 0) {
- mUserSensorList.removeItemsAt(orientationIndex);
- }
- }
-
- run("SensorService",PRIORITY_URGENT_DISPLAY);
- mInitCheck = NO_ERROR;
- }
- }
- }
上面代码首先通过SensorDevice::getInstance()创建对象dev,调用dev.getSensorList(&list)获得传感器列表,将取出的sensor_t类型list传感器列表,塑造了HardwareSensor对象,传递给了registerSensor方法,通过registerSensor注册传感器,然后通过单例模型创建了SensorFusion对象,创建并注册了一系列的虚拟传感器,疑惑,极大的疑惑,怎么传感器还有虚拟的??其实你注意看这几个传感器最前面的条件,if(hasGyro),表示如果存在陀螺仪的话,会创建这些虚拟设备,再看这些虚拟设备:旋转,重力,加速器,方向等,这些设备都对应一个物理硬件:陀螺仪,所以这些逻辑上存在,物理上不存在的设备叫虚拟设备。在初始化了虚拟设备后,将mSensorList传感器列表赋值给mUserSensorList,mSensorList是由registerSensor初始化的,mUserSensorList是要提交给Java框架层的传感器列表,最后通过run方法运行了SensorService线程,我们先来看下registerSensor的代码:
- void SensorService::registerSensor(SensorInterface* s)
- {
- sensors_event_t event;
- memset(&event,0, sizeof(event));
-
- const Sensorsensor(s->getSensor());
-
- mSensorList.add(sensor);
-
- mSensorMap.add(sensor.getHandle(), s);
-
- mLastEventSeen.add(sensor.getHandle(), event);
- }
通过分析上面代码可知,将传入的HardwareSensor对象塑造了Sensor,添加到mSensorList向量表里,然后将HardwareSensor对象添加到mSensroMap键值对里,将新建的传感器事件数据封装对象event添加到mLastEventSeen键值对中。
我们通过下面的时序图来看下Sensor列表的获取过程。
1) SensorService监听线程及传感器事件的捕获
让我们再来看看SensorService线程,还记得前面SensorService的父类中有一个Thread类,当调用run方法时会创建线程并调用threadLoop方法。
- bool SensorService::threadLoop()
- {
- LOGD("nuSensorService thread starting...");
-
- const size_tnumEventMax = 16 * (1 + mVirtualSensorList.size());
- sensors_event_t buffer[numEventMax];
- sensors_event_t scratch[numEventMax];
- SensorDevice& device(SensorDevice::getInstance());
- const size_tvcount = mVirtualSensorList.size();
-
- ssize_tcount;
- do {
-
- count = device.poll(buffer,numEventMax);
- if(count<0) {
- LOGE("sensor poll failed (%s)", strerror(-count));
- break;
- }
-
- recordLastValue(buffer, count);
-
-
- if (count&& vcount) {
- sensors_event_t const * const event = buffer;
-
- constDefaultKeyedVector<int, SensorInterface*> virtualSensors(
- getActiveVirtualSensors());
- constsize_t activeVirtualSensorCount = virtualSensors.size();
- if(activeVirtualSensorCount) {
- size_t k = 0;
- SensorFusion& fusion(SensorFusion::getInstance());
- if (fusion.isEnabled()) {
- for (size_t i=0 ; i<size_t(count) ; i++) {
- fusion.process(event[i]);
- }
- }
- for (size_t i=0 ; i<size_t(count) ; i++) {
- for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
- sensors_event_t out;
- if (virtualSensors.valueAt(j)->process(&out, event[i])) {
- buffer[count + k] =out;
- k++;
- }
- }
- }
- if (k) {
-
- recordLastValue(&buffer[count], k);
- count += k;
-
- sortEventBuffer(buffer, count);
- }
- }
- }
-
-
-
- constSortedVector< wp<SensorEventConnection> > activeConnections(
- getActiveConnections());
- size_tnumConnections = activeConnections.size();
- for(size_t i=0 ; i<numConnections ; i++) {
- sp<SensorEventConnection> connection(
- activeConnections[i].promote());
- if(connection != 0) {
-
- connection->sendEvents(buffer, count, scratch);
- }
- }
- } while (count>= 0 || Thread::exitPending());
-
- LOGW("Exiting SensorService::threadLoop => aborting...");
- abort();
- return false;
- }
我们看到device.poll方法,阻塞在了SensorDevice的poll方法上,它肯定就是读取Sensor硬件上的数据了,将传感器数据保存在buff中,然后调用recordLastValue方法,只保存同一类型传感器的最新数据(最后采集的一组数据)到键值对象mLastEventSeen里对应传感器的值域中。如果传感器设备是虚拟设备则调用SensorFusion.Process()方法对虚拟设备数据进行处理。SensorFusion关联一个SensorDevice,它是虚拟传感器设备的一个加工类,负责虚拟传感器数据的计算、处理、设备激活、设置延迟、获得功耗信息等操作。
让我们来回顾下整个过程吧。
1. SensorManager对象创建并调用assertStateLocked方法
2. 在assertStateLocked方法中调用getService,获得SensorService服务
3. 当SensorService第一次强引用时,自动调用OnFirstRef方法
4.获得SensorDevice单例对象
6. 调用SensorDevice.getSensorList方法sensor_t列表保存在SensorService中
8. 调用registerSensor注册传感器,添加到mSensorList列表中
9. 启动SensorService线程,准备监听所有注册的传感器设备
12. 多路监听注册的传感器设备,当有传感器事件时,返回sensor_event_t封装的事件信息
16. 记录产生传感器事件的设备信息
17. 调用getActiveConnections获得所有的活动的客户端SensorEventConnection类对象
19.向客户端发送传感器事件信息