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