Welcome.java revision 9f976e29a0406a51dd7e15778625060d3e5e7031
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.R; 23import com.android.email.activity.setup.AccountSetupBasics; 24import com.android.email.service.MailService; 25import com.android.emailcommon.provider.EmailContent; 26import com.android.emailcommon.provider.EmailContent.Account; 27import com.android.emailcommon.provider.EmailContent.Mailbox; 28import com.android.emailcommon.utility.Utility; 29 30import android.app.Activity; 31import android.content.Context; 32import android.content.Intent; 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 return context.getResources().getBoolean(R.bool.use_two_pane); 88 } 89 90 /** 91 * Launch this activity. Note: It's assumed that this activity is only called as a means to 92 * 'reset' the UI state; Because of this, it is always launched with FLAG_ACTIVITY_CLEAR_TOP, 93 * which will drop any other activities on the stack (e.g. AccountFolderList or MessageList). 94 */ 95 public static void actionStart(Activity fromActivity) { 96 Intent i = Utility.createRestartAppIntent(fromActivity, Welcome.class); 97 fromActivity.startActivity(i); 98 } 99 100 /** 101 * Create an Intent to open email activity. If <code>accountId</code> is not -1, the 102 * specified account will be automatically be opened when the activity starts. 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 // Restore accounts, if it has not happened already 159 // NOTE: This is blocking, which it should not be (in the UI thread) 160 // We're going to live with this for the short term and replace with something 161 // smarter. Long-term fix: Move this, and most of the code below, to an AsyncTask 162 // and do the DB work in a thread. Then post handler to finish() as appropriate. 163 AccountBackupRestore.restoreAccountsIfNeeded(this); 164 165 // Because the app could be reloaded (for debugging, etc.), we need to make sure that 166 // ExchangeService gets a chance to start. There is no harm to starting it if it has 167 // already been started 168 // When the service starts, it reconciles EAS accounts. 169 // TODO More completely separate ExchangeService from Email app 170 ExchangeUtils.startExchangeService(this); 171 172 final long accountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1); 173 final long mailboxId = getIntent().getLongExtra(EXTRA_MAILBOX_ID, -1); 174 final long messageId = getIntent().getLongExtra(EXTRA_MESSAGE_ID, -1); 175 final int debugPaneMode = getDebugPaneMode(getIntent()); 176 new MainActivityLauncher(this, accountId, mailboxId, messageId, debugPaneMode).execute(); 177 } 178 179 @Override 180 public void onDestroy() { 181 super.onDestroy(); 182 } 183 184 /** 185 * Open an account with the Activity appropriate to the current configuration. 186 * If there's no accounts set up, open the "add account" screen. 187 * 188 * if {@code account} is -1, open the default account. 189 */ 190 private static class MainActivityLauncher extends AsyncTask<Void, Void, Void> { 191 private final Activity mFromActivity; 192 private final int mDebugPaneMode; 193 private final long mAccountId; 194 private final long mMailboxId; 195 private final long mMessageId; 196 197 public MainActivityLauncher(Activity fromActivity, long accountId, long mailboxId, 198 long messageId, int debugPaneMode) { 199 mFromActivity = fromActivity; 200 mAccountId = accountId; 201 mMailboxId = mailboxId; 202 mMessageId = messageId; 203 mDebugPaneMode = debugPaneMode; 204 } 205 206 private boolean isMailboxSelected() { 207 return mMailboxId != -1; 208 } 209 210 private boolean isMessageSelected() { 211 return mMessageId != -1; 212 } 213 214 @Override 215 protected Void doInBackground(Void... params) { 216 // Reconcile POP/IMAP accounts. EAS accounts are taken care of by ExchangeService. 217 MailService.reconcilePopImapAccountsSync(mFromActivity); 218 219 final int numAccount = 220 EmailContent.count(mFromActivity, EmailContent.Account.CONTENT_URI); 221 if (numAccount == 0) { 222 AccountSetupBasics.actionNewAccount(mFromActivity); 223 } else { 224 long accountId = mAccountId; 225 if (accountId == -1 || !Account.isValidId(mFromActivity, accountId)) { 226 accountId = EmailContent.Account.getDefaultAccountId(mFromActivity); 227 } 228 229 final boolean useTwoPane = (mDebugPaneMode == 2) 230 || (useTwoPane(mFromActivity) && mDebugPaneMode == 0); 231 232 if (useTwoPane) { 233 if (isMessageSelected()) { 234 MessageListXL.actionOpenMessage(mFromActivity, accountId, mMailboxId, 235 mMessageId); 236 } else if (isMailboxSelected()) { 237 MessageListXL.actionOpenMailbox(mFromActivity, accountId, mMailboxId); 238 } else { 239 MessageListXL.actionOpenAccount(mFromActivity, accountId); 240 } 241 } else { 242 if (isMessageSelected()) { 243 MessageView.actionView(mFromActivity, mMessageId, mMailboxId); 244 } else if (isMailboxSelected()) { 245 MessageList.actionHandleMailbox(mFromActivity, mMailboxId); 246 } else { 247 MessageList.actionHandleAccount( 248 mFromActivity, accountId, Mailbox.TYPE_INBOX); 249 } 250 } 251 } 252 return null; 253 } 254 255 @Override 256 protected void onPostExecute(Void result) { 257 mFromActivity.finish(); 258 } 259 } 260} 261