blob: 3939a73e290d20556ac2347363bcd389086fb25d [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;
Aurimas Liutikas8c43f062018-03-28 08:10:28 -070026import androidx.annotation.Nullable;
27import androidx.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,
Annie Chin532b77e2016-12-06 13:30:35 -080065 @StringRes int message, @StringRes int positiveButtonLabel, @Nullable String tag) {
Christine Franks7452d3a2016-10-27 13:41:18 -070066 showMessageDialog(activity, title != 0 ? activity.getString(title) : null,
67 activity.getString(message),
Annie Chin532b77e2016-12-06 13:30:35 -080068 positiveButtonLabel != 0 ? activity.getString(positiveButtonLabel) : null,
69 tag);
Christine Franks7452d3a2016-10-27 13:41:18 -070070 }
Hans Boehma3723842015-06-24 17:54:13 -070071
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070072 /**
73 * Create and show a DialogFragment with the given message.
Christine Franks7452d3a2016-10-27 13:41:18 -070074 *
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070075 * @param activity originating Activity
Christine Franks7452d3a2016-10-27 13:41:18 -070076 * @param title displayed title, if any
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070077 * @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 */
Christine Franks7452d3a2016-10-27 13:41:18 -070081 public static void showMessageDialog(Activity activity, @Nullable CharSequence title,
Annie Chin532b77e2016-12-06 13:30:35 -080082 CharSequence message, @Nullable CharSequence positiveButtonLabel, @Nullable String tag)
83 {
Annie Chin06fd3cf2016-11-07 16:04:33 -080084 final FragmentManager manager = activity.getFragmentManager();
85 if (manager == null || manager.isDestroyed()) {
86 return;
87 }
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070088 final AlertDialogFragment dialogFragment = new AlertDialogFragment();
Hans Boehma3723842015-06-24 17:54:13 -070089 final Bundle args = new Bundle();
90 args.putCharSequence(KEY_MESSAGE, message);
91 args.putCharSequence(KEY_BUTTON_NEGATIVE, activity.getString(R.string.dismiss));
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070092 if (positiveButtonLabel != null) {
93 args.putCharSequence(KEY_BUTTON_POSITIVE, positiveButtonLabel);
94 }
Christine Franks7452d3a2016-10-27 13:41:18 -070095 args.putCharSequence(KEY_TITLE, title);
Hans Boehma3723842015-06-24 17:54:13 -070096 dialogFragment.setArguments(args);
Annie Chin532b77e2016-12-06 13:30:35 -080097 dialogFragment.show(manager, tag /* tag */);
Hans Boehma3723842015-06-24 17:54:13 -070098 }
99
100 public AlertDialogFragment() {
101 setStyle(STYLE_NO_TITLE, android.R.attr.alertDialogTheme);
102 }
103
104 @Override
105 public Dialog onCreateDialog(Bundle savedInstanceState) {
106 final Bundle args = getArguments() == null ? Bundle.EMPTY : getArguments();
Justin Klaassenddf960d2016-08-03 12:31:04 -0700107 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
108
109 final LayoutInflater inflater = LayoutInflater.from(builder.getContext());
Christine Franks7452d3a2016-10-27 13:41:18 -0700110 final TextView messageView = (TextView) inflater.inflate(
Justin Klaassenddf960d2016-08-03 12:31:04 -0700111 R.layout.dialog_message, null /* root */);
Christine Franks7452d3a2016-10-27 13:41:18 -0700112 messageView.setText(args.getCharSequence(KEY_MESSAGE));
113 builder.setView(messageView);
Justin Klaassenddf960d2016-08-03 12:31:04 -0700114
Justin Klaassenddf960d2016-08-03 12:31:04 -0700115 builder.setNegativeButton(args.getCharSequence(KEY_BUTTON_NEGATIVE), null /* listener */);
116
Hans Boehm5e6a0ca2015-09-22 17:09:01 -0700117 final CharSequence positiveButtonLabel = args.getCharSequence(KEY_BUTTON_POSITIVE);
118 if (positiveButtonLabel != null) {
119 builder.setPositiveButton(positiveButtonLabel, this);
120 }
Justin Klaassenddf960d2016-08-03 12:31:04 -0700121
Christine Franks7452d3a2016-10-27 13:41:18 -0700122 builder.setTitle(args.getCharSequence(KEY_TITLE));
123
Hans Boehm5e6a0ca2015-09-22 17:09:01 -0700124 return builder.create();
125 }
126
127 @Override
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 }
Hans Boehma3723842015-06-24 17:54:13 -0700133 }
134}