MoveMessageToDialog.java revision a7bc0319a75184ad706bb35c049af107ac3688e6
1/* 2 * Copyright (C) 2010 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 17package com.android.email.activity; 18 19import com.android.email.R; 20import com.android.email.Utility; 21import com.android.emailcommon.provider.EmailContent.Account; 22import com.android.emailcommon.provider.EmailContent.Mailbox; 23import com.android.emailcommon.provider.EmailContent.Message; 24 25import android.app.Activity; 26import android.app.AlertDialog; 27import android.app.Dialog; 28import android.app.DialogFragment; 29import android.app.Fragment; 30import android.app.LoaderManager; 31import android.content.AsyncTaskLoader; 32import android.content.Context; 33import android.content.DialogInterface; 34import android.content.Loader; 35import android.database.Cursor; 36import android.os.Bundle; 37import android.os.Handler; 38 39import java.security.InvalidParameterException; 40 41/** 42 * "Move (messages) to" dialog. 43 * 44 * TODO The check logic in MessageCheckerCallback is not efficient. It shouldn't restore full 45 * Message objects. But we don't bother at this point as the UI is still temporary. 46 */ 47public class MoveMessageToDialog extends DialogFragment implements DialogInterface.OnClickListener { 48 private static final String BUNDLE_MESSAGE_IDS = "message_ids"; 49 50 /** Message IDs passed to {@link #newInstance} */ 51 private long[] mMessageIds; 52 private MailboxesAdapter mAdapter; 53 54 /** Account ID is restored by {@link MailboxesLoaderCallbacks} */ 55 private long mAccountId; 56 57 private boolean mDestroyed; 58 59 /** 60 * Callback that target fragments, or the owner activity should implement. 61 */ 62 public interface Callback { 63 public void onMoveToMailboxSelected(long newMailboxId, long[] messageIds); 64 } 65 66 /** 67 * Create and return a new instance. 68 * 69 * @param parent owner activity. 70 * @param messageIds IDs of the messages to be moved. 71 * @param callbackFragment Fragment that gets a callback. The fragment must implement 72 * {@link Callback}. If null is passed, then the owner activity is used instead, in which case 73 * it must implement {@link Callback} instead. 74 */ 75 public static MoveMessageToDialog newInstance(Activity parent, 76 long[] messageIds, Fragment callbackFragment) { 77 if (messageIds.length == 0) { 78 throw new InvalidParameterException(); 79 } 80 MoveMessageToDialog dialog = new MoveMessageToDialog(); 81 Bundle args = new Bundle(); 82 args.putLongArray(BUNDLE_MESSAGE_IDS, messageIds); 83 dialog.setArguments(args); 84 if (callbackFragment != null) { 85 dialog.setTargetFragment(callbackFragment, 0); 86 } 87 return dialog; 88 } 89 90 @Override 91 public void onCreate(Bundle savedInstanceState) { 92 super.onCreate(savedInstanceState); 93 mMessageIds = getArguments().getLongArray(BUNDLE_MESSAGE_IDS); 94 setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Light); 95 } 96 97 @Override 98 public void onDestroy() { 99 mDestroyed = true; 100 super.onDestroy(); 101 } 102 103 @Override 104 public Dialog onCreateDialog(Bundle savedInstanceState) { 105 final Activity activity = getActivity(); 106 107 // Build adapter & dialog 108 // Make sure to pass Builder's context to the adapter, so that it'll get the correct theme. 109 AlertDialog.Builder builder = new AlertDialog.Builder(activity) 110 .setTitle(activity.getResources().getString(R.string.move_to_folder_dialog_title)); 111 112 mAdapter = 113 new MailboxesAdapter(builder.getContext(), MailboxesAdapter.MODE_MOVE_TO_TARGET, 114 new MailboxesAdapter.EmptyCallback()); 115 builder.setSingleChoiceItems(mAdapter, -1, this); 116 117 getLoaderManager().initLoader( 118 ActivityHelper.GLOBAL_LOADER_ID_MOVE_TO_DIALOG_MESSAGE_CHECKER, 119 null, new MessageCheckerCallback()); 120 121 return builder.show(); 122 } 123 124 @Override 125 public void onClick(DialogInterface dialog, int position) { 126 final long mailboxId = mAdapter.getItemId(position); 127 128 getCallback().onMoveToMailboxSelected(mailboxId, mMessageIds); 129 dismiss(); 130 } 131 132 private Callback getCallback() { 133 Fragment targetFragment = getTargetFragment(); 134 if (targetFragment != null) { 135 // If a target is set, it MUST implement Callback. 136 return (Callback) targetFragment; 137 } 138 // If not the parent activity MUST implement Callback. 139 return (Callback) getActivity(); 140 } 141 142 /** 143 * Delay-call {@link #dismiss()} using a {@link Handler}. Calling {@link #dismiss()} from 144 * {@link LoaderManager.LoaderCallbacks#onLoadFinished} is not allowed, so we use it instead. 145 */ 146 private void dismissAsync() { 147 new Handler().post(new Runnable() { 148 @Override 149 public void run() { 150 if (!mDestroyed) { 151 dismiss(); 152 } 153 } 154 }); 155 } 156 157 /** 158 * Loader callback for {@link MessageChecker} 159 */ 160 private class MessageCheckerCallback implements LoaderManager.LoaderCallbacks<Long> { 161 @Override 162 public Loader<Long> onCreateLoader(int id, Bundle args) { 163 return new MessageChecker(getActivity(), mMessageIds); 164 } 165 166 @Override 167 public void onLoadFinished(Loader<Long> loader, Long accountId) { 168 if (mDestroyed) { 169 return; 170 } 171 // accountId shouldn't be null, but I'm paranoia. 172 if ((accountId == null) || (accountId == -1)) { 173 // Some of the messages can't be moved. Close the dialog. 174 dismissAsync(); 175 return; 176 } 177 mAccountId = accountId; 178 getLoaderManager().initLoader( 179 ActivityHelper.GLOBAL_LOADER_ID_MOVE_TO_DIALOG_MAILBOX_LOADER, 180 null, new MailboxesLoaderCallbacks()); 181 } 182 183 @Override 184 public void onLoaderReset(Loader<Long> loader) { 185 } 186 } 187 188 /** 189 * Loader callback for destination mailbox list. 190 */ 191 private class MailboxesLoaderCallbacks implements LoaderManager.LoaderCallbacks<Cursor> { 192 @Override 193 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 194 return MailboxesAdapter.createLoader(getActivity().getApplicationContext(), mAccountId, 195 MailboxesAdapter.MODE_MOVE_TO_TARGET); 196 } 197 198 @Override 199 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 200 if (mDestroyed) { 201 return; 202 } 203 mAdapter.swapCursor(data); 204 } 205 206 @Override 207 public void onLoaderReset(Loader<Cursor> loader) { 208 mAdapter.swapCursor(null); 209 } 210 } 211 212 /** 213 * A loader that checks if the messages can be moved, and return the Id of the account that owns 214 * the messages. (If any of the messages can't be moved, return -1.) 215 */ 216 private static class MessageChecker extends AsyncTaskLoader<Long> { 217 private final Activity mActivity; 218 private final long[] mMessageIds; 219 220 public MessageChecker(Activity activity, long[] messageIds) { 221 super(activity); 222 mActivity = activity; 223 mMessageIds = messageIds; 224 } 225 226 @Override 227 public Long loadInBackground() { 228 final Context c = getContext(); 229 230 long accountId = -1; // -1 == account not found yet. 231 232 for (long messageId : mMessageIds) { 233 // TODO This shouln't restore a full Message object. 234 final Message message = Message.restoreMessageWithId(c, messageId); 235 if (message == null) { 236 continue; // Skip removed messages. 237 } 238 239 // First, check account. 240 if (accountId == -1) { 241 // First message -- see if the account supports move. 242 accountId = message.mAccountKey; 243 if (!Account.supportsMoveMessages(c, accountId)) { 244 Utility.showToast( 245 mActivity, R.string.cannot_move_protocol_not_supported_toast); 246 return -1L; 247 } 248 } else { 249 // Following messages -- have to belong to the same account 250 if (message.mAccountKey != accountId) { 251 Utility.showToast(mActivity, R.string.cannot_move_multiple_accounts_toast); 252 return -1L; 253 } 254 } 255 // Second, check mailbox. 256 if (!Mailbox.canMoveFrom(c, message.mMailboxKey)) { 257 Utility.showToast(mActivity, R.string.cannot_move_special_mailboxes_toast); 258 return -1L; 259 } 260 } 261 262 // If all messages have been removed, accountId remains -1, which is what we should 263 // return here. 264 return accountId; 265 } 266 267 @Override 268 protected void onStartLoading() { 269 cancelLoad(); 270 forceLoad(); 271 } 272 273 @Override 274 protected void onStopLoading() { 275 cancelLoad(); 276 } 277 278 @Override 279 protected void onReset() { 280 stopLoading(); 281 } 282 } 283} 284