Home | History | Annotate | Download | only in doze
      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;
     20 import android.hardware.display.AmbientDisplayConfiguration;
     21 import android.os.Handler;
     22 import android.os.UserHandle;
     23 import android.util.Log;
     24 
     25 import com.android.systemui.dock.DockManager;
     26 import com.android.systemui.doze.DozeMachine.State;
     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 
     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 
     45     private int mDockState = DockManager.STATE_NONE;
     46 
     47     public DozeDockHandler(Context context, DozeMachine machine, DozeHost dozeHost,
     48             AmbientDisplayConfiguration config, Handler handler, DockManager dockManager) {
     49         mMachine = machine;
     50         mDozeHost = dozeHost;
     51         mConfig = config;
     52         mHandler = handler;
     53         mDockManager = dockManager;
     54     }
     55 
     56     @Override
     57     public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
     58         switch (newState) {
     59             case INITIALIZED:
     60                 mDockEventListener.register();
     61                 break;
     62             case DOZE_AOD:
     63                 if (mDockState == DockManager.STATE_DOCKED_HIDE) {
     64                     mMachine.requestState(State.DOZE);
     65                     break;
     66                 }
     67                 // continue below
     68             case DOZE:
     69                 if (mDockState == DockManager.STATE_DOCKED) {
     70                     mHandler.post(() -> requestPulse(newState));
     71                 }
     72                 break;
     73             case FINISH:
     74                 mDockEventListener.unregister();
     75                 break;
     76             default:
     77                 // no-op
     78         }
     79     }
     80 
     81     private void requestPulse(State dozeState) {
     82         if (mDozeHost.isPulsingBlocked() || !dozeState.canPulse()) {
     83             return;
     84         }
     85 
     86         mMachine.requestPulse(DozeLog.PULSE_REASON_DOCKING);
     87     }
     88 
     89     private void requestPulseOutNow(State dozeState) {
     90         if (dozeState == State.DOZE_REQUEST_PULSE || dozeState == State.DOZE_PULSING
     91                 || dozeState == State.DOZE_PULSING_BRIGHT) {
     92             final int pulseReason = mMachine.getPulseReason();
     93             if (pulseReason == DozeLog.PULSE_REASON_DOCKING) {
     94                 mDozeHost.stopPulsing();
     95             }
     96         }
     97     }
     98 
     99     private boolean isDocked() {
    100         return mDockState == DockManager.STATE_DOCKED
    101                 || mDockState == DockManager.STATE_DOCKED_HIDE;
    102     }
    103 
    104     @Override
    105     public void dump(PrintWriter pw) {
    106         pw.print(" DozeDockTriggers docking="); pw.println(isDocked());
    107     }
    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);
    115             final DozeMachine.State dozeState = mMachine.getState();
    116             mDockState = event;
    117             switch (mDockState) {
    118                 case DockManager.STATE_DOCKED:
    119                     requestPulse(dozeState);
    120                     break;
    121                 case DockManager.STATE_NONE:
    122                     if (dozeState == State.DOZE
    123                             && mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) {
    124                         mMachine.requestState(State.DOZE_AOD);
    125                     }
    126                     else {
    127                         requestPulseOutNow(dozeState);
    128                     }
    129                     break;
    130                 case DockManager.STATE_DOCKED_HIDE:
    131                     if (dozeState == State.DOZE_AOD) {
    132                         mMachine.requestState(State.DOZE);
    133                     } else {
    134                         requestPulseOutNow(dozeState);
    135                     }
    136                     break;
    137                 default:
    138                     // no-op
    139             }
    140         }
    141 
    142         void register() {
    143             if (mRegistered) {
    144                 return;
    145             }
    146             if (mDockManager != null) {
    147                 mDockManager.addListener(this);
    148             }
    149             mRegistered = true;
    150         }
    151 
    152         void unregister() {
    153             if (!mRegistered) {
    154                 return;
    155             }
    156             if (mDockManager != null) {
    157                 mDockManager.removeListener(this);
    158             }
    159             mRegistered = false;
    160         }
    161     }
    162 }
    163