2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.doze;
19 import android.content.Context;
20 import android.hardware.display.AmbientDisplayConfiguration;
21 import android.os.Handler;
22 import android.os.UserHandle;
23 import android.util.Log;
25 import com.android.systemui.dock.DockManager;
26 import com.android.systemui.doze.DozeMachine.State;
28 import java.io.PrintWriter;
31 * Handles dock events for ambient state changes.
33 public class DozeDockHandler implements DozeMachine.Part {
35 private static final String TAG = "DozeDockHandler";
36 private static final boolean DEBUG = DozeService.DEBUG;
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;
45 private int mDockState = DockManager.STATE_NONE;
46 private boolean mPulsePending;
48 public DozeDockHandler(Context context, DozeMachine machine, DozeHost dozeHost,
49 AmbientDisplayConfiguration config, Handler handler, DockManager dockManager) {
54 mDockManager = dockManager;
58 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
61 mDockEventListener.register();
64 if (mDockState == DockManager.STATE_DOCKED_HIDE) {
65 mMachine.requestState(State.DOZE);
70 if (mDockState == DockManager.STATE_DOCKED && !mPulsePending) {
72 mHandler.post(() -> requestPulse(newState));
76 mDockEventListener.unregister();
83 private void requestPulse(State dozeState) {
84 if (!mDozeHost.isPulsingBlocked() && dozeState.canPulse()) {
85 mMachine.requestPulse(DozeLog.PULSE_REASON_DOCKING);
87 mPulsePending = false;
90 private void requestPulseOutNow(State dozeState) {
91 if (dozeState == State.DOZE_REQUEST_PULSE || dozeState == State.DOZE_PULSING
92 || dozeState == State.DOZE_PULSING_BRIGHT) {
93 final int pulseReason = mMachine.getPulseReason();
94 if (pulseReason == DozeLog.PULSE_REASON_DOCKING) {
95 mDozeHost.stopPulsing();
100 private boolean isDocked() {
101 return mDockState == DockManager.STATE_DOCKED
102 || mDockState == DockManager.STATE_DOCKED_HIDE;
106 public void dump(PrintWriter pw) {
107 pw.print(" DozeDockTriggers docking="); pw.println(isDocked());
110 private class DockEventListener implements DockManager.DockEventListener {
111 private boolean mRegistered;
114 public void onEvent(int event) {
115 if (DEBUG) Log.d(TAG, "dock event = " + event);
116 final DozeMachine.State dozeState = mMachine.getState();
118 switch (mDockState) {
119 case DockManager.STATE_DOCKED:
120 requestPulse(dozeState);
122 case DockManager.STATE_NONE:
123 if (dozeState == State.DOZE
124 && mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) {
125 mMachine.requestState(State.DOZE_AOD);
128 requestPulseOutNow(dozeState);
131 case DockManager.STATE_DOCKED_HIDE:
132 if (dozeState == State.DOZE_AOD) {
133 mMachine.requestState(State.DOZE);
135 requestPulseOutNow(dozeState);
147 if (mDockManager != null) {
148 mDockManager.addListener(this);
157 if (mDockManager != null) {
158 mDockManager.removeListener(this);