android 7.1 遮蔽按壓兩次電源鍵(KEYCODE_POWER)開啟相機

ansondroider發表於2020-11-16

平臺

RK3288 + Android7.1

需求

Android 支援通過快速按下電源鍵兩次啟動攝像頭, 在新的需求中, 需要遮蔽此功能.

修改

frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

    private void interceptPowerKeyDown(KeyEvent event, boolean interactive) {
        // Hold a wake lock until the power key is released.
        /**省略**/
        GestureLauncherService gestureService = LocalServices.getService(
                GestureLauncherService.class);
        boolean gesturedServiceIntercepted = false;
        if (gestureService != null) {
        	//註釋下面兩行程式碼.
            //gesturedServiceIntercepted = gestureService.interceptPowerKeyDown(event, interactive,
            //        mTmpBoolean);
            if (mTmpBoolean.value && mGoingToSleep) {
                mCameraGestureTriggeredDuringGoingToSleep = true;
            }
        }
        /**省略**/     

流程

LOG


system_process D/WindowManager: ALog interceptKeyBeforeQueueing KEYCODE_POWER
system_process D/WindowManager: ALog interceptKeyBeforeQueueing KEYCODE_POWER
system_process I/PowerManagerService: Going to sleep due to power button (uid 1000)...
system_process I/PowerManagerService: Sleeping (uid 1000)...
system_process I/VrManagerService: VR mode is disallowed
system_process D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000
? D/PermissionCache: checking android.permission.READ_FRAME_BUFFER for uid=1000 => granted (732 us)
com.android.systemui W/FingerprintManager: isFingerprintHardwareDetected(): Service not connected!
system_process D/WindowManager: ALog interceptKeyBeforeQueueing KEYCODE_POWER
system_process I/GestureLauncherService: Power button double tap gesture detected, launching camera. Interval=220ms
system_process I/PowerManagerService: Waking up from sleep (uid 1000)...
system_process I/VrManagerService: VR mode is allowed
com.android.systemui I/KeyguardViewMediator: Camera gesture was triggered, preventing Keyguard locking.
system_process I/SensorsHal: set batch: handle = 0, period_ns = 20000000ns, timeout = 0ns
system_process D/SensorsHal: Couldn't open /dev/mma8452_daemon (No such file or directory)
system_process I/SensorsHal: MmaSensor update delay: 20ms
system_process E/SensorsHal: fail to perform GSENSOR_IOCTL_APP_SET_RATE, result = -1, error is 'Bad file descriptor'
system_process E/SensorService: sensor batch failed 0xa98df420 0 0 20000000 100000000 err=Operation not permitted
system_process I/SensorsHal: set batch: handle = 0, period_ns = 20000000ns, timeout = 0ns
system_process D/SensorsHal: Couldn't open /dev/mma8452_daemon (No such file or directory)
system_process I/SensorsHal: MmaSensor update delay: 20ms
system_process E/SensorsHal: fail to perform GSENSOR_IOCTL_APP_SET_RATE, result = -1, error is 'Bad file descriptor'
system_process E/SensorService: sensor batch failed 0xa98df420 0 0 20000000 0 err=Operation not permitted
system_process V/KeyguardServiceDelegate: onStartedWakingUp()
system_process W/VibratorService: Tried to stop vibrating but there is no vibrator device.
? D/AudioHardwareTiny: adev_set_parameters: kvpairs = screen_state=on
system_process W/VibratorService: Tried to vibrate but there is no vibrator device.
system_process D/WindowManager: ALog interceptKeyBeforeQueueing KEYCODE_POWER
system_process I/ActivityManager: START u0 {act=android.media.action.STILL_IMAGE_CAMERA flg=0x14000000 cmp=com.android.camera2/com.android.camera.CameraActivity} from uid 10018 on display 0
system_process E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
system_process E/ActivityManager: applyOptionsLocked: Unknown animationType=0
system_process I/ActivityManager: Start proc 1467:com.android.camera2/u0a25 for activity com.android.camera2/com.android.camera.CameraActivity
  • 加了LOG interceptKeyBeforeQueueing KEYCODE_POWER
  • 在觸發後有輸出關鍵LOG Power button double tap gesture detected, launching camera. Interval=220ms
  • 之後啟動了相機 ActivityManager: START u0 {act=android.media.action.STILL_IMAGE_CAMERA flg=0x14000000 cmp=com.android.camera2/com.android.camera.CameraActivity} from uid 10018 on display 0

frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

    /** {@inheritDoc} */
    @Override
    public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
        /**省略**/
        // Handle special keys.
        switch (keyCode) {
            /**省略**/
            case KeyEvent.KEYCODE_POWER: {
                android.util.Log.d(TAG, "ALog interceptKeyBeforeQueueing KEYCODE_POWER");
                try {
                    if (mWindowManager.getDualScreenFlag()) {
                        if (mWindowManager.getSecondDisplayTaskId() != -1) {
                            break;
                        }
                    }
                } catch (RemoteException e) {

                }
                result &= ~ACTION_PASS_TO_USER;
                isWakeKey = false; // wake-up will be handled separately
                if (down) {
                    interceptPowerKeyDown(event, interactive);
                } else {
                    interceptPowerKeyUp(event, interactive, canceled);
                }
                break;
            }

        /**省略**/
    }
    private void interceptPowerKeyDown(KeyEvent event, boolean interactive) {
        // Hold a wake lock until the power key is released.
        /**省略**/
        GestureLauncherService gestureService = LocalServices.getService(
                GestureLauncherService.class);
        boolean gesturedServiceIntercepted = false;
        if (gestureService != null) {
            gesturedServiceIntercepted = gestureService.interceptPowerKeyDown(event, interactive,
                    mTmpBoolean);
            if (mTmpBoolean.value && mGoingToSleep) {
                mCameraGestureTriggeredDuringGoingToSleep = true;
            }
        }
        /**省略**/     

frameworks/base/services/core/java/com/android/server/GestureLauncherService.java

    public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive,
            MutableBoolean outLaunched) {
        boolean launched = false;
        boolean intercept = false;
        long doubleTapInterval;
        synchronized (this) {
            doubleTapInterval = event.getEventTime() - mLastPowerDown;
            if (mCameraDoubleTapPowerEnabled
                    && doubleTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS) {
                launched = true;
                intercept = interactive;
            }
            mLastPowerDown = event.getEventTime();
        }
        if (launched) {
            Slog.i(TAG, "Power button double tap gesture detected, launching camera. Interval="
                    + doubleTapInterval + "ms");
            launched = handleCameraLaunchGesture(false /* useWakelock */,
                    StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP);
            if (launched) {
                MetricsLogger.action(mContext, MetricsEvent.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE,
                        (int) doubleTapInterval);
            }
        }
        MetricsLogger.histogram(mContext, "power_double_tap_interval", (int) doubleTapInterval);
        outLaunched.value = launched;
        return intercept && launched;
    }
    /**
     * @return true if camera was launched, false otherwise.
     */
    private boolean handleCameraLaunchGesture(boolean useWakelock, int source) {
        boolean userSetupComplete = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0;
        if (!userSetupComplete) {
            if (DBG) Slog.d(TAG, String.format(
                    "userSetupComplete = %s, ignoring camera launch gesture.",
                    userSetupComplete));
            return false;
        }
        if (DBG) Slog.d(TAG, String.format(
                "userSetupComplete = %s, performing camera launch gesture.",
                userSetupComplete));

        if (useWakelock) {
            // Make sure we don't sleep too early
            mWakeLock.acquire(500L);
        }
        StatusBarManagerInternal service = LocalServices.getService(
                StatusBarManagerInternal.class);
        service.onCameraLaunchGestureDetected(source);
        return true;
    }

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java

    @Override
    public void onCameraLaunchGestureDetected(int source) {
        mLastCameraLaunchSource = source;
        if (mStartedGoingToSleep) {
            mLaunchCameraOnFinishedGoingToSleep = true;
            return;
        }
        if (!mNotificationPanel.canCameraGestureBeLaunched(
                mStatusBarKeyguardViewManager.isShowing() && mExpandedVisible)) {
            return;
        }
        if (!mDeviceInteractive) {
            PowerManager pm = mContext.getSystemService(PowerManager.class);
            pm.wakeUp(SystemClock.uptimeMillis(), "com.android.systemui:CAMERA_GESTURE");
            mStatusBarKeyguardViewManager.notifyDeviceWakeUpRequested();
        }
        vibrateForCameraGesture();
        if (!mStatusBarKeyguardViewManager.isShowing()) {
        	//啟動攝像頭
            startActivity(KeyguardBottomAreaView.INSECURE_CAMERA_INTENT,
                    true /* dismissShade */);
        } else {
            if (!mDeviceInteractive) {
                // Avoid flickering of the scrim when we instant launch the camera and the bouncer
                // comes on.
                mScrimController.dontAnimateBouncerChangesUntilNextFrame();
                mGestureWakeLock.acquire(LAUNCH_TRANSITION_TIMEOUT_MS + 1000L);
            }
            if (mScreenTurningOn || mStatusBarKeyguardViewManager.isScreenTurnedOn()) {
                mNotificationPanel.launchCamera(mDeviceInteractive /* animate */, source);
            } else {
                // We need to defer the camera launch until the screen comes on, since otherwise
                // we will dismiss us too early since we are waiting on an activity to be drawn and
                // incorrectly get notified because of the screen on event (which resumes and pauses
                // some activities)
                mLaunchCameraOnScreenTurningOn = true;
            }
        }
    }

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java

    public static final Intent INSECURE_CAMERA_INTENT =
            new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);

相關文章