您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > 由IntentACTION_DOCK_EVENT(亮度传感器)引发的血案
由Intent.ACTION_DOCK_EVENT(亮度传感器)引发的血案在调试光感传感器(android2.2)发现,传递过来的lightvalue值是驱动值,但是实现的亮度变到最亮后却没有改变,查看代码发现如下问题1:mIsDocked何时改变powerManagerService.java(framwork/base/services/java/com/android/server/)lightSensorChangedLocked(){.........intlcdValue=getAutoBrightnessValue(//(mIsDocked?value:mHighestLightSensorValue),//因为这里mIsDocked的缘故value,mLcdBacklightValues);}privatevoiddockStateChanged(intstate){synchronized(mLocks){//在这里改变的mIsDocked=(state!=Intent.EXTRA_DOCK_STATE_UNDOCKED);if(mIsDocked){mHighestLightSensorValue=-1;}if((mPowerState&SCREEN_ON_BIT)!=0){//forcelightsrecalculationintvalue=(int)mLightSensorValue;mLightSensorValue=-1;lightSensorChangedLocked(value);}}//在这里定义了一个receiver接受消息,接受EXTRA_DOCK_STATEprivatefinalclassDockReceiverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Contextcontext,Intentintent){//获取EXTRA_DOCK_STATE的状态,并初始化为EXTRA_DOCK_STATE_UNDOCKED状态intstate=intent.getIntExtra(Intent.EXTRA_DOCK_STATE,Intent.EXTRA_DOCK_STATE_UNDOCKED);dockStateChanged(state);}}voidinitInThread(){.................filter=newIntentFilter();//定义一个filterfilter.addAction(Intent.ACTION_DOCK_EVENT);//向filter中添加Intent.ACTION_DOCK_EVENT消息mContext.registerReceiver(newDockReceiver(),filter);//将filter注册斤receiver中,这样DockReceiver就能接受含有ACTION_DOCK_EVENT的消息了.........}2:Intent.ACTION_DOCK_EVENT何时发送在DockObserver.java(frameworks/base/services/java/com/android/server/)中发送//记住这个函数下面会用到privatefinalvoidupdate(){mHandler.sendEmptyMessage(MSG_DOCK_STATE);}privatefinalHandlermHandler=newHandler(){@OverridepublicvoidhandleMessage(Messagemsg){switch(msg.what){caseMSG_DOCK_STATE:synchronized(this){Slog.i(TAG,Dockstatechanged:+mDockState);finalContentResolvercr=mContext.getContentResolver();if(Settings.Secure.getInt(cr,Settings.Secure.DEVICE_PROVISIONED,0)==0){Slog.i(TAG,Devicenotprovisioned,skippingdockbroadcast);return;}//PackupthevaluesandbroadcastthemtoeveryoneIntentintent=newIntent(Intent.ACTION_DOCK_EVENT);//在这里创建了一个intent,将Intent.EXTRA_DOCK_STATE设置成了mDockState状态,这样上面的reciver就能接受到了intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);intent.putExtra(Intent.EXTRA_DOCK_STATE,mDockState);}}};}mDockState是怎么改变的呢?//记住这个函数,在后面会讲到他是如何接受驱动发送上来的数据的,他是一个虚函数必须在子类中实现它父类是UEventObserver,//参数UEventObserver.UEventevent是从父类中传过来的,也就是说父类已经收到了数据,在子类处理而已publicvoidonUEvent(UEventObserver.UEventevent){if(Log.isLoggable(TAG,Log.VERBOSE)){Slog.v(TAG,DockUEVENT:+event.toString());}synchronized(this){try{intnewState=Integer.parseInt(event.get(SWITCH_STATE));if(newState!=mDockState){mPreviousDockState=mDockState;mDockState=newState;if(mSystemReady){//Don'tforcescreenonwhenundockingfromthedeskdock.//Thechangeinpowerstatewilldothisanyway.//FIXME-weshouldbeconfigurable.if(mPreviousDockState!=Intent.EXTRA_DOCK_STATE_DESK||mDockState!=Intent.EXTRA_DOCK_STATE_UNDOCKED){mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(),false,true);}update();//调用了上面的函数}}}catch(NumberFormatExceptione){Slog.e(TAG,Couldnotparseswitchstatefromevent+event);}}}父类UEventObserver是如何接受数据的?请看UEventObserver类UEventObserver.java(frameworks/base/core/java/android/os)//开启了一个线程不断获取数据publicvoidrun(){native_setup();byte[]buffer=newbyte[1024];intlen;while(true){len=next_event(buffer);//这个函数非常重要,他是jni层的函数,可以直接调用hal层中的函数,获取驱动发上来的uevent数据,在后面会讲到if(len0){StringbufferStr=newString(buffer,0,len);//easiertosearchaStringsynchronized(mObservers){for(inti=0;imObservers.size();i+=2){//在此找到一个属于我们的mObserversif(bufferStr.indexOf((String)mObservers.get(i))!=-1){//在这里向子类调用了onUEvent方法,将onUEvent是虚函数,所有会直接调用到子类中去((UEventObserver)mObservers.get(i+1)).onUEvent(newUEvent(bufferStr));}}}}}}我们的mObservers是怎样产生的?先看查找UEventObserver.java发现mObservers的定义/**Manytomanymappingofstringmatchtoobserver.*Multimapwouldbebetter,butnotavailableinandroid,souse*anArrayListwhereevenelementsaretheStringmatchandodd*elementsthecorrespondingUEventObserverobserver*/privateArrayListObjectmObservers=newArrayListObject();//发现在子类(DockObserver)的构造函数中调用了startObserving,这样就把我们自己的mObservers加好了publicfinalsynchronizedvoidstartObserving(Stringmatch){ensureThreadStarted();sThread.addObserver(match,this);}privatestaticUEventThreadsThread;publicvoidaddObserver(Stringmatch,UEventObserverobserver){synchronized(mObservers){mObservers.add(match);mObservers.add(observer);}}DockObserver的构造函数publicDockObserver(Contextcontext,PowerManagerServicepm){//看到这个参数了吧,在PowerManagerService就会和DockObserver关联了mContext=context;mPowerManager=pm;init();//setinitialstatusstartObserving(DOCK_UEVENT_MATCH);}privatestaticfinalStringDOCK_UEVENT_MATCH=DEVPATH=/devices/virtual/switch/dock;privatestaticfinalStringDOCK_STATE_PATH=/sys/class/switch/dock/state;
本文标题:由IntentACTION_DOCK_EVENT(亮度传感器)引发的血案
链接地址:https://www.777doc.com/doc-1086631 .html