1 /* 2 * Copyright (C) 2016 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 androidx.wear.ble.view; 17 18 import android.annotation.TargetApi; 19 import android.app.Dialog; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.graphics.drawable.Drawable; 23 import android.os.Build; 24 import android.view.View; 25 import android.widget.ImageButton; 26 import android.widget.ImageView; 27 import android.widget.Space; 28 import android.widget.TextView; 29 30 import androidx.annotation.StyleRes; 31 32 import com.android.permissioncontroller.R; 33 34 /** 35 * A dialog to display a title, a message, and/or an icon with a positive and a negative button. 36 * 37 * <p>The buttons are hidden away unless there is a listener attached to the button. Since there's 38 * no click listener attached by default, the buttons are hidden be default. 39 */ 40 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 41 public class AcceptDenyDialog extends Dialog { 42 /** Icon at the top of the dialog. */ 43 protected ImageView mIcon; 44 /** Title at the top of the dialog. */ 45 protected TextView mTitle; 46 /** Message content of the dialog. */ 47 protected TextView mMessage; 48 /** Panel containing the buttons. */ 49 protected View mButtonPanel; 50 /** Positive button in the button panel. */ 51 protected ImageButton mPositiveButton; 52 /** Negative button in the button panel. */ 53 protected ImageButton mNegativeButton; 54 /** 55 * Click listener for the positive button. Positive button should hide if this is <code>null 56 * </code>. 57 */ 58 protected DialogInterface.OnClickListener mPositiveButtonListener; 59 /** 60 * Click listener for the negative button. Negative button should hide if this is <code>null 61 * </code>. 62 */ 63 protected DialogInterface.OnClickListener mNegativeButtonListener; 64 /** Spacer between the positive and negative button. Hidden if one button is hidden. */ 65 protected View mSpacer; 66 67 private final View.OnClickListener mButtonHandler = (v) -> { 68 if (v == mPositiveButton && mPositiveButtonListener != null) { 69 mPositiveButtonListener.onClick(this, DialogInterface.BUTTON_POSITIVE); 70 dismiss(); 71 } else if (v == mNegativeButton && mNegativeButtonListener != null) { 72 mNegativeButtonListener.onClick(this, DialogInterface.BUTTON_NEGATIVE); 73 dismiss(); 74 } 75 }; 76 AcceptDenyDialog(Context context)77 public AcceptDenyDialog(Context context) { 78 this(context, 0 /* use default context theme */); 79 } 80 AcceptDenyDialog(Context context, @StyleRes int themeResId)81 public AcceptDenyDialog(Context context, @StyleRes int themeResId) { 82 super(context, themeResId); 83 84 setContentView(R.layout.accept_deny_dialog); 85 86 mTitle = (TextView) findViewById(android.R.id.title); 87 mMessage = (TextView) findViewById(android.R.id.message); 88 mIcon = (ImageView) findViewById(android.R.id.icon); 89 mPositiveButton = (ImageButton) findViewById(android.R.id.button1); 90 mPositiveButton.setOnClickListener(mButtonHandler); 91 mNegativeButton = (ImageButton) findViewById(android.R.id.button2); 92 mNegativeButton.setOnClickListener(mButtonHandler); 93 mSpacer = (Space) findViewById(R.id.spacer); 94 mButtonPanel = findViewById(R.id.buttonPanel); 95 } 96 getButton(int whichButton)97 public ImageButton getButton(int whichButton) { 98 switch (whichButton) { 99 case DialogInterface.BUTTON_POSITIVE: 100 return mPositiveButton; 101 case DialogInterface.BUTTON_NEGATIVE: 102 return mNegativeButton; 103 default: 104 return null; 105 } 106 } 107 setIcon(Drawable icon)108 public void setIcon(Drawable icon) { 109 mIcon.setVisibility(icon == null ? View.GONE : View.VISIBLE); 110 mIcon.setImageDrawable(icon); 111 } 112 113 /** 114 * @param resId the resourceId of the drawable to use as the icon or 0 if you don't want an icon. 115 */ setIcon(int resId)116 public void setIcon(int resId) { 117 mIcon.setVisibility(resId == 0 ? View.GONE : View.VISIBLE); 118 mIcon.setImageResource(resId); 119 } 120 121 /** @param message the content message text of the dialog. */ setMessage(CharSequence message)122 public void setMessage(CharSequence message) { 123 mMessage.setText(message); 124 mMessage.setVisibility(message == null ? View.GONE : View.VISIBLE); 125 } 126 127 /** @param title the title text of the dialog. */ 128 @Override setTitle(CharSequence title)129 public void setTitle(CharSequence title) { 130 mTitle.setText(title); 131 } 132 133 /** 134 * Sets a click listener for a button. 135 * 136 * <p>Will hide button bar if all buttons are hidden (i.e. their click listeners are <code>null 137 * </code>). 138 * 139 * @param whichButton {@link DialogInterface.BUTTON_POSITIVE} or {@link 140 * DialogInterface.BUTTON_NEGATIVE} 141 * @param listener the listener to set for the button. Hide button if <code>null</code>. 142 */ setButton(int whichButton, DialogInterface.OnClickListener listener)143 public void setButton(int whichButton, DialogInterface.OnClickListener listener) { 144 switch (whichButton) { 145 case DialogInterface.BUTTON_POSITIVE: 146 mPositiveButtonListener = listener; 147 break; 148 case DialogInterface.BUTTON_NEGATIVE: 149 mNegativeButtonListener = listener; 150 break; 151 default: 152 return; 153 } 154 155 mSpacer.setVisibility(mPositiveButtonListener == null || mNegativeButtonListener == null 156 ? View.GONE : View.INVISIBLE); 157 mPositiveButton.setVisibility( 158 mPositiveButtonListener == null ? View.GONE : View.VISIBLE); 159 mNegativeButton.setVisibility( 160 mNegativeButtonListener == null ? View.GONE : View.VISIBLE); 161 mButtonPanel.setVisibility( 162 mPositiveButtonListener == null && mNegativeButtonListener == null 163 ? View.GONE : View.VISIBLE); 164 } 165 166 /** 167 * Convenience method for <code>setButton(DialogInterface.BUTTON_POSITIVE, listener)</code>. 168 * 169 * @param listener the listener for the positive button. 170 */ setPositiveButton(DialogInterface.OnClickListener listener)171 public void setPositiveButton(DialogInterface.OnClickListener listener) { 172 setButton(DialogInterface.BUTTON_POSITIVE, listener); 173 } 174 175 /** 176 * Convenience method for <code>setButton(DialogInterface.BUTTON_NEGATIVE, listener)</code>. 177 * 178 * @param listener the listener for the positive button. 179 */ setNegativeButton(DialogInterface.OnClickListener listener)180 public void setNegativeButton(DialogInterface.OnClickListener listener) { 181 setButton(DialogInterface.BUTTON_NEGATIVE, listener); 182 } 183 } 184