Home | History | Annotate | Download | only in calendar
      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;
     22 import android.content.DialogInterface.OnDismissListener;
     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  */
     29 public class EditResponseHelper implements DialogInterface.OnClickListener, OnDismissListener {
     30     private final Activity mParent;
     31     private int mWhichEvents = -1;
     32     private AlertDialog mAlertDialog;
     33     private boolean mClickedOk = false;
     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;
     40 
     41     public EditResponseHelper(Activity parent) {
     42         mParent = parent;
     43     }
     44 
     45     public void setOnClickListener(DialogInterface.OnClickListener listener) {
     46         mDialogListener = listener;
     47     }
     48 
     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      */
     57     public int getWhichEvents() {
     58         return mWhichEvents;
     59     }
     60 
     61     public void setWhichEvents(int which) {
     62         mWhichEvents = which;
     63     }
     64 
     65     @Override
     66     public void onClick(DialogInterface dialog, int which) {
     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;
     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;
    101 
    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     };
    108 
    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 
    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         }
    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)
    132                 .setPositiveButton(android.R.string.ok, mDialogListener)
    133                 .setNegativeButton(android.R.string.cancel, null).show();
    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);
    137         mAlertDialog = dialog;
    138 
    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     }
    146 
    147     public void dismissAlertDialog() {
    148         if (mAlertDialog != null) {
    149             mAlertDialog.dismiss();
    150         }
    151     }
    152 
    153 }
    154