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.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     }
97 
98     /**
99      * Dismiss the warning.
100      */
dismiss(boolean animated)101     public void dismiss(boolean animated) {
102         if (getParent() == null) {
103             return;
104         }
105 
106         mDismissing = true;
107 
108         if (animated) {
109             postOnAnimation(mFadeOutAnimator::start);
110         } else {
111             removeView();
112         }
113     }
114 
115     /**
116      * @param windowToken token for the window
117      */
getLayoutParams(IBinder windowToken)118     private WindowManager.LayoutParams getLayoutParams(IBinder windowToken) {
119         final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
120                 ViewGroup.LayoutParams.MATCH_PARENT,
121                 ViewGroup.LayoutParams.MATCH_PARENT,
122                 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
123                 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
124                 PixelFormat.TRANSLUCENT);
125         lp.privateFlags |= WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS;
126         lp.setTitle("InattentiveSleepWarning");
127         lp.token = windowToken;
128         return lp;
129     }
130 }
131