Welcome.java revision 6c5ee59c4fcd5cb2992a53234245df8faeaf8a5d
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 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        // Quickly check for bulk upgrades (from older app versions) and switch to the
160        // upgrade activity if necessary
161        if (UpgradeAccounts.doBulkUpgradeIfNecessary(this)) {
162            finish();
163            return;
164        }
165
166        // Restore accounts, if it has not happened already
167        // NOTE:  This is blocking, which it should not be (in the UI thread)
168        // We're going to live with this for the short term and replace with something
169        // smarter.  Long-term fix:  Move this, and most of the code below, to an AsyncTask
170        // and do the DB work in a thread.  Then post handler to finish() as appropriate.
171        AccountBackupRestore.restoreAccountsIfNeeded(this);
172
173        // Because the app could be reloaded (for debugging, etc.), we need to make sure that
174        // ExchangeService gets a chance to start.  There is no harm to starting it if it has
175        // already been started
176        // When the service starts, it reconciles EAS accounts.
177        // TODO More completely separate ExchangeService from Email app
178        ExchangeUtils.startExchangeService(this);
179
180        final long accountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1);
181        final long mailboxId = getIntent().getLongExtra(EXTRA_MAILBOX_ID, -1);
182        final long messageId = getIntent().getLongExtra(EXTRA_MESSAGE_ID, -1);
183        final int debugPaneMode = getDebugPaneMode(getIntent());
184        new MainActivityLauncher(this, accountId, mailboxId, messageId, debugPaneMode).execute();
185    }
186
187    @Override
188    public void onDestroy() {
189        super.onDestroy();
190    }
191
192    /**
193     * Open an account with the Activity appropriate to the current configuration.
194     * If there's no accounts set up, open the "add account" screen.
195     *
196     * if {@code account} is -1, open the default account.
197     */
198    private static class MainActivityLauncher extends AsyncTask<Void, Void, Void> {
199        private final Activity mFromActivity;
200        private final int mDebugPaneMode;
201        private final long mAccountId;
202        private final long mMailboxId;
203        private final long mMessageId;
204
205        public MainActivityLauncher(Activity fromActivity, long accountId, long mailboxId,
206                long messageId, int debugPaneMode) {
207            mFromActivity = fromActivity;
208            mAccountId = accountId;
209            mMailboxId = mailboxId;
210            mMessageId = messageId;
211            mDebugPaneMode = debugPaneMode;
212        }
213
214        private boolean isMailboxSelected() {
215            return mMailboxId != -1;
216        }
217
218        private boolean isMessageSelected() {
219            return mMessageId != -1;
220        }
221
222        @Override
223        protected Void doInBackground(Void... params) {
224            // Reconcile POP/IMAP accounts.  EAS accounts are taken care of by ExchangeService.
225            MailService.reconcilePopImapAccountsSync(mFromActivity);
226
227            final int numAccount =
228                    EmailContent.count(mFromActivity, EmailContent.Account.CONTENT_URI);
229            if (numAccount == 0) {
230                AccountSetupBasics.actionNewAccount(mFromActivity);
231            } else {
232                long accountId = mAccountId;
233                if (accountId == -1 || !Account.isValidId(mFromActivity, accountId)) {
234                    accountId = EmailContent.Account.getDefaultAccountId(mFromActivity);
235                }
236
237                final boolean useTwoPane = (mDebugPaneMode == 2)
238                        || (useTwoPane(mFromActivity) && mDebugPaneMode == 0);
239
240                if (useTwoPane) {
241                    if (isMessageSelected()) {
242                        MessageListXL.actionOpenMessage(mFromActivity, accountId, mMailboxId,
243                                mMessageId);
244                    } else if (isMailboxSelected()) {
245                        MessageListXL.actionOpenMailbox(mFromActivity, accountId, mMailboxId);
246                    } else {
247                        MessageListXL.actionOpenAccount(mFromActivity, accountId);
248                    }
249                } else {
250                    if (isMessageSelected()) {
251                        MessageView.actionView(mFromActivity, mMessageId, mMailboxId);
252                    } else if (isMailboxSelected()) {
253                        MessageList.actionHandleMailbox(mFromActivity, mMailboxId);
254                    } else {
255                        MessageList.actionHandleAccount(
256                                mFromActivity, accountId, Mailbox.TYPE_INBOX);
257                    }
258                }
259            }
260            return null;
261        }
262
263        @Override
264        protected void onPostExecute(Void result) {
265            mFromActivity.finish();
266        }
267    }
268}
269