1 /* 2 * Copyright (C) 2019 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.power; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorInflater; 21 import android.animation.AnimatorListenerAdapter; 22 import android.content.Context; 23 import android.graphics.PixelFormat; 24 import android.os.Binder; 25 import android.os.IBinder; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.view.WindowManager; 30 import android.widget.FrameLayout; 31 32 import com.android.systemui.res.R; 33 34 /** 35 * View that shows a warning shortly before the device goes into sleep 36 * after prolonged user inactivity when bound to. 37 */ 38 public class InattentiveSleepWarningView extends FrameLayout { 39 private final IBinder mWindowToken = new Binder(); 40 private final WindowManager mWindowManager; 41 private Animator mFadeOutAnimator; 42 private boolean mDismissing; 43 InattentiveSleepWarningView(Context context)44 InattentiveSleepWarningView(Context context) { 45 super(context); 46 mWindowManager = mContext.getSystemService(WindowManager.class); 47 48 final LayoutInflater layoutInflater = LayoutInflater.from(mContext); 49 layoutInflater.inflate(R.layout.inattentive_sleep_warning, this, true /* attachToRoot */); 50 51 setFocusable(true); 52 setOnKeyListener((v, keyCode, event) -> { 53 // overlay consumes key presses 54 return true; 55 }); 56 57 mFadeOutAnimator = AnimatorInflater.loadAnimator(getContext(), 58 com.android.internal.R.animator.fade_out); 59 mFadeOutAnimator.setTarget(this); 60 mFadeOutAnimator.addListener(new AnimatorListenerAdapter() { 61 @Override 62 public void onAnimationEnd(Animator animation) { 63 removeView(); 64 } 65 66 @Override 67 public void onAnimationCancel(Animator animation) { 68 mDismissing = false; 69 setAlpha(1f); 70 setVisibility(View.VISIBLE); 71 } 72 }); 73 } 74 removeView()75 private void removeView() { 76 if (mDismissing) { 77 setVisibility(View.INVISIBLE); 78 mWindowManager.removeView(InattentiveSleepWarningView.this); 79 } 80 } 81 82 /** 83 * Show the warning. 84 */ show()85 public void show() { 86 if (getParent() != null) { 87 if (mFadeOutAnimator.isStarted()) { 88 mFadeOutAnimator.cancel(); 89 } 90 return; 91 } 92 93 setAlpha(1f); 94 setVisibility(View.VISIBLE); 95 mWindowManager.addView(this, getLayoutParams(mWindowToken)); 96 announceForAccessibility( 97 getContext().getString(R.string.inattentive_sleep_warning_message)); 98 } 99 100 /** 101 * Dismiss the warning. 102 */ dismiss(boolean animated)103 public void dismiss(boolean animated) { 104 if (getParent() == null) { 105 return; 106 } 107 108 mDismissing = true; 109 110 if (animated) { 111 postOnAnimation(mFadeOutAnimator::start); 112 } else { 113 removeView(); 114 } 115 } 116 117 /** 118 * @param windowToken token for the window 119 */ getLayoutParams(IBinder windowToken)120 private WindowManager.LayoutParams getLayoutParams(IBinder windowToken) { 121 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams( 122 ViewGroup.LayoutParams.MATCH_PARENT, 123 ViewGroup.LayoutParams.MATCH_PARENT, 124 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 125 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, 126 PixelFormat.TRANSLUCENT); 127 lp.privateFlags |= WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS; 128 lp.setTitle("InattentiveSleepWarning"); 129 lp.token = windowToken; 130 return lp; 131 } 132 } 133