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