1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.globalactions;
16 
17 import static android.app.StatusBarManager.DISABLE2_GLOBAL_ACTIONS;
18 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
19 
20 import android.app.Dialog;
21 import android.app.KeyguardManager;
22 import android.app.WallpaperManager;
23 import android.content.Context;
24 import android.graphics.Point;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.view.Window;
28 import android.view.WindowManager;
29 import android.widget.ProgressBar;
30 import android.widget.TextView;
31 
32 import com.android.internal.R;
33 import com.android.internal.colorextraction.ColorExtractor.GradientColors;
34 import com.android.internal.colorextraction.drawable.GradientDrawable;
35 import com.android.settingslib.Utils;
36 import com.android.systemui.Dependency;
37 import com.android.systemui.SysUiServiceProvider;
38 import com.android.systemui.colorextraction.SysuiColorExtractor;
39 import com.android.systemui.plugins.GlobalActions;
40 import com.android.systemui.statusbar.CommandQueue;
41 import com.android.systemui.statusbar.policy.DeviceProvisionedController;
42 import com.android.systemui.statusbar.policy.KeyguardMonitor;
43 
44 public class GlobalActionsImpl implements GlobalActions, CommandQueue.Callbacks {
45 
46     private static final float SHUTDOWN_SCRIM_ALPHA = 0.95f;
47 
48     private final Context mContext;
49     private final KeyguardMonitor mKeyguardMonitor;
50     private final DeviceProvisionedController mDeviceProvisionedController;
51     private GlobalActionsDialog mGlobalActions;
52     private boolean mDisabled;
53 
GlobalActionsImpl(Context context)54     public GlobalActionsImpl(Context context) {
55         mContext = context;
56         mKeyguardMonitor = Dependency.get(KeyguardMonitor.class);
57         mDeviceProvisionedController = Dependency.get(DeviceProvisionedController.class);
58         SysUiServiceProvider.getComponent(context, CommandQueue.class).addCallbacks(this);
59     }
60 
61     @Override
destroy()62     public void destroy() {
63         SysUiServiceProvider.getComponent(mContext, CommandQueue.class).removeCallbacks(this);
64     }
65 
66     @Override
showGlobalActions(GlobalActionsManager manager)67     public void showGlobalActions(GlobalActionsManager manager) {
68         if (mDisabled) return;
69         if (mGlobalActions == null) {
70             mGlobalActions = new GlobalActionsDialog(mContext, manager);
71         }
72         mGlobalActions.showDialog(mKeyguardMonitor.isShowing(),
73                 mDeviceProvisionedController.isDeviceProvisioned());
74     }
75 
76     @Override
showShutdownUi(boolean isReboot, String reason)77     public void showShutdownUi(boolean isReboot, String reason) {
78         GradientDrawable background = new GradientDrawable(mContext);
79         background.setAlpha((int) (SHUTDOWN_SCRIM_ALPHA * 255));
80 
81         Dialog d = new Dialog(mContext,
82                 com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
83         // Window initialization
84         Window window = d.getWindow();
85         window.requestFeature(Window.FEATURE_NO_TITLE);
86         window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
87                 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
88                 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
89         // Inflate the decor view, so the attributes below are not overwritten by the theme.
90         window.getDecorView();
91         window.getAttributes().width = ViewGroup.LayoutParams.MATCH_PARENT;
92         window.getAttributes().height = ViewGroup.LayoutParams.MATCH_PARENT;
93         window.getAttributes().layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
94         window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
95         window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
96         window.addFlags(
97                 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
98                         | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
99                         | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
100                         | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
101                         | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
102                         | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
103         window.setBackgroundDrawable(background);
104         window.setWindowAnimations(R.style.Animation_Toast);
105 
106         d.setContentView(R.layout.shutdown_dialog);
107         d.setCancelable(false);
108 
109         int color = Utils.getColorAttr(mContext, com.android.systemui.R.attr.wallpaperTextColor);
110         boolean onKeyguard = mContext.getSystemService(
111                 KeyguardManager.class).isKeyguardLocked();
112 
113         ProgressBar bar = d.findViewById(R.id.progress);
114         bar.getIndeterminateDrawable().setTint(color);
115         TextView message = d.findViewById(R.id.text1);
116         message.setTextColor(color);
117         if (isReboot) message.setText(R.string.reboot_to_reset_message);
118 
119         Point displaySize = new Point();
120         mContext.getDisplay().getRealSize(displaySize);
121         GradientColors colors = Dependency.get(SysuiColorExtractor.class).getColors(
122                 onKeyguard ? WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
123         background.setColors(colors, false);
124         background.setScreenSize(displaySize.x, displaySize.y);
125 
126         d.show();
127     }
128 
129     @Override
disable(int state1, int state2, boolean animate)130     public void disable(int state1, int state2, boolean animate) {
131         final boolean disabled = (state2 & DISABLE2_GLOBAL_ACTIONS) != 0;
132         if (disabled == mDisabled) return;
133         mDisabled = disabled;
134         if (disabled && mGlobalActions != null) {
135             mGlobalActions.dismissDialog();
136         }
137     }
138 }
139