Hans Boehm | a372384 | 2015-06-24 17:54:13 -0700 | [diff] [blame] | 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; |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame^] | 23 | import android.app.FragmentManager; |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 24 | import android.content.DialogInterface; |
Hans Boehm | a372384 | 2015-06-24 17:54:13 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 26 | import android.support.annotation.Nullable; |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 27 | import android.support.annotation.StringRes; |
Hans Boehm | a372384 | 2015-06-24 17:54:13 -0700 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
| 29 | import android.widget.TextView; |
| 30 | |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 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 | */ |
Justin Klaassen | ddf960d | 2016-08-03 12:31:04 -0700 | [diff] [blame] | 45 | void onClick(AlertDialogFragment fragment, int which); |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 46 | } |
Hans Boehm | a372384 | 2015-06-24 17:54:13 -0700 | [diff] [blame] | 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"; |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 51 | private static final String KEY_BUTTON_POSITIVE = NAME + "_button_positive"; |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 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 | */ |
| 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 Boehm | a372384 | 2015-06-24 17:54:13 -0700 | [diff] [blame] | 70 | |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Create and show a DialogFragment with the given message. |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 73 | * |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 74 | * @param activity originating Activity |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 75 | * @param title displayed title, if any |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 76 | * @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 Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 80 | public static void showMessageDialog(Activity activity, @Nullable CharSequence title, |
| 81 | CharSequence message, @Nullable CharSequence positiveButtonLabel) { |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame^] | 82 | final FragmentManager manager = activity.getFragmentManager(); |
| 83 | if (manager == null || manager.isDestroyed()) { |
| 84 | return; |
| 85 | } |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 86 | final AlertDialogFragment dialogFragment = new AlertDialogFragment(); |
Hans Boehm | a372384 | 2015-06-24 17:54:13 -0700 | [diff] [blame] | 87 | final Bundle args = new Bundle(); |
| 88 | args.putCharSequence(KEY_MESSAGE, message); |
| 89 | args.putCharSequence(KEY_BUTTON_NEGATIVE, activity.getString(R.string.dismiss)); |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 90 | if (positiveButtonLabel != null) { |
| 91 | args.putCharSequence(KEY_BUTTON_POSITIVE, positiveButtonLabel); |
| 92 | } |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 93 | args.putCharSequence(KEY_TITLE, title); |
Hans Boehm | a372384 | 2015-06-24 17:54:13 -0700 | [diff] [blame] | 94 | dialogFragment.setArguments(args); |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame^] | 95 | dialogFragment.show(manager, null /* tag */); |
Hans Boehm | a372384 | 2015-06-24 17:54:13 -0700 | [diff] [blame] | 96 | } |
| 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 Klaassen | ddf960d | 2016-08-03 12:31:04 -0700 | [diff] [blame] | 105 | final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
| 106 | |
| 107 | final LayoutInflater inflater = LayoutInflater.from(builder.getContext()); |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 108 | final TextView messageView = (TextView) inflater.inflate( |
Justin Klaassen | ddf960d | 2016-08-03 12:31:04 -0700 | [diff] [blame] | 109 | R.layout.dialog_message, null /* root */); |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 110 | messageView.setText(args.getCharSequence(KEY_MESSAGE)); |
| 111 | builder.setView(messageView); |
Justin Klaassen | ddf960d | 2016-08-03 12:31:04 -0700 | [diff] [blame] | 112 | |
Justin Klaassen | ddf960d | 2016-08-03 12:31:04 -0700 | [diff] [blame] | 113 | builder.setNegativeButton(args.getCharSequence(KEY_BUTTON_NEGATIVE), null /* listener */); |
| 114 | |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 115 | final CharSequence positiveButtonLabel = args.getCharSequence(KEY_BUTTON_POSITIVE); |
| 116 | if (positiveButtonLabel != null) { |
| 117 | builder.setPositiveButton(positiveButtonLabel, this); |
| 118 | } |
Justin Klaassen | ddf960d | 2016-08-03 12:31:04 -0700 | [diff] [blame] | 119 | |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 120 | builder.setTitle(args.getCharSequence(KEY_TITLE)); |
| 121 | |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 122 | 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 Boehm | a372384 | 2015-06-24 17:54:13 -0700 | [diff] [blame] | 131 | } |
| 132 | } |