1 /*
2  * Copyright (C) 2015 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.calculator2;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 import android.support.annotation.Nullable;
27 import android.view.LayoutInflater;
28 import android.widget.TextView;
29 
30 /**
31  * Display a message with a dismiss putton, and optionally a second button.
32  */
33 public class AlertDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
34 
35     public interface OnClickListener {
36         /**
37          * This method will be invoked when a button in the dialog is clicked.
38          *
39          * @param fragment the AlertDialogFragment that received the click
40          * @param which the button that was clicked (e.g.
41          *            {@link DialogInterface#BUTTON_POSITIVE}) or the position
42          *            of the item clicked
43          */
onClick(AlertDialogFragment fragment, int which)44         public void onClick(AlertDialogFragment fragment, int which);
45     }
46 
47     private static final String NAME = AlertDialogFragment.class.getName();
48     private static final String KEY_MESSAGE = NAME + "_message";
49     private static final String KEY_BUTTON_NEGATIVE = NAME + "_button_negative";
50     private static final String KEY_BUTTON_POSITIVE = NAME + "_button_positive";
51 
52     /**
53      * Create and show a DialogFragment with the given message.
54      * @param activity originating Activity
55      * @param message displayed message
56      * @param positiveButtonLabel label for second button, if any.  If non-null, activity must
57      * implement AlertDialogFragment.OnClickListener to respond.
58      */
showMessageDialog(Activity activity, CharSequence message, @Nullable CharSequence positiveButtonLabel)59     public static void showMessageDialog(Activity activity, CharSequence message,
60             @Nullable CharSequence positiveButtonLabel) {
61         final AlertDialogFragment dialogFragment = new AlertDialogFragment();
62         final Bundle args = new Bundle();
63         args.putCharSequence(KEY_MESSAGE, message);
64         args.putCharSequence(KEY_BUTTON_NEGATIVE, activity.getString(R.string.dismiss));
65         if (positiveButtonLabel != null) {
66             args.putCharSequence(KEY_BUTTON_POSITIVE, positiveButtonLabel);
67         }
68         dialogFragment.setArguments(args);
69         dialogFragment.show(activity.getFragmentManager(), null /* tag */);
70     }
71 
AlertDialogFragment()72     public AlertDialogFragment() {
73         setStyle(STYLE_NO_TITLE, android.R.attr.alertDialogTheme);
74     }
75 
76     @Override
onCreateDialog(Bundle savedInstanceState)77     public Dialog onCreateDialog(Bundle savedInstanceState) {
78         final Bundle args = getArguments() == null ? Bundle.EMPTY : getArguments();
79         final Context context = getContext();
80         final LayoutInflater inflater = LayoutInflater.from(context);
81         final TextView textView = (TextView) inflater.inflate(R.layout.dialog_message,
82                 null /* root */);
83         textView.setText(args.getCharSequence(KEY_MESSAGE));
84         final AlertDialog.Builder builder = new AlertDialog.Builder(context)
85                 .setView(textView)
86                 .setNegativeButton(args.getCharSequence(KEY_BUTTON_NEGATIVE), null /* listener */);
87         final CharSequence positiveButtonLabel = args.getCharSequence(KEY_BUTTON_POSITIVE);
88         if (positiveButtonLabel != null) {
89             builder.setPositiveButton(positiveButtonLabel, this);
90         }
91         return builder.create();
92     }
93 
94     @Override
onClick(DialogInterface dialog, int which)95     public void onClick(DialogInterface dialog, int which) {
96         final Activity activity = getActivity();
97         if (activity instanceof AlertDialogFragment.OnClickListener /* always true */) {
98             ((AlertDialogFragment.OnClickListener) activity).onClick(this, which);
99         }
100     }
101 }
102