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.mail.providers.Account; 21 import com.android.mail.providers.UIProvider; 22 23 import android.app.AlertDialog; 24 import android.app.Dialog; 25 import android.app.DialogFragment; 26 import android.app.Fragment; 27 import android.app.LoaderManager; 28 import android.content.Context; 29 import android.content.CursorLoader; 30 import android.content.DialogInterface; 31 import android.content.Loader; 32 import android.database.Cursor; 33 import android.os.Bundle; 34 import android.view.View; 35 import android.widget.AdapterView; 36 import android.widget.AdapterView.OnItemClickListener; 37 import android.widget.ListView; 38 import android.widget.SimpleCursorAdapter; 39 40 /** 41 * Dialog which lists QuickResponses for the specified account. On user selection, will call 42 * Callback.onQuickResponseSelected() with the selected QuickResponse text. 43 */ 44 public class InsertQuickResponseDialog extends DialogFragment { 45 // Key for the Account object in the arguments bundle 46 private static final String ACCOUNT_KEY = "account"; 47 48 /** 49 * Callback interface for when user selects a QuickResponse. 50 */ 51 public interface Callback { 52 /** 53 * Handles the text of the selected QuickResponse. 54 */ 55 public void onQuickResponseSelected(CharSequence quickResponse); 56 } 57 58 // Public no-args constructor needed for fragment re-instantiation 59 public InsertQuickResponseDialog() {} 60 61 /** 62 * Create and returns new dialog. 63 * 64 * @param callbackFragment fragment that implements {@link Callback}. Or null, in which case 65 * the parent activity must implement {@link Callback}. 66 */ 67 public static InsertQuickResponseDialog 68 newInstance(Fragment callbackFragment, Account account) { 69 final InsertQuickResponseDialog dialog = new InsertQuickResponseDialog(); 70 71 // If a target is set, it MUST implement Callback. Fail-fast if not. 72 if (callbackFragment != null) { 73 if (!(callbackFragment instanceof Callback)) { 74 throw new ClassCastException(callbackFragment.toString() 75 + " must implement Callback"); 76 } 77 dialog.setTargetFragment(callbackFragment, 0); 78 } 79 80 Bundle args = new Bundle(); 81 args.putParcelable(ACCOUNT_KEY, account); 82 dialog.setArguments(args); 83 return dialog; 84 } 85 86 @Override 87 public Dialog onCreateDialog(Bundle savedInstanceState) { 88 // If target not set, the parent activity MUST implement Callback. Fail-fast if not. 89 final Fragment targetFragment = getTargetFragment(); 90 if (targetFragment != null) { 91 if (!(getActivity() instanceof Callback)) { 92 throw new ClassCastException(getActivity().toString() + " must implement Callback"); 93 } 94 } 95 96 // Now that Callback implementation is verified, build the dialog 97 final Context context = getActivity(); 98 99 final SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), 100 R.layout.quick_response_item, null, 101 new String[] {UIProvider.QuickResponseColumns.TEXT}, 102 new int[] {R.id.quick_response_text}, 0); 103 104 final ListView listView = new ListView(context); 105 listView.setAdapter(adapter); 106 107 listView.setOnItemClickListener(new OnItemClickListener() { 108 @Override 109 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 110 final Cursor c = (Cursor) listView.getItemAtPosition(position); 111 final String quickResponseText = 112 c.getString(c.getColumnIndex(UIProvider.QuickResponseColumns.TEXT)); 113 getCallback().onQuickResponseSelected(quickResponseText); 114 dismiss(); 115 } 116 }); 117 118 final Account account = getArguments().getParcelable(ACCOUNT_KEY); 119 120 getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() { 121 @Override 122 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 123 return new CursorLoader(getActivity(), account.quickResponseUri, 124 UIProvider.QUICK_RESPONSE_PROJECTION, null, null, null); 125 } 126 127 @Override 128 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 129 adapter.swapCursor(data); 130 } 131 132 @Override 133 public void onLoaderReset(Loader<Cursor> loader) { 134 adapter.swapCursor(null); 135 } 136 }); 137 138 final AlertDialog.Builder b = new AlertDialog.Builder(context); 139 b.setTitle(getResources() 140 .getString(R.string.message_compose_insert_quick_response_list_title)) 141 .setView(listView) 142 .setNegativeButton(R.string.cancel_action, new DialogInterface.OnClickListener() { 143 @Override 144 public void onClick(DialogInterface dialog, int which) { 145 dialog.cancel(); 146 } 147 }); 148 return b.create(); 149 } 150 151 private Callback getCallback() { 152 Fragment targetFragment = getTargetFragment(); 153 if (targetFragment != null) { 154 return (Callback) targetFragment; 155 } 156 return (Callback) getActivity(); 157 } 158 } 159