服务创建过程
- SensorService::onFirstRef
- SensorDevice::getInstance
- new SensorDevice
- hw_get_module
- sensors_open_1
- mSensorModule->get_sensors_list
- new SensorDevice
- dev.getSensorList
- mSensorModule->get_sensors_list
- foreach list
- registerSensor(new HardwareSensor)
- SensorFusion::getInstance
- if (hasGyro)(如果有陀螺仪)
- registerVirtualSensor(new RotationVectorSensor)
- registerVirtualSensor(new GravitySensor)
- registerVirtualSensor(new LinearAccelerationSensor)
- registerVirtualSensor(new OrientationSensor)
- SensorDevice::getInstance
应用创建过程
- SystemSensorManager.SystemSensorManager
- nativeClassInit(jni方法)
- do
- nativeGetNextSensor(jni方法)
- SensorManager::getInstance(libgui)
- mgr.getSensorList
- assertStateLocked
- getService(获取SensorService接口代理)
- mSensorServer->getSensorList
- assertStateLocked
- nativeGetNextSensor(jni方法)
注册Listener
- 应用层处理
- SensorManager.registerListener
- SystemSensorManager.registerListenerImpl
- mSensorListeners.get(通过listener查找queue)
- if (queue == null)
- new SensorEventQueue(一个queue对应一个listener)
- BaseEventQueue.BaseEventQueue
- nativeInitBaseEventQueue(nativeInitSensorEventQueue)
- SensorManager::getInstance
- mgr.createEventQueue
- mSensorServer->createSensorEventConnection(binder调用)
- new SensorEventQueue
- mSensorServer->createSensorEventConnection(binder调用)
- new Receiver
- nativeInitBaseEventQueue(nativeInitSensorEventQueue)
- BaseEventQueue.BaseEventQueue
- queue.addSensor
- enableSensor
- nativeEnableSensor
- SensorEventQueue::enableSensor
- mSensorEventConnection->enableDisable(binder调用)
- SensorEventQueue::enableSensor
- nativeEnableSensor
- enableSensor
- mSensorListeners.put
- new SensorEventQueue(一个queue对应一个listener)
- else
- queue.addSensor
- SystemSensorManager.registerListenerImpl
- 服务层ISensorServer处理createSensorEventConnection
- SensorService::createSensorEventConnection
- new SensorEventConnection
- SensorDevice::getInstance
- new BitTube(内部包含BitTube)
- new SensorEventConnection
- 服务层ISensorEventConnection处理enableDisable
- SensorService::SensorEventConnection::enableDisable
- mService->enable(SensorService::enable)
- if (rec == 0)(没有对应该传感器的记录)
- new SensorRecord(connection)
- else
- rec->addConnection
- if (sensor MinDelay == 0)(不是一个连续事件传感器)
- connection->sendEvents(发送最后一次的值)
- connection->addSensor
- sensor->batch(HardwareSensor::batch,先调用batch)
- mSensorDevice.batch
- connection->setFirstFlushPending(只有该连接等待flush完成事件)
- sensor->flush(再调用flush)
- sensor->activate(再调用activate)
- mSensorDevice.activate
- if (rec == 0)(没有对应该传感器的记录)
- mService->enable(SensorService::enable)
通过setFirstFlushPending机制,过滤enable时硬件缓存中的事件。
也有可能是虚拟传感器,其batch方法只会调用setDelay,而setDelay,active方法会调用硬件传感器对应的方法。
注销Listener
- 应用层处理
- SensorManager.unregisterListener
- SystemSensorManager.unregisterListenerImpl
- mSensorListeners.get
- queue.removeSensor(BaseEventQueue)
- disableSensor
- nativeDisableSensor
- mSensorListeners.remove
-
- SensorEventQueue::disableSensor
- mSensorEventConnection->enableDisable(binder调用)
- SensorEventQueue::disableSensor
-
- mSensorListeners.remove
- nativeDisableSensor
- disableSensor
- SystemSensorManager.unregisterListenerImpl
- 服务层ISensorEventConnection处理enableDisable
- SensorService::SensorEventConnection::enableDisable
- mService->disable(SensorService::disable)
- cleanupWithoutDisableLocked
- connection->removeSensor
- sensor->activate
- mSensorDevice.activate
- cleanupWithoutDisableLocked
- mService->disable(SensorService::disable)
刷新flush
- 应用层处理
- SensorManager.flush
- SystemSensorManager.flushImpl
- nativeFlushSensor
- SensorEventQueue::flush(libgui)
- mSensorEventConnection->flush(binder调用)
- SensorEventQueue::flush(libgui)
- nativeFlushSensor
- SystemSensorManager.flushImpl
- 服务层处理
- SensorService::SensorEventConnection::flush
- foreach mSensorInfo(所有关注的传感器同时flush)
- if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 || isVirtualSensor)
- flushInfo.mPendingFlushEventsToSend++(模拟一个flush完成事件)
- else
- mService->flushSensor(SensorService::flushSensor)
- sensor->flush(HardwareSensor::flush)
- mSensorDevice.flush(SensorDevice::flush)
- mSensorDevice->flush(sensors_poll_device_1_t)
- mSensorDevice.flush(SensorDevice::flush)
- sensor->flush(HardwareSensor::flush)
- mService->flushSensor(SensorService::flushSensor)
- if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 || isVirtualSensor)
- foreach mSensorInfo(所有关注的传感器同时flush)
注意:应用也可能收到其他应用调用硬件flush引起的flush完成事件。
事件传递
- 服务层获取发送事件
- SensorService::threadLoop
- SensorDevice::getInstance
- device.poll
- recordLastValue
- if (fusion.isEnabled())
- fusion.process(处理所有的事件)
- 虚拟传感器处理事件,生成新的事件
- foreach mActiveConnections(每个活动的连接,都发送事件)
- connection->sendEvents
- 连接自己过滤事件
- 不是自己关注的传感器事件,跳过
- 对该传感器处于flush等待状态,跳过
- foreach mSensorInfo(发送等待的flush完成事件)
- while (flushInfo.mPendingFlushEventsToSend > 0)
- SensorEventQueue::write(发送flush完成事件)
- SensorEventQueue::write
- if (size == -EAGAIN)(发送阻塞,丢弃非flush完成事件)
- countFlushCompleteEventsLocked(统计flush完成事件)
- while (flushInfo.mPendingFlushEventsToSend > 0)
- 连接自己过滤事件
- cleanupAutoDisabledSensor
- foreach events(所有事件)
- if (type == SENSOR_TYPE_SIGNIFICANT_MOTION)(特定类型)
- if (connection->hasSensor)(连接关注该事件对应的传感器)
- sensor->autoDisable
- SensorDevice::autoDisable
- cleanupWithoutDisable
- sensor->autoDisable
- if (connection->hasSensor)(连接关注该事件对应的传感器)
- if (type == SENSOR_TYPE_SIGNIFICANT_MOTION)(特定类型)
- foreach events(所有事件)
- connection->sendEvents
- 应用层回调Listener
- Receiver.handleEvent
- SensorEventQueue::read(读取多个事件)
- for events(一个事件调用一次回调)
- env->CallVoidMethod(SensorEventQueue::dispatchSensorEvent)
- System.arraycopy(只拷贝实际矢量长度)
- 该传感器精度变化或者第一个事件
- mListener.onAccuracyChanged
- mListener.onSensorChanged
- env->CallVoidMethod(SensorEventQueue::dispatchSensorEvent)