Lighting sensor 工作流程

lighting sensor 源代码主要在这2个文档:

 

sensorManager.java

PowerManagerService.java 

 

 

java层(framwork/base/core/java/android/hardware/sensorManager.java)

A:要打开sensorManager.java,保证可以正确接收服务,修改以下内容

private int getLegacySensorType(int type) {
。。。。。。。。
                case Sensor.TYPE_LIGHT:
                     return SENSOR_LIGHT;

。。。。。。。。
        return 0;
    }

 

 public int getSensors() {
 。。。。。。
               case Sensor.TYPE_LIGHT:
                result |= SensorManager.SENSOR_LIGHT;
               break;

 。。。。。。

    }

         

   public boolean registerListener(SensorListener listener, int sensors, int rate) {
        if (listener == null) {
            return false;
        }
。。。。。。。。。。
                 result = registerLegacyListener(SENSOR_LIGHT,    Sensor.TYPE_LIGHT,
                listener, sensors, rate) || result;

。。。。。。。。。。
    }

 

    public void unregisterListener(SensorListener listener, int sensors) {
。。。。。。。。。。
                unregisterLegacyListener(SENSOR_LIGHT, Sensor.TYPE_LIGHT,
                listener, sensors);

。。。。。。。。。。
    }

 

          原理介绍       

         

       public void run() {                                          //这是thread的运行函数,java通过启动线程来读取驱动数据
                。。。。。。。
                while (true) {
                    // wait for an event
                    final int sensor = sensors_data_poll(values, status, timestamp); //调用hal层的poll函数不断轮询读取驱动数据
                    。。。。。。。
                        final Sensor sensorObject = sHandleToSensor.get(sensor);  
                        if (sensorObject != null) {                                                            
                            final int size = sListeners.size();                

                            for (int i=0 ; i<size ; i++) {
                                ListenerDelegate listener = sListeners.get(i);
                                if (listener.hasSensor(sensorObject)) {
                                    // this is asynchronous (okay to call
                                    // with sListeners lock held).
                                    listener.onSensorChangedLocked(sensorObject, values, timestamp, accuracy);
                                }
                            }
                        }
                    }
                }
                //Log.d(TAG, "exiting main sensor thread");
            }

                       。。。。。。。。

                        

                   static SparseArray<Sensor> sHandleToSensor = new SparseArray<Sensor>();

                       

                       。。。。。。。。
    public SensorManager(Looper mainLooper) {
        mSensorService = ISensorService.Stub.asInterface(
                ServiceManager.getService(Context.SENSOR_SERVICE));
        mMainLooper = mainLooper;
        synchronized(sListeners) {
            if (!sSensorModuleInitialized) {
                sSensorModuleInitialized = true;
                                 。。。。。。。。
                // initialize the sensor list
                sensors_module_init();
                final ArrayList<Sensor> fullList = sFullSensorsList;
                int i = 0;
                do {
                    Sensor sensor = new Sensor();  //创建一个类          
                    i = sensors_module_get_next_sensor(sensor, i);

// 返回hal层中定义的sSensorListInit列表,要加新的sensor就要先在sSensorListInit中添加
                    if (i>=0) {
                        //Log.d(TAG, "found sensor: " + sensor.getName() +
                        //        ", handle=" + sensor.getHandle());
                        sensor.setLegacyType(getLegacySensorType(sensor.getType())); //设置一下sensor类中的成员变量
                        fullList.add(sensor);
                        sHandleToSensor.append(sensor.getHandle(), sensor);  //在这里将新sensor加入到sHandleToSensor中了
                    }
                } while (i>0);
                sSensorThread = new SensorThread();
            }
        }
    }

   。。。。。。。。。。。

    static final ArrayList<ListenerDelegate> sListeners =
        new ArrayList<ListenerDelegate>();

   。。。。。。。。。。。。
    public boolean registerListener(SensorEventListener listener, Sensor sensor, int rate,
            Handler handler) {
        if (listener == null || sensor == null) {
            return false;
        }
 。。。。。。。。。。。

//遍历sListeners,若要register的sensor已经在这个链表中,就用mSensorService.enableSensor激活,否则加入到sListeners中,然后激活

        try {
            synchronized (sListeners) {
                ListenerDelegate l = null;
                for (ListenerDelegate i : sListeners) {  

                    if (i.getListener() == listener) {
                        l = i;
                        break;
                    }
                }

                String name = sensor.getName();
                int handle = sensor.getHandle();
                if (l == null) {
                    result = false;
                    l = new ListenerDelegate(listener, sensor, handler);          //创建一个新的ListenerDelegate类
                    sListeners.add(l);                                                      //添加到sListeners中
                    if (!sListeners.isEmpty()) {                                 
                        result = sSensorThread.startLocked(mSensorService);
                        if (result) {                 //下面 enableSensor会调用到HAL层的control__activate去激活sensor
                 result = mSensorService.enableSensor(l, name, handle, delay);


                            if (!result) {                                                 //如果失败,则从sListeners删除
                                // there was an error, remove the listeners
                                sListeners.remove(l);
                            }
                        }
                    }
                } else {
                    result = mSensorService.enableSensor(l, name, handle, delay);
                    if (result) {
                        l.addSensor(sensor);
                    }
                }
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in registerListener: ", e);
            result = false;
        }
        return result;
    }

 

B:设置lighting sensor 开关,以及屏幕亮度等级,只需设置3个地方:

1:设置默认开关:

在菜单“设置”中,用户可以打开或关闭自动亮度调整功能,默认情况下是关闭的,但是我们可以修改这个默认值:

打开framework/base/core/res/res/values/config.xml

  <bool name="config_automatic_brightness_available">true</bool>   //此处设为true ,表示默认打开自动背光功能。

2:设置调整档位config_autoBrightnessLevels

              由于sensor在间歇性地检测环境的亮度,实际使用时不可能不停地设置背光,这样屏幕就会闪烁,因此我们把检测的亮度分为几个不同的档位,在每个档位之间,保持屏幕亮度不变,当检测的亮度跨越不同的档位时,我们才重新设置屏幕的亮度,这样就达到了自动调整背光的效果。 我们可以增加或则减少档位,也可以改变每个档位的分界值,至于如何改,没有什么规定,以实际使用效果为准。

       <integer-array name="config_autoBrightnessLevels">//

        <item>200</item>
        <item>400</item>
        <item>1000</item>
        <item>3000</item>
    </integer-array>
3:设置屏幕背光亮度config_autoBrightnessLcdBacklightValues

sensor检测的数据在不同的档位时,屏幕就需要显示不同的亮度值,个数与所划分的档位需要相等。
     <integer-array name="config_autoBrightnessLcdBacklightValues"> //根据上报的adc值确定的当前背光值
        <item>25</item>
        <item>55</item>
        <item>70</item>
        <item>170</item>
        <item>250</item>
    </integer-array>

你可能感兴趣的:(Lighting sensor 工作流程)