1 /*
2  * Copyright (C) 2017 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.annotation.Nullable;
20 import android.app.IWallpaperManager;
21 import android.os.RemoteException;
22 import android.util.Log;
23 
24 import com.android.systemui.statusbar.notification.stack.StackStateAnimator;
25 import com.android.systemui.statusbar.phone.BiometricUnlockController;
26 import com.android.systemui.statusbar.phone.DozeParameters;
27 
28 import java.io.PrintWriter;
29 
30 /**
31  * Propagates doze state to wallpaper engine.
32  */
33 public class DozeWallpaperState implements DozeMachine.Part {
34 
35     private static final String TAG = "DozeWallpaperState";
36     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
37 
38     @Nullable
39     private final IWallpaperManager mWallpaperManagerService;
40     private final DozeParameters mDozeParameters;
41     private final BiometricUnlockController mBiometricUnlockController;
42     private boolean mIsAmbientMode;
43 
DozeWallpaperState( IWallpaperManager wallpaperManagerService, BiometricUnlockController biometricUnlockController, DozeParameters parameters)44     public DozeWallpaperState(
45             IWallpaperManager wallpaperManagerService,
46             BiometricUnlockController biometricUnlockController,
47             DozeParameters parameters) {
48         mWallpaperManagerService = wallpaperManagerService;
49         mBiometricUnlockController = biometricUnlockController;
50         mDozeParameters = parameters;
51     }
52 
53     @Override
transitionTo(DozeMachine.State oldState, DozeMachine.State newState)54     public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
55         final boolean isAmbientMode;
56         switch (newState) {
57             case DOZE:
58             case DOZE_AOD:
59             case DOZE_AOD_DOCKED:
60             case DOZE_AOD_PAUSING:
61             case DOZE_AOD_PAUSED:
62             case DOZE_REQUEST_PULSE:
63             case DOZE_PULSE_DONE:
64             case DOZE_PULSING:
65                 isAmbientMode = true;
66                 break;
67             case DOZE_PULSING_BRIGHT:
68             default:
69                 isAmbientMode = false;
70         }
71         final boolean animated;
72         if (isAmbientMode) {
73             animated = mDozeParameters.shouldControlScreenOff();
74         } else {
75             boolean wakingUpFromPulse = oldState == DozeMachine.State.DOZE_PULSING
76                     && newState == DozeMachine.State.FINISH;
77             boolean fastDisplay = !mDozeParameters.getDisplayNeedsBlanking();
78             animated = (fastDisplay && !mBiometricUnlockController.unlockedByWakeAndUnlock())
79                     || wakingUpFromPulse;
80         }
81 
82         if (isAmbientMode != mIsAmbientMode) {
83             mIsAmbientMode = isAmbientMode;
84             if (mWallpaperManagerService != null) {
85                 try {
86                     long duration = animated ? StackStateAnimator.ANIMATION_DURATION_WAKEUP : 0L;
87                     if (DEBUG) {
88                         Log.i(TAG, "AOD wallpaper state changed to: " + mIsAmbientMode
89                             + ", animationDuration: " + duration);
90                     }
91                     mWallpaperManagerService.setInAmbientMode(mIsAmbientMode, duration);
92                 } catch (RemoteException e) {
93                     // Cannot notify wallpaper manager service, but it's fine, let's just skip it.
94                     Log.w(TAG, "Cannot notify state to WallpaperManagerService: " + mIsAmbientMode);
95                 }
96             }
97         }
98     }
99 
100     @Override
dump(PrintWriter pw)101     public void dump(PrintWriter pw) {
102         pw.println("DozeWallpaperState:");
103         pw.println(" isAmbientMode: " + mIsAmbientMode);
104         pw.println(" hasWallpaperService: " + (mWallpaperManagerService != null));
105     }
106 }
107