Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2011 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.email.activity;
     18 
     19 import com.android.email.R;
     20 import com.android.email.activity.setup.
     21         AccountSettingsEditQuickResponsesFragment.QuickResponseFinder;
     22 import com.android.emailcommon.provider.Account;
     23 import com.android.emailcommon.utility.EmailAsyncTask;
     24 
     25 import android.app.AlertDialog;
     26 import android.app.Dialog;
     27 import android.app.DialogFragment;
     28 import android.app.Fragment;
     29 import android.content.Context;
     30 import android.content.DialogInterface;
     31 import android.os.Bundle;
     32 import android.view.View;
     33 import android.widget.AdapterView;
     34 import android.widget.AdapterView.OnItemClickListener;
     35 import android.widget.ListView;
     36 
     37 /**
     38  * Dialog which lists QuickResponses for the specified account. On user selection, will call
     39  * Callback.onQuickResponseSelected() with the selected QuickResponse text.
     40  */
     41 public class InsertQuickResponseDialog extends DialogFragment
     42         implements DialogInterface.OnClickListener, OnItemClickListener {
     43     private ListView mQuickResponsesView;
     44     private EmailAsyncTask.Tracker mTaskTracker;
     45 
     46     // Key for the Account object in the arguments bundle
     47     private static final String ACCOUNT_KEY = "account";
     48 
     49     /**
     50      * Callback interface for when user selects a QuickResponse.
     51      */
     52     public interface Callback {
     53         /**
     54          * Handles the text of the selected QuickResponse.
     55          */
     56         public void onQuickResponseSelected(CharSequence quickResponse);
     57     }
     58 
     59     /**
     60      * Create and returns new dialog.
     61      *
     62      * @param callbackFragment fragment that implements {@link Callback}.  Or null, in which case
     63      * the parent activity must implement {@link Callback}.
     64      */
     65     public static InsertQuickResponseDialog
     66             newInstance(Fragment callbackFragment, Account account) {
     67         final InsertQuickResponseDialog dialog = new InsertQuickResponseDialog();
     68 
     69         // If a target is set, it MUST implement Callback. Fail-fast if not.
     70         final Callback callback;
     71         if (callbackFragment != null) {
     72             try {
     73                 callback = (Callback) callbackFragment;
     74             } catch (ClassCastException e) {
     75                 throw new ClassCastException(callbackFragment.toString()
     76                         + " must implement Callback");
     77             }
     78             dialog.setTargetFragment(callbackFragment, 0);
     79         }
     80 
     81         Bundle args = new Bundle();
     82         args.putParcelable(ACCOUNT_KEY, account);
     83         dialog.setArguments(args);
     84         return dialog;
     85     }
     86 
     87     @Override
     88     public Dialog onCreateDialog(Bundle savedInstanceState) {
     89         // If target not set, the parent activity MUST implement Callback. Fail-fast if not.
     90         final Fragment targetFragment = getTargetFragment();
     91         if (targetFragment != null) {
     92             final Callback callback;
     93             try {
     94                 callback = (Callback) getActivity();
     95             } catch (ClassCastException e) {
     96                 throw new ClassCastException(getActivity().toString() + " must implement Callback");
     97             }
     98         }
     99 
    100         // Now that Callback implementation is verified, build the dialog
    101         final Context context = getActivity();
    102         final AlertDialog.Builder b = new AlertDialog.Builder(context);
    103 
    104         mQuickResponsesView = new ListView(context);
    105 
    106         Account account = (Account) getArguments().getParcelable(ACCOUNT_KEY);
    107         mTaskTracker = new EmailAsyncTask.Tracker();
    108         new QuickResponseFinder(mTaskTracker, account.mId, mQuickResponsesView,
    109                 context, null, this, false).executeParallel();
    110 
    111         b.setTitle(getResources()
    112                 .getString(R.string.message_compose_insert_quick_response_list_title))
    113                 .setView(mQuickResponsesView)
    114                 .setNegativeButton(R.string.cancel_action, this);
    115         return b.create();
    116     }
    117 
    118     @Override
    119     public void onDestroy() {
    120         mTaskTracker.cancellAllInterrupt();
    121         super.onDestroy();
    122     }
    123 
    124     /**
    125      * Implements OnItemClickListener.
    126      */
    127     @Override
    128     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    129         getCallback().onQuickResponseSelected(
    130                 mQuickResponsesView.getItemAtPosition(position).toString());
    131         dismiss();
    132     }
    133 
    134     /**
    135      * Implements DialogInterface.OnClickListener
    136      */
    137     @Override
    138     public void onClick(DialogInterface dialog, int which) {
    139         if (which == DialogInterface.BUTTON_NEGATIVE) {
    140             dialog.cancel();
    141         }
    142     }
    143 
    144     private Callback getCallback() {
    145         Fragment targetFragment = getTargetFragment();
    146         if (targetFragment != null) {
    147             return (Callback) targetFragment;
    148         }
    149         return (Callback) getActivity();
    150     }
    151 }
    152