package com.fsck.k9.activity.setup; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Process; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import com.fsck.k9.Account; import com.fsck.k9.R; import com.fsck.k9.mail.AuthenticationFailedException; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Store; import com.fsck.k9.mail.Transport; import com.fsck.k9.mail.CertificateValidationException; /** * Checks the given settings to make sure that they can be used to send and * receive mail. * * XXX NOTE: The manifest for this app has it ignore config changes, because * it doesn't correctly deal with restarting while its thread is running. */ public class AccountSetupCheckSettings extends Activity implements OnClickListener { private static final String EXTRA_ACCOUNT = "account"; private static final String EXTRA_CHECK_INCOMING = "checkIncoming"; private static final String EXTRA_CHECK_OUTGOING = "checkOutgoing"; private Handler mHandler = new Handler(); private ProgressBar mProgressBar; private TextView mMessageView; private Account mAccount; private boolean mCheckIncoming; private boolean mCheckOutgoing; private boolean mCanceled; private boolean mDestroyed; public static void actionCheckSettings(Activity context, Account account, boolean checkIncoming, boolean checkOutgoing) { Intent i = new Intent(context, AccountSetupCheckSettings.class); i.putExtra(EXTRA_ACCOUNT, account); i.putExtra(EXTRA_CHECK_INCOMING, checkIncoming); i.putExtra(EXTRA_CHECK_OUTGOING, checkOutgoing); context.startActivityForResult(i, 1); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.account_setup_check_settings); mMessageView = (TextView)findViewById(R.id.message); mProgressBar = (ProgressBar)findViewById(R.id.progress); ((Button)findViewById(R.id.cancel)).setOnClickListener(this); setMessage(R.string.account_setup_check_settings_retr_info_msg); mProgressBar.setIndeterminate(true); mAccount = (Account)getIntent().getSerializableExtra(EXTRA_ACCOUNT); mCheckIncoming = (boolean)getIntent().getBooleanExtra(EXTRA_CHECK_INCOMING, false); mCheckOutgoing = (boolean)getIntent().getBooleanExtra(EXTRA_CHECK_OUTGOING, false); new Thread() { public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); try { if (mDestroyed) { return; } if (mCanceled) { finish(); return; } if (mCheckIncoming) { setMessage(R.string.account_setup_check_settings_check_incoming_msg); Store store = Store.getInstance(mAccount.getStoreUri(), getApplication()); store.checkSettings(); } if (mDestroyed) { return; } if (mCanceled) { finish(); return; } if (mCheckOutgoing) { setMessage(R.string.account_setup_check_settings_check_outgoing_msg); Transport transport = Transport.getInstance(mAccount.getTransportUri()); transport.close(); transport.open(); transport.close(); } if (mDestroyed) { return; } if (mCanceled) { finish(); return; } setResult(RESULT_OK); finish(); } catch (final AuthenticationFailedException afe) { showErrorDialog( R.string.account_setup_failed_dlg_auth_message_fmt, afe.getMessage() == null ? "" : afe.getMessage()); } catch (final CertificateValidationException cve) { showErrorDialog( R.string.account_setup_failed_dlg_certificate_message_fmt, cve.getMessage() == null ? "" : cve.getMessage()); } catch (final MessagingException me) { showErrorDialog( R.string.account_setup_failed_dlg_server_message_fmt, me.getMessage() == null ? "" : me.getMessage()); } } }.start(); } @Override public void onDestroy() { super.onDestroy(); mDestroyed = true; mCanceled = true; } private void setMessage(final int resId) { mHandler.post(new Runnable() { public void run() { if (mDestroyed) { return; } mMessageView.setText(getString(resId)); } }); } private void showErrorDialog(final int msgResId, final Object... args) { mHandler.post(new Runnable() { public void run() { if (mDestroyed) { return; } mProgressBar.setIndeterminate(false); new AlertDialog.Builder(AccountSetupCheckSettings.this) .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 onCancel() { mCanceled = true; setMessage(R.string.account_setup_check_settings_canceling_msg); } public void onClick(View v) { switch (v.getId()) { case R.id.cancel: onCancel(); break; } } }