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