blob: 12840ae6dd28476c4c3fe90988f48075b4868ebe [file] [log] [blame]
Hans Boehma3723842015-06-24 17:54:13 -07001/*
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
17package com.android.calculator2;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
Annie Chin06fd3cf2016-11-07 16:04:33 -080023import android.app.FragmentManager;
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070024import android.content.DialogInterface;
Hans Boehma3723842015-06-24 17:54:13 -070025import android.os.Bundle;
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070026import android.support.annotation.Nullable;
Christine Franks7452d3a2016-10-27 13:41:18 -070027import android.support.annotation.StringRes;
Hans Boehma3723842015-06-24 17:54:13 -070028import android.view.LayoutInflater;
29import android.widget.TextView;
30
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070031/**
32 * Display a message with a dismiss putton, and optionally a second button.
33 */
34public 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 */
Justin Klaassenddf960d2016-08-03 12:31:04 -070045 void onClick(AlertDialogFragment fragment, int which);
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070046 }
Hans Boehma3723842015-06-24 17:54:13 -070047
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";
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070051 private static final String KEY_BUTTON_POSITIVE = NAME + "_button_positive";
Christine Franks7452d3a2016-10-27 13:41:18 -070052 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 */
64 public static void showMessageDialog(Activity activity, @StringRes int title,
65 @StringRes int message, @StringRes int positiveButtonLabel) {
66 showMessageDialog(activity, title != 0 ? activity.getString(title) : null,
67 activity.getString(message),
68 positiveButtonLabel != 0 ? activity.getString(positiveButtonLabel) : null);
69 }
Hans Boehma3723842015-06-24 17:54:13 -070070
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070071 /**
72 * Create and show a DialogFragment with the given message.
Christine Franks7452d3a2016-10-27 13:41:18 -070073 *
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070074 * @param activity originating Activity
Christine Franks7452d3a2016-10-27 13:41:18 -070075 * @param title displayed title, if any
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070076 * @param message displayed message
77 * @param positiveButtonLabel label for second button, if any. If non-null, activity must
78 * implement AlertDialogFragment.OnClickListener to respond.
79 */
Christine Franks7452d3a2016-10-27 13:41:18 -070080 public static void showMessageDialog(Activity activity, @Nullable CharSequence title,
81 CharSequence message, @Nullable CharSequence positiveButtonLabel) {
Annie Chin06fd3cf2016-11-07 16:04:33 -080082 final FragmentManager manager = activity.getFragmentManager();
83 if (manager == null || manager.isDestroyed()) {
84 return;
85 }
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070086 final AlertDialogFragment dialogFragment = new AlertDialogFragment();
Hans Boehma3723842015-06-24 17:54:13 -070087 final Bundle args = new Bundle();
88 args.putCharSequence(KEY_MESSAGE, message);
89 args.putCharSequence(KEY_BUTTON_NEGATIVE, activity.getString(R.string.dismiss));
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070090 if (positiveButtonLabel != null) {
91 args.putCharSequence(KEY_BUTTON_POSITIVE, positiveButtonLabel);
92 }
Christine Franks7452d3a2016-10-27 13:41:18 -070093 args.putCharSequence(KEY_TITLE, title);
Hans Boehma3723842015-06-24 17:54:13 -070094 dialogFragment.setArguments(args);
Annie Chin06fd3cf2016-11-07 16:04:33 -080095 dialogFragment.show(manager, null /* tag */);
Hans Boehma3723842015-06-24 17:54:13 -070096 }
97
98 public AlertDialogFragment() {
99 setStyle(STYLE_NO_TITLE, android.R.attr.alertDialogTheme);
100 }
101
102 @Override
103 public Dialog onCreateDialog(Bundle savedInstanceState) {
104 final Bundle args = getArguments() == null ? Bundle.EMPTY : getArguments();
Justin Klaassenddf960d2016-08-03 12:31:04 -0700105 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
106
107 final LayoutInflater inflater = LayoutInflater.from(builder.getContext());
Christine Franks7452d3a2016-10-27 13:41:18 -0700108 final TextView messageView = (TextView) inflater.inflate(
Justin Klaassenddf960d2016-08-03 12:31:04 -0700109 R.layout.dialog_message, null /* root */);
Christine Franks7452d3a2016-10-27 13:41:18 -0700110 messageView.setText(args.getCharSequence(KEY_MESSAGE));
111 builder.setView(messageView);
Justin Klaassenddf960d2016-08-03 12:31:04 -0700112
Justin Klaassenddf960d2016-08-03 12:31:04 -0700113 builder.setNegativeButton(args.getCharSequence(KEY_BUTTON_NEGATIVE), null /* listener */);
114
Hans Boehm5e6a0ca2015-09-22 17:09:01 -0700115 final CharSequence positiveButtonLabel = args.getCharSequence(KEY_BUTTON_POSITIVE);
116 if (positiveButtonLabel != null) {
117 builder.setPositiveButton(positiveButtonLabel, this);
118 }
Justin Klaassenddf960d2016-08-03 12:31:04 -0700119
Christine Franks7452d3a2016-10-27 13:41:18 -0700120 builder.setTitle(args.getCharSequence(KEY_TITLE));
121
Hans Boehm5e6a0ca2015-09-22 17:09:01 -0700122 return builder.create();
123 }
124
125 @Override
126 public void onClick(DialogInterface dialog, int which) {
127 final Activity activity = getActivity();
128 if (activity instanceof AlertDialogFragment.OnClickListener /* always true */) {
129 ((AlertDialogFragment.OnClickListener) activity).onClick(this, which);
130 }
Hans Boehma3723842015-06-24 17:54:13 -0700131 }
132}