AccountSetupAccountType.java revision 27c70e332231aea5cbe7813d8256e9f873d13b39
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.activity.ActivityHelper; 22import com.android.email.mail.Store; 23import com.android.email.provider.EmailContent; 24import com.android.email.provider.EmailContent.Account; 25import com.android.email.provider.EmailContent.HostAuth; 26 27import android.app.Activity; 28import android.content.Intent; 29import android.database.Cursor; 30import android.os.Bundle; 31import android.view.View; 32import android.view.View.OnClickListener; 33import android.widget.Button; 34 35import java.net.URI; 36import java.net.URISyntaxException; 37 38/** 39 * Prompts the user to select an account type. The account type, along with the 40 * passed in email address, password and makeDefault are then passed on to the 41 * AccountSetupIncoming activity. 42 */ 43public class AccountSetupAccountType extends AccountSetupActivity implements OnClickListener { 44 45 public static void actionSelectAccountType(Activity fromActivity) { 46 fromActivity.startActivity(new Intent(fromActivity, AccountSetupAccountType.class)); 47 } 48 49 @Override 50 public void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 ActivityHelper.debugSetWindowFlags(this); 53 int flowMode = SetupData.getFlowMode(); 54 55 // If we're in account setup flow mode, for EAS, skip this screen and "click" EAS 56 if (flowMode == SetupData.FLOW_MODE_ACCOUNT_MANAGER_EAS) { 57 onExchange(); 58 return; 59 } 60 61 // Otherwise proceed into this screen 62 setContentView(R.layout.account_setup_account_type); 63 ((Button)findViewById(R.id.pop)).setOnClickListener(this); 64 ((Button)findViewById(R.id.imap)).setOnClickListener(this); 65 final Button exchangeButton = (Button) findViewById(R.id.exchange); 66 exchangeButton.setOnClickListener(this); 67 final Button previousButton = (Button) findViewById(R.id.previous); 68 if (previousButton != null) previousButton.setOnClickListener(this); 69 70 // TODO If we decide to exclude the Exchange option in POP_IMAP mode, use the following line 71 // instead of the line that follows it 72 //if (isExchangeAvailable() && flowMode != SetupData.FLOW_MODE_POP_IMAP) { 73 if (isExchangeAvailable()) { 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. Try to put together a URI using the entered 118 * email address. Also set the mail delete policy here, because there is no UI (for exchange), 119 * and switch the default sync interval to "push". 120 */ 121 private void onExchange() { 122 Account account = SetupData.getAccount(); 123 try { 124 URI uri = new URI(account.getStoreUri(this)); 125 uri = new URI("eas+ssl+", uri.getUserInfo(), uri.getHost(), uri.getPort(), 126 null, null, null); 127 account.setStoreUri(this, uri.toString()); 128 account.setSenderUri(this, uri.toString()); 129 } catch (URISyntaxException use) { 130 /* 131 * This should not happen. 132 */ 133 throw new Error(use); 134 } 135 // TODO: Confirm correct delete policy for exchange 136 account.setDeletePolicy(Account.DELETE_POLICY_ON_DELETE); 137 account.setSyncInterval(Account.CHECK_INTERVAL_PUSH); 138 account.setSyncLookback(1); 139 SetupData.setCheckSettingsMode(SetupData.CHECK_AUTODISCOVER); 140 AccountSetupExchange.actionIncomingSettings(this, SetupData.getFlowMode(), account); 141 finish(); 142 } 143 144 /** 145 * Determine if we can show the "exchange" option 146 * 147 * TODO: This should be dynamic and data-driven for all account types, not just hardcoded 148 * like this. 149 */ 150 private boolean isExchangeAvailable() { 151 //EXCHANGE-REMOVE-SECTION-START 152 try { 153 URI uri = new URI(SetupData.getAccount().getStoreUri(this)); 154 uri = new URI("eas", uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null); 155 Store.StoreInfo storeInfo = Store.StoreInfo.getStoreInfo(uri.toString(), this); 156 return (storeInfo != null && checkAccountInstanceLimit(storeInfo)); 157 } catch (URISyntaxException e) { 158 } 159 //EXCHANGE-REMOVE-SECTION-END 160 return false; 161 } 162 163 /** 164 * If the optional store specifies a limit on the number of accounts, make sure that we 165 * don't violate that limit. 166 * @return true if OK to create another account, false if not OK (limit reached) 167 */ 168 /* package */ boolean checkAccountInstanceLimit(Store.StoreInfo storeInfo) { 169 // return immediately if account defines no limit 170 if (storeInfo.mAccountInstanceLimit < 0) { 171 return true; 172 } 173 174 // count existing accounts 175 int currentAccountsCount = 0; 176 Cursor c = null; 177 try { 178 c = this.getContentResolver().query( 179 Account.CONTENT_URI, 180 Account.CONTENT_PROJECTION, 181 null, null, null); 182 while (c.moveToNext()) { 183 Account account = EmailContent.getContent(c, Account.class); 184 String storeUri = account.getStoreUri(this); 185 if (storeUri != null && storeUri.startsWith(storeInfo.mScheme)) { 186 currentAccountsCount++; 187 } 188 } 189 } finally { 190 if (c != null) { 191 c.close(); 192 } 193 } 194 195 // return true if we can accept another account 196 return (currentAccountsCount < storeInfo.mAccountInstanceLimit); 197 } 198 199 public void onClick(View v) { 200 switch (v.getId()) { 201 case R.id.pop: 202 onPop(); 203 break; 204 case R.id.imap: 205 onImap(); 206 break; 207 case R.id.exchange: 208 onExchange(); 209 break; 210 case R.id.previous: 211 finish(); 212 break; 213 } 214 } 215} 216