• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.app.Fragment;
23 import android.app.LoaderManager;
24 import android.content.Context;
25 import android.content.CursorLoader;
26 import android.content.DialogInterface;
27 import android.content.Loader;
28 import android.database.Cursor;
29 import android.os.Bundle;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.widget.AdapterView;
33 import android.widget.AdapterView.OnItemClickListener;
34 import android.widget.ListView;
35 import android.widget.SimpleCursorAdapter;
36 
37 import com.android.email.R;
38 import com.android.mail.providers.Account;
39 import com.android.mail.providers.UIProvider;
40 
41 /**
42  * Dialog which lists QuickResponses for the specified account. On user selection, will call
43  * Callback.onQuickResponseSelected() with the selected QuickResponse text.
44  */
45 public class InsertQuickResponseDialog extends DialogFragment {
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          */
onQuickResponseSelected(CharSequence quickResponse)56         public void onQuickResponseSelected(CharSequence quickResponse);
57     }
58 
59     // Public no-args constructor needed for fragment re-instantiation
InsertQuickResponseDialog()60     public InsertQuickResponseDialog() {}
61 
62     /**
63      * Create and returns new dialog.
64      *
65      * @param callbackFragment fragment that implements {@link Callback}.  Or null, in which case
66      * the parent activity must implement {@link Callback}.
67      */
68     public static InsertQuickResponseDialog
newInstance(Fragment callbackFragment, Account account)69             newInstance(Fragment callbackFragment, Account account) {
70         final InsertQuickResponseDialog dialog = new InsertQuickResponseDialog();
71 
72         // If a target is set, it MUST implement Callback. Fail-fast if not.
73         if (callbackFragment != null) {
74             if (!(callbackFragment instanceof Callback)) {
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
onCreateDialog(Bundle savedInstanceState)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             if (!(getActivity() instanceof Callback)) {
93                 throw new ClassCastException(getActivity().toString() + " must implement Callback");
94             }
95         }
96 
97         // Now that Callback implementation is verified, build the dialog
98         final Context context = getActivity();
99 
100         final SimpleCursorAdapter adapter = new SimpleCursorAdapter(context,
101                 R.layout.quick_response_item, null,
102                 new String[] {UIProvider.QuickResponseColumns.TEXT},
103                 new int[] {R.id.quick_response_text}, 0);
104 
105         final AlertDialog.Builder builder = new AlertDialog.Builder(context);
106 
107         // inflate the view to show in the dialog
108         final LayoutInflater li = LayoutInflater.from(builder.getContext());
109         final View quickResponsesView = li.inflate(R.layout.quick_responses, null);
110 
111         // the view contains both a ListView and its associated empty view; wire them together
112         final ListView listView = (ListView) quickResponsesView.findViewById(R.id.quick_responses);
113         listView.setEmptyView(quickResponsesView.findViewById(R.id.quick_responses_empty_view));
114         listView.setAdapter(adapter);
115         listView.setOnItemClickListener(new OnItemClickListener() {
116             @Override
117             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
118                 final Cursor c = (Cursor) listView.getItemAtPosition(position);
119                 final String quickResponseText =
120                         c.getString(c.getColumnIndex(UIProvider.QuickResponseColumns.TEXT));
121                 getCallback().onQuickResponseSelected(quickResponseText);
122                 dismiss();
123             }
124         });
125 
126         final Account account = getArguments().getParcelable(ACCOUNT_KEY);
127 
128         getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {
129             @Override
130             public Loader<Cursor> onCreateLoader(int id, Bundle args) {
131                 return new CursorLoader(context, account.quickResponseUri,
132                         UIProvider.QUICK_RESPONSE_PROJECTION, null, null, null);
133             }
134 
135             @Override
136             public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
137                 adapter.swapCursor(data);
138             }
139 
140             @Override
141             public void onLoaderReset(Loader<Cursor> loader) {
142                 adapter.swapCursor(null);
143             }
144         });
145 
146         final String dialogTitle = getResources()
147                 .getString(R.string.message_compose_insert_quick_response_list_title);
148 
149         return builder
150                 .setTitle(dialogTitle)
151                 .setView(quickResponsesView)
152                 .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
153                     @Override
154                     public void onClick(DialogInterface dialog, int which) {
155                         dialog.cancel();
156                     }
157                 })
158                 .create();
159     }
160 
161     private Callback getCallback() {
162         Fragment targetFragment = getTargetFragment();
163         if (targetFragment != null) {
164             return (Callback) targetFragment;
165         }
166         return (Callback) getActivity();
167     }
168 }
169