Welcome.java revision de4901bcc3c2e1e7012bf647afbc5e8bfb2c1f58
1/* 2 * Copyright (C) 2008 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 android.app.Activity; 20import android.content.Context; 21import android.content.Intent; 22import android.net.Uri; 23import android.os.Bundle; 24import android.text.TextUtils; 25import android.util.Log; 26import android.view.LayoutInflater; 27import android.view.Menu; 28import android.view.MenuItem; 29import android.view.View; 30import android.view.ViewGroup.LayoutParams; 31 32import com.android.email.Email; 33import com.android.email.Preferences; 34import com.android.email.R; 35import com.android.email.activity.setup.AccountSettings; 36import com.android.email.activity.setup.AccountSetupBasics; 37import com.android.email.service.EmailServiceUtils; 38import com.android.email.service.MailService; 39import com.android.emailcommon.Logging; 40import com.android.emailcommon.provider.Account; 41import com.android.emailcommon.provider.EmailContent; 42import com.android.emailcommon.provider.EmailContent.Message; 43import com.android.emailcommon.provider.Mailbox; 44import com.android.emailcommon.utility.EmailAsyncTask; 45import com.android.emailcommon.utility.IntentUtilities; 46import com.android.emailcommon.utility.Utility; 47import com.google.common.annotations.VisibleForTesting; 48 49/** 50 * The Welcome activity initializes the application and starts {@link EmailActivity}, or launch 51 * {@link AccountSetupBasics} if no accounts are configured. 52 * 53 * TOOD Show "your messages are on the way" message like gmail does during the inbox lookup. 54 */ 55public class Welcome extends Activity { 56 /* 57 * Commands for testing... 58 * Open 1 pane 59 adb shell am start -a android.intent.action.MAIN \ 60 -d '"content://ui.email.android.com/view/mailbox"' \ 61 -e DEBUG_PANE_MODE 1 62 63 * Open 2 pane 64 adb shell am start -a android.intent.action.MAIN \ 65 -d '"content://ui.email.android.com/view/mailbox"' \ 66 -e DEBUG_PANE_MODE 2 67 68 * Open an account (ID=1) in 2 pane 69 adb shell am start -a android.intent.action.MAIN \ 70 -d '"content://ui.email.android.com/view/mailbox?ACCOUNT_ID=1"' \ 71 -e DEBUG_PANE_MODE 2 72 73 * Open a message (account id=1, mailbox id=2, message id=3) 74 adb shell am start -a android.intent.action.MAIN \ 75 -d '"content://ui.email.android.com/view/mailbox?ACCOUNT_ID=1&MAILBOX_ID=2&MESSAGE_ID=3"' \ 76 -e DEBUG_PANE_MODE 2 77 78 * Open the combined starred on the combined view 79 adb shell am start -a android.intent.action.MAIN \ 80 -d '"content://ui.email.android.com/view/mailbox?ACCOUNT_ID=1152921504606846976&MAILBOX_ID=-4"' \ 81 -e DEBUG_PANE_MODE 2 82 */ 83 84 /** 85 * Extra for debugging. Set 1 to force one-pane. Set 2 to force two-pane. 86 */ 87 private static final String EXTRA_DEBUG_PANE_MODE = "DEBUG_PANE_MODE"; 88 89 private static final String VIEW_MAILBOX_INTENT_URL_PATH = "/view/mailbox"; 90 91 private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker(); 92 93 private View mWaitingForSyncView; 94 95 private long mAccountId; 96 private long mMailboxId; 97 private long mMessageId; 98 private String mAccountUuid; 99 100 private MailboxFinder mInboxFinder; 101 102 /** 103 * Launch this activity. Note: It's assumed that this activity is only called as a means to 104 * 'reset' the UI state; Because of this, it is always launched with FLAG_ACTIVITY_CLEAR_TOP, 105 * which will drop any other activities on the stack (e.g. AccountFolderList or MessageList). 106 */ 107 public static void actionStart(Activity fromActivity) { 108 Intent i = IntentUtilities.createRestartAppIntent(fromActivity, Welcome.class); 109 fromActivity.startActivity(i); 110 } 111 112 /** 113 * Create an Intent to open email activity. If <code>accountId</code> is not -1, the 114 * specified account will be automatically be opened when the activity starts. 115 */ 116 public static Intent createOpenAccountInboxIntent(Context context, long accountId) { 117 final Uri.Builder b = IntentUtilities.createActivityIntentUrlBuilder( 118 VIEW_MAILBOX_INTENT_URL_PATH); 119 IntentUtilities.setAccountId(b, accountId); 120 return IntentUtilities.createRestartAppIntent(b.build()); 121 } 122 123 /** 124 * Create an Intent to open a message. 125 */ 126 public static Intent createOpenMessageIntent(Context context, long accountId, 127 long mailboxId, long messageId) { 128 final Uri.Builder b = IntentUtilities.createActivityIntentUrlBuilder( 129 VIEW_MAILBOX_INTENT_URL_PATH); 130 IntentUtilities.setAccountId(b, accountId); 131 IntentUtilities.setMailboxId(b, mailboxId); 132 IntentUtilities.setMessageId(b, messageId); 133 return IntentUtilities.createRestartAppIntent(b.build()); 134 } 135 136 /** 137 * Open account's inbox. 138 */ 139 public static void actionOpenAccountInbox(Activity fromActivity, long accountId) { 140 fromActivity.startActivity(createOpenAccountInboxIntent(fromActivity, accountId)); 141 } 142 143 /** 144 * Create an {@link Intent} for account shortcuts. The returned intent stores the account's 145 * UUID rather than the account ID, which will be changed after account restore. 146 */ 147 public static Intent createAccountShortcutIntent(Context context, String uuid, long mailboxId) { 148 final Uri.Builder b = IntentUtilities.createActivityIntentUrlBuilder( 149 VIEW_MAILBOX_INTENT_URL_PATH); 150 IntentUtilities.setAccountUuid(b, uuid); 151 IntentUtilities.setMailboxId(b, mailboxId); 152 return IntentUtilities.createRestartAppIntent(b.build()); 153 } 154 155 /** 156 * If the {@link #EXTRA_DEBUG_PANE_MODE} extra is "1" or "2", return 1 or 2 respectively. 157 * Otherwise return 0. 158 * 159 * @see UiUtilities#setDebugPaneMode(int) 160 * @see UiUtilities#useTwoPane(Context) 161 */ 162 private static int getDebugPaneMode(Intent i) { 163 Bundle extras = i.getExtras(); 164 if (extras != null) { 165 String s = extras.getString(EXTRA_DEBUG_PANE_MODE); 166 if ("1".equals(s)) { 167 return 1; 168 } else if ("2".equals(s)) { 169 return 2; 170 } 171 } 172 return 0; 173 } 174 175 @Override 176 public void onCreate(Bundle icicle) { 177 super.onCreate(icicle); 178 ActivityHelper.debugSetWindowFlags(this); 179 180 // Because the app could be reloaded (for debugging, etc.), we need to make sure that 181 // ExchangeService gets a chance to start. There is no harm to starting it if it has 182 // already been started 183 // When the service starts, it reconciles EAS accounts. 184 // TODO More completely separate ExchangeService from Email app 185 EmailServiceUtils.startExchangeService(this); 186 187 // Extract parameters from the intent. 188 final Intent intent = getIntent(); 189 mAccountId = IntentUtilities.getAccountIdFromIntent(intent); 190 mMailboxId = IntentUtilities.getMailboxIdFromIntent(intent); 191 mMessageId = IntentUtilities.getMessageIdFromIntent(intent); 192 mAccountUuid = IntentUtilities.getAccountUuidFromIntent(intent); 193 UiUtilities.setDebugPaneMode(getDebugPaneMode(intent)); 194 195 // Reconcile POP/IMAP accounts. EAS accounts are taken care of by ExchangeService. 196 EmailAsyncTask.runAsyncParallel(new Runnable() { 197 @Override 198 public void run() { 199 // Reconciling can be heavy - so do it in the background. 200 if (MailService.hasMismatchInPopImapAccounts(Welcome.this)) { 201 MailService.reconcilePopImapAccountsSync(Welcome.this); 202 } 203 resolveAccount(); 204 } 205 }); 206 207 // Reset the "accounts changed" notification, now that we're here 208 Email.setNotifyUiAccountsChanged(false); 209 } 210 211 @Override 212 public boolean onCreateOptionsMenu(Menu menu) { 213 // Only create the menu if we had to stop and show a loading spinner - otherwise 214 // this is a transient activity with no UI. 215 if (mInboxFinder == null) { 216 return super.onCreateOptionsMenu(menu); 217 } 218 219 getMenuInflater().inflate(R.menu.welcome, menu); 220 return true; 221 } 222 223 @Override 224 public boolean onOptionsItemSelected(MenuItem item) { 225 if (item.getItemId() == R.id.account_settings) { 226 AccountSettings.actionSettings(this, mAccountId); 227 return true; 228 } 229 return super.onOptionsItemSelected(item); 230 } 231 232 @Override 233 protected void onStop() { 234 // Cancel all running tasks. 235 // (If it's stopping for configuration changes, we just re-do everything on the new 236 // instance) 237 stopInboxLookup(); 238 mTaskTracker.cancellAllInterrupt(); 239 240 super.onStop(); 241 242 if (!isChangingConfigurations()) { 243 // This means the user opened some other app. 244 // Just close self and not launch EmailActivity. 245 if (Email.DEBUG && Logging.DEBUG_LIFECYCLE) { 246 Log.d(Logging.LOG_TAG, "Welcome: Closing self..."); 247 } 248 finish(); 249 } 250 } 251 252 /** 253 * {@inheritDoc} 254 * 255 * When launching an activity from {@link Welcome}, we always want to set 256 * {@link Intent#FLAG_ACTIVITY_FORWARD_RESULT}. 257 */ 258 @Override 259 public void startActivity(Intent intent) { 260 intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 261 super.startActivity(intent); 262 } 263 264 /** 265 * Stop inbox lookup. This MSUT be called on the UI thread. 266 */ 267 private void stopInboxLookup() { 268 if (mInboxFinder != null) { 269 mInboxFinder.cancel(); 270 mInboxFinder = null; 271 } 272 } 273 274 /** 275 * Start inbox lookup. This MSUT be called on the UI thread. 276 */ 277 private void startInboxLookup() { 278 Log.i(Logging.LOG_TAG, "Inbox not found. Starting mailbox finder..."); 279 stopInboxLookup(); // Stop if already running -- it shouldn't be but just in case. 280 mInboxFinder = new MailboxFinder(this, mAccountId, Mailbox.TYPE_INBOX, 281 mMailboxFinderCallback); 282 mInboxFinder.startLookup(); 283 284 // Show "your email will appear shortly" message. 285 mWaitingForSyncView = LayoutInflater.from(this).inflate( 286 R.layout.waiting_for_sync_message, null); 287 addContentView(mWaitingForSyncView, new LayoutParams( 288 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 289 invalidateOptionsMenu(); 290 } 291 292 /** 293 * Determine which account to open with the given account ID and UUID. 294 * 295 * @return ID of the account to use. 296 */ 297 @VisibleForTesting 298 static long resolveAccountId(Context context, long inputAccountId, String inputUuid) { 299 final long accountId; 300 301 if (!TextUtils.isEmpty(inputUuid)) { 302 // If a UUID is specified, try to use it. 303 // If the UUID is invalid, accountId will be NO_ACCOUNT. 304 accountId = Account.getAccountIdFromUuid(context, inputUuid); 305 306 } else if (inputAccountId != Account.NO_ACCOUNT) { 307 // If a valid account ID is specified, just use it. 308 if (inputAccountId == Account.ACCOUNT_ID_COMBINED_VIEW 309 || Account.isValidId(context, inputAccountId)) { 310 accountId = inputAccountId; 311 } else { 312 accountId = Account.NO_ACCOUNT; 313 } 314 } else { 315 // Neither an accountID or a UUID is specified. 316 // Use the last account used, falling back to the default. 317 long lastUsedId = Preferences.getPreferences(context).getLastUsedAccountId(); 318 if (lastUsedId != Account.NO_ACCOUNT) { 319 if (!Account.isValidId(context, lastUsedId)) { 320 // The last account that was used has since been deleted. 321 lastUsedId = Account.NO_ACCOUNT; 322 Preferences.getPreferences(context).setLastUsedAccountId(Account.NO_ACCOUNT); 323 } 324 } 325 accountId = (lastUsedId == Account.NO_ACCOUNT) 326 ? Account.getDefaultAccountId(context) 327 : lastUsedId; 328 } 329 if (accountId != Account.NO_ACCOUNT) { 330 // Okay, the given account is valid. 331 return accountId; 332 } else { 333 // No, it's invalid. Show the warning toast and use the default. 334 Utility.showToast(context, R.string.toast_account_not_found); 335 return Account.getDefaultAccountId(context); 336 } 337 } 338 339 /** 340 * Determine which account to use according to the number of accounts already set up, 341 * {@link #mAccountId} and {@link #mAccountUuid}. 342 * 343 * <pre> 344 * 1. If there's no account configured, start account setup. 345 * 2. Otherwise detemine which account to open with {@link #resolveAccountId} and 346 * 2a. If the account doesn't have inbox yet, start inbox finder. 347 * 2b. Otherwise open the main activity. 348 * </pre> 349 */ 350 private void resolveAccount() { 351 final int numAccount = EmailContent.count(this, Account.CONTENT_URI); 352 if (numAccount == 0) { 353 AccountSetupBasics.actionNewAccount(this); 354 finish(); 355 return; 356 } else { 357 mAccountId = resolveAccountId(this, mAccountId, mAccountUuid); 358 if (Account.isNormalAccount(mAccountId) && 359 Mailbox.findMailboxOfType(this, mAccountId, Mailbox.TYPE_INBOX) 360 == Mailbox.NO_MAILBOX) { 361 startInboxLookup(); 362 return; 363 } 364 } 365 startEmailActivity(); 366 } 367 368 /** 369 * Start {@link EmailActivity} using {@link #mAccountId}, {@link #mMailboxId} and 370 * {@link #mMessageId}. 371 */ 372 private void startEmailActivity() { 373 final Intent i; 374 if (mMessageId != Message.NO_MESSAGE) { 375 i = EmailActivity.createOpenMessageIntent(this, mAccountId, mMailboxId, mMessageId); 376 } else if (mMailboxId != Mailbox.NO_MAILBOX) { 377 i = EmailActivity.createOpenMailboxIntent(this, mAccountId, mMailboxId); 378 } else { 379 i = EmailActivity.createOpenAccountIntent(this, mAccountId); 380 } 381 startActivity(i); 382 finish(); 383 } 384 385 private final MailboxFinder.Callback mMailboxFinderCallback = new MailboxFinder.Callback() { 386 // This MUST be called from callback methods. 387 private void cleanUp() { 388 mInboxFinder = null; 389 } 390 391 @Override 392 public void onAccountNotFound() { 393 cleanUp(); 394 // Account removed? Clear the IDs and restart the task. Which will result in either 395 // a) show account setup if there's really no accounts or b) open the default account. 396 397 mAccountId = Account.NO_ACCOUNT; 398 mMailboxId = Mailbox.NO_MAILBOX; 399 mMessageId = Message.NO_MESSAGE; 400 mAccountUuid = null; 401 402 // Restart the account resolution. 403 resolveAccount(); 404 } 405 406 @Override 407 public void onMailboxNotFound(long accountId) { 408 // Just do the same thing as "account not found". 409 onAccountNotFound(); 410 } 411 412 @Override 413 public void onAccountSecurityHold(long accountId) { 414 cleanUp(); 415 416 ActivityHelper.showSecurityHoldDialog(Welcome.this, accountId); 417 finish(); 418 } 419 420 @Override 421 public void onMailboxFound(long accountId, long mailboxId) { 422 cleanUp(); 423 424 // Okay the account has Inbox now. Start the main activity. 425 startEmailActivity(); 426 } 427 }; 428} 429