FileDocCategorySizeDatePackage
AccountSetupBasics.javaAPI DocAndroid 1.5 API17603Wed May 06 22:42:46 BST 2009com.android.email.activity.setup

AccountSetupBasics

public class AccountSetupBasics extends android.app.Activity implements android.view.View.OnClickListener, android.text.TextWatcher
Prompts the user for the email address and password. Also prompts for "Use this account as default" if this is the 2nd+ account being set up. Attempts to lookup default settings for the domain the user specified. If the domain is known the settings are handed off to the AccountSetupCheckSettings activity. If no settings are found the settings are handed off to the AccountSetupAccountType activity.

Fields Summary
private static final boolean
ENTER_DEBUG_SCREEN
private static final String
EXTRA_ACCOUNT
private static final int
DIALOG_NOTE
private static final String
STATE_KEY_PROVIDER
private static final int
DEFAULT_ACCOUNT_CHECK_INTERVAL
private com.android.email.Preferences
mPrefs
private android.widget.EditText
mEmailView
private android.widget.EditText
mPasswordView
private android.widget.CheckBox
mDefaultView
private android.widget.Button
mNextButton
private android.widget.Button
mManualSetupButton
private com.android.email.Account
mAccount
private Provider
mProvider
private com.android.email.EmailAddressValidator
mEmailValidator
Constructors Summary
Methods Summary
public static voidactionNewAccount(android.content.Context context)


         
        Intent i = new Intent(context, AccountSetupBasics.class);
        context.startActivity(i);
    
public voidafterTextChanged(android.text.Editable s)

        validateFields();
    
public voidbeforeTextChanged(java.lang.CharSequence s, int start, int count, int after)

    
private com.android.email.activity.setup.AccountSetupBasics$ProviderfindProviderForDomain(java.lang.String domain)
Search the list of known Email providers looking for one that matches the user's email domain. We look in providers_product.xml first, followed by the entries in platform providers.xml. This provides a nominal override capability. A match is defined as any provider entry for which the "domain" attribute matches.

param
domain The domain portion of the user's email address
return
suitable Provider definition, or null if no match found

        Provider p = findProviderForDomain(domain, R.xml.providers_product);
        if (p == null) {
            p = findProviderForDomain(domain, R.xml.providers);
        }
        return p;
    
private com.android.email.activity.setup.AccountSetupBasics$ProviderfindProviderForDomain(java.lang.String domain, int resourceId)
Search a single resource containing known Email provider definitions.

param
domain The domain portion of the user's email address
param
resourceId Id of the provider resource to scan
return
suitable Provider definition, or null if no match found

        try {
            XmlResourceParser xml = getResources().getXml(resourceId);
            int xmlEventType;
            Provider provider = null;
            while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) {
                if (xmlEventType == XmlResourceParser.START_TAG
                        && "provider".equals(xml.getName())
                        && domain.equalsIgnoreCase(getXmlAttribute(xml, "domain"))) {
                    provider = new Provider();
                    provider.id = getXmlAttribute(xml, "id");
                    provider.label = getXmlAttribute(xml, "label");
                    provider.domain = getXmlAttribute(xml, "domain");
                    provider.note = getXmlAttribute(xml, "note");
                }
                else if (xmlEventType == XmlResourceParser.START_TAG
                        && "incoming".equals(xml.getName())
                        && provider != null) {
                    provider.incomingUriTemplate = new URI(getXmlAttribute(xml, "uri"));
                    provider.incomingUsernameTemplate = getXmlAttribute(xml, "username");
                }
                else if (xmlEventType == XmlResourceParser.START_TAG
                        && "outgoing".equals(xml.getName())
                        && provider != null) {
                    provider.outgoingUriTemplate = new URI(getXmlAttribute(xml, "uri"));
                    provider.outgoingUsernameTemplate = getXmlAttribute(xml, "username");
                }
                else if (xmlEventType == XmlResourceParser.END_TAG
                        && "provider".equals(xml.getName())
                        && provider != null) {
                    return provider;
                }
            }
        }
        catch (Exception e) {
            Log.e(Email.LOG_TAG, "Error while trying to load provider settings.", e);
        }
        return null;
    
private voidfinishAutoSetup()

        String email = mEmailView.getText().toString().trim();
        String password = mPasswordView.getText().toString().trim();
        String[] emailParts = email.split("@");
        String user = emailParts[0];
        String domain = emailParts[1];
        URI incomingUri = null;
        URI outgoingUri = null;
        try {
            String incomingUsername = mProvider.incomingUsernameTemplate;
            incomingUsername = incomingUsername.replaceAll("\\$email", email);
            incomingUsername = incomingUsername.replaceAll("\\$user", user);
            incomingUsername = incomingUsername.replaceAll("\\$domain", domain);

            URI incomingUriTemplate = mProvider.incomingUriTemplate;
            incomingUri = new URI(incomingUriTemplate.getScheme(), incomingUsername + ":"
                    + password, incomingUriTemplate.getHost(), incomingUriTemplate.getPort(), null,
                    null, null);

            String outgoingUsername = mProvider.outgoingUsernameTemplate;
            outgoingUsername = outgoingUsername.replaceAll("\\$email", email);
            outgoingUsername = outgoingUsername.replaceAll("\\$user", user);
            outgoingUsername = outgoingUsername.replaceAll("\\$domain", domain);

            URI outgoingUriTemplate = mProvider.outgoingUriTemplate;
            outgoingUri = new URI(outgoingUriTemplate.getScheme(), outgoingUsername + ":"
                    + password, outgoingUriTemplate.getHost(), outgoingUriTemplate.getPort(), null,
                    null, null);
        } catch (URISyntaxException use) {
            /*
             * If there is some problem with the URI we give up and go on to
             * manual setup.
             */
            onManualSetup();
            return;
        }

        mAccount = new Account(this);
        mAccount.setName(getOwnerName());
        mAccount.setEmail(email);
        mAccount.setStoreUri(incomingUri.toString());
        mAccount.setSenderUri(outgoingUri.toString());
        mAccount.setDraftsFolderName(getString(R.string.special_mailbox_name_drafts));
        mAccount.setTrashFolderName(getString(R.string.special_mailbox_name_trash));
        mAccount.setOutboxFolderName(getString(R.string.special_mailbox_name_outbox));
        mAccount.setSentFolderName(getString(R.string.special_mailbox_name_sent));
        if (incomingUri.toString().startsWith("imap")) {
            // Delete policy must be set explicitly, because IMAP does not provide a UI selection
            // for it. This logic needs to be followed in the auto setup flow as well.
            mAccount.setDeletePolicy(Account.DELETE_POLICY_ON_DELETE);
        }
        mAccount.setAutomaticCheckIntervalMinutes(DEFAULT_ACCOUNT_CHECK_INTERVAL);
        AccountSetupCheckSettings.actionCheckSettings(this, mAccount, true, true);
    
private java.lang.StringgetOwnerName()

        String name = null;
        String projection[] = {
            ContactMethods.NAME
        };
        Cursor c = getContentResolver().query(
                Uri.withAppendedPath(Contacts.People.CONTENT_URI, "owner"), projection, null, null,
                null);
        if (c.getCount() > 0) {
            c.moveToFirst();
            name = c.getString(0);
            c.close();
        }

        if (name == null || name.length() == 0) {
            Account account = Preferences.getPreferences(this).getDefaultAccount();
            if (account != null) {
                name = account.getName();
            }
        }
        return name;
    
private java.lang.StringgetXmlAttribute(android.content.res.XmlResourceParser xml, java.lang.String name)
Attempts to get the given attribute as a String resource first, and if it fails returns the attribute as a simple String value.

param
xml
param
name
return

        int resId = xml.getAttributeResourceValue(null, name, 0);
        if (resId == 0) {
            return xml.getAttributeValue(null, name);
        }
        else {
            return getString(resId);
        }
    
public voidonActivityResult(int requestCode, int resultCode, android.content.Intent data)

        if (resultCode == RESULT_OK) {
            mAccount.setDescription(mAccount.getEmail());
            mAccount.save(Preferences.getPreferences(this));
            if (mDefaultView.isChecked()) {
                Preferences.getPreferences(this).setDefaultAccount(mAccount);
            }
            Email.setServicesEnabled(this);
            AccountSetupNames.actionSetNames(this, mAccount);
            finish();
        }
    
public voidonClick(android.view.View v)

        switch (v.getId()) {
            case R.id.next:
                onNext();
                break;
            case R.id.manual_setup:
                onManualSetup();
                break;
        }
    
public voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_setup_basics);
        mPrefs = Preferences.getPreferences(this);
        mEmailView = (EditText)findViewById(R.id.account_email);
        mPasswordView = (EditText)findViewById(R.id.account_password);
        mDefaultView = (CheckBox)findViewById(R.id.account_default);
        mNextButton = (Button)findViewById(R.id.next);
        mManualSetupButton = (Button)findViewById(R.id.manual_setup);

        mNextButton.setOnClickListener(this);
        mManualSetupButton.setOnClickListener(this);

        mEmailView.addTextChangedListener(this);
        mPasswordView.addTextChangedListener(this);

        if (mPrefs.getAccounts().length > 0) {
            mDefaultView.setVisibility(View.VISIBLE);
        }

        if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_ACCOUNT)) {
            mAccount = (Account)savedInstanceState.getSerializable(EXTRA_ACCOUNT);
        }

        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_KEY_PROVIDER)) {
            mProvider = (Provider)savedInstanceState.getSerializable(STATE_KEY_PROVIDER);
        }
    
public android.app.DialogonCreateDialog(int id)

        if (id == DIALOG_NOTE) {
            if (mProvider != null && mProvider.note != null) {
                return new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle(android.R.string.dialog_alert_title)
                    .setMessage(mProvider.note)
                    .setPositiveButton(
                            getString(R.string.okay_action),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    finishAutoSetup();
                                }
                            })
                    .setNegativeButton(
                            getString(R.string.cancel_action),
                            null)
                    .create();
            }
        }
        return null;
    
private voidonManualSetup()

        String email = mEmailView.getText().toString().trim();
        String password = mPasswordView.getText().toString().trim();
        String[] emailParts = email.split("@");
        String user = emailParts[0].trim();
        String domain = emailParts[1].trim();
        
        // Alternate entry to the debug options screen (for devices without a physical keyboard:
        //  Username: d@d
        //  Password: debug
        if (ENTER_DEBUG_SCREEN && "d@d".equals(email) && "debug".equals(password)) {
            startActivity(new Intent(this, Debug.class));
            return;
        }

        mAccount = new Account(this);
        mAccount.setName(getOwnerName());
        mAccount.setEmail(email);
        try {
            URI uri = new URI("placeholder", user + ":" + password, domain, -1, null, null, null);
            mAccount.setStoreUri(uri.toString());
            mAccount.setSenderUri(uri.toString());
        } catch (URISyntaxException use) {
            // If we can't set up the URL, don't continue - account setup pages will fail too
            Toast.makeText(this, R.string.account_setup_username_password_toast, Toast.LENGTH_LONG)
                    .show();
            mAccount = null;
            return;
        }
        mAccount.setDraftsFolderName(getString(R.string.special_mailbox_name_drafts));
        mAccount.setTrashFolderName(getString(R.string.special_mailbox_name_trash));
        mAccount.setOutboxFolderName(getString(R.string.special_mailbox_name_outbox));
        mAccount.setSentFolderName(getString(R.string.special_mailbox_name_sent));
        
        mAccount.setAutomaticCheckIntervalMinutes(DEFAULT_ACCOUNT_CHECK_INTERVAL);

        AccountSetupAccountType.actionSelectAccountType(this, mAccount, mDefaultView.isChecked());
        finish();
    
private voidonNext()

        String email = mEmailView.getText().toString().trim();
        String[] emailParts = email.split("@");
        String domain = emailParts[1].trim();
        mProvider = findProviderForDomain(domain);
        if (mProvider == null) {
            /*
             * We don't have default settings for this account, start the manual
             * setup process.
             */
            onManualSetup();
            return;
        }

        if (mProvider.note != null) {
            showDialog(DIALOG_NOTE);
        }
        else {
            finishAutoSetup();
        }
    
public voidonResume()

        super.onResume();
        validateFields();
    
public voidonSaveInstanceState(android.os.Bundle outState)

        super.onSaveInstanceState(outState);
        outState.putSerializable(EXTRA_ACCOUNT, mAccount);
        if (mProvider != null) {
            outState.putSerializable(STATE_KEY_PROVIDER, mProvider);
        }
    
public voidonTextChanged(java.lang.CharSequence s, int start, int before, int count)

    
private voidvalidateFields()

        boolean valid = Utility.requiredFieldValid(mEmailView)
                && Utility.requiredFieldValid(mPasswordView)
                && mEmailValidator.isValid(mEmailView.getText().toString().trim());
        mNextButton.setEnabled(valid);
        mManualSetupButton.setEnabled(valid);
        /*
         * Dim the next button's icon to 50% if the button is disabled.
         * TODO this can probably be done with a stateful drawable. Check into it.
         * android:state_enabled
         */
        Utility.setCompoundDrawablesAlpha(mNextButton, mNextButton.isEnabled() ? 255 : 128);