Welcome.java revision 191448b430f36167bd1706c13d77e48d7e7505cf
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 com.android.email.AccountBackupRestore; 20import com.android.email.Email; 21import com.android.email.ExchangeUtils; 22import com.android.email.Utility; 23import com.android.email.activity.setup.AccountSetupBasics; 24import com.android.email.provider.EmailContent; 25import com.android.email.provider.EmailContent.Mailbox; 26import com.android.email.service.MailService; 27 28import android.accounts.AccountManager; 29import android.accounts.OnAccountsUpdateListener; 30import android.app.Activity; 31import android.content.Intent; 32import android.content.res.Configuration; 33import android.database.Cursor; 34import android.os.AsyncTask; 35import android.os.Bundle; 36import android.os.Handler; 37 38/** 39 * The Welcome activity initializes the application and decides what Activity 40 * the user should start with. 41 * If no accounts are configured the user is taken to the AccountSetupBasics Activity where they 42 * can configure an account. 43 * If a single account is configured the user is taken directly to the MessageList for 44 * the INBOX of that account. 45 * If more than one account is configured the user is taken to the AccountFolderList Activity so 46 * they can select an account. 47 */ 48public class Welcome extends Activity { 49 50 private AccountsUpdatedListener mAccountsUpdatedListener; 51 private Handler mHandler = new Handler(); 52 53 /** 54 * Launch this activity. Note: It's assumed that this activity is only called as a means to 55 * 'reset' the UI state; Because of this, it is always launched with FLAG_ACTIVITY_CLEAR_TOP, 56 * which will drop any other activities on the stack (e.g. AccountFolderList or MessageList). 57 */ 58 public static void actionStart(Activity fromActivity) { 59 Intent i = new Intent(fromActivity, Welcome.class); 60 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 61 fromActivity.startActivity(i); 62 } 63 64 @Override 65 public void onCreate(Bundle icicle) { 66 super.onCreate(icicle); 67 68 // Reset the "accounts changed" notification, now that we're here 69 Email.setNotifyUiAccountsChanged(false); 70 71 // Quickly check for bulk upgrades (from older app versions) and switch to the 72 // upgrade activity if necessary 73 if (UpgradeAccounts.doBulkUpgradeIfNecessary(this)) { 74 finish(); 75 return; 76 } 77 78 // Restore accounts, if it has not happened already 79 // NOTE: This is blocking, which it should not be (in the UI thread) 80 // We're going to live with this for the short term and replace with something 81 // smarter. Long-term fix: Move this, and most of the code below, to an AsyncTask 82 // and do the DB work in a thread. Then post handler to finish() as appropriate. 83 AccountBackupRestore.restoreAccountsIfNeeded(this); 84 85 // Because the app could be reloaded (for debugging, etc.), we need to make sure that 86 // SyncManager gets a chance to start. There is no harm to starting it if it has already 87 // been started 88 // TODO More completely separate SyncManager from Email app 89 ExchangeUtils.startExchangeService(this); 90 91 // TODO Move this listener code to a more central location 92 // Set up our observer for AccountManager 93 mAccountsUpdatedListener = new AccountsUpdatedListener(); 94 AccountManager.get(getApplication()).addOnAccountsUpdatedListener( 95 mAccountsUpdatedListener, mHandler, true); 96 // Run reconciliation to make sure we're up-to-date on account status 97 mAccountsUpdatedListener.onAccountsUpdated(null); 98 99 new MainActivityLauncher(this).execute(); 100 } 101 102 @Override 103 public void onDestroy() { 104 super.onDestroy(); 105 if (mAccountsUpdatedListener != null) { 106 AccountManager.get(this).removeOnAccountsUpdatedListener(mAccountsUpdatedListener); 107 } 108 } 109 110 /** 111 * Reconcile accounts when accounts are added/removed from AccountManager 112 */ 113 public class AccountsUpdatedListener implements OnAccountsUpdateListener { 114 public void onAccountsUpdated(android.accounts.Account[] accounts) { 115 Utility.runAsync(new Runnable() { 116 public void run() { 117 MailService.reconcilePopImapAccounts(Welcome.this); 118 } 119 }); 120 } 121 } 122 123 /** 124 * Open the Activity appropriate to the current configuration. 125 * 126 * - If there's 0 accounts, open AccountSetupBasics. 127 * - If it has XL screen, open MessageListXL. 128 * - If there's 1 account, open MessageList. 129 * - Otherwise open AccountFolderList. 130 */ 131 private static class MainActivityLauncher extends AsyncTask<Void, Void, Void> { 132 private final Activity mFromActivity; 133 134 public MainActivityLauncher(Activity fromActivity) { 135 mFromActivity = fromActivity; 136 } 137 138 @Override 139 protected Void doInBackground(Void... params) { 140 final int numAccount = 141 EmailContent.count(mFromActivity, EmailContent.Account.CONTENT_URI); 142 if (numAccount == 0) { 143 AccountSetupBasics.actionNewAccount(mFromActivity); 144 } else { 145 final int screenLayout = mFromActivity.getResources().getConfiguration() 146 .screenLayout; 147 if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0) { 148 MessageListXL.actionStart(mFromActivity); 149 } else { 150 if (numAccount == 1) { 151 long accountId = EmailContent.Account.getDefaultAccountId(mFromActivity); 152 MessageList.actionHandleAccount(mFromActivity, accountId, 153 Mailbox.TYPE_INBOX); 154 } else { 155 AccountFolderList.actionShowAccounts(mFromActivity); 156 } 157 } 158 } 159 return null; 160 } 161 162 @Override 163 protected void onPostExecute(Void result) { 164 mFromActivity.finish(); 165 } 166 } 167} 168