AccountSetupBasics.java revision ea4c5642612803753fd221f33bbdf47fd40bc7dd
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.Email;
20import com.android.email.EmailAddressValidator;
21import com.android.email.R;
22import com.android.email.Utility;
23import com.android.email.VendorPolicyLoader;
24import com.android.email.activity.ActivityHelper;
25import com.android.email.activity.Welcome;
26import com.android.email.activity.setup.AccountSettingsUtils.Provider;
27import com.android.email.provider.EmailContent;
28import com.android.email.provider.EmailContent.Account;
29import com.android.email.provider.EmailContent.HostAuth;
30
31import android.accounts.AccountAuthenticatorResponse;
32import android.accounts.AccountManager;
33import android.app.Activity;
34import android.app.ActivityManager;
35import android.app.AlertDialog;
36import android.app.Dialog;
37import android.app.DialogFragment;
38import android.app.FragmentTransaction;
39import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
42import android.os.AsyncTask;
43import android.os.Bundle;
44import android.text.Editable;
45import android.text.TextUtils;
46import android.text.TextWatcher;
47import android.util.Log;
48import android.view.View;
49import android.view.View.OnClickListener;
50import android.widget.Button;
51import android.widget.CheckBox;
52import android.widget.EditText;
53import android.widget.TextView;
54import android.widget.Toast;
55
56import java.net.URI;
57import java.net.URISyntaxException;
58import java.util.concurrent.Callable;
59import java.util.concurrent.ExecutionException;
60import java.util.concurrent.FutureTask;
61
62/**
63 * Prompts the user for the email address and password. Also prompts for "Use this account as
64 * default" if this is the 2nd+ account being set up.
65 *
66 * If the domain is well-known, the account is configured fully and checked immediately
67 * using AccountCheckSettingsFragment.  If this succeeds we proceed directly to AccountSetupOptions.
68 *
69 * If the domain is not known, or the user selects Manual setup, we invoke the
70 * AccountSetupAccountType activity where the user can begin to manually configure the account.
71 *
72 * === Support for automated testing ==
73 * This activity can also be launched directly via ACTION_CREATE_ACCOUNT.  This is intended
74 * only for use by continuous test systems, and is currently only available when
75 * {@link ActivityManager#isRunningInTestHarness()} is set.  To use this mode, you must construct
76 * an intent which contains all necessary information to create the account.  No connection
77 * checking is done, so the account may or may not actually work.  Here is a sample command, for a
78 * gmail account "test_account" with a password of "test_password".
79 *
80 *      $ adb shell am start -a com.android.email.CREATE_ACCOUNT \
81 *          -e EMAIL test_account@gmail.com \
82 *          -e USER "Test Account Name" \
83 *          -e INCOMING imap+ssl+://test_account:test_password@imap.gmail.com \
84 *          -e OUTGOING smtp+ssl+://test_account:test_password@smtp.gmail.com
85 *
86 * Note: For accounts that require the full email address in the login, encode the @ as %40.
87 * Note: Exchange accounts that require device security policies cannot be created automatically.
88 */
89public class AccountSetupBasics extends AccountSetupActivity
90        implements OnClickListener, TextWatcher, AccountCheckSettingsFragment.Callbacks {
91
92    private final static boolean ENTER_DEBUG_SCREEN = true;
93
94    /**
95     * Direct access for forcing account creation
96     * For use by continuous automated test system (e.g. in conjunction with monkey tests)
97     */
98    private final String ACTION_CREATE_ACCOUNT = "com.android.email.CREATE_ACCOUNT";
99    private final String EXTRA_CREATE_ACCOUNT_EMAIL = "EMAIL";
100    private final String EXTRA_CREATE_ACCOUNT_USER = "USER";
101    private final String EXTRA_CREATE_ACCOUNT_INCOMING = "INCOMING";
102    private final String EXTRA_CREATE_ACCOUNT_OUTGOING = "OUTGOING";
103    private final Boolean DEBUG_ALLOW_NON_TEST_HARNESS_CREATION = false;
104
105    private final static String STATE_KEY_PROVIDER = "AccountSetupBasics.provider";
106
107    // NOTE: If you change this value, confirm that the new interval exists in arrays.xml
108    private final static int DEFAULT_ACCOUNT_CHECK_INTERVAL = 15;
109
110    // Support for UI
111    private TextView mWelcomeView;
112    private EditText mEmailView;
113    private EditText mPasswordView;
114    private CheckBox mDefaultView;
115    private EmailAddressValidator mEmailValidator = new EmailAddressValidator();
116    private Provider mProvider;
117    private Button mManualButton;
118    private Button mNextButton;
119    private boolean mNextButtonInhibit;
120
121    // Used when this Activity is called as part of account authentification flow,
122    // which requires to do extra work before and after the account creation.
123    // See also AccountAuthenticatorActivity.
124    private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
125    private Bundle mResultBundle = null;
126
127    // FutureTask to look up the owner
128    FutureTask<String> mOwnerLookupTask;
129
130    public static void actionNewAccount(Activity fromActivity) {
131        SetupData.init(SetupData.FLOW_MODE_NORMAL);
132        fromActivity.startActivity(new Intent(fromActivity, AccountSetupBasics.class));
133    }
134
135    /**
136     * This generates setup data that can be used to start a self-contained account creation flow
137     * for exchange accounts.
138     */
139    public static Intent actionSetupExchangeIntent(Context context) {
140        SetupData.init(SetupData.FLOW_MODE_ACCOUNT_MANAGER_EAS);
141        return new Intent(context, AccountSetupBasics.class);
142    }
143
144    /**
145     * This generates setup data that can be used to start a self-contained account creation flow
146     * for pop/imap accounts.
147     */
148    public static Intent actionSetupPopImapIntent(Context context) {
149        SetupData.init(SetupData.FLOW_MODE_ACCOUNT_MANAGER_POP_IMAP);
150        return new Intent(context, AccountSetupBasics.class);
151    }
152
153    public static void actionAccountCreateFinishedAccountFlow(Activity fromActivity) {
154        Intent i= new Intent(fromActivity, AccountSetupBasics.class);
155        // If we're in the "account flow" (from AccountManager), we want to return to the caller
156        // (in the settings app)
157        SetupData.init(SetupData.FLOW_MODE_RETURN_TO_CALLER);
158        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
159        fromActivity.startActivity(i);
160    }
161
162    public static void actionAccountCreateFinished(final Activity fromActivity,
163            final long accountId) {
164        Utility.runAsync(new Runnable() {
165           public void run() {
166               Intent i = new Intent(fromActivity, AccountSetupBasics.class);
167               // If we're not in the "account flow" (from AccountManager), we want to show the
168               // message list for the new inbox
169               Account account = Account.restoreAccountWithId(fromActivity, accountId);
170               SetupData.init(SetupData.FLOW_MODE_RETURN_TO_MESSAGE_LIST, account);
171               i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
172               fromActivity.startActivity(i);
173            }});
174    }
175
176    @Override
177    public void onCreate(Bundle savedInstanceState) {
178        super.onCreate(savedInstanceState);
179        ActivityHelper.debugSetWindowFlags(this);
180
181        // Check for forced account creation first, as it comes from an externally-generated
182        // intent and won't have any SetupData prepared.
183        String action = getIntent().getAction();
184        if (ACTION_CREATE_ACCOUNT.equals(action)) {
185            SetupData.init(SetupData.FLOW_MODE_FORCE_CREATE);
186        }
187
188        int flowMode = SetupData.getFlowMode();
189        if (flowMode == SetupData.FLOW_MODE_RETURN_TO_CALLER) {
190            // Return to the caller who initiated account creation
191            finish();
192            return;
193        } else if (flowMode == SetupData.FLOW_MODE_RETURN_TO_MESSAGE_LIST) {
194            Account account = SetupData.getAccount();
195            if (account != null && account.mId >= 0) {
196                // Show the message list for the new account
197                Welcome.actionOpenAccountInbox(this, account.mId);
198                finish();
199                return;
200            }
201        }
202
203        setContentView(R.layout.account_setup_basics);
204
205        mWelcomeView = (TextView) findViewById(R.id.instructions);
206        mEmailView = (EditText) findViewById(R.id.account_email);
207        mPasswordView = (EditText) findViewById(R.id.account_password);
208        mDefaultView = (CheckBox) findViewById(R.id.account_default);
209
210        mEmailView.addTextChangedListener(this);
211        mPasswordView.addTextChangedListener(this);
212
213        // If there are one or more accounts already in existence, then display
214        // the "use as default" checkbox (it defaults to hidden).
215        new DisplayCheckboxTask().execute();
216
217        boolean manualButtonDisplayed = true;
218        boolean alternateStrings = false;
219        if (flowMode == SetupData.FLOW_MODE_ACCOUNT_MANAGER_EAS) {
220            // No need for manual button -> next is appropriate
221            manualButtonDisplayed = false;
222            // Swap welcome text for EAS-specific text
223            alternateStrings = VendorPolicyLoader.getInstance(this).useAlternateExchangeStrings();
224            setTitle(alternateStrings
225                    ? R.string.account_setup_basics_exchange_title_alternate
226                    : R.string.account_setup_basics_exchange_title);
227            mWelcomeView.setText(alternateStrings
228                    ? R.string.accounts_welcome_exchange_alternate
229                    : R.string.accounts_welcome_exchange);
230        }
231
232        // Configure buttons
233        mManualButton = (Button) findViewById(R.id.manual_setup);
234        mNextButton = (Button) findViewById(R.id.next);
235        mManualButton.setVisibility(manualButtonDisplayed ? View.VISIBLE : View.INVISIBLE);
236        mManualButton.setOnClickListener(this);
237        mNextButton.setOnClickListener(this);
238        // Force disabled until validator notifies otherwise
239        onEnableProceedButtons(false);
240        // Lightweight debounce while Async tasks underway
241        mNextButtonInhibit = false;
242
243        mAccountAuthenticatorResponse =
244            getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
245
246        if (mAccountAuthenticatorResponse != null) {
247            mAccountAuthenticatorResponse.onRequestContinued();
248        }
249
250        // Load fields, but only once
251        String userName = SetupData.getUsername();
252        if (userName != null) {
253            mEmailView.setText(userName);
254            SetupData.setUsername(null);
255        }
256        String password = SetupData.getPassword();
257        if (userName != null) {
258            mPasswordView.setText(password);
259            SetupData.setPassword(null);
260        }
261
262        // Handle force account creation immediately (now that fragment is set up)
263        // This is never allowed in a normal user build and will exit immediately.
264        if (SetupData.getFlowMode() == SetupData.FLOW_MODE_FORCE_CREATE) {
265            if (!DEBUG_ALLOW_NON_TEST_HARNESS_CREATION &&
266                    !ActivityManager.isRunningInTestHarness()) {
267                Log.e(Email.LOG_TAG,
268                        "ERROR: Force account create only allowed while in test harness");
269                finish();
270                return;
271            }
272            Intent intent = getIntent();
273            String email = intent.getStringExtra(EXTRA_CREATE_ACCOUNT_EMAIL);
274            String user = intent.getStringExtra(EXTRA_CREATE_ACCOUNT_USER);
275            String incoming = intent.getStringExtra(EXTRA_CREATE_ACCOUNT_INCOMING);
276            String outgoing = intent.getStringExtra(EXTRA_CREATE_ACCOUNT_OUTGOING);
277            if (TextUtils.isEmpty(email) || TextUtils.isEmpty(user) ||
278                    TextUtils.isEmpty(incoming) || TextUtils.isEmpty(outgoing)) {
279                Log.e(Email.LOG_TAG, "ERROR: Force account create requires extras EMAIL, USER, " +
280                        "INCOMING, OUTGOING");
281                finish();
282                return;
283            }
284            forceCreateAccount(email, user, incoming, outgoing);
285            onCheckSettingsComplete(AccountCheckSettingsFragment.CHECK_SETTINGS_OK); // calls finish
286            return;
287        }
288
289        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_KEY_PROVIDER)) {
290            mProvider = (Provider) savedInstanceState.getSerializable(STATE_KEY_PROVIDER);
291        }
292
293        // Launch a worker to look up the owner name.  It should be ready well in advance of
294        // the time the user clicks next or manual.
295        mOwnerLookupTask = new FutureTask<String>(mOwnerLookupCallable);
296        Utility.runAsync(mOwnerLookupTask);
297    }
298
299    @Override
300    public void finish() {
301        if (mAccountAuthenticatorResponse != null) {
302            // send the result bundle back if set, otherwise send an error.
303            if (mResultBundle != null) {
304                mAccountAuthenticatorResponse.onResult(mResultBundle);
305            } else {
306                mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,
307                        "canceled");
308            }
309            mAccountAuthenticatorResponse = null;
310        }
311        super.finish();
312    }
313
314    @Override
315    public void onSaveInstanceState(Bundle outState) {
316        super.onSaveInstanceState(outState);
317        if (mProvider != null) {
318            outState.putSerializable(STATE_KEY_PROVIDER, mProvider);
319        }
320    }
321
322    /**
323     * Implements OnClickListener
324     */
325    @Override
326    public void onClick(View v) {
327        switch (v.getId()) {
328            case R.id.next:
329                // Simple debounce - just ignore while async checks are underway
330                if (mNextButtonInhibit) {
331                    return;
332                }
333                onNext();
334                break;
335            case R.id.manual_setup:
336                onManualSetup(false);
337                break;
338        }
339    }
340
341    /**
342     * Implements TextWatcher
343     */
344    public void afterTextChanged(Editable s) {
345        validateFields();
346    }
347
348    /**
349     * Implements TextWatcher
350     */
351    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
352    }
353
354    /**
355     * Implements TextWatcher
356     */
357    public void onTextChanged(CharSequence s, int start, int before, int count) {
358    }
359
360    private void validateFields() {
361        boolean valid = Utility.isTextViewNotEmpty(mEmailView)
362                && Utility.isTextViewNotEmpty(mPasswordView)
363                && mEmailValidator.isValid(mEmailView.getText().toString().trim());
364        onEnableProceedButtons(valid);
365    }
366
367    /**
368     * Return an existing username if found, or null.  This is the result of the Callable (below).
369     */
370    private String getOwnerName() {
371        String result = null;
372        try {
373            result = mOwnerLookupTask.get();
374        } catch (InterruptedException e) {
375        } catch (ExecutionException e) {
376        }
377        return result;
378    }
379
380    /**
381     * Callable that returns the username (based on other accounts) or null.
382     */
383    private Callable<String> mOwnerLookupCallable = new Callable<String>() {
384        public String call() {
385            Context context = AccountSetupBasics.this;
386            String name = null;
387            long defaultId = Account.getDefaultAccountId(context);
388            if (defaultId != -1) {
389                Account account = Account.restoreAccountWithId(context, defaultId);
390                if (account != null) {
391                    name = account.getSenderName();
392                }
393            }
394            return name;
395        }
396    };
397
398    /**
399     * Finish the auto setup process, in some cases after showing a warning dialog.
400     */
401    private void finishAutoSetup() {
402        String email = mEmailView.getText().toString().trim();
403        String password = mPasswordView.getText().toString().trim();
404        String[] emailParts = email.split("@");
405        String user = emailParts[0];
406        String domain = emailParts[1];
407        URI incomingUri = null;
408        URI outgoingUri = null;
409        String incomingUsername = mProvider.incomingUsernameTemplate;
410        try {
411            incomingUsername = incomingUsername.replaceAll("\\$email", email);
412            incomingUsername = incomingUsername.replaceAll("\\$user", user);
413            incomingUsername = incomingUsername.replaceAll("\\$domain", domain);
414
415            URI incomingUriTemplate = mProvider.incomingUriTemplate;
416            incomingUri = new URI(incomingUriTemplate.getScheme(), incomingUsername + ":"
417                    + password, incomingUriTemplate.getHost(), incomingUriTemplate.getPort(),
418                    incomingUriTemplate.getPath(), null, null);
419
420            String outgoingUsername = mProvider.outgoingUsernameTemplate;
421            outgoingUsername = outgoingUsername.replaceAll("\\$email", email);
422            outgoingUsername = outgoingUsername.replaceAll("\\$user", user);
423            outgoingUsername = outgoingUsername.replaceAll("\\$domain", domain);
424
425            URI outgoingUriTemplate = mProvider.outgoingUriTemplate;
426            outgoingUri = new URI(outgoingUriTemplate.getScheme(), outgoingUsername + ":"
427                    + password, outgoingUriTemplate.getHost(), outgoingUriTemplate.getPort(),
428                    outgoingUriTemplate.getPath(), null, null);
429
430        } catch (URISyntaxException use) {
431            /*
432             * If there is some problem with the URI we give up and go on to
433             * manual setup.  Technically speaking, AutoDiscover is OK here, since user clicked
434             * "Next" to get here.  This would never happen in practice because we don't expect
435             * to find any EAS accounts in the providers list.
436             */
437            onManualSetup(true);
438            return;
439        }
440
441        // Populate the setup data, assuming that the duplicate account check will succeed
442        populateSetupData(getOwnerName(), email, mDefaultView.isChecked(),
443                incomingUri.toString(), outgoingUri.toString());
444
445        // Stop here if the login credentials duplicate an existing account
446        // Launch an Async task to do the work
447        new DuplicateCheckTask(this, incomingUri.getHost(), incomingUsername).execute();
448    }
449
450    /**
451     * Async task that continues the work of finishAutoSetup().  Checks for a duplicate
452     * account and then either alerts the user, or continues.
453     */
454    private class DuplicateCheckTask extends AsyncTask<Void, Void, Account> {
455        private final Context mContext;
456        private final String mCheckHost;
457        private final String mCheckLogin;
458
459        public DuplicateCheckTask(Context context, String checkHost, String checkLogin) {
460            mContext = context;
461            mCheckHost = checkHost;
462            mCheckLogin = checkLogin;
463            // Prevent additional clicks on the next button during Async lookup
464            mNextButtonInhibit = true;
465        }
466
467        @Override
468        protected Account doInBackground(Void... params) {
469            EmailContent.Account account = Utility.findExistingAccount(mContext, -1,
470                    mCheckHost, mCheckLogin);
471            return account;
472        }
473
474        @Override
475        protected void onPostExecute(Account duplicateAccount) {
476            mNextButtonInhibit = false;
477            // Show duplicate account warning, or proceed
478            if (duplicateAccount != null) {
479                DuplicateAccountDialogFragment dialogFragment =
480                    DuplicateAccountDialogFragment.newInstance(duplicateAccount.mDisplayName);
481                dialogFragment.show(getFragmentManager(), DuplicateAccountDialogFragment.TAG);
482                return;
483            } else {
484                AccountCheckSettingsFragment checkerFragment =
485                    AccountCheckSettingsFragment.newInstance(
486                        SetupData.CHECK_INCOMING | SetupData.CHECK_OUTGOING, null);
487                FragmentTransaction transaction = getFragmentManager().openTransaction();
488                transaction.add(checkerFragment, AccountCheckSettingsFragment.TAG);
489                transaction.addToBackStack("back");
490                transaction.commit();
491            }
492        }
493    }
494
495
496    /**
497     * When "next" button is clicked
498     */
499    private void onNext() {
500        // Try auto-configuration from XML providers (unless in EAS mode, we can skip it)
501        if (SetupData.getFlowMode() != SetupData.FLOW_MODE_ACCOUNT_MANAGER_EAS) {
502            String email = mEmailView.getText().toString().trim();
503            String[] emailParts = email.split("@");
504            String domain = emailParts[1].trim();
505            mProvider = AccountSettingsUtils.findProviderForDomain(this, domain);
506            if (mProvider != null) {
507                if (mProvider.note != null) {
508                    NoteDialogFragment dialogFragment =
509                            NoteDialogFragment.newInstance(mProvider.note);
510                    dialogFragment.show(getFragmentManager(), NoteDialogFragment.TAG);
511                } else {
512                    finishAutoSetup();
513                }
514                return;
515            }
516        }
517        // Can't use auto setup (although EAS accounts may still be able to AutoDiscover)
518        onManualSetup(true);
519    }
520
521    /**
522     * When "manual setup" button is clicked
523     *
524     * @param allowAutoDiscover - true if the user clicked 'next' and (if the account is EAS)
525     * it's OK to use autodiscover.  false to prevent autodiscover and go straight to manual setup.
526     * Ignored for IMAP & POP accounts.
527     */
528    private void onManualSetup(boolean allowAutoDiscover) {
529        String email = mEmailView.getText().toString().trim();
530        String password = mPasswordView.getText().toString();
531        String[] emailParts = email.split("@");
532        String user = emailParts[0].trim();
533        String domain = emailParts[1].trim();
534
535        // Alternate entry to the debug options screen (for devices without a physical keyboard:
536        //  Username: d@d.d
537        //  Password: debug
538        if (ENTER_DEBUG_SCREEN && "d@d.d".equals(email) && "debug".equals(password)) {
539            mEmailView.setText("");
540            mPasswordView.setText("");
541            AccountSettingsXL.actionSettingsWithDebug(this);
542            return;
543        }
544
545        String uriString = null;
546        try {
547            URI uri = new URI("placeholder", user + ":" + password, domain, -1, null, null, null);
548            uriString = uri.toString();
549        } catch (URISyntaxException use) {
550            // If we can't set up the URL, don't continue - account setup pages will fail too
551            Toast.makeText(this, R.string.account_setup_username_password_toast,
552                    Toast.LENGTH_LONG).show();
553            return;
554        }
555
556        populateSetupData(getOwnerName(), email, mDefaultView.isChecked(), uriString, uriString);
557
558        SetupData.setAllowAutodiscover(allowAutoDiscover);
559        AccountSetupAccountType.actionSelectAccountType(this);
560    }
561
562    /**
563     * To support continuous testing, we allow the forced creation of accounts.
564     * This works in a manner fairly similar to automatic setup, in which the complete server
565     * Uri's are available, except that we will also skip checking (as if both checks were true)
566     * and all other UI.
567     *
568     * @param email The email address for the new account
569     * @param user The user name for the new account
570     * @param incoming The URI-style string defining the incoming account
571     * @param outgoing The URI-style string defining the outgoing account
572     */
573    private void forceCreateAccount(String email, String user, String incoming, String outgoing) {
574        populateSetupData(user, email, false, incoming, outgoing);
575    }
576
577    /**
578     * Populate SetupData's account with complete setup info.
579     */
580    private void populateSetupData(String senderName, String senderEmail, boolean isDefault,
581            String incoming, String outgoing) {
582        Account account = SetupData.getAccount();
583        account.setSenderName(senderName);
584        account.setEmailAddress(senderEmail);
585        account.setDisplayName(senderEmail);
586        account.setDefaultAccount(isDefault);
587        SetupData.setDefault(isDefault);        // TODO - why duplicated, if already set in account
588        account.setStoreUri(this, incoming);
589        account.setSenderUri(this, outgoing);
590
591        // Set sync and delete policies for specific account types
592        if (incoming.startsWith("imap")) {
593            // Delete policy must be set explicitly, because IMAP does not provide a UI selection
594            // for it. This logic needs to be followed in the auto setup flow as well.
595            account.setDeletePolicy(EmailContent.Account.DELETE_POLICY_ON_DELETE);
596        }
597
598        if (incoming.startsWith("eas")) {
599            account.setSyncInterval(Account.CHECK_INTERVAL_PUSH);
600        } else {
601            account.setSyncInterval(DEFAULT_ACCOUNT_CHECK_INTERVAL);
602        }
603    }
604
605    /**
606     * Implements AccountCheckSettingsFragment.Callbacks
607     *
608     * This is used in automatic setup mode to jump directly down to the options screen.
609     */
610    @Override
611    public void onCheckSettingsComplete(int result) {
612        if (result == AccountCheckSettingsFragment.CHECK_SETTINGS_OK) {
613            AccountSetupOptions.actionOptions(this);
614            finish();
615        }
616    }
617
618    /**
619     * Implements AccountCheckSettingsFragment.Callbacks
620     * This is overridden only by AccountSetupExchange
621     */
622    @Override
623    public void onAutoDiscoverComplete(int result, HostAuth hostAuth) {
624        throw new IllegalStateException();
625    }
626
627    /**
628     * AsyncTask checks count of accounts and displays "use this account as default" checkbox
629     * if there are more than one.
630     */
631    private class DisplayCheckboxTask extends AsyncTask<Void, Void, Integer> {
632
633        @Override
634        protected Integer doInBackground(Void... params) {
635            return EmailContent.count(AccountSetupBasics.this, EmailContent.Account.CONTENT_URI);
636        }
637
638        @Override
639        protected void onPostExecute(Integer numAccounts) {
640            if (numAccounts > 0) {
641                Activity activity = AccountSetupBasics.this;
642                activity.findViewById(R.id.account_default_divider_1).setVisibility(View.VISIBLE);
643                mDefaultView.setVisibility(View.VISIBLE);
644                activity.findViewById(R.id.account_default_divider_2).setVisibility(View.VISIBLE);
645            }
646        }
647    }
648
649    private void onEnableProceedButtons(boolean enabled) {
650        mManualButton.setEnabled(enabled);
651        mNextButton.setEnabled(enabled);
652    }
653
654    /**
655     * Dialog fragment to show "setup note" dialog
656     */
657    public static class NoteDialogFragment extends DialogFragment {
658        private final static String TAG = "NoteDialogFragment";
659
660        // Argument bundle keys
661        private final static String BUNDLE_KEY_NOTE = "NoteDialogFragment.Note";
662
663        /**
664         * Create the dialog with parameters
665         */
666        public static NoteDialogFragment newInstance(String note) {
667            NoteDialogFragment f = new NoteDialogFragment();
668            Bundle b = new Bundle();
669            b.putString(BUNDLE_KEY_NOTE, note);
670            f.setArguments(b);
671            return f;
672        }
673
674        @Override
675        public Dialog onCreateDialog(Bundle savedInstanceState) {
676            Context context = getActivity();
677            final String note = getArguments().getString(BUNDLE_KEY_NOTE);
678
679            return new AlertDialog.Builder(context)
680                .setIcon(android.R.drawable.ic_dialog_alert)
681                .setTitle(android.R.string.dialog_alert_title)
682                .setMessage(note)
683                .setPositiveButton(
684                        R.string.okay_action,
685                        new DialogInterface.OnClickListener() {
686                            public void onClick(DialogInterface dialog, int which) {
687                                Activity a = getActivity();
688                                if (a instanceof AccountSetupBasics) {
689                                    ((AccountSetupBasics)a).finishAutoSetup();
690                                }
691                                dismiss();
692                            }
693                        })
694                .setNegativeButton(
695                        context.getString(R.string.cancel_action),
696                        null)
697                .create();
698        }
699    }
700}
701