Welcome.java revision 0c75f83f03b3ef8e54163cba2d67ba086cf8af58
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.Account; 26import com.android.email.provider.EmailContent.Mailbox; 27import com.android.email.service.MailService; 28 29import android.app.Activity; 30import android.content.Context; 31import android.content.Intent; 32import android.content.res.Configuration; 33import android.os.AsyncTask; 34import android.os.Bundle; 35import android.os.Handler; 36 37/** 38 * The Welcome activity initializes the application and decides what Activity 39 * the user should start with. 40 * 41 * This class knows which activity should be launched under the current configuration (screen size) 42 * and the number of accounts configured. So if you want to open an account or a mailbox, 43 * you should alawys do so via its static methods, such as {@link #actionOpenAccountInbox}. 44 */ 45public class Welcome extends Activity { 46 /* 47 * Commands for testing... 48 * Open 1 pane 49 adb shell am start -a android.intent.action.MAIN \ 50 -n com.google.android.email/com.android.email.activity.Welcome \ 51 -e DEBUG_PANE_MODE 1 52 53 * Open 2 pane 54 adb shell am start -a android.intent.action.MAIN \ 55 -n com.google.android.email/com.android.email.activity.Welcome \ 56 -e DEBUG_PANE_MODE 2 57 58 * Open an account (ID=2) in 2 pane 59 adb shell am start -a android.intent.action.MAIN \ 60 -n com.google.android.email/com.android.email.activity.Welcome \ 61 -e DEBUG_PANE_MODE 2 --el ACCOUNT_ID 2 62 63 * Open a message (account id=1, mailbox id=2, message id=3) 64 adb shell am start -a android.intent.action.MAIN \ 65 -n com.google.android.email/com.android.email.activity.Welcome \ 66 -e DEBUG_PANE_MODE 2 \ 67 --el ACCOUNT_ID 1 \ 68 --el MAILBOX_ID 2 \ 69 --el MESSAGE_ID 3 70 71 */ 72 private static final String EXTRA_ACCOUNT_ID = "ACCOUNT_ID"; 73 private static final String EXTRA_MAILBOX_ID = "MAILBOX_ID"; 74 private static final String EXTRA_MESSAGE_ID = "MESSAGE_ID"; 75 76 /** 77 * Extra for debugging. Set 1 to force one-pane. Set 2 to force two-pane. 78 */ 79 private static final String EXTRA_DEBUG_PANE_MODE = "DEBUG_PANE_MODE"; 80 81 private Handler mHandler = new Handler(); 82 83 /** 84 * @return true if the two-pane activity should be used on the current configuration. 85 */ 86 public static boolean useTwoPane(Context context) { 87 final int screenLayout = context.getResources().getConfiguration().screenLayout; 88 return (screenLayout & Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0; 89 } 90 91 /** 92 * Launch this activity. Note: It's assumed that this activity is only called as a means to 93 * 'reset' the UI state; Because of this, it is always launched with FLAG_ACTIVITY_CLEAR_TOP, 94 * which will drop any other activities on the stack (e.g. AccountFolderList or MessageList). 95 */ 96 public static void actionStart(Activity fromActivity) { 97 Intent i = Utility.createRestartAppIntent(fromActivity, Welcome.class); 98 fromActivity.startActivity(i); 99 } 100 101 /** 102 * Create an Intent to open account's inbox. 103 */ 104 public static Intent createOpenAccountInboxIntent(Context context, long accountId) { 105 Intent i = Utility.createRestartAppIntent(context, Welcome.class); 106 if (accountId != -1) { 107 i.putExtra(EXTRA_ACCOUNT_ID, accountId); 108 } 109 return i; 110 } 111 112 /** 113 * Create an Intent to open a message. 114 */ 115 public static Intent createOpenMessageIntent(Context context, long accountId, 116 long mailboxId, long messageId) { 117 Intent i = Utility.createRestartAppIntent(context, Welcome.class); 118 if (accountId != -1) { 119 i.putExtra(EXTRA_ACCOUNT_ID, accountId); 120 i.putExtra(EXTRA_MAILBOX_ID, mailboxId); 121 i.putExtra(EXTRA_MESSAGE_ID, messageId); 122 } 123 return i; 124 } 125 126 /** 127 * Open account's inbox. 128 */ 129 public static void actionOpenAccountInbox(Activity fromActivity, long accountId) { 130 fromActivity.startActivity(createOpenAccountInboxIntent(fromActivity, accountId)); 131 } 132 133 /** 134 * Parse the {@link #EXTRA_DEBUG_PANE_MODE} extra and return 1 or 2, if it's set to "1" or "2". 135 * Return 0 otherwise. 136 */ 137 private static int getDebugPaneMode(Intent i) { 138 Bundle extras = i.getExtras(); 139 if (extras != null) { 140 String s = extras.getString(EXTRA_DEBUG_PANE_MODE); 141 if ("1".equals(s)) { 142 return 1; 143 } else if ("2".equals(s)) { 144 return 2; 145 } 146 } 147 return 0; 148 } 149 150 @Override 151 public void onCreate(Bundle icicle) { 152 super.onCreate(icicle); 153 ActivityHelper.debugSetWindowFlags(this); 154 155 // Reset the "accounts changed" notification, now that we're here 156 Email.setNotifyUiAccountsChanged(false); 157 158 // Quickly check for bulk upgrades (from older app versions) and switch to the 159 // upgrade activity if necessary 160 if (UpgradeAccounts.doBulkUpgradeIfNecessary(this)) { 161 finish(); 162 return; 163 } 164 165 // Restore accounts, if it has not happened already 166 // NOTE: This is blocking, which it should not be (in the UI thread) 167 // We're going to live with this for the short term and replace with something 168 // smarter. Long-term fix: Move this, and most of the code below, to an AsyncTask 169 // and do the DB work in a thread. Then post handler to finish() as appropriate. 170 AccountBackupRestore.restoreAccountsIfNeeded(this); 171 172 // Because the app could be reloaded (for debugging, etc.), we need to make sure that 173 // ExchangeService gets a chance to start. There is no harm to starting it if it has 174 // already been started 175 // When the service starts, it reconciles EAS accounts. 176 // TODO More completely separate ExchangeService from Email app 177 ExchangeUtils.startExchangeService(this); 178 179 final long accountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1); 180 final long mailboxId = getIntent().getLongExtra(EXTRA_MAILBOX_ID, -1); 181 final long messageId = getIntent().getLongExtra(EXTRA_MESSAGE_ID, -1); 182 final int debugPaneMode = getDebugPaneMode(getIntent()); 183 new MainActivityLauncher(this, accountId, mailboxId, messageId, debugPaneMode).execute(); 184 } 185 186 @Override 187 public void onDestroy() { 188 super.onDestroy(); 189 } 190 191 /** 192 * Open an account with the Activity appropriate to the current configuration. 193 * If there's no accounts set up, open the "add account" screen. 194 * 195 * if {@code account} is -1, open the default account. 196 */ 197 private static class MainActivityLauncher extends AsyncTask<Void, Void, Void> { 198 private final Activity mFromActivity; 199 private final int mDebugPaneMode; 200 private final long mAccountId; 201 private final long mMailboxId; 202 private final long mMessageId; 203 204 public MainActivityLauncher(Activity fromActivity, long accountId, long mailboxId, 205 long messageId, int debugPaneMode) { 206 mFromActivity = fromActivity; 207 mAccountId = accountId; 208 mMailboxId = mailboxId; 209 mMessageId = messageId; 210 mDebugPaneMode = debugPaneMode; 211 } 212 213 private boolean isMailboxSelected() { 214 return mMailboxId != -1; 215 } 216 217 private boolean isMessageSelected() { 218 return mMessageId != -1; 219 } 220 221 @Override 222 protected Void doInBackground(Void... params) { 223 // Reconcile POP/IMAP accounts. EAS accounts are taken care of by ExchangeService. 224 MailService.reconcilePopImapAccountsSync(mFromActivity); 225 226 final int numAccount = 227 EmailContent.count(mFromActivity, EmailContent.Account.CONTENT_URI); 228 if (numAccount == 0) { 229 AccountSetupBasics.actionNewAccount(mFromActivity); 230 } else { 231 long accountId = mAccountId; 232 if (accountId == -1 || !Account.isValidId(mFromActivity, accountId)) { 233 accountId = EmailContent.Account.getDefaultAccountId(mFromActivity); 234 } 235 236 final boolean useTwoPane = (mDebugPaneMode == 2) 237 || (useTwoPane(mFromActivity) && mDebugPaneMode == 0); 238 239 if (useTwoPane) { 240 if (isMessageSelected()) { 241 MessageListXL.actionOpenMessage(mFromActivity, accountId, mMailboxId, 242 mMessageId); 243 } else if (isMailboxSelected()) { 244 MessageListXL.actionOpenMailbox(mFromActivity, accountId, mMailboxId); 245 } else { 246 MessageListXL.actionOpenAccount(mFromActivity, accountId); 247 } 248 } else { 249 if (isMessageSelected()) { 250 MessageView.actionView(mFromActivity, mMessageId, mMailboxId); 251 } else if (isMailboxSelected()) { 252 MessageList.actionHandleMailbox(mFromActivity, mMailboxId); 253 } else { 254 MessageList.actionHandleAccount( 255 mFromActivity, accountId, Mailbox.TYPE_INBOX); 256 } 257 } 258 } 259 return null; 260 } 261 262 @Override 263 protected void onPostExecute(Void result) { 264 mFromActivity.finish(); 265 } 266 } 267} 268