huangcm
2025-04-11 48566d1cda2d109a94496c806286f47b8984166d
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.android.systemui.doze;
 
import android.content.Context;
import android.hardware.display.AmbientDisplayConfiguration;
import android.os.Handler;
import android.os.UserHandle;
import android.util.Log;
 
import com.android.systemui.dock.DockManager;
import com.android.systemui.doze.DozeMachine.State;
 
import java.io.PrintWriter;
 
/**
 * Handles dock events for ambient state changes.
 */
public class DozeDockHandler implements DozeMachine.Part {
 
    private static final String TAG = "DozeDockHandler";
    private static final boolean DEBUG = DozeService.DEBUG;
 
    private final DozeMachine mMachine;
    private final DozeHost mDozeHost;
    private final AmbientDisplayConfiguration mConfig;
    private final Handler mHandler;
    private final DockEventListener mDockEventListener = new DockEventListener();
    private final DockManager mDockManager;
 
    private int mDockState = DockManager.STATE_NONE;
 
    public DozeDockHandler(Context context, DozeMachine machine, DozeHost dozeHost,
            AmbientDisplayConfiguration config, Handler handler, DockManager dockManager) {
        mMachine = machine;
        mDozeHost = dozeHost;
        mConfig = config;
        mHandler = handler;
        mDockManager = dockManager;
    }
 
    @Override
    public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
        switch (newState) {
            case INITIALIZED:
                mDockEventListener.register();
                break;
            case DOZE_AOD:
                if (mDockState == DockManager.STATE_DOCKED_HIDE) {
                    mMachine.requestState(State.DOZE);
                    break;
                }
                // continue below
            case DOZE:
                if (mDockState == DockManager.STATE_DOCKED) {
                    mHandler.post(() -> requestPulse(newState));
                }
                break;
            case FINISH:
                mDockEventListener.unregister();
                break;
            default:
                // no-op
        }
    }
 
    private void requestPulse(State dozeState) {
        if (mDozeHost.isPulsingBlocked() || !dozeState.canPulse()) {
            return;
        }
 
        mMachine.requestPulse(DozeLog.PULSE_REASON_DOCKING);
    }
 
    private void requestPulseOutNow(State dozeState) {
        if (dozeState == State.DOZE_REQUEST_PULSE || dozeState == State.DOZE_PULSING
                || dozeState == State.DOZE_PULSING_BRIGHT) {
            final int pulseReason = mMachine.getPulseReason();
            if (pulseReason == DozeLog.PULSE_REASON_DOCKING) {
                mDozeHost.stopPulsing();
            }
        }
    }
 
    private boolean isDocked() {
        return mDockState == DockManager.STATE_DOCKED
                || mDockState == DockManager.STATE_DOCKED_HIDE;
    }
 
    @Override
    public void dump(PrintWriter pw) {
        pw.print(" DozeDockTriggers docking="); pw.println(isDocked());
    }
 
    private class DockEventListener implements DockManager.DockEventListener {
        private boolean mRegistered;
 
        @Override
        public void onEvent(int event) {
            if (DEBUG) Log.d(TAG, "dock event = " + event);
            final DozeMachine.State dozeState = mMachine.getState();
            mDockState = event;
            switch (mDockState) {
                case DockManager.STATE_DOCKED:
                    requestPulse(dozeState);
                    break;
                case DockManager.STATE_NONE:
                    if (dozeState == State.DOZE
                            && mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) {
                        mMachine.requestState(State.DOZE_AOD);
                    }
                    else {
                        requestPulseOutNow(dozeState);
                    }
                    break;
                case DockManager.STATE_DOCKED_HIDE:
                    if (dozeState == State.DOZE_AOD) {
                        mMachine.requestState(State.DOZE);
                    } else {
                        requestPulseOutNow(dozeState);
                    }
                    break;
                default:
                    // no-op
            }
        }
 
        void register() {
            if (mRegistered) {
                return;
            }
            if (mDockManager != null) {
                mDockManager.addListener(this);
            }
            mRegistered = true;
        }
 
        void unregister() {
            if (!mRegistered) {
                return;
            }
            if (mDockManager != null) {
                mDockManager.removeListener(this);
            }
            mRegistered = false;
        }
    }
}