• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.FragmentManager;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 import android.support.annotation.Nullable;
27 import android.support.annotation.StringRes;
28 import android.view.LayoutInflater;
29 import android.widget.TextView;
30 
31 /**
32  * Display a message with a dismiss putton, and optionally a second button.
33  */
34 public class AlertDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
35 
36     public interface OnClickListener {
37         /**
38          * This method will be invoked when a button in the dialog is clicked.
39          *
40          * @param fragment the AlertDialogFragment that received the click
41          * @param which the button that was clicked (e.g.
42          *            {@link DialogInterface#BUTTON_POSITIVE}) or the position
43          *            of the item clicked
44          */
onClick(AlertDialogFragment fragment, int which)45         void onClick(AlertDialogFragment fragment, int which);
46     }
47 
48     private static final String NAME = AlertDialogFragment.class.getName();
49     private static final String KEY_MESSAGE = NAME + "_message";
50     private static final String KEY_BUTTON_NEGATIVE = NAME + "_button_negative";
51     private static final String KEY_BUTTON_POSITIVE = NAME + "_button_positive";
52     private static final String KEY_TITLE = NAME + "_title";
53 
54     /**
55      * Convenience method for creating and showing a DialogFragment with the given message and
56      * title.
57      *
58      * @param activity originating Activity
59      * @param title resource id for the title string
60      * @param message resource id for the displayed message string
61      * @param positiveButtonLabel label for second button, if any.  If non-null, activity must
62      * implement AlertDialogFragment.OnClickListener to respond.
63      */
showMessageDialog(Activity activity, @StringRes int title, @StringRes int message, @StringRes int positiveButtonLabel, @Nullable String tag)64     public static void showMessageDialog(Activity activity, @StringRes int title,
65             @StringRes int message, @StringRes int positiveButtonLabel, @Nullable String tag) {
66         showMessageDialog(activity, title != 0 ? activity.getString(title) : null,
67                 activity.getString(message),
68                 positiveButtonLabel != 0 ? activity.getString(positiveButtonLabel) : null,
69                 tag);
70     }
71 
72     /**
73      * Create and show a DialogFragment with the given message.
74      *
75      * @param activity originating Activity
76      * @param title displayed title, if any
77      * @param message displayed message
78      * @param positiveButtonLabel label for second button, if any.  If non-null, activity must
79      * implement AlertDialogFragment.OnClickListener to respond.
80      */
showMessageDialog(Activity activity, @Nullable CharSequence title, CharSequence message, @Nullable CharSequence positiveButtonLabel, @Nullable String tag)81     public static void showMessageDialog(Activity activity, @Nullable CharSequence title,
82             CharSequence message, @Nullable CharSequence positiveButtonLabel, @Nullable String tag)
83     {
84         final FragmentManager manager = activity.getFragmentManager();
85         if (manager == null || manager.isDestroyed()) {
86             return;
87         }
88         final AlertDialogFragment dialogFragment = new AlertDialogFragment();
89         final Bundle args = new Bundle();
90         args.putCharSequence(KEY_MESSAGE, message);
91         args.putCharSequence(KEY_BUTTON_NEGATIVE, activity.getString(R.string.dismiss));
92         if (positiveButtonLabel != null) {
93             args.putCharSequence(KEY_BUTTON_POSITIVE, positiveButtonLabel);
94         }
95         args.putCharSequence(KEY_TITLE, title);
96         dialogFragment.setArguments(args);
97         dialogFragment.show(manager, tag /* tag */);
98     }
99 
AlertDialogFragment()100     public AlertDialogFragment() {
101         setStyle(STYLE_NO_TITLE, android.R.attr.alertDialogTheme);
102     }
103 
104     @Override
onCreateDialog(Bundle savedInstanceState)105     public Dialog onCreateDialog(Bundle savedInstanceState) {
106         final Bundle args = getArguments() == null ? Bundle.EMPTY : getArguments();
107         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
108 
109         final LayoutInflater inflater = LayoutInflater.from(builder.getContext());
110         final TextView messageView = (TextView) inflater.inflate(
111                 R.layout.dialog_message, null /* root */);
112         messageView.setText(args.getCharSequence(KEY_MESSAGE));
113         builder.setView(messageView);
114 
115         builder.setNegativeButton(args.getCharSequence(KEY_BUTTON_NEGATIVE), null /* listener */);
116 
117         final CharSequence positiveButtonLabel = args.getCharSequence(KEY_BUTTON_POSITIVE);
118         if (positiveButtonLabel != null) {
119             builder.setPositiveButton(positiveButtonLabel, this);
120         }
121 
122         builder.setTitle(args.getCharSequence(KEY_TITLE));
123 
124         return builder.create();
125     }
126 
127     @Override
onClick(DialogInterface dialog, int which)128     public void onClick(DialogInterface dialog, int which) {
129         final Activity activity = getActivity();
130         if (activity instanceof AlertDialogFragment.OnClickListener /* always true */) {
131             ((AlertDialogFragment.OnClickListener) activity).onClick(this, which);
132         }
133     }
134 }
135