MoveMessageToDialog.java revision a685d3b0191f472de9902b3d5590fc3e8408400c
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.emailcommon.provider.EmailContent.Account; 21import com.android.emailcommon.provider.EmailContent.Mailbox; 22import com.android.emailcommon.provider.EmailContent.Message; 23import com.android.emailcommon.utility.Utility; 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 #dismissAllowingStateLoss()} using a {@link Handler}. Calling 144 * {@link #dismissAllowingStateLoss()} from {@link LoaderManager.LoaderCallbacks#onLoadFinished} 145 * is not allowed, so we use it instead. 146 */ 147 private void dismissAsync() { 148 new Handler().post(new Runnable() { 149 @Override 150 public void run() { 151 if (!mDestroyed) { 152 dismissAllowingStateLoss(); 153 } 154 } 155 }); 156 } 157 158 /** 159 * Loader callback for {@link MessageChecker} 160 */ 161 private class MessageCheckerCallback implements LoaderManager.LoaderCallbacks<Long> { 162 @Override 163 public Loader<Long> onCreateLoader(int id, Bundle args) { 164 return new MessageChecker(getActivity(), mMessageIds); 165 } 166 167 @Override 168 public void onLoadFinished(Loader<Long> loader, Long accountId) { 169 if (mDestroyed) { 170 return; 171 } 172 // accountId shouldn't be null, but I'm paranoia. 173 if ((accountId == null) || (accountId == -1)) { 174 // Some of the messages can't be moved. Close the dialog. 175 dismissAsync(); 176 return; 177 } 178 mAccountId = accountId; 179 getLoaderManager().initLoader( 180 ActivityHelper.GLOBAL_LOADER_ID_MOVE_TO_DIALOG_MAILBOX_LOADER, 181 null, new MailboxesLoaderCallbacks()); 182 } 183 184 @Override 185 public void onLoaderReset(Loader<Long> loader) { 186 } 187 } 188 189 /** 190 * Loader callback for destination mailbox list. 191 */ 192 private class MailboxesLoaderCallbacks implements LoaderManager.LoaderCallbacks<Cursor> { 193 @Override 194 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 195 return MailboxesAdapter.createLoader(getActivity().getApplicationContext(), mAccountId, 196 MailboxesAdapter.MODE_MOVE_TO_TARGET); 197 } 198 199 @Override 200 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 201 if (mDestroyed) { 202 return; 203 } 204 mAdapter.swapCursor(data); 205 } 206 207 @Override 208 public void onLoaderReset(Loader<Cursor> loader) { 209 mAdapter.swapCursor(null); 210 } 211 } 212 213 /** 214 * A loader that checks if the messages can be moved, and return the Id of the account that owns 215 * the messages. (If any of the messages can't be moved, return -1.) 216 */ 217 private static class MessageChecker extends AsyncTaskLoader<Long> { 218 private final Activity mActivity; 219 private final long[] mMessageIds; 220 221 public MessageChecker(Activity activity, long[] messageIds) { 222 super(activity); 223 mActivity = activity; 224 mMessageIds = messageIds; 225 } 226 227 @Override 228 public Long loadInBackground() { 229 final Context c = getContext(); 230 231 long accountId = -1; // -1 == account not found yet. 232 233 for (long messageId : mMessageIds) { 234 // TODO This shouln't restore a full Message object. 235 final Message message = Message.restoreMessageWithId(c, messageId); 236 if (message == null) { 237 continue; // Skip removed messages. 238 } 239 240 // First, check account. 241 if (accountId == -1) { 242 // First message -- see if the account supports move. 243 accountId = message.mAccountKey; 244 if (!Account.supportsMoveMessages(c, accountId)) { 245 Utility.showToast( 246 mActivity, R.string.cannot_move_protocol_not_supported_toast); 247 return -1L; 248 } 249 } else { 250 // Following messages -- have to belong to the same account 251 if (message.mAccountKey != accountId) { 252 Utility.showToast(mActivity, R.string.cannot_move_multiple_accounts_toast); 253 return -1L; 254 } 255 } 256 // Second, check mailbox. 257 if (!Mailbox.canMoveFrom(c, message.mMailboxKey)) { 258 Utility.showToast(mActivity, R.string.cannot_move_special_mailboxes_toast); 259 return -1L; 260 } 261 } 262 263 // If all messages have been removed, accountId remains -1, which is what we should 264 // return here. 265 return accountId; 266 } 267 268 @Override 269 protected void onStartLoading() { 270 cancelLoad(); 271 forceLoad(); 272 } 273 274 @Override 275 protected void onStopLoading() { 276 cancelLoad(); 277 } 278 279 @Override 280 protected void onReset() { 281 stopLoading(); 282 } 283 } 284} 285