1 /*
2  * Copyright (C) 2020 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 package com.android.systemui.globalactions;
17 
18 import android.annotation.NonNull;
19 import android.app.Dialog;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.view.Window;
26 import android.view.WindowManager;
27 import android.widget.ListAdapter;
28 
29 /**
30  * Creates a customized Dialog for displaying the Shut Down and Restart actions.
31  */
32 public class GlobalActionsPowerDialog {
33 
34     /**
35      * Create a dialog for displaying Shut Down and Restart actions.
36      */
create(@onNull Context context, ListAdapter adapter)37     public static Dialog create(@NonNull Context context, ListAdapter adapter) {
38         ViewGroup listView = (ViewGroup) LayoutInflater.from(context).inflate(
39                 com.android.systemui.R.layout.global_actions_power_dialog, null);
40 
41         for (int i = 0; i < adapter.getCount(); i++) {
42             View action = adapter.getView(i, null, listView);
43             listView.addView(action);
44         }
45 
46         Resources res = context.getResources();
47 
48         Dialog dialog = new Dialog(context);
49         dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
50         dialog.setContentView(listView);
51 
52         Window window = dialog.getWindow();
53         window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
54         window.setTitle(""); // prevent Talkback from speaking first item name twice
55         window.setBackgroundDrawable(res.getDrawable(
56                 com.android.systemui.R.drawable.control_background, context.getTheme()));
57         window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
58 
59         return dialog;
60     }
61 }
62