AccountSetupAccountType.java revision 3a5c1fb274a9ce72d708d88509bf2607cb018ddd
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.ExchangeUtils;
20import com.android.email.R;
21import com.android.email.VendorPolicyLoader;
22import com.android.email.activity.ActivityHelper;
23import com.android.email.mail.Store;
24import com.android.emailcommon.provider.EmailContent;
25import com.android.emailcommon.provider.EmailContent.Account;
26import com.android.emailcommon.provider.EmailContent.HostAuth;
27
28import android.app.Activity;
29import android.content.Intent;
30import android.database.Cursor;
31import android.os.Bundle;
32import android.view.View;
33import android.view.View.OnClickListener;
34import android.widget.Button;
35
36import java.net.URI;
37import java.net.URISyntaxException;
38
39/**
40 * Prompts the user to select an account type. The account type, along with the
41 * passed in email address, password and makeDefault are then passed on to the
42 * AccountSetupIncoming activity.
43 */
44public class AccountSetupAccountType extends AccountSetupActivity implements OnClickListener {
45
46    public static void actionSelectAccountType(Activity fromActivity) {
47        fromActivity.startActivity(new Intent(fromActivity, AccountSetupAccountType.class));
48    }
49
50    @Override
51    public void onCreate(Bundle savedInstanceState) {
52        super.onCreate(savedInstanceState);
53        ActivityHelper.debugSetWindowFlags(this);
54        int flowMode = SetupData.getFlowMode();
55
56        // If we're in account setup flow mode, for EAS, skip this screen and "click" EAS
57        if (flowMode == SetupData.FLOW_MODE_ACCOUNT_MANAGER_EAS) {
58            onExchange();
59            return;
60        }
61
62        // Otherwise proceed into this screen
63        setContentView(R.layout.account_setup_account_type);
64        ((Button)findViewById(R.id.pop)).setOnClickListener(this);
65        ((Button)findViewById(R.id.imap)).setOnClickListener(this);
66        final Button exchangeButton = (Button) findViewById(R.id.exchange);
67        exchangeButton.setVisibility(View.INVISIBLE);
68        final Button previousButton = (Button) findViewById(R.id.previous);
69        if (previousButton != null) previousButton.setOnClickListener(this);
70
71        // TODO If we decide to exclude the Exchange option in POP_IMAP mode, use the following line
72        // instead of the line that follows it
73        //if (ExchangeUtils.isExchangeAvailable(this) && flowMode != SetupData.FLOW_MODE_POP_IMAP) {
74        if (ExchangeUtils.isExchangeAvailable(this)) {
75            exchangeButton.setOnClickListener(this);
76            exchangeButton.setVisibility(View.VISIBLE);
77            if (VendorPolicyLoader.getInstance(this).useAlternateExchangeStrings()) {
78                exchangeButton.setText(
79                        R.string.account_setup_account_type_exchange_action_alternate);
80            }
81        }
82        // TODO: Dynamic creation of buttons, instead of just hiding things we don't need
83    }
84
85    /**
86     * For POP accounts, we rewrite the username to the full user@domain, and we set the
87     * default server name to pop3.domain
88     */
89    private void onPop() {
90        Account account = SetupData.getAccount();
91        HostAuth hostAuth = account.mHostAuthRecv;
92        hostAuth.mProtocol = "pop3";
93        hostAuth.mLogin = hostAuth.mLogin + "@" + hostAuth.mAddress;
94        hostAuth.mAddress = AccountSettingsUtils.inferServerName(hostAuth.mAddress, "pop3", null);
95        SetupData.setCheckSettingsMode(SetupData.CHECK_INCOMING | SetupData.CHECK_OUTGOING);
96        AccountSetupIncoming.actionIncomingSettings(this, SetupData.getFlowMode(), account);
97        finish();
98    }
99
100    /**
101     * The user has selected an IMAP account type.  Try to put together a URI using the entered
102     * email address.  Also set the mail delete policy here, because there is no UI (for IMAP).
103     */
104    private void onImap() {
105        Account account = SetupData.getAccount();
106        HostAuth hostAuth = account.mHostAuthRecv;
107        hostAuth.mProtocol = "imap";
108        hostAuth.mLogin = hostAuth.mLogin + "@" + hostAuth.mAddress;
109        hostAuth.mAddress = AccountSettingsUtils.inferServerName(hostAuth.mAddress, "imap", null);
110        // Delete policy must be set explicitly, because IMAP does not provide a UI selection
111        // for it. This logic needs to be followed in the auto setup flow as well.
112        account.setDeletePolicy(Account.DELETE_POLICY_ON_DELETE);
113        SetupData.setCheckSettingsMode(SetupData.CHECK_INCOMING | SetupData.CHECK_OUTGOING);
114        AccountSetupIncoming.actionIncomingSettings(this, SetupData.getFlowMode(), account);
115        finish();
116    }
117
118    /**
119     * The user has selected an exchange account type.  Try to put together a URI using the entered
120     * email address.  Also set the mail delete policy here, because there is no UI (for exchange),
121     * and switch the default sync interval to "push".
122     */
123    private void onExchange() {
124        Account account = SetupData.getAccount();
125        try {
126            URI uri = new URI(account.getStoreUri(this));
127            uri = new URI("eas+ssl+", uri.getUserInfo(), uri.getHost(), uri.getPort(),
128                    null, null, null);
129            account.setStoreUri(this, uri.toString());
130            account.setSenderUri(this, uri.toString());
131        } catch (URISyntaxException use) {
132            /*
133             * This should not happen.
134             */
135            throw new Error(use);
136        }
137        // TODO: Confirm correct delete policy for exchange
138        account.setDeletePolicy(Account.DELETE_POLICY_ON_DELETE);
139        account.setSyncInterval(Account.CHECK_INTERVAL_PUSH);
140        account.setSyncLookback(1);
141        SetupData.setCheckSettingsMode(SetupData.CHECK_AUTODISCOVER);
142        AccountSetupExchange.actionIncomingSettings(this, SetupData.getFlowMode(), account);
143        finish();
144    }
145
146    /**
147     * If the optional store specifies a limit on the number of accounts, make sure that we
148     * don't violate that limit.
149     * @return true if OK to create another account, false if not OK (limit reached)
150     */
151    /* package */ boolean checkAccountInstanceLimit(Store.StoreInfo storeInfo) {
152        // return immediately if account defines no limit
153        if (storeInfo.mAccountInstanceLimit < 0) {
154            return true;
155        }
156
157        // count existing accounts
158        int currentAccountsCount = 0;
159        Cursor c = null;
160        try {
161            c = this.getContentResolver().query(
162                    Account.CONTENT_URI,
163                    Account.CONTENT_PROJECTION,
164                    null, null, null);
165            while (c.moveToNext()) {
166                Account account = EmailContent.getContent(c, Account.class);
167                String storeUri = account.getStoreUri(this);
168                if (storeUri != null && storeUri.startsWith(storeInfo.mScheme)) {
169                    currentAccountsCount++;
170                }
171            }
172        } finally {
173            if (c != null) {
174                c.close();
175            }
176        }
177
178        // return true if we can accept another account
179        return (currentAccountsCount < storeInfo.mAccountInstanceLimit);
180    }
181
182    public void onClick(View v) {
183        switch (v.getId()) {
184            case R.id.pop:
185                onPop();
186                break;
187            case R.id.imap:
188                onImap();
189                break;
190            case R.id.exchange:
191                onExchange();
192                break;
193            case R.id.previous:
194                finish();
195                break;
196        }
197    }
198}
199