Android4.4 4.2keyguard锁屏流程梳理.doc_第1页
Android4.4 4.2keyguard锁屏流程梳理.doc_第2页
Android4.4 4.2keyguard锁屏流程梳理.doc_第3页
Android4.4 4.2keyguard锁屏流程梳理.doc_第4页
Android4.4 4.2keyguard锁屏流程梳理.doc_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Android4.4 4.2keyguard锁屏流程梳理目录(?)-1 文件目录1 a锁屏代理是在Frameworksbasepolicysrccomandroidinternalpolicyimplkeyguard下1 b整个工程应用在frameworkpackage下结构和功能现在都和 systemUI类似1 ckeyguard的对外接口FrameworksbasecorejavaandroidappkeyguardManagerjava2 Keyguard锁屏流程图3 Keyguard锁屏view层次图4 Keyguard锁屏重要类分析4 PhoneWindowManagerjava4 KeyguardServiceDelegatejava和KeyguardServiceWrapperjava4 keyguardServicejava4 KeyguardViewMediatorjava4 KeyguardViewManagerjava4 KeyguardHostVIewjava4 KeyguardUpdateMonitorjava谷歌android升到4.4,发现锁屏有很大变化,可以左右滑页,添加删除widget,添加删除分页。简直就是一个简化版的launcher。在android 4.4中这个模块的改动简直是巨大,这里略作整理。1.文件目录:a,锁屏代理是在Frameworks/base/policy/src/com/android/internal/policy/impl/keyguard下:b,整个工程应用在framework/package下,结构和功能现在都和 systemUI类似:c,keyguard的对外接口Frameworks/base/core/java/android/app/keyguardManager.java:android4.2前做一些第三方锁屏软件都会用到该服务接口来控制系统锁屏(比如禁止系统锁屏),现在该接口已经不建议使用了,有更好的做法:5 /* 6 * deprecated使用link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD 7 * and/or link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED 8 * 来代替; 利用该方式可以使应用达到禁用锁屏的效果而不需要额外的权限申请。this allows you to 9 * Enables you to lock or unlock thekeyboard. Get an instance of this class by 10 * calling link android.content.Context#getSystemService(java.lang.String)Context.getSystemService(). 11 * This class is wrapped by link android.app.KeyguardManagerKeyguardManager. 12 * param tag A tag that informallyidentifies who you are (for debugging who 13 * is disabling he keyguard). 14 * 15 * return A link KeyguardLock handle to use todisable and reenable the 16 * keyguard. 17 */ 18 Deprecated 19 public KeyguardLock newKeyguardLock(Stringtag) 20 return new KeyguardLock(tag); 21 Keyguard锁屏流程图Keyguard锁屏view层次图: Keyguard锁屏重要类分析:1.PhoneWindowManager.java这个类很厉害也很重要,它由WindowManagerService派生,处理了phone的顶层逻辑,主要有以下几块:a) 横竖屏处理(屏幕旋转等)22 publicvoidsetCurrentOrientationLw(intnewOrientation) 23 synchronized (mLock) 24 if (newOrientation != mCurrentAppOrientation) 25 mCurrentAppOrientation = newOrientation; 26 updateOrientationListenerLp(); 27 28 29 b) 是否显示状态条或者navigation_bar。30 privateintupdateSystemUiVisibilityLw() 31 / If there is no window focused,there will be nobody to handle the events 32 / anyway, so just hang on inwhatever state were in until things settle down. 33 WindowState win = mFocusedWindow != null ? mFocusedWindow :mTopFullscreenOpaqueWindowState; 34 if (win = null) 35 return 0; 36 37 if (win.getAttrs().type = TYPE_KEYGUARD&mHideLockScreen = true) 38 / We are updating at a pointwhere the keyguard has gotten 39 / focus, but we were last in astate where the top window is 40 / hiding it. This is probably because the keyguardas been 41 / shown while the top window wasdisplayed, so we want to ignore 42 / it here because this is just avery transient change and it 43 / will quickly lose focus once itcorrectly gets hidden. 44 return 0; 45 46 47 inttmpVisibility = win.getSystemUiVisibility() 48 & mResettingSystemUiFlags 49 & mForceClearedSystemUiFlags; 50 if (mForcingShowNavBar&win.getSurfaceLayer()mForcingShowNavBarLayer) 51 tmpVisibility&= View.SYSTEM_UI_CLEARABLE_FLAGS; 52 53 finalint visibility = updateSystemBarsLw(win,mLastSystemUiFlags,tmpVisibility); 54 finalint diff = visibility mLastSystemUiFlags; 55 finalbooleanneedsMenu = win.getNeedsMenuLw(mTopFullscreenOpaqueWindowState); 56 if (diff = 0 &mLastFocusNeedsMenu = needsMenu 57 &mFocusedApp =win.getAppToken() 58 return 0; 59 60 mLastSystemUiFlags = visibility; 61 mLastFocusNeedsMenu = needsMenu; 62 mFocusedApp = win.getAppToken(); 63 mHandler.post(new Runnable() 64 Override 65 publicvoid run() 66 try 67 IStatusBarServicestatusbar = getStatusBarService(); 68 if (statusbar != null) 69 statusbar.setSystemUiVisibility(visibility,0xffffffff); 70 statusbar.topAppWindowChanged(needsMenu); 71 72 catch (RemoteException e) 73 / re-acquire status bar servicenext time it is needed. 74 mStatusBarService = null; 75 76 77 ); 78 return diff; 79 c) 各种按键事件的拦截和分发(比如长按home键)Home键的事件是在phonewindow这一层就拦截的,所以一般情况应用本身无法正常拦截该事件。80 privatevoidhandleLongPressOnHome() 81 if (mLongPressOnHomeBehavior != LONG_PRESS_HOME_NOTHING) 82 mHomeConsumed = true; 83 performHapticFeedbackLw(null,HapticFeedbackConstants.LONG_PRESS, false); 84 85 if (mLongPressOnHomeBehavior = LONG_PRESS_HOME_RECENT_SYSTEM_UI) 86 toggleRecentApps(); 87 elseif (mLongPressOnHomeBehavior = LONG_PRESS_HOME_ASSIST) 88 launchAssistAction(); 89 90 91 d) 锁屏事件处理和响应92 publicvoidsystemReady() 93 if (!mHeadless) 94 mKeyguardDelegate = newKeyguardServiceDelegate(mContext, null); 95 mKeyguardDelegate.onSystemReady(); 96 97 synchronized (mLock) 98 updateOrientationListenerLp(); 99 mSystemReady = true; 100 mHandler.post(new Runnable() 101 Override 102 publicvoid run() 103 updateSettings(); 104 105 ); 106 107 2.KeyguardServiceDelegate.java和KeyguardServiceWrapper.java这两个类是android 4.4新增加的,分别对KeyguardService进行了代理和包装,代理类里面有一个Scrim视图在keyguard崩溃时显示。包装类就是对keyguardService的简单包装,最终把调度都会传给keyguardService。3.keyguardService.java上面一再说到该类,那么它有啥出众的地方呢,其实它就是keyguard的入口,锁屏所有的往生都因它而起,这个类很简单,实例化了一个 IKeyguardService.Stub供其他类bindservice时调用,需要特别注意的是整个keyguard的核心实力派 KeyguardViewMediator在这里诞生了。4.KeyguardViewMediator.java字面上的意思是keyguard视图调度者,功能上是负责处理keyguard视图的事件,比如完成锁屏和解锁这些动作的视图响应,它作为一个位高权重的调度使当然不会亲手去做这些,它有个得力干将KeyguardviewManager,所有的大小任务都会放权给它。108 /* 109 *有关锁屏请求的调度者。包括锁屏状态的查询,power management事件影响锁屏是否应该被显示或者重置,特定的回调函数来 110 *通知windowmanager锁屏是什么时候显示,以及接受view视图传过来的消息表明已经成功完成解锁。 111 *请注意锁屏是在灭屏后立即被调用显示的。这样当你点亮屏幕,锁屏才能第一时间显示出来。 112 *例如外部事件调度锁屏视图流程: 113 * 114 *-灭屏动作-重置锁屏并显示它为下次点亮屏幕做好准备。 115 *-锁屏很自然流畅的打开了-如果他不是安全的,隐藏之。 116 * 117 *来自于锁屏视图的事件: 118 *-用户成功完成解锁条件-隐藏锁屏视图,不再对输入事件进行拦截。 119 *请再注意:第三方应用通过条用power managment实例可以屏蔽系统的锁屏。 120 * 121 *线程和同步: 122 *该类是由WindowManagerPolicy创建并运行在它的线程里,锁屏UI也是这个类的构造函数里面产生。这个apis也可以被其他线程所调用。 123 *然而,这个类的方法手势同步的,同时任何一个锁屏视图都会发消息到handle来保证它是在锁屏UI线程里面执行的。 124 */ 125 126 public class KeyguardViewMediatorimplements KeyguardViewCallback, 127 KeyguardUpdateMonitor.InfoCallback,KeyguardUpdateMonitor.SimStateCallback 128 private static final intKEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT = 30000; 129 /* 130 * This handler will be associated with the policy thread, which willalso 131 * be the UI thread of the keyguard. Since the apis of the policy, and therefore 132 * this class, can be called by other threads, any action that directly 133 * interacts with the keyguard ui should be posted to this handler,rather 134 * than called directly. 135 */ 136 private Handler mHandler = new Handler() 137 Override 138 public void handleMessage(Message msg) 139 switch (msg.what) 140 case TIMEOUT: 141 handleTimeout(msg.arg1); 142 return ; 143 case SHOW: 144 handleShow(); 145 return ; 146 case HIDE: 147 handleHide(); 148 return ; 149 case RESET: 150 handleReset(); 151 return ; 152 case VERIFY_UNLOCK: 153 handleVerifyUnlock(); 154 return; 155 case NOTIFY_SCREEN_OFF: 156 handleNotifyScreenOff(); 157 return; 158 case NOTIFY_SCREEN_ON: 159 handleNotifyScreenOn(KeyguardViewManager.ShowListener)msg.obj); 160 return; 161 case WAKE_WHEN_READY: 162 handleWakeWhenReady(msg.arg1); 163 return; 164 case KEYGUARD_DONE: 165 handleKeyguardDone(msg.arg1!= 0); 166 return; 167 case KEYGUARD_DONE_DRAWING: 168 handleKeyguardDoneDrawing(); 169 return; 170 caseKEYGUARD_DONE_AUTHENTICATING: 171 keyguardDone(true); 172 return; 173 case SET_HIDDEN: 174 handleSetHidden(msg.arg1 !=0); 175 break; 176 case KEYGUARD_TIMEOUT: 177 synchronized(KeyguardViewMediator.this) 178 doKeyguardLocked(); 179 180 break; 181 182 183 ; 184 private void adjustStatusBarLocked() 185 ./控制是否能在锁屏界面下拉状态栏。 186 187 5.KeyguardViewManager.java如果说mediator相当于总裁,那这个就是经理,而且是视图部门老大,它有一个类型为FrameLayout名叫ViewManager的内部 类,用来作为keyguard的viewroot。在viewroot里添加了KeyguardHostView,我们叫它mKeyguardView。 Keyguard里任何的view细节和问题都能通过它找到蛛丝马迹。188 /* 189 * Manages creating, showing, hiding andresetting the keyguard. Callsback 190 * via link KeyguardViewMediator.ViewMediatorCallback to poke 191 * the wake lock and report that the keyguardis done, which is in turn, 192 * reported to this class by the current link KeyguardViewBase. 193 */ 194 public class KeyguardViewManager 195 private final static boolean DEBUG = KeyguardViewMediator.DEBUG; 196 private static String TAG = KeyguardViewManager; 197 public static boolean USE_UPPER_CASE = true; 198 public final static String IS_SWITCHING_USER = is_switching_user; 199 200 / Timeoutused for keypresses 201 static final int DIGIT_PRESS_WAKE_MILLIS = 5000; 202 203 private final Context mContext; 204 private final ViewManager mViewManager; 205 private final KeyguardViewMediator.ViewMediatorCallbackmViewMediatorCallback; 206 207 private WindowManager.LayoutParams mWindowLayoutParams; 208 private boolean mNeedsInput = false; 209 210 private ViewManagerHost mKeyguardHost; 211 private KeyguardHostView mKeyguardView; 6.KeyguardHostVIew.java这里完成keyguardView布局,实例化。分析一个自定义的viewgroup,重点需要分析的是它的构造方法和onFinishInflate()方法:212 public KeyguardHostView(Context context, AttributeSet attrs) 213 super(context, attrs); 214 215 if (DEBUG) Log.e(TAG, KeyguardHostView(); 216 217 mLockPatternUtils = newLockPatternUtils(context); 218 219 / Note: This depends on KeyguardHostView getting reconstructed every timethe 220 / user switches, since mUserId will be used for the entire session. 221 / Once created, keyguard should *never* re-use this instance withanother user. 222 / In other words, mUserId should never change - hence its marked final. 223 mUserId = mLockPatternUtils.getCurrentUser(); 224 225 DevicePolicyManager dpm = 226 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); 227 if (dpm != null) 228 mDisabledFeatures =getDisabledFeatures(dpm); 229 mCameraDisabled =dpm.getCameraDisabled(null); 230 231 232 mSafeModeEnabled= LockPatternUtils.isSafeModeEnabled(); 233 234 / These need to be created with the user context. 235 Context userContext = null; 236 try 237 final String packageName = system; 238 userContext = mContext.createPackageContextAsUser(packageName,0, 239 new UserHandle(mUserId); 240 241 catch (NameNotFoundException e) 242 e.printStackTrace(); 243 / This should never happen, but its better to have no widgets than tocrash. 244 userContext = context; 245 246 247 mAppWidgetHost = new AppWidgetHost(userContext, APPWIDGET_HOST_ID, mOnClickHandler, 248 Looper.myLooper(); 249 250 mAppWidgetManager =AppWidgetManager.getInstance(userContext); 251 252 mSecurityModel = new KeyguardSecurityModel(context); 253 254 mViewStateManager = newKeyguardViewStateManager(this); 255 256 257 258 Override 259 protected void onFinishInflate() 260 / Grab instances of and make any necessary changes to the main layouts.Create 261 / view state manager and wire up necessary listeners / callbacks. 262 View deleteDropTarget = findViewById(R.id.keyguard_widget_pager_delete_target); 263 mAppWidgetContainer =(KeyguardWidgetPager) findViewById(R.id.app_widget_container); 264 mAppWidgetContainer.setVisibility(VISIBLE); 265 mAppWidgetContainer.setCallbacks(mWidgetCallbacks); 266 mAppWidgetContainer.setDeleteDropTarget(deleteDropTarget); 267 mAppWidgetContainer.setMinScale(0.5f); 268 269 mSlidingChallengeLayout =(SlidingChallengeLayout) findViewById(R.id.sliding_layout); 270 if (mSlidingChallengeLayout != null) 271 mSlidingChallengeLayout.setOnChallengeScrolledListener(mViewStateManager); 272 273 mAppWidgetContainer.setViewStateManager(mViewStateManager); 274 mAppWidgetContainer.setLockPatternUtils(mLockPatternUtils); 275 276 mMultiPaneChallengeLayout = 277 (MultiPaneChallengeLayout)findViewById(R.id.multi_pane_challenge); 278 ChallengeLayout challenge =mSlidingChallengeLayout != null ? mSlidingChallengeLayout : 279 mMultiPaneChallengeLayout; 280 challenge.setOnBouncerStateChangedListener(mViewStateManager); 281 mAppWidgetContainer.setBouncerAnimationDuration(challenge.getBouncerAnimationDuration(); 282 mViewStateManager.setPagedView(mAppWidgetContainer); 283 mViewStateManager.setChallengeLayout(challenge); 284 mSecurityViewContainer = (KeyguardSecurityViewFlipper)findViewById(R.id.view_flipper); 285 mKeyguardSelectorView =(KeyguardSelectorView) findViewById(R.id.keyguard_selector_view); 286 mViewStateManager.setSecurityViewContainer(mSecurityViewContainer); 287 288 setBackButtonEnabled(false); 289 290 if (KeyguardUpdateMonitor.getInstance(mContext).hasBootCompleted() 291 updateAndAddWidgets(); 292 else 293 / We cant add widgets until after boot completes because AppWidgetHostmay try 294 / to contact the providers. Do itlater. 295 mPostBootCompletedRunnable = new Runnable() 296 Override 297 public void run() 298 updateAndAddWidgets(); 299 300 ; 301 302 303 showPrimarySecurityScreen(false); 304 updateSecurityViews(); 305 enableUserSelectorIfNecessary(); 306 7.KeyguardUpdateMonitor.java说明:监听系统状态值的改变如时间、SIM卡状态、电池电量等,状态值的改变会回调监听了该状态信息的对象实例。如果只是关注功能的话只需要看hadle里面的每个消息调用的方法即可。307 /* 308 *Watches for updates that may be interesting to the keyguard, and provides 309 *the up to date information as well as a registration for callbacks that care 310 * tobe updated. 311 * 312 *Note: under time crunch, this has been extended to include some stuff that 313 *doesnt really belong here. see link#handleBatteryUpdate where it shutdowns 314 *the device, and link #getFailedAttempts(), link #reportFailedAttempt() 315 *and link #clearFailedAttempts(). Maybe we should rename this KeyguardContext. 316 */ 317 public class KeyguardUpdateMonitor 318 private Handler mHandler; 319 private ContentObserver mContentObserver; 320 private int mRingMode; 321 private int mPhoneState; 322 . 323 324 /* 325 * SIM卡状态改变捕获赋值。 326 * the intent and provide a link SimCard.State result. 327 */ 328 private static class SimArgs 329 330 public final IccCard.State simState; 331 332 private SimArgs(Intent intent) 333 if(!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction() 334 throw newIllegalArgumentException(only handles intentACTION_SIM_STATE_CHANGED); 335 336 String stateExtra = intent.getStringExtra(IccCard.INTENT_KEY_ICC_STATE); 337 if (IccCard.INTENT_VALUE_ICC_ABSENT.equals(state

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论