1 /* 2 * Copyright (C) 2014 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.statusbar.phone; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.os.UserHandle; 27 import android.view.WindowManager; 28 import android.view.WindowManager.LayoutParams; 29 30 import com.android.systemui.Dependency; 31 import com.android.systemui.R; 32 import com.android.systemui.statusbar.policy.KeyguardMonitor; 33 34 35 /** 36 * Base class for dialogs that should appear over panels and keyguard. 37 */ 38 public class SystemUIDialog extends AlertDialog { 39 40 private final Context mContext; 41 SystemUIDialog(Context context)42 public SystemUIDialog(Context context) { 43 this(context, R.style.Theme_SystemUI_Dialog); 44 } 45 SystemUIDialog(Context context, int theme)46 public SystemUIDialog(Context context, int theme) { 47 super(context, theme); 48 mContext = context; 49 50 applyFlags(this); 51 WindowManager.LayoutParams attrs = getWindow().getAttributes(); 52 attrs.setTitle(getClass().getSimpleName()); 53 getWindow().setAttributes(attrs); 54 55 registerDismissListener(this); 56 } 57 setShowForAllUsers(boolean show)58 public void setShowForAllUsers(boolean show) { 59 setShowForAllUsers(this, show); 60 } 61 setMessage(int resId)62 public void setMessage(int resId) { 63 setMessage(mContext.getString(resId)); 64 } 65 setPositiveButton(int resId, OnClickListener onClick)66 public void setPositiveButton(int resId, OnClickListener onClick) { 67 setButton(BUTTON_POSITIVE, mContext.getString(resId), onClick); 68 } 69 setNegativeButton(int resId, OnClickListener onClick)70 public void setNegativeButton(int resId, OnClickListener onClick) { 71 setButton(BUTTON_NEGATIVE, mContext.getString(resId), onClick); 72 } 73 setNeutralButton(int resId, OnClickListener onClick)74 public void setNeutralButton(int resId, OnClickListener onClick) { 75 setButton(BUTTON_NEUTRAL, mContext.getString(resId), onClick); 76 } 77 setShowForAllUsers(Dialog dialog, boolean show)78 public static void setShowForAllUsers(Dialog dialog, boolean show) { 79 if (show) { 80 dialog.getWindow().getAttributes().privateFlags |= 81 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; 82 } else { 83 dialog.getWindow().getAttributes().privateFlags &= 84 ~WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; 85 } 86 } 87 setWindowOnTop(Dialog dialog)88 public static void setWindowOnTop(Dialog dialog) { 89 if (Dependency.get(KeyguardMonitor.class).isShowing()) { 90 dialog.getWindow().setType(LayoutParams.TYPE_STATUS_BAR_PANEL); 91 } else { 92 dialog.getWindow().setType(LayoutParams.TYPE_STATUS_BAR_SUB_PANEL); 93 } 94 } 95 applyFlags(AlertDialog dialog)96 public static AlertDialog applyFlags(AlertDialog dialog) { 97 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL); 98 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM 99 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 100 return dialog; 101 } 102 registerDismissListener(Dialog dialog)103 public static void registerDismissListener(Dialog dialog) { 104 DismissReceiver dismissReceiver = new DismissReceiver(dialog); 105 dismissReceiver.register(); 106 } 107 108 private static class DismissReceiver extends BroadcastReceiver implements OnDismissListener { 109 private static final IntentFilter INTENT_FILTER = new IntentFilter(); 110 static { 111 INTENT_FILTER.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 112 INTENT_FILTER.addAction(Intent.ACTION_SCREEN_OFF); 113 } 114 115 private final Dialog mDialog; 116 private boolean mRegistered; 117 DismissReceiver(Dialog dialog)118 DismissReceiver(Dialog dialog) { 119 mDialog = dialog; 120 } 121 register()122 void register() { 123 mDialog.getContext() 124 .registerReceiverAsUser(this, UserHandle.CURRENT, INTENT_FILTER, null, null); 125 mRegistered = true; 126 } 127 128 @Override onReceive(Context context, Intent intent)129 public void onReceive(Context context, Intent intent) { 130 mDialog.dismiss(); 131 } 132 133 @Override onDismiss(DialogInterface dialog)134 public void onDismiss(DialogInterface dialog) { 135 if (mRegistered) { 136 mDialog.getContext().unregisterReceiver(this); 137 mRegistered = false; 138 } 139 } 140 }} 141