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