TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.doze; |
| 18 | |
| 19 | import android.content.Context; |
Issei Suzuki | ca19e6e | 2019-02-26 12:39:11 +0100 | [diff] [blame] | 20 | import android.hardware.display.AmbientDisplayConfiguration; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 21 | import android.os.Handler; |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 22 | import android.os.UserHandle; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 25 | import com.android.systemui.dock.DockManager; |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 26 | import com.android.systemui.doze.DozeMachine.State; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 27 | |
| 28 | import java.io.PrintWriter; |
| 29 | |
| 30 | /** |
| 31 | * Handles dock events for ambient state changes. |
| 32 | */ |
| 33 | public class DozeDockHandler implements DozeMachine.Part { |
| 34 | |
| 35 | private static final String TAG = "DozeDockHandler"; |
| 36 | private static final boolean DEBUG = DozeService.DEBUG; |
| 37 | |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 38 | private final DozeMachine mMachine; |
| 39 | private final DozeHost mDozeHost; |
| 40 | private final AmbientDisplayConfiguration mConfig; |
| 41 | private final Handler mHandler; |
| 42 | private final DockEventListener mDockEventListener = new DockEventListener(); |
| 43 | private final DockManager mDockManager; |
| 44 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 45 | private int mDockState = DockManager.STATE_NONE; |
Jerry Chang | 9fa6d73 | 2019-06-21 18:05:41 +0800 | [diff] [blame] | 46 | private boolean mPulsePending; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 47 | |
| 48 | public DozeDockHandler(Context context, DozeMachine machine, DozeHost dozeHost, |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 49 | AmbientDisplayConfiguration config, Handler handler, DockManager dockManager) { |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 50 | mMachine = machine; |
| 51 | mDozeHost = dozeHost; |
| 52 | mConfig = config; |
| 53 | mHandler = handler; |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 54 | mDockManager = dockManager; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) { |
| 59 | switch (newState) { |
| 60 | case INITIALIZED: |
| 61 | mDockEventListener.register(); |
| 62 | break; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 63 | case DOZE_AOD: |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 64 | if (mDockState == DockManager.STATE_DOCKED_HIDE) { |
| 65 | mMachine.requestState(State.DOZE); |
| 66 | break; |
| 67 | } |
| 68 | // continue below |
| 69 | case DOZE: |
Jerry Chang | 9fa6d73 | 2019-06-21 18:05:41 +0800 | [diff] [blame] | 70 | if (mDockState == DockManager.STATE_DOCKED && !mPulsePending) { |
| 71 | mPulsePending = true; |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 72 | mHandler.post(() -> requestPulse(newState)); |
| 73 | } |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 74 | break; |
| 75 | case FINISH: |
| 76 | mDockEventListener.unregister(); |
| 77 | break; |
| 78 | default: |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 79 | // no-op |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 83 | private void requestPulse(State dozeState) { |
Jerry Chang | 9fa6d73 | 2019-06-21 18:05:41 +0800 | [diff] [blame] | 84 | if (!mDozeHost.isPulsingBlocked() && dozeState.canPulse()) { |
| 85 | mMachine.requestPulse(DozeLog.PULSE_REASON_DOCKING); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 86 | } |
Jerry Chang | 9fa6d73 | 2019-06-21 18:05:41 +0800 | [diff] [blame] | 87 | mPulsePending = false; |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 90 | private void requestPulseOutNow(State dozeState) { |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 91 | if (dozeState == State.DOZE_REQUEST_PULSE || dozeState == State.DOZE_PULSING |
| 92 | || dozeState == State.DOZE_PULSING_BRIGHT) { |
TYM Tsai | 2d23690 | 2018-12-27 16:46:06 +0800 | [diff] [blame] | 93 | final int pulseReason = mMachine.getPulseReason(); |
| 94 | if (pulseReason == DozeLog.PULSE_REASON_DOCKING) { |
| 95 | mDozeHost.stopPulsing(); |
| 96 | } |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 100 | private boolean isDocked() { |
| 101 | return mDockState == DockManager.STATE_DOCKED |
| 102 | || mDockState == DockManager.STATE_DOCKED_HIDE; |
| 103 | } |
| 104 | |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 105 | @Override |
| 106 | public void dump(PrintWriter pw) { |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 107 | pw.print(" DozeDockTriggers docking="); pw.println(isDocked()); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private class DockEventListener implements DockManager.DockEventListener { |
| 111 | private boolean mRegistered; |
| 112 | |
| 113 | @Override |
| 114 | public void onEvent(int event) { |
| 115 | if (DEBUG) Log.d(TAG, "dock event = " + event); |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 116 | final DozeMachine.State dozeState = mMachine.getState(); |
| 117 | mDockState = event; |
| 118 | switch (mDockState) { |
| 119 | case DockManager.STATE_DOCKED: |
| 120 | requestPulse(dozeState); |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 121 | break; |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 122 | case DockManager.STATE_NONE: |
| 123 | if (dozeState == State.DOZE |
| 124 | && mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) { |
| 125 | mMachine.requestState(State.DOZE_AOD); |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 126 | } |
Joanne Chung | 57be7e1 | 2019-03-07 20:02:57 +0800 | [diff] [blame] | 127 | else { |
| 128 | requestPulseOutNow(dozeState); |
| 129 | } |
| 130 | break; |
Jerry Chang | 5d3eb447 | 2018-12-21 11:49:06 +0800 | [diff] [blame] | 131 | case DockManager.STATE_DOCKED_HIDE: |
Joanne Chung | 57be7e1 | 2019-03-07 20:02:57 +0800 | [diff] [blame] | 132 | if (dozeState == State.DOZE_AOD) { |
| 133 | mMachine.requestState(State.DOZE); |
| 134 | } else { |
| 135 | requestPulseOutNow(dozeState); |
| 136 | } |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 137 | break; |
| 138 | default: |
| 139 | // no-op |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void register() { |
| 144 | if (mRegistered) { |
| 145 | return; |
| 146 | } |
TYM Tsai | 2fc027d | 2018-12-04 19:28:19 +0800 | [diff] [blame] | 147 | if (mDockManager != null) { |
| 148 | mDockManager.addListener(this); |
| 149 | } |
| 150 | mRegistered = true; |
| 151 | } |
| 152 | |
| 153 | void unregister() { |
| 154 | if (!mRegistered) { |
| 155 | return; |
| 156 | } |
| 157 | if (mDockManager != null) { |
| 158 | mDockManager.removeListener(this); |
| 159 | } |
| 160 | mRegistered = false; |
| 161 | } |
| 162 | } |
| 163 | } |