Welcome.java revision 31d9acbf0623872f9d4a2b3210b5970854b654c7
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.activity.setup.AccountSetupBasics; 23import com.android.email.service.MailService; 24import com.android.emailcommon.provider.EmailContent; 25import com.android.emailcommon.provider.EmailContent.Account; 26import com.android.emailcommon.provider.EmailContent.Mailbox; 27import com.android.emailcommon.utility.Utility; 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 email activity. If <code>accountId</code> is not -1, the 103 * specified account will be automatically be opened when the activity starts. 104 */ 105 public static Intent createOpenAccountInboxIntent(Context context, long accountId) { 106 Intent i = Utility.createRestartAppIntent(context, Welcome.class); 107 if (accountId != -1) { 108 i.putExtra(EXTRA_ACCOUNT_ID, accountId); 109 } 110 return i; 111 } 112 113 /** 114 * Create an Intent to open a message. 115 */ 116 public static Intent createOpenMessageIntent(Context context, long accountId, 117 long mailboxId, long messageId) { 118 Intent i = Utility.createRestartAppIntent(context, Welcome.class); 119 if (accountId != -1) { 120 i.putExtra(EXTRA_ACCOUNT_ID, accountId); 121 i.putExtra(EXTRA_MAILBOX_ID, mailboxId); 122 i.putExtra(EXTRA_MESSAGE_ID, messageId); 123 } 124 return i; 125 } 126 127 /** 128 * Open account's inbox. 129 */ 130 public static void actionOpenAccountInbox(Activity fromActivity, long accountId) { 131 fromActivity.startActivity(createOpenAccountInboxIntent(fromActivity, accountId)); 132 } 133 134 /** 135 * Parse the {@link #EXTRA_DEBUG_PANE_MODE} extra and return 1 or 2, if it's set to "1" or "2". 136 * Return 0 otherwise. 137 */ 138 private static int getDebugPaneMode(Intent i) { 139 Bundle extras = i.getExtras(); 140 if (extras != null) { 141 String s = extras.getString(EXTRA_DEBUG_PANE_MODE); 142 if ("1".equals(s)) { 143 return 1; 144 } else if ("2".equals(s)) { 145 return 2; 146 } 147 } 148 return 0; 149 } 150 151 @Override 152 public void onCreate(Bundle icicle) { 153 super.onCreate(icicle); 154 ActivityHelper.debugSetWindowFlags(this); 155 156 // Reset the "accounts changed" notification, now that we're here 157 Email.setNotifyUiAccountsChanged(false); 158 159 // Restore accounts, if it has not happened already 160 // NOTE: This is blocking, which it should not be (in the UI thread) 161 // We're going to live with this for the short term and replace with something 162 // smarter. Long-term fix: Move this, and most of the code below, to an AsyncTask 163 // and do the DB work in a thread. Then post handler to finish() as appropriate. 164 AccountBackupRestore.restoreAccountsIfNeeded(this); 165 166 // Because the app could be reloaded (for debugging, etc.), we need to make sure that 167 // ExchangeService gets a chance to start. There is no harm to starting it if it has 168 // already been started 169 // When the service starts, it reconciles EAS accounts. 170 // TODO More completely separate ExchangeService from Email app 171 ExchangeUtils.startExchangeService(this); 172 173 final long accountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1); 174 final long mailboxId = getIntent().getLongExtra(EXTRA_MAILBOX_ID, -1); 175 final long messageId = getIntent().getLongExtra(EXTRA_MESSAGE_ID, -1); 176 final int debugPaneMode = getDebugPaneMode(getIntent()); 177 new MainActivityLauncher(this, accountId, mailboxId, messageId, debugPaneMode).execute(); 178 } 179 180 @Override 181 public void onDestroy() { 182 super.onDestroy(); 183 } 184 185 /** 186 * Open an account with the Activity appropriate to the current configuration. 187 * If there's no accounts set up, open the "add account" screen. 188 * 189 * if {@code account} is -1, open the default account. 190 */ 191 private static class MainActivityLauncher extends AsyncTask<Void, Void, Void> { 192 private final Activity mFromActivity; 193 private final int mDebugPaneMode; 194 private final long mAccountId; 195 private final long mMailboxId; 196 private final long mMessageId; 197 198 public MainActivityLauncher(Activity fromActivity, long accountId, long mailboxId, 199 long messageId, int debugPaneMode) { 200 mFromActivity = fromActivity; 201 mAccountId = accountId; 202 mMailboxId = mailboxId; 203 mMessageId = messageId; 204 mDebugPaneMode = debugPaneMode; 205 } 206 207 private boolean isMailboxSelected() { 208 return mMailboxId != -1; 209 } 210 211 private boolean isMessageSelected() { 212 return mMessageId != -1; 213 } 214 215 @Override 216 protected Void doInBackground(Void... params) { 217 // Reconcile POP/IMAP accounts. EAS accounts are taken care of by ExchangeService. 218 MailService.reconcilePopImapAccountsSync(mFromActivity); 219 220 final int numAccount = 221 EmailContent.count(mFromActivity, EmailContent.Account.CONTENT_URI); 222 if (numAccount == 0) { 223 AccountSetupBasics.actionNewAccount(mFromActivity); 224 } else { 225 long accountId = mAccountId; 226 if (accountId == -1 || !Account.isValidId(mFromActivity, accountId)) { 227 accountId = EmailContent.Account.getDefaultAccountId(mFromActivity); 228 } 229 230 final boolean useTwoPane = (mDebugPaneMode == 2) 231 || (useTwoPane(mFromActivity) && mDebugPaneMode == 0); 232 233 if (useTwoPane) { 234 if (isMessageSelected()) { 235 MessageListXL.actionOpenMessage(mFromActivity, accountId, mMailboxId, 236 mMessageId); 237 } else if (isMailboxSelected()) { 238 MessageListXL.actionOpenMailbox(mFromActivity, accountId, mMailboxId); 239 } else { 240 MessageListXL.actionOpenAccount(mFromActivity, accountId); 241 } 242 } else { 243 if (isMessageSelected()) { 244 MessageView.actionView(mFromActivity, mMessageId, mMailboxId); 245 } else if (isMailboxSelected()) { 246 MessageList.actionHandleMailbox(mFromActivity, mMailboxId); 247 } else { 248 MessageList.actionHandleAccount( 249 mFromActivity, accountId, Mailbox.TYPE_INBOX); 250 } 251 } 252 } 253 return null; 254 } 255 256 @Override 257 protected void onPostExecute(Void result) { 258 mFromActivity.finish(); 259 } 260 } 261} 262