前言 事件处理主要都是在 PhoneWindowManager.java 中,电源键处理在 PhoneWindowManager#interceptKeyBeforeQueueing() 中:frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public int interceptKeyBeforeQueueing (KeyEvent event, int policyFlags) { if ((event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0 ) { Log.d(TAG,"handleKeyGesture +000" ); handleKeyGesture(event, interactiveAndOn); } } private void handleKeyGesture (KeyEvent event, boolean interactive) { if (event.getKeyCode() == KEYCODE_POWER && event.getAction() == KeyEvent.ACTION_DOWN) { mPowerKeyHandled = handleCameraGesture(event, interactive); if (mPowerKeyHandled) { mSingleKeyGestureDetector.reset(); return ; } } mSingleKeyGestureDetector.interceptKey(event, interactive); } private boolean handleCameraGesture (KeyEvent event, boolean interactive) { final boolean intercept = mGestureLauncherService.interceptPowerKeyDown(event, interactive, outLaunched); }
接着看 GestureLauncherService.java frameworks/base/services/core/java/com/android/server/GestureLauncherService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public boolean interceptPowerKeyDown (KeyEvent event, boolean interactive, MutableBoolean outLaunched) { if (launchCamera) { Slog.i(TAG, "Power button double tap gesture detected, launching camera. Interval=" + powerTapInterval + "ms" ); launchCamera = handleCameraGesture(false , StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP); if (launchCamera) { mMetricsLogger.action(MetricsEvent.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE, (int ) powerTapInterval); mUiEventLogger.log(GestureLauncherEvent.GESTURE_CAMERA_DOUBLE_TAP_POWER); } } else if (launchEmergencyGesture) { Slog.i(TAG, "Emergency gesture detected, launching." ); launchEmergencyGesture = handleEmergencyGesture(); mUiEventLogger.log(GestureLauncherEvent.GESTURE_EMERGENCY_TAP_POWER); } } @VisibleForTesting boolean handleCameraGesture (boolean useWakelock, int source) { if (useWakelock) { mWakeLock.acquire(500L ); } StatusBarManagerInternal service = LocalServices.getService( StatusBarManagerInternal.class); service.onCameraLaunchGestureDetected(source); return true ; }
StatusBarManagerInternal.java 是一个 interface 接口,实现在 StatusBarManagerService.java 。StatusBarManagerService#onCameraLaunchGestureDetected() frameworks/base/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
1 2 3 4 5 6 7 8 9 @Override public void onCameraLaunchGestureDetected (int source) { if (mBar != null ) { try { mBar.onCameraLaunchGestureDetected(source); } catch (RemoteException e) { } } }
mBar 是 IStatusBar 对象,被 CommandQueue.java 继承;CentralSurfacesCommandQueueCallbacks 又继承了实现了 CommandQueue.Callbacks ,最终下一步会调用到 CentralSurfacesCommandQueueCallbacks#onCameraLaunchGestureDetected()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 public void onCameraLaunchGestureDetected (int source) { mCentralSurfaces.setLastCameraLaunchSource(source); if (mCentralSurfaces.isGoingToSleep()) { if (CentralSurfaces.DEBUG_CAMERA_LIFT) { Slog.d(CentralSurfaces.TAG, "Finish going to sleep before launching camera" ); } mCentralSurfaces.setLaunchCameraOnFinishedGoingToSleep(true ); return ; } if (!mNotificationPanelViewController.canCameraGestureBeLaunched()) { if (CentralSurfaces.DEBUG_CAMERA_LIFT) { Slog.d(CentralSurfaces.TAG, "Can't launch camera right now" ); } return ; } if (!mCentralSurfaces.isDeviceInteractive()) { mPowerManager.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_CAMERA_LAUNCH, "com.android.systemui:CAMERA_GESTURE" ); } vibrateForCameraGesture(); if (source == StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP) { Log.v(CentralSurfaces.TAG, "Camera launch" ); mKeyguardUpdateMonitor.onCameraLaunched(); } if (!mStatusBarKeyguardViewManager.isShowing()) { final Intent cameraIntent = CameraIntents.getInsecureCameraIntent(mContext); mCentralSurfaces.startActivityDismissingKeyguard(cameraIntent, false , true , true , null , 0 , null , UserHandle.CURRENT); } else { if (!mCentralSurfaces.isDeviceInteractive()) { mCentralSurfaces.acquireGestureWakeLock( CentralSurfaces.LAUNCH_TRANSITION_TIMEOUT_MS + 1000L ); } if (isWakingUpOrAwake()) { if (CentralSurfaces.DEBUG_CAMERA_LIFT) { Slog.d(CentralSurfaces.TAG, "Launching camera" ); } if (mStatusBarKeyguardViewManager.isBouncerShowing()) { mStatusBarKeyguardViewManager.reset(true ); } mNotificationPanelViewController.launchCamera(mCentralSurfaces.isDeviceInteractive() , source); mCentralSurfaces.updateScrimController(); } else { if (CentralSurfaces.DEBUG_CAMERA_LIFT) { Slog.d(CentralSurfaces.TAG, "Deferring until screen turns on" ); } mCentralSurfaces.setLaunchCameraOnFinishedWaking(true ); } } }
再看 NotificationPanelViewController#launchCamera() :frameworks/base/packages/SystemUI/src/com/android/systemui/shade/NotificationPanelViewController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public void launchCamera (boolean animate, int source) { if (source == StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP) { mLastCameraLaunchSource = KeyguardBottomAreaView.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP; } else if (source == StatusBarManager.CAMERA_LAUNCH_SOURCE_WIGGLE) { mLastCameraLaunchSource = KeyguardBottomAreaView.CAMERA_LAUNCH_SOURCE_WIGGLE; } else if (source == StatusBarManager.CAMERA_LAUNCH_SOURCE_LIFT_TRIGGER) { mLastCameraLaunchSource = KeyguardBottomAreaView.CAMERA_LAUNCH_SOURCE_LIFT_TRIGGER; } else { mLastCameraLaunchSource = KeyguardBottomAreaView.CAMERA_LAUNCH_SOURCE_AFFORDANCE; } if (!isFullyCollapsed()) { setLaunchingAffordance(true ); } else { animate = false ; } mAffordanceHasPreview = mKeyguardBottomArea.getRightPreview() != null ; mAffordanceHelper.launchAffordance( animate, mView.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL); }
接着看 KeyguardAffordanceHelper#launchAffordance() :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public void launchAffordance (boolean animate, boolean left) { if (mSwipingInProgress) { return ; } KeyguardAffordanceView targetView = left ? mLeftIcon : mRightIcon; KeyguardAffordanceView otherView = left ? mRightIcon : mLeftIcon; startSwiping(targetView); if (targetView.getVisibility() != View.VISIBLE) { animate = false ; } if (animate) { fling(0 , false , !left); updateIcon(otherView, 0.0f , 0 , true , false , true , false ); } else { mCallback.onAnimationToSideStarted(!left, mTranslation, 0 ); mTranslation = left ? mCallback.getMaxTranslationDistance() : mCallback.getMaxTranslationDistance(); updateIcon(otherView, 0.0f , 0.0f , false , false , true , false ); targetView.instantFinishAnimation(); mFlingEndListener.onAnimationEnd(null ); mAnimationEndRunnable.run(); } }
当动画执行完成,则会执行 mAnimationEndRunnable.run(),进而回调到: KeyguardAffordanceHelperCallback#onAnimationToSideEnded() : 方法里
1 2 3 4 5 6 7 8 9 10 public void onAnimationToSideEnded () { mIsLaunchTransitionRunning = false ; mIsLaunchTransitionFinished = true ; if (mLaunchAnimationEndRunnable != null ) { mLaunchAnimationEndRunnable.run(); mLaunchAnimationEndRunnable = null ; } mCentralSurfaces.readyForKeyguardDone(); }
这里补充个流程: 在未解锁的情况下,打开临时占用的app,比如 相机,紧急拨号等会调用如下函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public void setOccluded (boolean occluded, boolean animate) { final boolean isOccluding = !mOccluded && occluded; final boolean isUnOccluding = mOccluded && !occluded; setOccludedAndUpdateStates(occluded); if (mShowing && isOccluding) { SysUiStatsLog.write(SysUiStatsLog.KEYGUARD_STATE_CHANGED, SysUiStatsLog.KEYGUARD_STATE_CHANGED__STATE__OCCLUDED); if (mCentralSurfaces.isInLaunchTransition()) { final Runnable endRunnable = new Runnable () { @Override public void run () { if (mOccluded) { mNotificationShadeWindowController.setKeyguardOccluded(mOccluded); reset(true ); } else { Log.d(TAG, "setOccluded.run() - mOccluded was set to false" ) ; } } }; mCentralSurfaces.fadeKeyguardAfterLaunchTransition( null , endRunnable, endRunnable); return ; } }
本文链接: http://longzhiye.top/2024/02/13/2024-02-13/