AccountSetupAccountType.java revision 63a22008c7b71675ab6d4c0526556a81a2c60632
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.Account;
20import com.android.email.Preferences;
21import com.android.email.R;
22import com.android.email.mail.Store;
23
24import android.app.Activity;
25import android.content.Intent;
26import android.os.Bundle;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.widget.Button;
30
31import java.net.URI;
32import java.net.URISyntaxException;
33
34/**
35 * Prompts the user to select an account type. The account type, along with the
36 * passed in email address, password and makeDefault are then passed on to the
37 * AccountSetupIncoming activity.
38 */
39public class AccountSetupAccountType extends Activity implements OnClickListener {
40    private static final String EXTRA_ACCOUNT = "account";
41
42    private static final String EXTRA_MAKE_DEFAULT = "makeDefault";
43
44    private Account mAccount;
45
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().getSerializableExtra(EXTRA_ACCOUNT);
65        mMakeDefault = (boolean)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());
76            uri = new URI("pop3", uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
77            mAccount.setStoreUri(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());
95            uri = new URI("imap", uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
96            mAccount.setStoreUri(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());
118            uri = new URI("eas", uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
119            mAccount.setStoreUri(uri.toString());
120            mAccount.setSenderUri(uri.toString());
121        } catch (URISyntaxException use) {
122            /*
123             * This should not happen.
124             */
125            throw new Error(use);
126        }
127        // TODO: Confirm correct delete policy for exchange
128        mAccount.setDeletePolicy(Account.DELETE_POLICY_ON_DELETE);
129        mAccount.setAutomaticCheckIntervalMinutes(Account.CHECK_INTERVAL_PUSH);
130        mAccount.setSyncWindow(Account.SYNC_WINDOW_1_DAY);
131        AccountSetupExchange.actionIncomingSettings(this, mAccount, mMakeDefault);
132        finish();
133    }
134
135    /**
136     * Determine if we can show the "exchange" option
137     *
138     * TODO: This should be dynamic and data-driven for all account types, not just hardcoded
139     * like this.
140     */
141    private boolean isExchangeAvailable() {
142        try {
143            URI uri = new URI(mAccount.getStoreUri());
144            uri = new URI("eas", uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
145            Store.StoreInfo storeInfo = Store.StoreInfo.getStoreInfo(uri.toString(), this);
146            return (storeInfo != null && checkAccountInstanceLimit(storeInfo));
147        } catch (URISyntaxException e) {
148            return false;
149        }
150    }
151
152    /**
153     * If the optional store specifies a limit on the number of accounts, make sure that we
154     * don't violate that limit.
155     * @return true if OK to create another account, false if not OK (limit reached)
156     */
157    /* package */ boolean checkAccountInstanceLimit(Store.StoreInfo storeInfo) {
158        // return immediately if account defines no limit
159        if (storeInfo.mAccountInstanceLimit < 0) {
160            return true;
161        }
162
163        // count existing accounts
164        int currentAccountsCount = 0;
165        Account[] accounts = Preferences.getPreferences(this).getAccounts();
166        for (Account account : accounts) {
167            String storeUri = account.getStoreUri();
168            if (storeUri != null && storeUri.startsWith(storeInfo.mScheme)) {
169                currentAccountsCount++;
170            }
171        }
172
173        // return true if we can accept another account
174        return (currentAccountsCount < storeInfo.mAccountInstanceLimit);
175    }
176
177    public void onClick(View v) {
178        switch (v.getId()) {
179            case R.id.pop:
180                onPop();
181                break;
182            case R.id.imap:
183                onImap();
184                break;
185            case R.id.exchange:
186                onExchange();
187                break;
188        }
189    }
190}
191