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.setup; 18 19 import com.android.email.R; 20 import com.android.email.activity.UiUtilities; 21 import com.android.mail.providers.Account; 22 import com.android.mail.providers.UIProvider; 23 import com.android.mail.utils.Utils; 24 25 import android.app.Activity; 26 import android.app.Fragment; 27 import android.app.LoaderManager; 28 import android.content.CursorLoader; 29 import android.content.Loader; 30 import android.database.Cursor; 31 import android.net.Uri; 32 import android.os.Bundle; 33 import android.view.LayoutInflater; 34 import android.view.Menu; 35 import android.view.MenuInflater; 36 import android.view.MenuItem; 37 import android.view.View; 38 import android.view.ViewGroup; 39 import android.widget.AdapterView; 40 import android.widget.ListView; 41 import android.widget.SimpleCursorAdapter; 42 import android.widget.TextView; 43 44 /** 45 * Lists quick responses associated with the specified email account. Allows users to create, 46 * edit, and delete quick responses. Owning activity must: 47 * <ul> 48 * <li>Launch this fragment using startPreferencePanel().</li> 49 * <li>Provide an Account as an argument named "account". This account's quick responses 50 * will be read and potentially modified.</li> 51 * </ul> 52 * 53 * <p>This fragment is run as a preference panel from AccountSettings.</p> 54 */ 55 public class AccountSettingsEditQuickResponsesFragment extends Fragment { 56 private Account mAccount; 57 58 private static final String ARG_ACCOUNT = "account"; 59 60 // Public no-args constructor needed for fragment re-instantiation AccountSettingsEditQuickResponsesFragment()61 public AccountSettingsEditQuickResponsesFragment() {} 62 createArgs(final Account account)63 public static Bundle createArgs(final Account account) { 64 final Bundle b = new Bundle(1); 65 b.putParcelable(ARG_ACCOUNT, account); 66 return b; 67 } 68 69 @Override onAttach(Activity activity)70 public void onAttach(Activity activity) { 71 super.onAttach(activity); 72 } 73 74 @Override onActivityCreated(Bundle savedInstanceState)75 public void onActivityCreated(Bundle savedInstanceState) { 76 super.onActivityCreated(savedInstanceState); 77 78 final SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), 79 R.layout.quick_response_item, null, 80 new String[] {UIProvider.QuickResponseColumns.TEXT}, 81 new int[] {R.id.quick_response_text}, 0); 82 83 final ListView listView = UiUtilities.getView(getView(), 84 R.id.account_settings_quick_responses_list); 85 listView.setAdapter(adapter); 86 87 getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() { 88 @Override 89 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 90 return new CursorLoader(getActivity(), mAccount.quickResponseUri, 91 UIProvider.QUICK_RESPONSE_PROJECTION, null, null, null); 92 } 93 94 @Override 95 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 96 adapter.swapCursor(data); 97 } 98 99 @Override 100 public void onLoaderReset(Loader<Cursor> loader) { 101 adapter.swapCursor(null); 102 } 103 }); 104 } 105 106 @Override onCreate(Bundle savedInstanceState)107 public void onCreate(Bundle savedInstanceState) { 108 super.onCreate(savedInstanceState); 109 110 Bundle args = getArguments(); 111 mAccount = args.getParcelable(ARG_ACCOUNT); 112 113 setHasOptionsMenu(true); 114 } 115 116 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)117 public View onCreateView(LayoutInflater inflater, ViewGroup container, 118 Bundle savedInstanceState) { 119 final View view = inflater.inflate(R.layout.account_settings_edit_quick_responses_fragment, 120 container, false); 121 122 final ListView listView = UiUtilities.getView(view, 123 R.id.account_settings_quick_responses_list); 124 final TextView emptyView = 125 UiUtilities.getView((ViewGroup) listView.getParent(), R.id.empty_view); 126 listView.setEmptyView(emptyView); 127 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 128 @Override 129 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 130 final Cursor c = (Cursor) listView.getItemAtPosition(position); 131 final String quickResponseText = 132 c.getString(c.getColumnIndex(UIProvider.QuickResponseColumns.TEXT)); 133 final Uri uri = Utils.getValidUri( 134 c.getString(c.getColumnIndex(UIProvider.QuickResponseColumns.URI))); 135 EditQuickResponseDialog.newInstance(quickResponseText, uri, false) 136 .show(getFragmentManager(), null); 137 } 138 }); 139 return view; 140 } 141 142 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)143 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 144 menu.clear(); 145 inflater.inflate(R.menu.quick_response_prefs_fragment_menu, menu); 146 } 147 148 @Override onOptionsItemSelected(MenuItem item)149 public boolean onOptionsItemSelected(MenuItem item) { 150 if (item.getItemId() == R.id.create_new) { 151 EditQuickResponseDialog.newInstance(null, mAccount.quickResponseUri, true) 152 .show(getFragmentManager(), null); 153 return true; 154 } 155 return super.onOptionsItemSelected(item); 156 } 157 } 158