2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.activity.setup;
|
|
|
|
|
2009-04-03 22:40:49 +00:00
|
|
|
import com.android.email.Email;
|
|
|
|
import com.android.email.R;
|
2009-04-08 00:35:13 +00:00
|
|
|
import com.android.email.mail.Store;
|
2009-07-27 17:24:58 +00:00
|
|
|
import com.android.email.mail.store.ExchangeStore;
|
2009-06-15 21:40:06 +00:00
|
|
|
import com.android.email.provider.EmailContent;
|
2009-04-03 22:40:49 +00:00
|
|
|
|
2009-08-26 19:23:02 +00:00
|
|
|
import android.accounts.AccountManagerCallback;
|
|
|
|
import android.accounts.AccountManagerFuture;
|
|
|
|
import android.accounts.AuthenticatorException;
|
|
|
|
import android.accounts.OperationCanceledException;
|
2009-03-04 03:32:22 +00:00
|
|
|
import android.app.Activity;
|
2009-08-26 19:23:02 +00:00
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.DialogInterface;
|
2009-03-04 03:32:22 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2009-08-26 19:23:02 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.util.Log;
|
2009-03-04 03:32:22 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.Spinner;
|
|
|
|
|
2009-08-26 19:23:02 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
public class AccountSetupOptions extends Activity implements OnClickListener {
|
|
|
|
|
2009-06-05 23:09:18 +00:00
|
|
|
private static final String EXTRA_ACCOUNT = "account";
|
2009-03-04 03:32:22 +00:00
|
|
|
private static final String EXTRA_MAKE_DEFAULT = "makeDefault";
|
2009-08-20 22:51:36 +00:00
|
|
|
private static final String EXTRA_EAS_FLOW = "easFlow";
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
private Spinner mCheckFrequencyView;
|
2009-05-01 22:30:33 +00:00
|
|
|
private Spinner mSyncWindowView;
|
2009-03-04 03:32:22 +00:00
|
|
|
private CheckBox mDefaultView;
|
|
|
|
private CheckBox mNotifyView;
|
2009-08-20 22:51:36 +00:00
|
|
|
private CheckBox mSyncContactsView;
|
2009-06-15 21:40:06 +00:00
|
|
|
private EmailContent.Account mAccount;
|
2009-08-20 22:51:36 +00:00
|
|
|
private boolean mEasFlowMode;
|
2009-08-26 19:23:02 +00:00
|
|
|
private Handler mHandler = new Handler();
|
2009-06-03 23:11:36 +00:00
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
public static void actionOptions(Activity fromActivity, EmailContent.Account account,
|
2009-08-20 22:51:36 +00:00
|
|
|
boolean makeDefault, boolean easFlowMode) {
|
2009-06-03 23:11:36 +00:00
|
|
|
Intent i = new Intent(fromActivity, AccountSetupOptions.class);
|
|
|
|
i.putExtra(EXTRA_ACCOUNT, account);
|
|
|
|
i.putExtra(EXTRA_MAKE_DEFAULT, makeDefault);
|
2009-08-20 22:51:36 +00:00
|
|
|
i.putExtra(EXTRA_EAS_FLOW, easFlowMode);
|
2009-06-03 23:11:36 +00:00
|
|
|
fromActivity.startActivity(i);
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.account_setup_options);
|
|
|
|
|
|
|
|
mCheckFrequencyView = (Spinner)findViewById(R.id.account_check_frequency);
|
2009-05-01 22:30:33 +00:00
|
|
|
mSyncWindowView = (Spinner) findViewById(R.id.account_sync_window);
|
2009-03-04 03:32:22 +00:00
|
|
|
mDefaultView = (CheckBox)findViewById(R.id.account_default);
|
|
|
|
mNotifyView = (CheckBox)findViewById(R.id.account_notify);
|
2009-08-20 22:51:36 +00:00
|
|
|
mSyncContactsView = (CheckBox) findViewById(R.id.account_sync_contacts);
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
findViewById(R.id.next).setOnClickListener(this);
|
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
mAccount = (EmailContent.Account) getIntent().getParcelableExtra(EXTRA_ACCOUNT);
|
2009-04-08 00:35:13 +00:00
|
|
|
boolean makeDefault = getIntent().getBooleanExtra(EXTRA_MAKE_DEFAULT, false);
|
2009-08-26 19:23:02 +00:00
|
|
|
|
2009-04-08 00:35:13 +00:00
|
|
|
// Generate spinner entries using XML arrays used by the preferences
|
|
|
|
int frequencyValuesId;
|
|
|
|
int frequencyEntriesId;
|
2009-06-05 23:09:18 +00:00
|
|
|
Store.StoreInfo info = Store.StoreInfo.getStoreInfo(mAccount.getStoreUri(this), this);
|
2009-04-08 00:35:13 +00:00
|
|
|
if (info.mPushSupported) {
|
|
|
|
frequencyValuesId = R.array.account_settings_check_frequency_values_push;
|
|
|
|
frequencyEntriesId = R.array.account_settings_check_frequency_entries_push;
|
|
|
|
} else {
|
|
|
|
frequencyValuesId = R.array.account_settings_check_frequency_values;
|
|
|
|
frequencyEntriesId = R.array.account_settings_check_frequency_entries;
|
|
|
|
}
|
|
|
|
CharSequence[] frequencyValues = getResources().getTextArray(frequencyValuesId);
|
|
|
|
CharSequence[] frequencyEntries = getResources().getTextArray(frequencyEntriesId);
|
2009-08-26 19:23:02 +00:00
|
|
|
|
2009-04-08 00:35:13 +00:00
|
|
|
// Now create the array used by the Spinner
|
|
|
|
SpinnerOption[] checkFrequencies = new SpinnerOption[frequencyEntries.length];
|
|
|
|
for (int i = 0; i < frequencyEntries.length; i++) {
|
|
|
|
checkFrequencies[i] = new SpinnerOption(
|
|
|
|
Integer.valueOf(frequencyValues[i].toString()), frequencyEntries[i].toString());
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
ArrayAdapter<SpinnerOption> checkFrequenciesAdapter = new ArrayAdapter<SpinnerOption>(this,
|
|
|
|
android.R.layout.simple_spinner_item, checkFrequencies);
|
|
|
|
checkFrequenciesAdapter
|
|
|
|
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
|
mCheckFrequencyView.setAdapter(checkFrequenciesAdapter);
|
2009-08-26 19:23:02 +00:00
|
|
|
|
2009-05-01 22:30:33 +00:00
|
|
|
if (info.mVisibleLimitDefault == -1) {
|
|
|
|
enableEASSyncWindowSpinner();
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2009-07-21 23:44:16 +00:00
|
|
|
// Note: It is OK to use mAccount.mIsDefault here *only* because the account
|
|
|
|
// has not been written to the DB yet. Ordinarily, call Account.getDefaultAccountId().
|
2009-06-05 23:09:18 +00:00
|
|
|
if (mAccount.mIsDefault || makeDefault) {
|
2009-03-04 03:32:22 +00:00
|
|
|
mDefaultView.setChecked(true);
|
|
|
|
}
|
2009-06-05 23:09:18 +00:00
|
|
|
mNotifyView.setChecked(
|
2009-06-15 21:40:06 +00:00
|
|
|
(mAccount.getFlags() & EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL) != 0);
|
2009-03-04 03:32:22 +00:00
|
|
|
SpinnerOption.setSpinnerOptionValue(mCheckFrequencyView, mAccount
|
2009-07-22 22:13:30 +00:00
|
|
|
.getSyncInterval());
|
2009-08-20 22:51:36 +00:00
|
|
|
|
|
|
|
// Setup any additional items to support EAS & EAS flow mode
|
|
|
|
mEasFlowMode = getIntent().getBooleanExtra(EXTRA_EAS_FLOW, false);
|
|
|
|
if ("eas".equals(info.mScheme)) {
|
|
|
|
// "also sync contacts" == "true"
|
|
|
|
mSyncContactsView.setVisibility(View.VISIBLE);
|
|
|
|
mSyncContactsView.setChecked(true);
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
2009-08-26 19:23:02 +00:00
|
|
|
AccountManagerCallback<Bundle> mAccountManagerCallback = new AccountManagerCallback<Bundle>() {
|
|
|
|
public void run(AccountManagerFuture<Bundle> future) {
|
|
|
|
try {
|
|
|
|
Bundle bundle = future.getResult();
|
|
|
|
bundle.keySet();
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
finishOnDone();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} catch (OperationCanceledException e) {
|
|
|
|
Log.d(Email.LOG_TAG, "addAccount was canceled");
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.d(Email.LOG_TAG, "addAccount failed: " + e);
|
|
|
|
} catch (AuthenticatorException e) {
|
|
|
|
Log.d(Email.LOG_TAG, "addAccount failed: " + e);
|
|
|
|
}
|
|
|
|
showErrorDialog(R.string.account_setup_failed_dlg_auth_message,
|
|
|
|
R.string.system_account_create_failed);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private void showErrorDialog(final int msgResId, final Object... args) {
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
new AlertDialog.Builder(AccountSetupOptions.this)
|
|
|
|
.setIcon(android.R.drawable.ic_dialog_alert)
|
|
|
|
.setTitle(getString(R.string.account_setup_failed_dlg_title))
|
|
|
|
.setMessage(getString(msgResId, args))
|
|
|
|
.setCancelable(true)
|
|
|
|
.setPositiveButton(
|
|
|
|
getString(R.string.account_setup_failed_dlg_edit_details_action),
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
private void finishOnDone() {
|
|
|
|
AccountSettingsUtils.commitSettings(this, mAccount);
|
|
|
|
Email.setServicesEnabled(this);
|
|
|
|
AccountSetupNames.actionSetNames(this, mAccount.mId, mEasFlowMode);
|
2009-09-19 03:34:29 +00:00
|
|
|
// Start up SyncManager (if it isn't already running)
|
|
|
|
startService(new Intent(getApplicationContext(), com.android.exchange.SyncManager.class));
|
2009-08-26 19:23:02 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
private void onDone() {
|
2009-07-22 22:13:30 +00:00
|
|
|
mAccount.setDisplayName(mAccount.getEmailAddress());
|
2009-06-15 21:40:06 +00:00
|
|
|
int newFlags = mAccount.getFlags() & ~(EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL);
|
2009-06-05 23:09:18 +00:00
|
|
|
if (mNotifyView.isChecked()) {
|
2009-06-15 21:40:06 +00:00
|
|
|
newFlags |= EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL;
|
2009-06-05 23:09:18 +00:00
|
|
|
}
|
|
|
|
mAccount.setFlags(newFlags);
|
2009-07-22 22:13:30 +00:00
|
|
|
mAccount.setSyncInterval((Integer)((SpinnerOption)mCheckFrequencyView
|
2009-03-04 03:32:22 +00:00
|
|
|
.getSelectedItem()).value);
|
2009-05-01 22:30:33 +00:00
|
|
|
if (mSyncWindowView.getVisibility() == View.VISIBLE) {
|
|
|
|
int window = (Integer)((SpinnerOption)mSyncWindowView.getSelectedItem()).value;
|
2009-07-22 22:13:30 +00:00
|
|
|
mAccount.setSyncLookback(window);
|
2009-05-01 22:30:33 +00:00
|
|
|
}
|
2009-06-05 23:09:18 +00:00
|
|
|
mAccount.setDefaultAccount(mDefaultView.isChecked());
|
2009-08-20 22:51:36 +00:00
|
|
|
|
|
|
|
// Call EAS to store account information for use by AccountManager
|
|
|
|
if (!mAccount.isSaved()
|
|
|
|
&& mAccount.mHostAuthRecv != null
|
2009-07-27 17:24:58 +00:00
|
|
|
&& mAccount.mHostAuthRecv.mProtocol.equals("eas")) {
|
2009-09-10 01:37:42 +00:00
|
|
|
boolean alsoSyncContacts = mSyncContactsView.isChecked();
|
2009-09-22 17:24:39 +00:00
|
|
|
// NOTE: We must use the Application here, rather than the current context, because
|
|
|
|
// all future references to AccountManager will end up using the context passed in here!
|
|
|
|
ExchangeStore.addSystemAccount(getApplication(), mAccount,
|
2009-08-26 19:23:02 +00:00
|
|
|
alsoSyncContacts, mAccountManagerCallback);
|
|
|
|
} else {
|
|
|
|
finishOnDone();
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
switch (v.getId()) {
|
|
|
|
case R.id.next:
|
|
|
|
onDone();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-26 19:23:02 +00:00
|
|
|
|
2009-05-01 22:30:33 +00:00
|
|
|
/**
|
|
|
|
* Enable an additional spinner using the arrays normally handled by preferences
|
|
|
|
*/
|
|
|
|
private void enableEASSyncWindowSpinner() {
|
|
|
|
// Show everything
|
|
|
|
findViewById(R.id.account_sync_window_label).setVisibility(View.VISIBLE);
|
|
|
|
mSyncWindowView.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
// Generate spinner entries using XML arrays used by the preferences
|
|
|
|
CharSequence[] windowValues = getResources().getTextArray(
|
|
|
|
R.array.account_settings_mail_window_values);
|
|
|
|
CharSequence[] windowEntries = getResources().getTextArray(
|
|
|
|
R.array.account_settings_mail_window_entries);
|
2009-08-26 19:23:02 +00:00
|
|
|
|
2009-05-01 22:30:33 +00:00
|
|
|
// Now create the array used by the Spinner
|
|
|
|
SpinnerOption[] windowOptions = new SpinnerOption[windowEntries.length];
|
|
|
|
for (int i = 0; i < windowEntries.length; i++) {
|
|
|
|
windowOptions[i] = new SpinnerOption(
|
|
|
|
Integer.valueOf(windowValues[i].toString()), windowEntries[i].toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayAdapter<SpinnerOption> windowOptionsAdapter = new ArrayAdapter<SpinnerOption>(this,
|
|
|
|
android.R.layout.simple_spinner_item, windowOptions);
|
|
|
|
windowOptionsAdapter
|
|
|
|
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
|
mSyncWindowView.setAdapter(windowOptionsAdapter);
|
2009-08-26 19:23:02 +00:00
|
|
|
|
2009-07-22 22:13:30 +00:00
|
|
|
SpinnerOption.setSpinnerOptionValue(mSyncWindowView, mAccount.getSyncLookback());
|
2009-05-01 22:30:33 +00:00
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|