The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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.calendar; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.content.DialogInterface; |
Sam Blitzstein | 3b91cc0 | 2013-02-11 17:01:11 -0800 | [diff] [blame] | 22 | import android.content.DialogInterface.OnDismissListener; |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 23 | import android.widget.Button; |
| 24 | |
| 25 | /** |
| 26 | * A helper class for editing the response to an invitation when the invitation |
| 27 | * is a repeating event. |
| 28 | */ |
Sam Blitzstein | 3b91cc0 | 2013-02-11 17:01:11 -0800 | [diff] [blame] | 29 | public class EditResponseHelper implements DialogInterface.OnClickListener, OnDismissListener { |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 30 | private final Activity mParent; |
| 31 | private int mWhichEvents = -1; |
| 32 | private AlertDialog mAlertDialog; |
Sam Blitzstein | 3b91cc0 | 2013-02-11 17:01:11 -0800 | [diff] [blame] | 33 | private boolean mClickedOk = false; |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * This callback is passed in to this object when this object is created |
| 37 | * and is invoked when the "Ok" button is selected. |
| 38 | */ |
| 39 | private DialogInterface.OnClickListener mDialogListener; |
Erik | f6b6fb3 | 2011-01-18 11:54:50 -0800 | [diff] [blame] | 40 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 41 | public EditResponseHelper(Activity parent) { |
| 42 | mParent = parent; |
| 43 | } |
Erik | f6b6fb3 | 2011-01-18 11:54:50 -0800 | [diff] [blame] | 44 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 45 | public void setOnClickListener(DialogInterface.OnClickListener listener) { |
| 46 | mDialogListener = listener; |
| 47 | } |
Erik | f6b6fb3 | 2011-01-18 11:54:50 -0800 | [diff] [blame] | 48 | |
Sam Blitzstein | 3b91cc0 | 2013-02-11 17:01:11 -0800 | [diff] [blame] | 49 | /** |
| 50 | * @return whichEvents, representing which events were selected on which to |
| 51 | * apply the response: |
| 52 | * -1 means no choice selected, or the dialog was |
| 53 | * canceled. |
| 54 | * 0 means just the single event. |
| 55 | * 1 means all events. |
| 56 | */ |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 57 | public int getWhichEvents() { |
| 58 | return mWhichEvents; |
| 59 | } |
Erik | f6b6fb3 | 2011-01-18 11:54:50 -0800 | [diff] [blame] | 60 | |
RoboErik | 86550ce | 2011-08-22 16:47:51 -0700 | [diff] [blame] | 61 | public void setWhichEvents(int which) { |
| 62 | mWhichEvents = which; |
| 63 | } |
| 64 | |
Sam Blitzstein | 3b91cc0 | 2013-02-11 17:01:11 -0800 | [diff] [blame] | 65 | @Override |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 66 | public void onClick(DialogInterface dialog, int which) { |
Sam Blitzstein | 3b91cc0 | 2013-02-11 17:01:11 -0800 | [diff] [blame] | 67 | setClickedOk(true); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void onDismiss(DialogInterface dialog) { |
| 72 | // If the click was not "OK", clear out whichEvents to represent |
| 73 | // that the dialog was canceled. |
| 74 | if (!getClickedOk()) { |
| 75 | setWhichEvents(-1); |
| 76 | } |
| 77 | setClickedOk(false); |
| 78 | |
| 79 | // Call the pre-set dismiss listener too. |
| 80 | if (mDismissListener != null) { |
| 81 | mDismissListener.onDismiss(dialog); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | private boolean getClickedOk() { |
| 87 | return mClickedOk; |
| 88 | } |
| 89 | |
| 90 | private void setClickedOk(boolean clickedOk) { |
| 91 | mClickedOk = clickedOk; |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * This callback is used when a list item is selected |
| 96 | */ |
| 97 | private DialogInterface.OnClickListener mListListener = |
| 98 | new DialogInterface.OnClickListener() { |
| 99 | public void onClick(DialogInterface dialog, int which) { |
| 100 | mWhichEvents = which; |
Erik | f6b6fb3 | 2011-01-18 11:54:50 -0800 | [diff] [blame] | 101 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 102 | // Enable the "ok" button now that the user has selected which |
| 103 | // events in the series to delete. |
| 104 | Button ok = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE); |
| 105 | ok.setEnabled(true); |
| 106 | } |
| 107 | }; |
Erik | f6b6fb3 | 2011-01-18 11:54:50 -0800 | [diff] [blame] | 108 | |
Sam Blitzstein | 3b91cc0 | 2013-02-11 17:01:11 -0800 | [diff] [blame] | 109 | private DialogInterface.OnDismissListener mDismissListener; |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Set the dismiss listener to be called when the dialog is ended. There, |
| 114 | * use getWhichEvents() to see how the dialog was dismissed; if it returns |
| 115 | * -1, the dialog was canceled out. If it is not -1, it's the index of |
| 116 | * which events the user wants to respond to. |
| 117 | * @param onDismissListener |
| 118 | */ |
| 119 | public void setDismissListener(OnDismissListener onDismissListener) { |
| 120 | mDismissListener = onDismissListener; |
| 121 | } |
| 122 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 123 | public void showDialog(int whichEvents) { |
| 124 | // We need to have a non-null listener, otherwise we get null when |
| 125 | // we try to fetch the "Ok" button. |
| 126 | if (mDialogListener == null) { |
| 127 | mDialogListener = this; |
| 128 | } |
Erik | f6b6fb3 | 2011-01-18 11:54:50 -0800 | [diff] [blame] | 129 | AlertDialog dialog = new AlertDialog.Builder(mParent).setTitle( |
| 130 | R.string.change_response_title).setIconAttribute(android.R.attr.alertDialogIcon) |
| 131 | .setSingleChoiceItems(R.array.change_response_labels, whichEvents, mListListener) |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 132 | .setPositiveButton(android.R.string.ok, mDialogListener) |
Erik | f6b6fb3 | 2011-01-18 11:54:50 -0800 | [diff] [blame] | 133 | .setNegativeButton(android.R.string.cancel, null).show(); |
Sam Blitzstein | 3b91cc0 | 2013-02-11 17:01:11 -0800 | [diff] [blame] | 134 | // The caller may set a dismiss listener to hear back when the dialog is |
| 135 | // finished. Use getWhichEvents() to see how the dialog was dismissed. |
| 136 | dialog.setOnDismissListener(this); |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 137 | mAlertDialog = dialog; |
Erik | f6b6fb3 | 2011-01-18 11:54:50 -0800 | [diff] [blame] | 138 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 139 | if (whichEvents == -1) { |
| 140 | // Disable the "Ok" button until the user selects which events to |
| 141 | // delete. |
| 142 | Button ok = dialog.getButton(DialogInterface.BUTTON_POSITIVE); |
| 143 | ok.setEnabled(false); |
| 144 | } |
| 145 | } |
Sam Blitzstein | 3b91cc0 | 2013-02-11 17:01:11 -0800 | [diff] [blame] | 146 | |
| 147 | public void dismissAlertDialog() { |
| 148 | if (mAlertDialog != null) { |
| 149 | mAlertDialog.dismiss(); |
| 150 | } |
| 151 | } |
| 152 | |
The Android Open Source Project | 146de36 | 2009-03-03 19:32:18 -0800 | [diff] [blame] | 153 | } |