AccountSetupOptions.java revision c6089bc01f2ae49fb11904a4b4f222811358254f
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 android.accounts.AccountAuthenticatorResponse; 20import android.accounts.AccountManager; 21import android.accounts.AccountManagerCallback; 22import android.accounts.AccountManagerFuture; 23import android.accounts.AuthenticatorException; 24import android.accounts.OperationCanceledException; 25import android.app.Activity; 26import android.app.AlertDialog; 27import android.content.Context; 28import android.content.DialogInterface; 29import android.content.Intent; 30import android.os.Bundle; 31import android.util.Log; 32import android.view.View; 33import android.view.View.OnClickListener; 34import android.widget.ArrayAdapter; 35import android.widget.CheckBox; 36import android.widget.Spinner; 37 38import com.android.email.R; 39import com.android.email.activity.ActivityHelper; 40import com.android.email.activity.UiUtilities; 41import com.android.email.service.EmailServiceUtils; 42import com.android.email.service.EmailServiceUtils.EmailServiceInfo; 43import com.android.email.service.MailService; 44import com.android.email2.ui.MailActivityEmail; 45import com.android.emailcommon.Logging; 46import com.android.emailcommon.provider.Account; 47import com.android.emailcommon.provider.Policy; 48import com.android.emailcommon.service.SyncWindow; 49import com.android.emailcommon.utility.Utility; 50 51import java.io.IOException; 52 53public class AccountSetupOptions extends AccountSetupActivity implements OnClickListener { 54 55 private Spinner mCheckFrequencyView; 56 private Spinner mSyncWindowView; 57 private CheckBox mDefaultView; 58 private CheckBox mNotifyView; 59 private CheckBox mSyncContactsView; 60 private CheckBox mSyncCalendarView; 61 private CheckBox mSyncEmailView; 62 private CheckBox mBackgroundAttachmentsView; 63 private View mAccountSyncWindowRow; 64 private boolean mDonePressed = false; 65 private EmailServiceInfo mServiceInfo; 66 67 public static final int REQUEST_CODE_ACCEPT_POLICIES = 1; 68 69 /** Default sync window for new EAS accounts */ 70 private static final int SYNC_WINDOW_EAS_DEFAULT = SyncWindow.SYNC_WINDOW_AUTO; 71 72 public static void actionOptions(Activity fromActivity) { 73 fromActivity.startActivity(new ForwardingIntent(fromActivity, AccountSetupOptions.class)); 74 } 75 76 @Override 77 public void onCreate(Bundle savedInstanceState) { 78 super.onCreate(savedInstanceState); 79 ActivityHelper.debugSetWindowFlags(this); 80 setContentView(R.layout.account_setup_options); 81 82 mCheckFrequencyView = (Spinner) UiUtilities.getView(this, R.id.account_check_frequency); 83 mSyncWindowView = (Spinner) UiUtilities.getView(this, R.id.account_sync_window); 84 mDefaultView = (CheckBox) UiUtilities.getView(this, R.id.account_default); 85 mNotifyView = (CheckBox) UiUtilities.getView(this, R.id.account_notify); 86 mSyncContactsView = (CheckBox) UiUtilities.getView(this, R.id.account_sync_contacts); 87 mSyncCalendarView = (CheckBox) UiUtilities.getView(this, R.id.account_sync_calendar); 88 mSyncEmailView = (CheckBox) UiUtilities.getView(this, R.id.account_sync_email); 89 mSyncEmailView.setChecked(true); 90 mBackgroundAttachmentsView = (CheckBox) UiUtilities.getView(this, 91 R.id.account_background_attachments); 92 mBackgroundAttachmentsView.setChecked(true); 93 UiUtilities.getView(this, R.id.previous).setOnClickListener(this); 94 UiUtilities.getView(this, R.id.next).setOnClickListener(this); 95 mAccountSyncWindowRow = UiUtilities.getView(this, R.id.account_sync_window_row); 96 97 Account account = SetupData.getAccount(); 98 mServiceInfo = EmailServiceUtils.getServiceInfo(getApplicationContext(), 99 account.mHostAuthRecv.mProtocol); 100 CharSequence[] frequencyValues = mServiceInfo.syncIntervals; 101 CharSequence[] frequencyEntries = mServiceInfo.syncIntervalStrings; 102 103 // Now create the array used by the sync interval Spinner 104 SpinnerOption[] checkFrequencies = new SpinnerOption[frequencyEntries.length]; 105 for (int i = 0; i < frequencyEntries.length; i++) { 106 checkFrequencies[i] = new SpinnerOption( 107 Integer.valueOf(frequencyValues[i].toString()), frequencyEntries[i].toString()); 108 } 109 ArrayAdapter<SpinnerOption> checkFrequenciesAdapter = new ArrayAdapter<SpinnerOption>(this, 110 android.R.layout.simple_spinner_item, checkFrequencies); 111 checkFrequenciesAdapter 112 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 113 mCheckFrequencyView.setAdapter(checkFrequenciesAdapter); 114 115 if (mServiceInfo.offerLookback) { 116 enableLookbackSpinner(); 117 } 118 119 // Note: It is OK to use mAccount.mIsDefault here *only* because the account 120 // has not been written to the DB yet. Ordinarily, call Account.getDefaultAccountId(). 121 if (account.mIsDefault || SetupData.isDefault()) { 122 mDefaultView.setChecked(true); 123 } 124 mNotifyView.setChecked( 125 (account.getFlags() & Account.FLAGS_NOTIFY_NEW_MAIL) != 0); 126 SpinnerOption.setSpinnerOptionValue(mCheckFrequencyView, account.getSyncInterval()); 127 128 if (mServiceInfo.syncContacts) { 129 mSyncContactsView.setVisibility(View.VISIBLE); 130 mSyncContactsView.setChecked(true); 131 UiUtilities.setVisibilitySafe(this, R.id.account_sync_contacts_divider, View.VISIBLE); 132 } 133 if (mServiceInfo.syncCalendar) { 134 mSyncCalendarView.setVisibility(View.VISIBLE); 135 mSyncCalendarView.setChecked(true); 136 UiUtilities.setVisibilitySafe(this, R.id.account_sync_calendar_divider, View.VISIBLE); 137 } 138 139 if (!mServiceInfo.offerAttachmentPreload) { 140 mBackgroundAttachmentsView.setVisibility(View.GONE); 141 UiUtilities.setVisibilitySafe(this, R.id.account_background_attachments_divider, 142 View.GONE); 143 } 144 145 // If we are just visiting here to fill in details, exit immediately 146 if (SetupData.isAutoSetup() || 147 SetupData.getFlowMode() == SetupData.FLOW_MODE_FORCE_CREATE) { 148 onDone(); 149 } 150 } 151 152 @Override 153 public void finish() { 154 // If the account manager initiated the creation, and success was not reported, 155 // then we assume that we're giving up (for any reason) - report failure. 156 AccountAuthenticatorResponse authenticatorResponse = 157 SetupData.getAccountAuthenticatorResponse(); 158 if (authenticatorResponse != null) { 159 authenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); 160 SetupData.setAccountAuthenticatorResponse(null); 161 } 162 super.finish(); 163 } 164 165 /** 166 * Respond to clicks in the "Next" or "Previous" buttons 167 */ 168 @Override 169 public void onClick(View view) { 170 switch (view.getId()) { 171 case R.id.next: 172 // Don't allow this more than once (Exchange accounts call an async method 173 // before finish()'ing the Activity, which allows this code to potentially be 174 // executed multiple times 175 if (!mDonePressed) { 176 onDone(); 177 mDonePressed = true; 178 } 179 break; 180 case R.id.previous: 181 onBackPressed(); 182 break; 183 } 184 } 185 186 /** 187 * Ths is called when the user clicks the "done" button. 188 * It collects the data from the UI, updates the setup account record, and commits 189 * the account to the database (making it real for the first time.) 190 * Finally, we call setupAccountManagerAccount(), which will eventually complete via callback. 191 */ 192 @SuppressWarnings("deprecation") 193 private void onDone() { 194 final Account account = SetupData.getAccount(); 195 if (account.isSaved()) { 196 // Disrupting the normal flow could get us here, but if the account is already 197 // saved, we've done this work 198 return; 199 } 200 account.setDisplayName(account.getEmailAddress()); 201 int newFlags = account.getFlags() & 202 ~(Account.FLAGS_NOTIFY_NEW_MAIL | Account.FLAGS_BACKGROUND_ATTACHMENTS); 203 if (mNotifyView.isChecked()) { 204 newFlags |= Account.FLAGS_NOTIFY_NEW_MAIL; 205 } 206 if (mBackgroundAttachmentsView.isChecked()) { 207 newFlags |= Account.FLAGS_BACKGROUND_ATTACHMENTS; 208 } 209 account.setFlags(newFlags); 210 account.setSyncInterval((Integer)((SpinnerOption)mCheckFrequencyView 211 .getSelectedItem()).value); 212 if (mAccountSyncWindowRow.getVisibility() == View.VISIBLE) { 213 int window = (Integer)((SpinnerOption)mSyncWindowView.getSelectedItem()).value; 214 account.setSyncLookback(window); 215 } 216 account.setDefaultAccount(mDefaultView.isChecked()); 217 218 if (account.mHostAuthRecv == null) { 219 throw new IllegalStateException("in AccountSetupOptions with null mHostAuthRecv"); 220 } 221 222 // Finish setting up the account, and commit it to the database 223 // Set the incomplete flag here to avoid reconciliation issues in ExchangeService 224 account.mFlags |= Account.FLAGS_INCOMPLETE; 225 if (SetupData.getPolicy() != null) { 226 account.mFlags |= Account.FLAGS_SECURITY_HOLD; 227 account.mPolicy = SetupData.getPolicy(); 228 } 229 boolean contacts = false; 230 if (mServiceInfo.syncContacts) { 231 contacts = mSyncContactsView.isChecked(); 232 } 233 boolean calendar = false; 234 if (mServiceInfo.syncCalendar) { 235 calendar = mSyncCalendarView.isChecked(); 236 } 237 boolean email = mSyncEmailView.isChecked(); 238 239 // Finally, write the completed account (for the first time) and then 240 // install it into the Account manager as well. These are done off-thread. 241 // The account manager will report back via the callback, which will take us to 242 // the next operations. 243 final boolean email2 = email; 244 final boolean calendar2 = calendar; 245 final boolean contacts2 = contacts; 246 247 Utility.runAsync(new Runnable() { 248 @Override 249 public void run() { 250 Context context = AccountSetupOptions.this; 251 AccountSettingsUtils.commitSettings(context, account); 252 MailService.setupAccountManagerAccount(context, account, 253 email2, calendar2, contacts2, mAccountManagerCallback); 254 } 255 }); 256 } 257 258 /** 259 * This is called at the completion of MailService.setupAccountManagerAccount() 260 */ 261 AccountManagerCallback<Bundle> mAccountManagerCallback = new AccountManagerCallback<Bundle>() { 262 @Override 263 public void run(AccountManagerFuture<Bundle> future) { 264 try { 265 Bundle bundle = future.getResult(); 266 bundle.keySet(); 267 AccountSetupOptions.this.runOnUiThread(new Runnable() { 268 @Override 269 public void run() { 270 optionsComplete(); 271 } 272 }); 273 return; 274 } catch (OperationCanceledException e) { 275 Log.d(Logging.LOG_TAG, "addAccount was canceled"); 276 } catch (IOException e) { 277 Log.d(Logging.LOG_TAG, "addAccount failed: " + e); 278 } catch (AuthenticatorException e) { 279 Log.d(Logging.LOG_TAG, "addAccount failed: " + e); 280 } 281 showErrorDialog(R.string.account_setup_failed_dlg_auth_message, 282 R.string.system_account_create_failed); 283 } 284 }; 285 286 /** 287 * This is called if MailService.setupAccountManagerAccount() fails for some reason 288 */ 289 private void showErrorDialog(final int msgResId, final Object... args) { 290 runOnUiThread(new Runnable() { 291 @Override 292 public void run() { 293 new AlertDialog.Builder(AccountSetupOptions.this) 294 .setIconAttribute(android.R.attr.alertDialogIcon) 295 .setTitle(getString(R.string.account_setup_failed_dlg_title)) 296 .setMessage(getString(msgResId, args)) 297 .setCancelable(true) 298 .setPositiveButton( 299 getString(R.string.account_setup_failed_dlg_edit_details_action), 300 new DialogInterface.OnClickListener() { 301 @Override 302 public void onClick(DialogInterface dialog, int which) { 303 finish(); 304 } 305 }) 306 .show(); 307 } 308 }); 309 } 310 311 /** 312 * This is called after the account manager creates the new account. 313 */ 314 private void optionsComplete() { 315 // If the account manager initiated the creation, report success at this point 316 AccountAuthenticatorResponse authenticatorResponse = 317 SetupData.getAccountAuthenticatorResponse(); 318 if (authenticatorResponse != null) { 319 authenticatorResponse.onResult(null); 320 SetupData.setAccountAuthenticatorResponse(null); 321 } 322 323 // Now that AccountManager account creation is complete, clear the INCOMPLETE flag 324 Account account = SetupData.getAccount(); 325 account.mFlags &= ~Account.FLAGS_INCOMPLETE; 326 AccountSettingsUtils.commitSettings(AccountSetupOptions.this, account); 327 328 // If we've got policies for this account, ask the user to accept. 329 if ((account.mFlags & Account.FLAGS_SECURITY_HOLD) != 0) { 330 Intent intent = AccountSecurity.actionUpdateSecurityIntent(this, account.mId, false); 331 startActivityForResult(intent, AccountSetupOptions.REQUEST_CODE_ACCEPT_POLICIES); 332 return; 333 } 334 saveAccountAndFinish(); 335 } 336 337 /** 338 * This is called after the AccountSecurity activity completes. 339 */ 340 @Override 341 public void onActivityResult(int requestCode, int resultCode, Intent data) { 342 saveAccountAndFinish(); 343 } 344 345 /** 346 * These are the final cleanup steps when creating an account: 347 * Clear incomplete & security hold flags 348 * Update account in DB 349 * Enable email services 350 * Enable exchange services 351 * Move to final setup screen 352 */ 353 @SuppressWarnings("deprecation") 354 private void saveAccountAndFinish() { 355 Utility.runAsync(new Runnable() { 356 @Override 357 public void run() { 358 AccountSetupOptions context = AccountSetupOptions.this; 359 // Clear the security hold flag now 360 Account account = SetupData.getAccount(); 361 account.mFlags &= ~Account.FLAGS_SECURITY_HOLD; 362 AccountSettingsUtils.commitSettings(context, account); 363 // Start up services based on new account(s) 364 MailActivityEmail.setServicesEnabledSync(context); 365 EmailServiceUtils.startService(context, account.mHostAuthRecv.mProtocol); 366 // Move to final setup screen 367 AccountSetupNames.actionSetNames(context); 368 finish(); 369 } 370 }); 371 } 372 373 /** 374 * Enable an additional spinner using the arrays normally handled by preferences 375 */ 376 private void enableLookbackSpinner() { 377 // Show everything 378 mAccountSyncWindowRow.setVisibility(View.VISIBLE); 379 380 // Generate spinner entries using XML arrays used by the preferences 381 CharSequence[] windowValues = getResources().getTextArray( 382 R.array.account_settings_mail_window_values); 383 CharSequence[] windowEntries = getResources().getTextArray( 384 R.array.account_settings_mail_window_entries); 385 386 // Find a proper maximum for email lookback, based on policy (if we have one) 387 int maxEntry = windowEntries.length; 388 Policy policy = SetupData.getAccount().mPolicy; 389 if (policy != null) { 390 int maxLookback = policy.mMaxEmailLookback; 391 if (maxLookback != 0) { 392 // Offset/Code 0 1 2 3 4 5 393 // Entries auto, 1 day, 3 day, 1 week, 2 week, 1 month 394 // Lookback N/A 1 day, 3 day, 1 week, 2 week, 1 month 395 // Since our test below is i < maxEntry, we must set maxEntry to maxLookback + 1 396 maxEntry = maxLookback + 1; 397 } 398 } 399 400 // Now create the array used by the Spinner 401 SpinnerOption[] windowOptions = new SpinnerOption[maxEntry]; 402 int defaultIndex = -1; 403 for (int i = 0; i < maxEntry; i++) { 404 final int value = Integer.valueOf(windowValues[i].toString()); 405 windowOptions[i] = new SpinnerOption(value, windowEntries[i].toString()); 406 if (value == SYNC_WINDOW_EAS_DEFAULT) { 407 defaultIndex = i; 408 } 409 } 410 411 ArrayAdapter<SpinnerOption> windowOptionsAdapter = new ArrayAdapter<SpinnerOption>(this, 412 android.R.layout.simple_spinner_item, windowOptions); 413 windowOptionsAdapter 414 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 415 mSyncWindowView.setAdapter(windowOptionsAdapter); 416 417 SpinnerOption.setSpinnerOptionValue(mSyncWindowView, 418 SetupData.getAccount().getSyncLookback()); 419 if (defaultIndex >= 0) { 420 mSyncWindowView.setSelection(defaultIndex); 421 } 422 } 423} 424