Android442keyguard锁屏流程梳理.docx

上传人:小飞机 文档编号:3152512 上传时间:2023-03-11 格式:DOCX 页数:23 大小:45.84KB
返回 下载 相关 举报
Android442keyguard锁屏流程梳理.docx_第1页
第1页 / 共23页
Android442keyguard锁屏流程梳理.docx_第2页
第2页 / 共23页
Android442keyguard锁屏流程梳理.docx_第3页
第3页 / 共23页
Android442keyguard锁屏流程梳理.docx_第4页
第4页 / 共23页
Android442keyguard锁屏流程梳理.docx_第5页
第5页 / 共23页
亲,该文档总共23页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Android442keyguard锁屏流程梳理.docx》由会员分享,可在线阅读,更多相关《Android442keyguard锁屏流程梳理.docx(23页珍藏版)》请在三一办公上搜索。

1、Android442keyguard锁屏流程梳理Android4.4 4.2keyguard锁屏流程梳理 目录(?)- 1 文件目录 1 a锁屏代理是在Frameworksbasepolicysrccomandroidinternalpolicyimplkeyguard下 1 b整个工程应用在frameworkpackage下结构和功能现在都和 systemUI类似 1 ckeyguard的对外接口FrameworksbasecorejavaandroidappkeyguardManagerjava 2 3 4 Keyguard锁屏流程图 Keyguard锁屏view层次图 Keyguard锁

2、屏重要类分析 4 PhoneWindowManagerjava 4 KeyguardServiceDelegatejava和KeyguardServiceWrapperjava 4 keyguardServicejava 4 KeyguardViewMediatorjava 4 KeyguardViewManagerjava 4 KeyguardHostVIewjava 4 KeyguardUpdateMonitorjava 谷歌android升到4.4,发现锁屏有很大变化,可以左右滑页,添加删除widget,添加删除分页。简直就是一个简化版的launcher。在android 4.4中这个模

3、块的改动简直是巨大,这里略作整理。 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前做一些第三方锁屏软件都会用到该服务接口来控制系统锁屏,现在该接口已经不建议使用了,有更好的做法: /* 6 * dep

4、recated使用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 5 * cal

5、ling 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

6、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 10 Keyguard锁屏流程图 Keyguard锁屏view层次图: Keyguard锁屏重要类分析: 1.PhoneWindowManager.java 这个类很厉害也很重要,它由WindowManagerService派生,处理了

7、phone的顶层逻辑,主要有以下几块: a) 横竖屏处理 22 23 24 25 26 27 28 29 publicvoidsetCurrentOrientationLw(intnewOrientation) synchronized (mLock) if (newOrientation != mCurrentAppOrientation) mCurrentAppOrientation = newOrientation; updateOrientationListenerLp; b) 是否显示状态条或者navigation_bar。 privateintupdateSystemUiVisib

8、ilityLw 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

9、 (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 wan

10、t 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.getSur

11、faceLayermForcingShowNavBarLayer) 51 tmpVisibility&= View.SYSTEM_UI_CLEARABLE_FLAGS; 30 53 finalint visibility = updateSystemBarsLw(win,mLastSystemUiFlags,tmpVisibility); 54 finalint diff = visibility mLastSystemUiFlags; 55 finalbooleanneedsMenu = win.getNeedsMenuLw(mTopFullscreenOpaqueWindowState);

12、 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 IStatusBarServicestatusb

13、ar = 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 dif

14、f; 79 52 c) 各种按键事件的拦截和分发 Home键的事件是在phonewindow这一层就拦截的,所以一般情况应用本身无法正常拦截该事件。 privatevoidhandleLongPressOnHome 81 if (mLongPressOnHomeBehavior != LONG_PRESS_HOME_NOTHING) 82 mHomeConsumed = true; 83 performHapticFeedbackLw(null,HapticFeedbackConstants.LONG_PRESS, false); 84 85 if (mLongPressOnHomeBehav

15、ior = LONG_PRESS_HOME_RECENT_SYSTEM_UI) 80 86 toggleRecentApps; 87 elseif (mLongPressOnHomeBehavior = LONG_PRESS_HOME_ASSIST) 88 launchAssistAction; 89 90 91 d) 锁屏事件处理和响应 publicvoidsystemReady 93 if (!mHeadless) 94 mKeyguardDelegate = newKeyguardServiceDelegate(mContext, null); 95 mKeyguardDelegate.

16、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 92 2.KeyguardServiceDelegate.java和KeyguardServiceWrapper.java 这两个类是android 4.4新增加的,分别对KeyguardServic

17、e进行了代理和包装,代理类里面有一个Scrim视图在keyguard崩溃时显示。包装类就是对keyguardService的简单包装,最终把调度都会传给keyguardService。 3.keyguardService.java 上面一再说到该类,那么它有啥出众的地方呢,其实它就是keyguard的入口,锁屏所有的往生都因它而起,这个类很简单,实例化了一个 IKeyguardService.Stub供其他类bindservice时调用,需要特别注意的是整个keyguard的核心实力派 KeyguardViewMediator在这里诞生了。 4.KeyguardViewMediator.jav

18、a 字面上的意思是keyguard视图调度者,功能上是负责处理keyguard视图的事件,比如完成锁屏和解锁这些动作的视图响应,它作为一个位高权重的调度使当然不会亲手去做这些,它有个得力干将KeyguardviewManager,所有的大小任务都会放权给它。 108 /* 109 *有关锁屏请求的调度者。包括锁屏状态的查询,power management事件影响锁屏是否应该被显示或者重置,特定的回调函数来 110 *通知windowmanager锁屏是什么时候显示,以及接受view视图传过来的消息表明已经成功完成解锁。 111 *请注意锁屏是在灭屏后立即被调用显示的。这样当你点亮屏幕,锁屏才

19、能第一时间显示出来。 112 *例如外部事件调度锁屏视图流程: 113 * 114 *-灭屏动作-重置锁屏并显示它为下次点亮屏幕做好准备。 115 *-锁屏很自然流畅的打开了-如果他不是安全的,隐藏之。 116 * 117 *来自于锁屏视图的事件: 118 *-用户成功完成解锁条件-隐藏锁屏视图,不再对输入事件进行拦截。 119 *请再注意:第三方应用通过条用power managment实例可以屏蔽系统的锁屏。 120 * 121 *线程和同步: 122 *该类是由WindowManagerPolicy创建并运行在它的线程里,锁屏UI也是这个类的构造函数里面产生。这个apis也可以被其他线程

20、所调用。 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;

21、 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

22、 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

23、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 ret

24、urn; 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;

25、 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相当

26、于总裁,那这个就是经理,而且是视图部门老大,它有一个类型为FrameLayout名叫ViewManager的内部 类,用来作为keyguard的viewroot。在viewroot里添加了KeyguardHostView,我们叫它mKeyguardView。 Keyguard里任何的view细节和问题都能通过它找到蛛丝马迹。 188 /* 189 * Manages creating, showing, hiding andresetting the keyguard. Callsback 190 * via link KeyguardViewMediator.ViewMediatorCall

27、back 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 =

28、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

29、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.Keygua

30、rdHostVIew.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 /

31、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 f

32、inal. 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

33、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

34、 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 mAppWidgetM

35、anager =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 mai

36、n 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(VIS

37、IBLE); 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 != nul

38、l) 271 mSlidingChallengeLayout.setOnChallengeScrolledListener(mViewStateManager); 272 273 mAppWidgetContainer.setViewStateManager(mViewStateManager); 274 mAppWidgetContainer.setLockPatternUtils(mLockPatternUtils); 275 276 mMultiPaneChallengeLayout = 277 (MultiPaneChallengeLayout)findViewById(R.id.mu

39、lti_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); 28

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号