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