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