AccountSetupOptions.java revision 0b8b68cbebdba372c7302a207e2321cd80b384ae
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.Email; 20import com.android.email.R; 21import com.android.email.mail.Store; 22import com.android.email.mail.store.ExchangeStore; 23import com.android.email.provider.EmailContent; 24import com.android.email.provider.EmailContent.Account; 25 26import android.accounts.AccountManagerCallback; 27import android.accounts.AccountManagerFuture; 28import android.accounts.AuthenticatorException; 29import android.accounts.OperationCanceledException; 30import android.app.Activity; 31import android.app.AlertDialog; 32import android.content.DialogInterface; 33import android.content.Intent; 34import android.os.Bundle; 35import android.os.Handler; 36import android.util.Log; 37import android.view.View; 38import android.view.View.OnClickListener; 39import android.widget.ArrayAdapter; 40import android.widget.CheckBox; 41import android.widget.Spinner; 42 43import java.io.IOException; 44 45public class AccountSetupOptions extends Activity implements OnClickListener { 46 47 private static final String EXTRA_ACCOUNT = "account"; 48 private static final String EXTRA_MAKE_DEFAULT = "makeDefault"; 49 private static final String EXTRA_EAS_FLOW = "easFlow"; 50 51 private Spinner mCheckFrequencyView; 52 private Spinner mSyncWindowView; 53 private CheckBox mDefaultView; 54 private CheckBox mNotifyView; 55 private CheckBox mSyncContactsView; 56 private EmailContent.Account mAccount; 57 private boolean mEasFlowMode; 58 private Handler mHandler = new Handler(); 59 60 public static void actionOptions(Activity fromActivity, EmailContent.Account account, 61 boolean makeDefault, boolean easFlowMode) { 62 Intent i = new Intent(fromActivity, AccountSetupOptions.class); 63 i.putExtra(EXTRA_ACCOUNT, account); 64 i.putExtra(EXTRA_MAKE_DEFAULT, makeDefault); 65 i.putExtra(EXTRA_EAS_FLOW, easFlowMode); 66 fromActivity.startActivity(i); 67 } 68 69 @Override 70 public void onCreate(Bundle savedInstanceState) { 71 super.onCreate(savedInstanceState); 72 setContentView(R.layout.account_setup_options); 73 74 mCheckFrequencyView = (Spinner)findViewById(R.id.account_check_frequency); 75 mSyncWindowView = (Spinner) findViewById(R.id.account_sync_window); 76 mDefaultView = (CheckBox)findViewById(R.id.account_default); 77 mNotifyView = (CheckBox)findViewById(R.id.account_notify); 78 mSyncContactsView = (CheckBox) findViewById(R.id.account_sync_contacts); 79 80 findViewById(R.id.next).setOnClickListener(this); 81 82 mAccount = (EmailContent.Account) getIntent().getParcelableExtra(EXTRA_ACCOUNT); 83 boolean makeDefault = getIntent().getBooleanExtra(EXTRA_MAKE_DEFAULT, false); 84 85 // Generate spinner entries using XML arrays used by the preferences 86 int frequencyValuesId; 87 int frequencyEntriesId; 88 Store.StoreInfo info = Store.StoreInfo.getStoreInfo(mAccount.getStoreUri(this), this); 89 if (info.mPushSupported) { 90 frequencyValuesId = R.array.account_settings_check_frequency_values_push; 91 frequencyEntriesId = R.array.account_settings_check_frequency_entries_push; 92 } else { 93 frequencyValuesId = R.array.account_settings_check_frequency_values; 94 frequencyEntriesId = R.array.account_settings_check_frequency_entries; 95 } 96 CharSequence[] frequencyValues = getResources().getTextArray(frequencyValuesId); 97 CharSequence[] frequencyEntries = getResources().getTextArray(frequencyEntriesId); 98 99 // Now create the array used by the Spinner 100 SpinnerOption[] checkFrequencies = new SpinnerOption[frequencyEntries.length]; 101 for (int i = 0; i < frequencyEntries.length; i++) { 102 checkFrequencies[i] = new SpinnerOption( 103 Integer.valueOf(frequencyValues[i].toString()), frequencyEntries[i].toString()); 104 } 105 106 ArrayAdapter<SpinnerOption> checkFrequenciesAdapter = new ArrayAdapter<SpinnerOption>(this, 107 android.R.layout.simple_spinner_item, checkFrequencies); 108 checkFrequenciesAdapter 109 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 110 mCheckFrequencyView.setAdapter(checkFrequenciesAdapter); 111 112 if (info.mVisibleLimitDefault == -1) { 113 enableEASSyncWindowSpinner(); 114 } 115 116 // Note: It is OK to use mAccount.mIsDefault here *only* because the account 117 // has not been written to the DB yet. Ordinarily, call Account.getDefaultAccountId(). 118 if (mAccount.mIsDefault || makeDefault) { 119 mDefaultView.setChecked(true); 120 } 121 mNotifyView.setChecked( 122 (mAccount.getFlags() & EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL) != 0); 123 SpinnerOption.setSpinnerOptionValue(mCheckFrequencyView, mAccount 124 .getSyncInterval()); 125 126 // Setup any additional items to support EAS & EAS flow mode 127 mEasFlowMode = getIntent().getBooleanExtra(EXTRA_EAS_FLOW, false); 128 if ("eas".equals(info.mScheme)) { 129 // "also sync contacts" == "true" 130 mSyncContactsView.setVisibility(View.VISIBLE); 131 mSyncContactsView.setChecked(true); 132 } 133 } 134 135 AccountManagerCallback<Bundle> mAccountManagerCallback = new AccountManagerCallback<Bundle>() { 136 public void run(AccountManagerFuture<Bundle> future) { 137 try { 138 Bundle bundle = future.getResult(); 139 bundle.keySet(); 140 mHandler.post(new Runnable() { 141 public void run() { 142 finishOnDone(); 143 } 144 }); 145 return; 146 } catch (OperationCanceledException e) { 147 Log.d(Email.LOG_TAG, "addAccount was canceled"); 148 } catch (IOException e) { 149 Log.d(Email.LOG_TAG, "addAccount failed: " + e); 150 } catch (AuthenticatorException e) { 151 Log.d(Email.LOG_TAG, "addAccount failed: " + e); 152 } 153 showErrorDialog(R.string.account_setup_failed_dlg_auth_message, 154 R.string.system_account_create_failed); 155 } 156 }; 157 158 private void showErrorDialog(final int msgResId, final Object... args) { 159 mHandler.post(new Runnable() { 160 public void run() { 161 new AlertDialog.Builder(AccountSetupOptions.this) 162 .setIcon(android.R.drawable.ic_dialog_alert) 163 .setTitle(getString(R.string.account_setup_failed_dlg_title)) 164 .setMessage(getString(msgResId, args)) 165 .setCancelable(true) 166 .setPositiveButton( 167 getString(R.string.account_setup_failed_dlg_edit_details_action), 168 new DialogInterface.OnClickListener() { 169 public void onClick(DialogInterface dialog, int which) { 170 finish(); 171 } 172 }) 173 .show(); 174 } 175 }); 176 } 177 178 private void finishOnDone() { 179 // Clear the incomplete flag now 180 mAccount.mFlags &= ~Account.FLAGS_INCOMPLETE; 181 AccountSettingsUtils.commitSettings(this, mAccount); 182 Email.setServicesEnabled(this); 183 AccountSetupNames.actionSetNames(this, mAccount.mId, mEasFlowMode); 184 // Start up SyncManager (if it isn't already running) 185 startService(new Intent(getApplicationContext(), com.android.exchange.SyncManager.class)); 186 finish(); 187 } 188 189 private void onDone() { 190 mAccount.setDisplayName(mAccount.getEmailAddress()); 191 int newFlags = mAccount.getFlags() & ~(EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL); 192 if (mNotifyView.isChecked()) { 193 newFlags |= EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL; 194 } 195 mAccount.setFlags(newFlags); 196 mAccount.setSyncInterval((Integer)((SpinnerOption)mCheckFrequencyView 197 .getSelectedItem()).value); 198 if (mSyncWindowView.getVisibility() == View.VISIBLE) { 199 int window = (Integer)((SpinnerOption)mSyncWindowView.getSelectedItem()).value; 200 mAccount.setSyncLookback(window); 201 } 202 mAccount.setDefaultAccount(mDefaultView.isChecked()); 203 204 // Call EAS to store account information for use by AccountManager 205 if (!mAccount.isSaved() 206 && mAccount.mHostAuthRecv != null 207 && mAccount.mHostAuthRecv.mProtocol.equals("eas")) { 208 boolean alsoSyncContacts = mSyncContactsView.isChecked(); 209 // Set the incomplete flag here to avoid reconciliation issues in SyncManager (EAS) 210 mAccount.mFlags |= Account.FLAGS_INCOMPLETE; 211 AccountSettingsUtils.commitSettings(this, mAccount); 212 ExchangeStore.addSystemAccount(getApplication(), mAccount, 213 alsoSyncContacts, mAccountManagerCallback); 214 } else { 215 finishOnDone(); 216 } 217 } 218 219 public void onClick(View v) { 220 switch (v.getId()) { 221 case R.id.next: 222 onDone(); 223 break; 224 } 225 } 226 227 /** 228 * Enable an additional spinner using the arrays normally handled by preferences 229 */ 230 private void enableEASSyncWindowSpinner() { 231 // Show everything 232 findViewById(R.id.account_sync_window_label).setVisibility(View.VISIBLE); 233 mSyncWindowView.setVisibility(View.VISIBLE); 234 235 // Generate spinner entries using XML arrays used by the preferences 236 CharSequence[] windowValues = getResources().getTextArray( 237 R.array.account_settings_mail_window_values); 238 CharSequence[] windowEntries = getResources().getTextArray( 239 R.array.account_settings_mail_window_entries); 240 241 // Now create the array used by the Spinner 242 SpinnerOption[] windowOptions = new SpinnerOption[windowEntries.length]; 243 for (int i = 0; i < windowEntries.length; i++) { 244 windowOptions[i] = new SpinnerOption( 245 Integer.valueOf(windowValues[i].toString()), windowEntries[i].toString()); 246 } 247 248 ArrayAdapter<SpinnerOption> windowOptionsAdapter = new ArrayAdapter<SpinnerOption>(this, 249 android.R.layout.simple_spinner_item, windowOptions); 250 windowOptionsAdapter 251 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 252 mSyncWindowView.setAdapter(windowOptionsAdapter); 253 254 SpinnerOption.setSpinnerOptionValue(mSyncWindowView, mAccount.getSyncLookback()); 255 } 256} 257