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