blob: 1f32ce01dbb1987f0d3630120dd07f270043c541 [file] [log] [blame]
The Android Open Source Project146de362009-03-03 19:32:18 -08001/*
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
17package com.android.calendar;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.content.DialogInterface;
Sam Blitzstein3b91cc02013-02-11 17:01:11 -080022import android.content.DialogInterface.OnDismissListener;
The Android Open Source Project146de362009-03-03 19:32:18 -080023import 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 Blitzstein3b91cc02013-02-11 17:01:11 -080029public class EditResponseHelper implements DialogInterface.OnClickListener, OnDismissListener {
The Android Open Source Project146de362009-03-03 19:32:18 -080030 private final Activity mParent;
31 private int mWhichEvents = -1;
32 private AlertDialog mAlertDialog;
Sam Blitzstein3b91cc02013-02-11 17:01:11 -080033 private boolean mClickedOk = false;
The Android Open Source Project146de362009-03-03 19:32:18 -080034
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;
Erikf6b6fb32011-01-18 11:54:50 -080040
The Android Open Source Project146de362009-03-03 19:32:18 -080041 public EditResponseHelper(Activity parent) {
42 mParent = parent;
43 }
Erikf6b6fb32011-01-18 11:54:50 -080044
The Android Open Source Project146de362009-03-03 19:32:18 -080045 public void setOnClickListener(DialogInterface.OnClickListener listener) {
46 mDialogListener = listener;
47 }
Erikf6b6fb32011-01-18 11:54:50 -080048
Sam Blitzstein3b91cc02013-02-11 17:01:11 -080049 /**
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 Project146de362009-03-03 19:32:18 -080057 public int getWhichEvents() {
58 return mWhichEvents;
59 }
Erikf6b6fb32011-01-18 11:54:50 -080060
RoboErik86550ce2011-08-22 16:47:51 -070061 public void setWhichEvents(int which) {
62 mWhichEvents = which;
63 }
64
Sam Blitzstein3b91cc02013-02-11 17:01:11 -080065 @Override
The Android Open Source Project146de362009-03-03 19:32:18 -080066 public void onClick(DialogInterface dialog, int which) {
Sam Blitzstein3b91cc02013-02-11 17:01:11 -080067 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 Project146de362009-03-03 19:32:18 -080092 }
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;
Erikf6b6fb32011-01-18 11:54:50 -0800101
The Android Open Source Project146de362009-03-03 19:32:18 -0800102 // 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 };
Erikf6b6fb32011-01-18 11:54:50 -0800108
Sam Blitzstein3b91cc02013-02-11 17:01:11 -0800109 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 Project146de362009-03-03 19:32:18 -0800123 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 }
Erikf6b6fb32011-01-18 11:54:50 -0800129 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 Project146de362009-03-03 19:32:18 -0800132 .setPositiveButton(android.R.string.ok, mDialogListener)
Erikf6b6fb32011-01-18 11:54:50 -0800133 .setNegativeButton(android.R.string.cancel, null).show();
Sam Blitzstein3b91cc02013-02-11 17:01:11 -0800134 // 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 Project146de362009-03-03 19:32:18 -0800137 mAlertDialog = dialog;
Erikf6b6fb32011-01-18 11:54:50 -0800138
The Android Open Source Project146de362009-03-03 19:32:18 -0800139 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 Blitzstein3b91cc02013-02-11 17:01:11 -0800146
147 public void dismissAlertDialog() {
148 if (mAlertDialog != null) {
149 mAlertDialog.dismiss();
150 }
151 }
152
The Android Open Source Project146de362009-03-03 19:32:18 -0800153}