AccountSetupBasics.java revision 2731aef45c6f2f9792ae698ebf7d65ca6338a02c
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.setup;
18
19import com.android.email.R;
20import com.android.email.Utility;
21import com.android.email.VendorPolicyLoader;
22import com.android.email.activity.Welcome;
23import com.android.email.provider.EmailContent.Account;
24import com.android.email.provider.EmailContent.HostAuth;
25
26import android.app.Activity;
27import android.app.FragmentTransaction;
28import android.content.Context;
29import android.content.Intent;
30import android.os.Bundle;
31import android.view.Menu;
32import android.view.MenuItem;
33
34/**
35 * Prompts the user for the email address and password. Also prompts for "Use this account as
36 * default" if this is the 2nd+ account being set up.
37 *
38 * If the domain is well-known, the account is configured fully and checked immediately
39 * using AccountCheckSettingsFragment.  If this succeeds we proceed directly to AccountSetupOptions.
40 *
41 * If the domain is not known, or the user selects Manual setup, we invoke the
42 * AccountSetupAccountType activity where the user can begin to manually configure the account.
43 */
44public class AccountSetupBasics extends AccountSetupActivity
45        implements AccountSetupBasicsFragment.Callback, AccountCheckSettingsFragment.Callbacks {
46
47    private AccountSetupBasicsFragment mFragment;
48    private boolean mManualButtonDisplayed;
49    private boolean mNextButtonEnabled;
50
51    public static void actionNewAccount(Activity fromActivity) {
52        SetupData.init(SetupData.FLOW_MODE_NORMAL);
53        fromActivity.startActivity(new Intent(fromActivity, AccountSetupBasics.class));
54    }
55
56    /**
57     * This generates setup data that can be used to start a self-contained account creation flow
58     * for exchange accounts.
59     */
60    public static Intent actionSetupExchangeIntent(Context context) {
61        SetupData.init(SetupData.FLOW_MODE_ACCOUNT_MANAGER_EAS);
62        return new Intent(context, AccountSetupBasics.class);
63    }
64
65    /**
66     * This generates setup data that can be used to start a self-contained account creation flow
67     * for pop/imap accounts.
68     */
69    public static Intent actionSetupPopImapIntent(Context context) {
70        SetupData.init(SetupData.FLOW_MODE_ACCOUNT_MAANGER_POP_IMAP);
71        return new Intent(context, AccountSetupBasics.class);
72    }
73
74    public static void actionAccountCreateFinishedAccountFlow(Activity fromActivity) {
75        Intent i= new Intent(fromActivity, AccountSetupBasics.class);
76        // If we're in the "account flow" (from AccountManager), we want to return to the caller
77        // (in the settings app)
78        SetupData.init(SetupData.FLOW_MODE_RETURN_TO_CALLER);
79        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
80        fromActivity.startActivity(i);
81    }
82
83    public static void actionAccountCreateFinished(final Activity fromActivity,
84            final long accountId) {
85        Utility.runAsync(new Runnable() {
86           public void run() {
87               Intent i = new Intent(fromActivity, AccountSetupBasics.class);
88               // If we're not in the "account flow" (from AccountManager), we want to show the
89               // message list for the new inbox
90               Account account = Account.restoreAccountWithId(fromActivity, accountId);
91               SetupData.init(SetupData.FLOW_MODE_RETURN_TO_MESSAGE_LIST, account);
92               i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
93               fromActivity.startActivity(i);
94            }});
95    }
96
97    @Override
98    public void onCreate(Bundle savedInstanceState) {
99        super.onCreate(savedInstanceState);
100
101        int flowMode = SetupData.getFlowMode();
102        if (flowMode == SetupData.FLOW_MODE_RETURN_TO_CALLER) {
103            // Return to the caller who initiated account creation
104            finish();
105            return;
106        } else if (flowMode == SetupData.FLOW_MODE_RETURN_TO_MESSAGE_LIST) {
107            Account account = SetupData.getAccount();
108            if (account != null && account.mId >= 0) {
109                // Show the message list for the new account
110                Welcome.actionOpenAccountInbox(this, account.mId);
111                finish();
112                return;
113            }
114        }
115
116        setContentView(R.layout.account_setup_basics);
117
118        mFragment = (AccountSetupBasicsFragment)
119                getFragmentManager().findFragmentById(R.id.setup_basics_fragment);
120
121        mManualButtonDisplayed = true;
122        boolean alternateStrings = false;
123        if (flowMode == SetupData.FLOW_MODE_ACCOUNT_MANAGER_EAS) {
124            // No need for manual button -> next is appropriate
125            mManualButtonDisplayed = false;
126            // Swap welcome text for EAS-specific text
127            alternateStrings = VendorPolicyLoader.getInstance(this).useAlternateExchangeStrings();
128            setTitle(alternateStrings
129                    ? R.string.account_setup_basics_exchange_title_alternate
130                            : R.string.account_setup_basics_exchange_title);
131        }
132
133        // Configure fragment
134        mFragment.setCallback(this, alternateStrings);
135    }
136
137    /**
138     * Implements AccountCheckSettingsFragment.Callbacks
139     *
140     * This is used in automatic setup mode to jump directly down to the names screen.
141     *
142     * NOTE:  With this organization, it is *not* possible to auto-create an exchange account,
143     * because certain necessary actions happen during AccountSetupOptions (which we are
144     * skipping here).
145     */
146    @Override
147    public void onCheckSettingsComplete(int result) {
148        if (result == AccountCheckSettingsFragment.CHECK_SETTINGS_OK) {
149            AccountSetupOptions.actionOptions(this);
150            finish();
151        }
152    }
153
154    /**
155     * Implements AccountCheckSettingsFragment.Callbacks
156     * This is overridden only by AccountSetupExchange
157     */
158    @Override
159    public void onAutoDiscoverComplete(int result, HostAuth hostAuth) {
160        throw new IllegalStateException();
161    }
162
163    /**
164     * Add "Next" & "Manual" buttons when this activity is displayed
165     */
166    @Override
167    public boolean onCreateOptionsMenu(Menu menu) {
168        int menuId = mManualButtonDisplayed
169                ? R.menu.account_setup_manual_next_option
170                : R.menu.account_setup_next_option;
171        getMenuInflater().inflate(menuId, menu);
172        return super.onCreateOptionsMenu(menu);
173    }
174
175    /**
176     * Enable/disable "Next" & "Manual" buttons
177     */
178    @Override
179    public boolean onPrepareOptionsMenu(Menu menu) {
180        menu.findItem(R.id.next).setEnabled(mNextButtonEnabled);
181        if (mManualButtonDisplayed) {
182            menu.findItem(R.id.manual_setup).setEnabled(mNextButtonEnabled);
183        }
184        return super.onPrepareOptionsMenu(menu);
185    }
186
187    /**
188     * Respond to clicks in the "Next" button
189     */
190    @Override
191    public boolean onOptionsItemSelected(MenuItem item) {
192        switch (item.getItemId()) {
193            case R.id.next:
194                mFragment.onNext();
195                return true;
196            case R.id.manual_setup:
197                // no AutoDiscover - user clicked "manual"
198                mFragment.onManualSetup(false);
199                return true;
200        }
201        return super.onOptionsItemSelected(item);
202    }
203
204    /**
205     * Implements AccountSetupBasicsFragment.Callback
206     */
207    @Override
208    public void onEnableProceedButtons(boolean enabled) {
209        boolean wasEnabled = mNextButtonEnabled;
210        mNextButtonEnabled = enabled;
211
212        if (enabled != wasEnabled) {
213            invalidateOptionsMenu();
214        }
215    }
216
217    /**
218     * Implements AccountSetupBasicsFragment.Callback
219     *
220     * This is called when auto-setup (from hardcoded server info) is attempted.
221     * Replace the name/password fragment with the account checker, which will begin to
222     * check incoming/outgoing.
223     */
224    @Override
225    public void onProceedAutomatic() {
226        AccountCheckSettingsFragment checkerFragment =
227            AccountCheckSettingsFragment.newInstance(
228                    SetupData.CHECK_INCOMING | SetupData.CHECK_OUTGOING, null);
229        FragmentTransaction transaction = getFragmentManager().openTransaction();
230        transaction.replace(R.id.setup_basics_fragment, checkerFragment);
231        transaction.addToBackStack("back");
232        transaction.commit();
233    }
234
235    /**
236     * Implements AccountSetupBasicsFragment.Callback
237     */
238    @Override
239    public void onProceedDebugSettings() {
240        AccountSettingsXL.actionSettingsWithDebug(this);
241    }
242
243    /**
244     * Implements AccountSetupBasicsFragment.Callback
245     */
246    @Override
247    public void onProceedManual(boolean allowAutoDiscover) {
248        SetupData.setAllowAutodiscover(allowAutoDiscover);
249        AccountSetupAccountType.actionSelectAccountType(this);
250    }
251}
252