AccountSetupAccountType.java revision f613489663cdc5df71b029dfcec5fd3c5173a549
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 } 103 104 /** 105 * The user has selected an IMAP account type. Try to put together a URI using the entered 106 * email address. Also set the mail delete policy here, because there is no UI (for IMAP). 107 */ 108 private void onImap() { 109 try { 110 URI uri = new URI(mAccount.getStoreUri(this)); 111 uri = new URI("imap", uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null); 112 mAccount.setStoreUri(this, uri.toString()); 113 } catch (URISyntaxException use) { 114 /* 115 * This should not happen. 116 */ 117 throw new Error(use); 118 } 119 // Delete policy must be set explicitly, because IMAP does not provide a UI selection 120 // for it. This logic needs to be followed in the auto setup flow as well. 121 mAccount.setDeletePolicy(Account.DELETE_POLICY_ON_DELETE); 122 AccountSetupIncoming.actionIncomingSettings(this, mAccount, mMakeDefault); 123 } 124 125 /** 126 * The user has selected an exchange account type. Try to put together a URI using the entered 127 * email address. Also set the mail delete policy here, because there is no UI (for exchange), 128 * and switch the default sync interval to "push". 129 */ 130 private void onExchange(boolean easFlowMode) { 131 try { 132 URI uri = new URI(mAccount.getStoreUri(this)); 133 uri = new URI("eas+ssl+", uri.getUserInfo(), uri.getHost(), uri.getPort(), 134 null, null, null); 135 mAccount.setStoreUri(this, uri.toString()); 136 mAccount.setSenderUri(this, uri.toString()); 137 } catch (URISyntaxException use) { 138 /* 139 * This should not happen. 140 */ 141 throw new Error(use); 142 } 143 // TODO: Confirm correct delete policy for exchange 144 mAccount.setDeletePolicy(Account.DELETE_POLICY_ON_DELETE); 145 mAccount.setSyncInterval(Account.CHECK_INTERVAL_PUSH); 146 mAccount.setSyncLookback(1); 147 AccountSetupExchange.actionIncomingSettings(this, mAccount, mMakeDefault, easFlowMode); 148 if (easFlowMode) { 149 finish(); 150 } 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 try { 161 URI uri = new URI(mAccount.getStoreUri(this)); 162 uri = new URI("eas", uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null); 163 Store.StoreInfo storeInfo = Store.StoreInfo.getStoreInfo(uri.toString(), this); 164 return (storeInfo != null && checkAccountInstanceLimit(storeInfo)); 165 } catch (URISyntaxException e) { 166 return false; 167 } 168 } 169 170 /** 171 * If the optional store specifies a limit on the number of accounts, make sure that we 172 * don't violate that limit. 173 * @return true if OK to create another account, false if not OK (limit reached) 174 */ 175 /* package */ boolean checkAccountInstanceLimit(Store.StoreInfo storeInfo) { 176 // return immediately if account defines no limit 177 if (storeInfo.mAccountInstanceLimit < 0) { 178 return true; 179 } 180 181 // count existing accounts 182 int currentAccountsCount = 0; 183 Cursor c = null; 184 try { 185 c = this.getContentResolver().query( 186 Account.CONTENT_URI, 187 Account.CONTENT_PROJECTION, 188 null, null, null); 189 while (c.moveToNext()) { 190 Account account = EmailContent.getContent(c, Account.class); 191 String storeUri = account.getStoreUri(this); 192 if (storeUri != null && storeUri.startsWith(storeInfo.mScheme)) { 193 currentAccountsCount++; 194 } 195 } 196 } finally { 197 if (c != null) { 198 c.close(); 199 } 200 } 201 202 // return true if we can accept another account 203 return (currentAccountsCount < storeInfo.mAccountInstanceLimit); 204 } 205 206 public void onClick(View v) { 207 switch (v.getId()) { 208 case R.id.pop: 209 onPop(); 210 break; 211 case R.id.imap: 212 onImap(); 213 break; 214 case R.id.exchange: 215 onExchange(false); 216 break; 217 } 218 } 219} 220