The Android Open Source Project | 96c5af4 | 2009-03-03 19:32:22 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.email.activity; |
| 18 | |
Ben Komalo | d8ab562 | 2011-07-05 11:41:11 -0400 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.net.Uri; |
| 23 | import android.os.Bundle; |
| 24 | import android.text.TextUtils; |
| 25 | import android.util.Log; |
| 26 | import android.view.LayoutInflater; |
Ben Komalo | 74acf8d | 2011-07-25 11:53:44 -0700 | [diff] [blame] | 27 | import android.view.Menu; |
| 28 | import android.view.MenuItem; |
Ben Komalo | d8ab562 | 2011-07-05 11:41:11 -0400 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.view.ViewGroup.LayoutParams; |
| 31 | |
Andrew Stadler | 5e354cd | 2010-04-14 09:28:04 -0700 | [diff] [blame] | 32 | import com.android.email.Email; |
Ben Komalo | 47bf70c | 2011-10-10 15:53:27 -0700 | [diff] [blame] | 33 | import com.android.email.Preferences; |
Makoto Onuki | c1ca222 | 2011-05-19 16:24:47 -0700 | [diff] [blame] | 34 | import com.android.email.R; |
Ben Komalo | 74acf8d | 2011-07-25 11:53:44 -0700 | [diff] [blame] | 35 | import com.android.email.activity.setup.AccountSettings; |
Andrew Stadler | 4e4abc6 | 2009-07-21 11:06:14 -0700 | [diff] [blame] | 36 | import com.android.email.activity.setup.AccountSetupBasics; |
Marc Blank | 66a47b8 | 2011-06-27 12:12:41 -0700 | [diff] [blame] | 37 | import com.android.email.service.EmailServiceUtils; |
Marc Blank | e6cc662 | 2010-07-08 10:08:47 -0700 | [diff] [blame] | 38 | import com.android.email.service.MailService; |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 39 | import com.android.emailcommon.Logging; |
Marc Blank | f5418f1 | 2011-06-13 15:32:27 -0700 | [diff] [blame] | 40 | import com.android.emailcommon.provider.Account; |
Marc Blank | a7bc031 | 2011-02-10 10:26:56 -0800 | [diff] [blame] | 41 | import com.android.emailcommon.provider.EmailContent; |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 42 | import com.android.emailcommon.provider.EmailContent.Message; |
| 43 | import com.android.emailcommon.provider.Mailbox; |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 44 | import com.android.emailcommon.provider.Policy; |
Makoto Onuki | af6079c | 2011-03-28 15:43:50 -0700 | [diff] [blame] | 45 | import com.android.emailcommon.utility.EmailAsyncTask; |
Ben Komalo | 0e6a521 | 2011-08-08 14:39:19 -0700 | [diff] [blame] | 46 | import com.android.emailcommon.utility.IntentUtilities; |
Makoto Onuki | c1ca222 | 2011-05-19 16:24:47 -0700 | [diff] [blame] | 47 | import com.android.emailcommon.utility.Utility; |
Makoto Onuki | f5492ea | 2011-05-09 14:31:11 -0700 | [diff] [blame] | 48 | import com.google.common.annotations.VisibleForTesting; |
The Android Open Source Project | 96c5af4 | 2009-03-03 19:32:22 -0800 | [diff] [blame] | 49 | |
The Android Open Source Project | 96c5af4 | 2009-03-03 19:32:22 -0800 | [diff] [blame] | 50 | /** |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 51 | * The Welcome activity initializes the application and starts {@link EmailActivity}, or launch |
| 52 | * {@link AccountSetupBasics} if no accounts are configured. |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 53 | * |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 54 | * TOOD Show "your messages are on the way" message like gmail does during the inbox lookup. |
The Android Open Source Project | 96c5af4 | 2009-03-03 19:32:22 -0800 | [diff] [blame] | 55 | */ |
| 56 | public class Welcome extends Activity { |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Commands for testing... |
| 59 | * Open 1 pane |
| 60 | adb shell am start -a android.intent.action.MAIN \ |
Makoto Onuki | 308ce92 | 2011-03-21 17:08:16 -0700 | [diff] [blame] | 61 | -d '"content://ui.email.android.com/view/mailbox"' \ |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 62 | -e DEBUG_PANE_MODE 1 |
| 63 | |
| 64 | * Open 2 pane |
| 65 | adb shell am start -a android.intent.action.MAIN \ |
Makoto Onuki | 308ce92 | 2011-03-21 17:08:16 -0700 | [diff] [blame] | 66 | -d '"content://ui.email.android.com/view/mailbox"' \ |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 67 | -e DEBUG_PANE_MODE 2 |
| 68 | |
Makoto Onuki | 308ce92 | 2011-03-21 17:08:16 -0700 | [diff] [blame] | 69 | * Open an account (ID=1) in 2 pane |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 70 | adb shell am start -a android.intent.action.MAIN \ |
Makoto Onuki | 308ce92 | 2011-03-21 17:08:16 -0700 | [diff] [blame] | 71 | -d '"content://ui.email.android.com/view/mailbox?ACCOUNT_ID=1"' \ |
| 72 | -e DEBUG_PANE_MODE 2 |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 73 | |
Makoto Onuki | 6a858c1 | 2010-12-03 16:28:25 -0800 | [diff] [blame] | 74 | * Open a message (account id=1, mailbox id=2, message id=3) |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 75 | adb shell am start -a android.intent.action.MAIN \ |
Makoto Onuki | 308ce92 | 2011-03-21 17:08:16 -0700 | [diff] [blame] | 76 | -d '"content://ui.email.android.com/view/mailbox?ACCOUNT_ID=1&MAILBOX_ID=2&MESSAGE_ID=3"' \ |
Makoto Onuki | 844bf74 | 2011-05-31 19:14:11 -0700 | [diff] [blame] | 77 | -e DEBUG_PANE_MODE 2 |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 78 | |
Makoto Onuki | 844bf74 | 2011-05-31 19:14:11 -0700 | [diff] [blame] | 79 | * Open the combined starred on the combined view |
| 80 | adb shell am start -a android.intent.action.MAIN \ |
| 81 | -d '"content://ui.email.android.com/view/mailbox?ACCOUNT_ID=1152921504606846976&MAILBOX_ID=-4"' \ |
| 82 | -e DEBUG_PANE_MODE 2 |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 83 | */ |
Andrew Stadler | 4e4abc6 | 2009-07-21 11:06:14 -0700 | [diff] [blame] | 84 | |
Makoto Onuki | d111cc7 | 2010-08-17 11:59:10 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Extra for debugging. Set 1 to force one-pane. Set 2 to force two-pane. |
| 87 | */ |
| 88 | private static final String EXTRA_DEBUG_PANE_MODE = "DEBUG_PANE_MODE"; |
| 89 | |
Makoto Onuki | 308ce92 | 2011-03-21 17:08:16 -0700 | [diff] [blame] | 90 | private static final String VIEW_MAILBOX_INTENT_URL_PATH = "/view/mailbox"; |
Marc Blank | e6cc662 | 2010-07-08 10:08:47 -0700 | [diff] [blame] | 91 | |
Makoto Onuki | af6079c | 2011-03-28 15:43:50 -0700 | [diff] [blame] | 92 | private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker(); |
| 93 | |
Makoto Onuki | b8b560f | 2011-06-21 17:14:43 -0700 | [diff] [blame] | 94 | private View mWaitingForSyncView; |
| 95 | |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 96 | private long mAccountId; |
| 97 | private long mMailboxId; |
| 98 | private long mMessageId; |
| 99 | private String mAccountUuid; |
| 100 | |
| 101 | private MailboxFinder mInboxFinder; |
| 102 | |
Andrew Stadler | 79ef52c | 2010-04-14 19:16:46 -0700 | [diff] [blame] | 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 | */ |
Marc Blank | f1efd65 | 2009-12-18 09:18:55 -0800 | [diff] [blame] | 108 | public static void actionStart(Activity fromActivity) { |
Makoto Onuki | 308ce92 | 2011-03-21 17:08:16 -0700 | [diff] [blame] | 109 | Intent i = IntentUtilities.createRestartAppIntent(fromActivity, Welcome.class); |
Marc Blank | f1efd65 | 2009-12-18 09:18:55 -0800 | [diff] [blame] | 110 | fromActivity.startActivity(i); |
| 111 | } |
| 112 | |
Makoto Onuki | b98f219 | 2010-08-09 16:17:53 -0700 | [diff] [blame] | 113 | /** |
Todd Kennedy | 6c5ee59 | 2011-01-25 16:09:16 -0800 | [diff] [blame] | 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. |
Makoto Onuki | b98f219 | 2010-08-09 16:17:53 -0700 | [diff] [blame] | 116 | */ |
Makoto Onuki | 0be2d85 | 2010-08-24 19:09:34 -0700 | [diff] [blame] | 117 | public static Intent createOpenAccountInboxIntent(Context context, long accountId) { |
Makoto Onuki | 308ce92 | 2011-03-21 17:08:16 -0700 | [diff] [blame] | 118 | final Uri.Builder b = IntentUtilities.createActivityIntentUrlBuilder( |
| 119 | VIEW_MAILBOX_INTENT_URL_PATH); |
| 120 | IntentUtilities.setAccountId(b, accountId); |
| 121 | return IntentUtilities.createRestartAppIntent(b.build()); |
Makoto Onuki | b98f219 | 2010-08-09 16:17:53 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
Makoto Onuki | 6a858c1 | 2010-12-03 16:28:25 -0800 | [diff] [blame] | 125 | * Create an Intent to open a message. |
| 126 | */ |
| 127 | public static Intent createOpenMessageIntent(Context context, long accountId, |
| 128 | long mailboxId, long messageId) { |
Makoto Onuki | 308ce92 | 2011-03-21 17:08:16 -0700 | [diff] [blame] | 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 | return IntentUtilities.createRestartAppIntent(b.build()); |
Makoto Onuki | 6a858c1 | 2010-12-03 16:28:25 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
Makoto Onuki | b98f219 | 2010-08-09 16:17:53 -0700 | [diff] [blame] | 138 | * Open account's inbox. |
| 139 | */ |
| 140 | public static void actionOpenAccountInbox(Activity fromActivity, long accountId) { |
| 141 | fromActivity.startActivity(createOpenAccountInboxIntent(fromActivity, accountId)); |
| 142 | } |
| 143 | |
Makoto Onuki | d111cc7 | 2010-08-17 11:59:10 -0700 | [diff] [blame] | 144 | /** |
Makoto Onuki | 0b6cb1c | 2011-04-04 12:00:55 -0700 | [diff] [blame] | 145 | * Create an {@link Intent} for account shortcuts. The returned intent stores the account's |
| 146 | * UUID rather than the account ID, which will be changed after account restore. |
| 147 | */ |
Todd Kennedy | 5675ea8 | 2011-05-24 09:20:14 -0700 | [diff] [blame] | 148 | public static Intent createAccountShortcutIntent(Context context, String uuid, long mailboxId) { |
Makoto Onuki | 0b6cb1c | 2011-04-04 12:00:55 -0700 | [diff] [blame] | 149 | final Uri.Builder b = IntentUtilities.createActivityIntentUrlBuilder( |
| 150 | VIEW_MAILBOX_INTENT_URL_PATH); |
Todd Kennedy | 5675ea8 | 2011-05-24 09:20:14 -0700 | [diff] [blame] | 151 | IntentUtilities.setAccountUuid(b, uuid); |
| 152 | IntentUtilities.setMailboxId(b, mailboxId); |
Makoto Onuki | 0b6cb1c | 2011-04-04 12:00:55 -0700 | [diff] [blame] | 153 | return IntentUtilities.createRestartAppIntent(b.build()); |
| 154 | } |
| 155 | |
| 156 | /** |
Makoto Onuki | 347ae23 | 2011-06-07 13:41:42 -0700 | [diff] [blame] | 157 | * If the {@link #EXTRA_DEBUG_PANE_MODE} extra is "1" or "2", return 1 or 2 respectively. |
| 158 | * Otherwise return 0. |
| 159 | * |
| 160 | * @see UiUtilities#setDebugPaneMode(int) |
| 161 | * @see UiUtilities#useTwoPane(Context) |
Makoto Onuki | d111cc7 | 2010-08-17 11:59:10 -0700 | [diff] [blame] | 162 | */ |
| 163 | private static int getDebugPaneMode(Intent i) { |
| 164 | Bundle extras = i.getExtras(); |
| 165 | if (extras != null) { |
| 166 | String s = extras.getString(EXTRA_DEBUG_PANE_MODE); |
| 167 | if ("1".equals(s)) { |
| 168 | return 1; |
| 169 | } else if ("2".equals(s)) { |
| 170 | return 2; |
| 171 | } |
| 172 | } |
| 173 | return 0; |
| 174 | } |
| 175 | |
The Android Open Source Project | 96c5af4 | 2009-03-03 19:32:22 -0800 | [diff] [blame] | 176 | @Override |
| 177 | public void onCreate(Bundle icicle) { |
| 178 | super.onCreate(icicle); |
Andrew Stadler | cd09545 | 2010-11-01 16:15:15 -0700 | [diff] [blame] | 179 | ActivityHelper.debugSetWindowFlags(this); |
The Android Open Source Project | 96c5af4 | 2009-03-03 19:32:22 -0800 | [diff] [blame] | 180 | |
Marc Blank | e59c872 | 2009-09-14 08:45:43 -0700 | [diff] [blame] | 181 | // Because the app could be reloaded (for debugging, etc.), we need to make sure that |
Marc Blank | 64b64cc | 2010-08-27 11:31:37 -0700 | [diff] [blame] | 182 | // ExchangeService gets a chance to start. There is no harm to starting it if it has |
| 183 | // already been started |
Makoto Onuki | c50b6f6 | 2011-01-14 12:00:17 -0800 | [diff] [blame] | 184 | // When the service starts, it reconciles EAS accounts. |
Marc Blank | 64b64cc | 2010-08-27 11:31:37 -0700 | [diff] [blame] | 185 | // TODO More completely separate ExchangeService from Email app |
Marc Blank | 66a47b8 | 2011-06-27 12:12:41 -0700 | [diff] [blame] | 186 | EmailServiceUtils.startExchangeService(this); |
Marc Blank | e59c872 | 2009-09-14 08:45:43 -0700 | [diff] [blame] | 187 | |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 188 | // Extract parameters from the intent. |
| 189 | final Intent intent = getIntent(); |
| 190 | mAccountId = IntentUtilities.getAccountIdFromIntent(intent); |
| 191 | mMailboxId = IntentUtilities.getMailboxIdFromIntent(intent); |
| 192 | mMessageId = IntentUtilities.getMessageIdFromIntent(intent); |
| 193 | mAccountUuid = IntentUtilities.getAccountUuidFromIntent(intent); |
| 194 | UiUtilities.setDebugPaneMode(getDebugPaneMode(intent)); |
| 195 | |
Ben Komalo | 002a180 | 2011-09-15 15:18:26 -0700 | [diff] [blame] | 196 | // Reconcile POP/IMAP accounts. EAS accounts are taken care of by ExchangeService. |
| 197 | if (MailService.hasMismatchInPopImapAccounts(this)) { |
| 198 | EmailAsyncTask.runAsyncParallel(new Runnable() { |
| 199 | @Override |
| 200 | public void run() { |
| 201 | // Reconciling can be heavy - so do it in the background. |
| 202 | MailService.reconcilePopImapAccountsSync(Welcome.this); |
| 203 | resolveAccount(); |
| 204 | } |
| 205 | }); |
| 206 | } else { |
| 207 | resolveAccount(); |
| 208 | } |
Ben Komalo | d8ab562 | 2011-07-05 11:41:11 -0400 | [diff] [blame] | 209 | |
| 210 | // Reset the "accounts changed" notification, now that we're here |
| 211 | Email.setNotifyUiAccountsChanged(false); |
The Android Open Source Project | 96c5af4 | 2009-03-03 19:32:22 -0800 | [diff] [blame] | 212 | } |
Marc Blank | e6cc662 | 2010-07-08 10:08:47 -0700 | [diff] [blame] | 213 | |
| 214 | @Override |
Ben Komalo | 74acf8d | 2011-07-25 11:53:44 -0700 | [diff] [blame] | 215 | public boolean onCreateOptionsMenu(Menu menu) { |
| 216 | // Only create the menu if we had to stop and show a loading spinner - otherwise |
| 217 | // this is a transient activity with no UI. |
| 218 | if (mInboxFinder == null) { |
| 219 | return super.onCreateOptionsMenu(menu); |
| 220 | } |
| 221 | |
| 222 | getMenuInflater().inflate(R.menu.welcome, menu); |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public boolean onOptionsItemSelected(MenuItem item) { |
| 228 | if (item.getItemId() == R.id.account_settings) { |
| 229 | AccountSettings.actionSettings(this, mAccountId); |
| 230 | return true; |
| 231 | } |
| 232 | return super.onOptionsItemSelected(item); |
| 233 | } |
| 234 | |
| 235 | @Override |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 236 | protected void onStop() { |
Makoto Onuki | b8b560f | 2011-06-21 17:14:43 -0700 | [diff] [blame] | 237 | // Cancel all running tasks. |
| 238 | // (If it's stopping for configuration changes, we just re-do everything on the new |
| 239 | // instance) |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 240 | stopInboxLookup(); |
Makoto Onuki | af6079c | 2011-03-28 15:43:50 -0700 | [diff] [blame] | 241 | mTaskTracker.cancellAllInterrupt(); |
Makoto Onuki | b8b560f | 2011-06-21 17:14:43 -0700 | [diff] [blame] | 242 | |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 243 | super.onStop(); |
Makoto Onuki | b8b560f | 2011-06-21 17:14:43 -0700 | [diff] [blame] | 244 | |
| 245 | if (!isChangingConfigurations()) { |
| 246 | // This means the user opened some other app. |
| 247 | // Just close self and not launch EmailActivity. |
| 248 | if (Email.DEBUG && Logging.DEBUG_LIFECYCLE) { |
| 249 | Log.d(Logging.LOG_TAG, "Welcome: Closing self..."); |
| 250 | } |
| 251 | finish(); |
| 252 | } |
Marc Blank | e6cc662 | 2010-07-08 10:08:47 -0700 | [diff] [blame] | 253 | } |
Makoto Onuki | 191448b | 2010-08-04 15:38:25 -0700 | [diff] [blame] | 254 | |
| 255 | /** |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 256 | * {@inheritDoc} |
Makoto Onuki | 191448b | 2010-08-04 15:38:25 -0700 | [diff] [blame] | 257 | * |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 258 | * When launching an activity from {@link Welcome}, we always want to set |
| 259 | * {@link Intent#FLAG_ACTIVITY_FORWARD_RESULT}. |
| 260 | */ |
| 261 | @Override |
| 262 | public void startActivity(Intent intent) { |
| 263 | intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); |
| 264 | super.startActivity(intent); |
| 265 | } |
| 266 | |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Stop inbox lookup. This MSUT be called on the UI thread. |
| 269 | */ |
| 270 | private void stopInboxLookup() { |
| 271 | if (mInboxFinder != null) { |
| 272 | mInboxFinder.cancel(); |
| 273 | mInboxFinder = null; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Start inbox lookup. This MSUT be called on the UI thread. |
| 279 | */ |
| 280 | private void startInboxLookup() { |
| 281 | Log.i(Logging.LOG_TAG, "Inbox not found. Starting mailbox finder..."); |
| 282 | stopInboxLookup(); // Stop if already running -- it shouldn't be but just in case. |
| 283 | mInboxFinder = new MailboxFinder(this, mAccountId, Mailbox.TYPE_INBOX, |
| 284 | mMailboxFinderCallback); |
| 285 | mInboxFinder.startLookup(); |
Makoto Onuki | b8b560f | 2011-06-21 17:14:43 -0700 | [diff] [blame] | 286 | |
Ben Komalo | d8ab562 | 2011-07-05 11:41:11 -0400 | [diff] [blame] | 287 | // Show "your email will appear shortly" message. |
| 288 | mWaitingForSyncView = LayoutInflater.from(this).inflate( |
| 289 | R.layout.waiting_for_sync_message, null); |
| 290 | addContentView(mWaitingForSyncView, new LayoutParams( |
| 291 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); |
Ben Komalo | 74acf8d | 2011-07-25 11:53:44 -0700 | [diff] [blame] | 292 | invalidateOptionsMenu(); |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Determine which account to open with the given account ID and UUID. |
| 297 | * |
| 298 | * @return ID of the account to use. |
Makoto Onuki | 191448b | 2010-08-04 15:38:25 -0700 | [diff] [blame] | 299 | */ |
Makoto Onuki | f5492ea | 2011-05-09 14:31:11 -0700 | [diff] [blame] | 300 | @VisibleForTesting |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 301 | static long resolveAccountId(Context context, long inputAccountId, String inputUuid) { |
| 302 | final long accountId; |
Makoto Onuki | 191448b | 2010-08-04 15:38:25 -0700 | [diff] [blame] | 303 | |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 304 | if (!TextUtils.isEmpty(inputUuid)) { |
| 305 | // If a UUID is specified, try to use it. |
| 306 | // If the UUID is invalid, accountId will be NO_ACCOUNT. |
| 307 | accountId = Account.getAccountIdFromUuid(context, inputUuid); |
Makoto Onuki | 0b6cb1c | 2011-04-04 12:00:55 -0700 | [diff] [blame] | 308 | |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 309 | } else if (inputAccountId != Account.NO_ACCOUNT) { |
| 310 | // If a valid account ID is specified, just use it. |
| 311 | if (inputAccountId == Account.ACCOUNT_ID_COMBINED_VIEW |
| 312 | || Account.isValidId(context, inputAccountId)) { |
| 313 | accountId = inputAccountId; |
Makoto Onuki | c1ca222 | 2011-05-19 16:24:47 -0700 | [diff] [blame] | 314 | } else { |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 315 | accountId = Account.NO_ACCOUNT; |
Makoto Onuki | 0b6cb1c | 2011-04-04 12:00:55 -0700 | [diff] [blame] | 316 | } |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 317 | } else { |
| 318 | // Neither an accountID or a UUID is specified. |
Ben Komalo | 47bf70c | 2011-10-10 15:53:27 -0700 | [diff] [blame] | 319 | // Use the last account used, falling back to the default. |
| 320 | long lastUsedId = Preferences.getPreferences(context).getLastUsedAccountId(); |
| 321 | if (lastUsedId != Account.NO_ACCOUNT) { |
| 322 | if (!Account.isValidId(context, lastUsedId)) { |
| 323 | // The last account that was used has since been deleted. |
| 324 | lastUsedId = Account.NO_ACCOUNT; |
| 325 | Preferences.getPreferences(context).setLastUsedAccountId(Account.NO_ACCOUNT); |
| 326 | } |
| 327 | } |
| 328 | accountId = (lastUsedId == Account.NO_ACCOUNT) |
| 329 | ? Account.getDefaultAccountId(context) |
| 330 | : lastUsedId; |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 331 | } |
| 332 | if (accountId != Account.NO_ACCOUNT) { |
| 333 | // Okay, the given account is valid. |
| 334 | return accountId; |
| 335 | } else { |
| 336 | // No, it's invalid. Show the warning toast and use the default. |
| 337 | Utility.showToast(context, R.string.toast_account_not_found); |
| 338 | return Account.getDefaultAccountId(context); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Determine which account to use according to the number of accounts already set up, |
| 344 | * {@link #mAccountId} and {@link #mAccountUuid}. |
| 345 | * |
| 346 | * <pre> |
| 347 | * 1. If there's no account configured, start account setup. |
| 348 | * 2. Otherwise detemine which account to open with {@link #resolveAccountId} and |
| 349 | * 2a. If the account doesn't have inbox yet, start inbox finder. |
| 350 | * 2b. Otherwise open the main activity. |
| 351 | * </pre> |
| 352 | */ |
Ben Komalo | 002a180 | 2011-09-15 15:18:26 -0700 | [diff] [blame] | 353 | private void resolveAccount() { |
| 354 | final int numAccount = EmailContent.count(this, Account.CONTENT_URI); |
Ben Komalo | 002a180 | 2011-09-15 15:18:26 -0700 | [diff] [blame] | 355 | if (numAccount == 0) { |
| 356 | AccountSetupBasics.actionNewAccount(this); |
| 357 | finish(); |
Marc Blank | 2fa3017 | 2011-09-20 17:36:17 -0700 | [diff] [blame] | 358 | return; |
Ben Komalo | 002a180 | 2011-09-15 15:18:26 -0700 | [diff] [blame] | 359 | } else { |
| 360 | mAccountId = resolveAccountId(this, mAccountId, mAccountUuid); |
| 361 | if (Account.isNormalAccount(mAccountId) && |
| 362 | Mailbox.findMailboxOfType(this, mAccountId, Mailbox.TYPE_INBOX) |
| 363 | == Mailbox.NO_MAILBOX) { |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 364 | startInboxLookup(); |
Ben Komalo | 002a180 | 2011-09-15 15:18:26 -0700 | [diff] [blame] | 365 | return; |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 366 | } |
Makoto Onuki | 191448b | 2010-08-04 15:38:25 -0700 | [diff] [blame] | 367 | } |
Ben Komalo | 002a180 | 2011-09-15 15:18:26 -0700 | [diff] [blame] | 368 | startEmailActivity(); |
Makoto Onuki | 191448b | 2010-08-04 15:38:25 -0700 | [diff] [blame] | 369 | } |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * Start {@link EmailActivity} using {@link #mAccountId}, {@link #mMailboxId} and |
| 373 | * {@link #mMessageId}. |
| 374 | */ |
| 375 | private void startEmailActivity() { |
| 376 | final Intent i; |
| 377 | if (mMessageId != Message.NO_MESSAGE) { |
| 378 | i = EmailActivity.createOpenMessageIntent(this, mAccountId, mMailboxId, mMessageId); |
| 379 | } else if (mMailboxId != Mailbox.NO_MAILBOX) { |
| 380 | i = EmailActivity.createOpenMailboxIntent(this, mAccountId, mMailboxId); |
| 381 | } else { |
| 382 | i = EmailActivity.createOpenAccountIntent(this, mAccountId); |
| 383 | } |
| 384 | startActivity(i); |
| 385 | finish(); |
| 386 | } |
| 387 | |
| 388 | private final MailboxFinder.Callback mMailboxFinderCallback = new MailboxFinder.Callback() { |
| 389 | // This MUST be called from callback methods. |
| 390 | private void cleanUp() { |
| 391 | mInboxFinder = null; |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public void onAccountNotFound() { |
| 396 | cleanUp(); |
| 397 | // Account removed? Clear the IDs and restart the task. Which will result in either |
| 398 | // a) show account setup if there's really no accounts or b) open the default account. |
| 399 | |
| 400 | mAccountId = Account.NO_ACCOUNT; |
| 401 | mMailboxId = Mailbox.NO_MAILBOX; |
| 402 | mMessageId = Message.NO_MESSAGE; |
| 403 | mAccountUuid = null; |
| 404 | |
Ben Komalo | 002a180 | 2011-09-15 15:18:26 -0700 | [diff] [blame] | 405 | // Restart the account resolution. |
| 406 | resolveAccount(); |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public void onMailboxNotFound(long accountId) { |
| 411 | // Just do the same thing as "account not found". |
| 412 | onAccountNotFound(); |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public void onAccountSecurityHold(long accountId) { |
| 417 | cleanUp(); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 418 | // If we can't find the account, we know what to do |
| 419 | Account account = Account.restoreAccountWithId(Welcome.this, accountId); |
| 420 | if (account == null) { |
| 421 | onAccountNotFound(); |
| 422 | return; |
| 423 | } |
| 424 | // If there's no policy or it's "unsupported", act like the account doesn't exist |
| 425 | Policy policy = Policy.restorePolicyWithId(Welcome.this, account.mPolicyKey); |
| 426 | if (policy == null || (policy.mProtocolPoliciesUnsupported != null)) { |
| 427 | onAccountNotFound(); |
| 428 | return; |
| 429 | } |
| 430 | // Otherwise, try advancing security |
Makoto Onuki | 1ce33fd | 2011-06-23 16:32:34 -0700 | [diff] [blame] | 431 | ActivityHelper.showSecurityHoldDialog(Welcome.this, accountId); |
Makoto Onuki | 95ad0f0 | 2011-06-20 21:02:15 -0700 | [diff] [blame] | 432 | finish(); |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void onMailboxFound(long accountId, long mailboxId) { |
| 437 | cleanUp(); |
| 438 | |
| 439 | // Okay the account has Inbox now. Start the main activity. |
| 440 | startEmailActivity(); |
| 441 | } |
| 442 | }; |
The Android Open Source Project | 96c5af4 | 2009-03-03 19:32:22 -0800 | [diff] [blame] | 443 | } |