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