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