blob: 9fc22340ea22aede3cfe5bc3ef0c60d6c118e639 [file] [log] [blame]
TYM Tsai2fc027d2018-12-04 19:28:19 +08001/*
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
17package com.android.systemui.doze;
18
19import android.content.Context;
20import android.os.Handler;
Jerry Chang5d3eb4472018-12-21 11:49:06 +080021import android.os.UserHandle;
TYM Tsai2fc027d2018-12-04 19:28:19 +080022import android.util.Log;
23
24import com.android.internal.hardware.AmbientDisplayConfiguration;
25import com.android.systemui.SysUiServiceProvider;
26import com.android.systemui.dock.DockManager;
Jerry Chang5d3eb4472018-12-21 11:49:06 +080027import com.android.systemui.doze.DozeMachine.State;
TYM Tsai2fc027d2018-12-04 19:28:19 +080028
29import java.io.PrintWriter;
30
31/**
32 * Handles dock events for ambient state changes.
33 */
34public class DozeDockHandler implements DozeMachine.Part {
35
36 private static final String TAG = "DozeDockHandler";
37 private static final boolean DEBUG = DozeService.DEBUG;
38
TYM Tsai2fc027d2018-12-04 19:28:19 +080039 private final DozeMachine mMachine;
40 private final DozeHost mDozeHost;
41 private final AmbientDisplayConfiguration mConfig;
42 private final Handler mHandler;
43 private final DockEventListener mDockEventListener = new DockEventListener();
44 private final DockManager mDockManager;
45
Jerry Chang5d3eb4472018-12-21 11:49:06 +080046 private int mDockState = DockManager.STATE_NONE;
TYM Tsai2fc027d2018-12-04 19:28:19 +080047
48 public DozeDockHandler(Context context, DozeMachine machine, DozeHost dozeHost,
49 AmbientDisplayConfiguration config, Handler handler) {
TYM Tsai2fc027d2018-12-04 19:28:19 +080050 mMachine = machine;
51 mDozeHost = dozeHost;
52 mConfig = config;
53 mHandler = handler;
54 mDockManager = SysUiServiceProvider.getComponent(context, DockManager.class);
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 Tsai2fc027d2018-12-04 19:28:19 +080063 case DOZE_AOD:
Jerry Chang5d3eb4472018-12-21 11:49:06 +080064 if (mDockState == DockManager.STATE_DOCKED_HIDE) {
65 mMachine.requestState(State.DOZE);
66 break;
67 }
68 // continue below
69 case DOZE:
70 if (mDockState == DockManager.STATE_DOCKED) {
71 mHandler.post(() -> requestPulse(newState));
72 }
TYM Tsai2fc027d2018-12-04 19:28:19 +080073 break;
74 case FINISH:
75 mDockEventListener.unregister();
76 break;
77 default:
Jerry Chang5d3eb4472018-12-21 11:49:06 +080078 // no-op
TYM Tsai2fc027d2018-12-04 19:28:19 +080079 }
80 }
81
Jerry Chang5d3eb4472018-12-21 11:49:06 +080082 private void requestPulse(State dozeState) {
83 if (mDozeHost.isPulsingBlocked() || !dozeState.canPulse()) {
TYM Tsai2fc027d2018-12-04 19:28:19 +080084 return;
85 }
86
87 mMachine.requestPulse(DozeLog.PULSE_REASON_DOCKING);
88 }
89
Jerry Chang5d3eb4472018-12-21 11:49:06 +080090 private void requestPulseOutNow(State dozeState) {
91 if (dozeState == State.DOZE_REQUEST_PULSE || dozeState == State.DOZE_PULSING) {
TYM Tsai2d236902018-12-27 16:46:06 +080092 final int pulseReason = mMachine.getPulseReason();
93 if (pulseReason == DozeLog.PULSE_REASON_DOCKING) {
94 mDozeHost.stopPulsing();
95 }
TYM Tsai2fc027d2018-12-04 19:28:19 +080096 }
97 }
98
Jerry Chang5d3eb4472018-12-21 11:49:06 +080099 private boolean isDocked() {
100 return mDockState == DockManager.STATE_DOCKED
101 || mDockState == DockManager.STATE_DOCKED_HIDE;
102 }
103
TYM Tsai2fc027d2018-12-04 19:28:19 +0800104 @Override
105 public void dump(PrintWriter pw) {
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800106 pw.print(" DozeDockTriggers docking="); pw.println(isDocked());
TYM Tsai2fc027d2018-12-04 19:28:19 +0800107 }
108
109 private class DockEventListener implements DockManager.DockEventListener {
110 private boolean mRegistered;
111
112 @Override
113 public void onEvent(int event) {
114 if (DEBUG) Log.d(TAG, "dock event = " + event);
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800115 final DozeMachine.State dozeState = mMachine.getState();
116 mDockState = event;
117 switch (mDockState) {
118 case DockManager.STATE_DOCKED:
119 requestPulse(dozeState);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800120 break;
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800121 case DockManager.STATE_NONE:
122 if (dozeState == State.DOZE
123 && mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) {
124 mMachine.requestState(State.DOZE_AOD);
125 break;
126 }
127 // continue below
128 case DockManager.STATE_DOCKED_HIDE:
129 requestPulseOutNow(dozeState);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800130 break;
131 default:
132 // no-op
133 }
134 }
135
136 void register() {
137 if (mRegistered) {
138 return;
139 }
TYM Tsai2fc027d2018-12-04 19:28:19 +0800140 if (mDockManager != null) {
141 mDockManager.addListener(this);
142 }
143 mRegistered = true;
144 }
145
146 void unregister() {
147 if (!mRegistered) {
148 return;
149 }
150 if (mDockManager != null) {
151 mDockManager.removeListener(this);
152 }
153 mRegistered = false;
154 }
155 }
156}