2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.activity.setup;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
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;
|
2009-04-15 09:05:29 -04:00
|
|
|
import android.util.Log;
|
2008-11-01 17:32:06 -04:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.*;
|
2010-05-19 14:17:06 -04:00
|
|
|
import com.fsck.k9.activity.K9Activity;
|
|
|
|
import com.fsck.k9.controller.MessagingController;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.mail.AuthenticationFailedException;
|
|
|
|
import com.fsck.k9.mail.CertificateValidationException;
|
|
|
|
import com.fsck.k9.mail.Store;
|
|
|
|
import com.fsck.k9.mail.Transport;
|
|
|
|
import com.fsck.k9.mail.store.TrustManagerFactory;
|
2010-10-23 21:03:29 -04:00
|
|
|
import com.fsck.k9.mail.store.WebDavStore;
|
2011-04-04 10:47:56 -04:00
|
|
|
import com.fsck.k9.mail.filter.Hex;
|
2010-10-23 21:03:29 -04:00
|
|
|
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.security.cert.CertificateException;
|
2011-04-04 10:47:56 -04:00
|
|
|
import java.security.cert.CertificateEncodingException;
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.security.cert.X509Certificate;
|
2011-04-04 10:47:56 -04:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.MessageDigest;
|
2009-12-09 22:16:42 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public class AccountSetupCheckSettings extends K9Activity implements OnClickListener {
|
2009-11-21 17:45:39 -05:00
|
|
|
|
|
|
|
public static final int ACTIVITY_REQUEST_CODE = 1;
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
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,
|
2011-02-06 17:09:48 -05:00
|
|
|
boolean checkIncoming, boolean checkOutgoing) {
|
2008-11-01 17:32:06 -04:00
|
|
|
Intent i = new Intent(context, AccountSetupCheckSettings.class);
|
2010-03-03 23:00:30 -05:00
|
|
|
i.putExtra(EXTRA_ACCOUNT, account.getUuid());
|
2008-11-01 17:32:06 -04:00
|
|
|
i.putExtra(EXTRA_CHECK_INCOMING, checkIncoming);
|
|
|
|
i.putExtra(EXTRA_CHECK_OUTGOING, checkOutgoing);
|
2009-03-24 14:20:16 -04:00
|
|
|
context.startActivityForResult(i, ACTIVITY_REQUEST_CODE);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2008-11-01 17:32:06 -04:00
|
|
|
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);
|
|
|
|
|
2010-03-03 23:00:30 -05:00
|
|
|
String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
|
|
|
|
mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
|
2010-11-30 22:06:12 -05:00
|
|
|
mCheckIncoming = getIntent().getBooleanExtra(EXTRA_CHECK_INCOMING, false);
|
|
|
|
mCheckOutgoing = getIntent().getBooleanExtra(EXTRA_CHECK_OUTGOING, false);
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
new Thread() {
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void run() {
|
2009-11-21 17:45:39 -05:00
|
|
|
Store store = null;
|
2008-11-01 17:32:06 -04:00
|
|
|
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
|
|
|
if (mDestroyed) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCanceled) {
|
2008-11-01 17:32:06 -04:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCheckIncoming) {
|
2010-03-03 23:00:30 -05:00
|
|
|
store = mAccount.getRemoteStore();
|
2010-10-23 21:03:29 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (store instanceof WebDavStore) {
|
2010-10-23 21:03:29 -04:00
|
|
|
setMessage(R.string.account_setup_check_settings_authenticate);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-10-23 21:03:29 -04:00
|
|
|
setMessage(R.string.account_setup_check_settings_check_incoming_msg);
|
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
store.checkSettings();
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (store instanceof WebDavStore) {
|
2010-10-23 21:03:29 -04:00
|
|
|
setMessage(R.string.account_setup_check_settings_fetch);
|
|
|
|
}
|
|
|
|
MessagingController.getInstance(getApplication()).listFoldersSynchronous(mAccount, true, null);
|
2010-05-15 15:35:07 -04:00
|
|
|
MessagingController.getInstance(getApplication()).synchronizeMailbox(mAccount, K9.INBOX , null, null);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mDestroyed) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCanceled) {
|
2008-11-01 17:32:06 -04:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCheckOutgoing) {
|
|
|
|
if (!(mAccount.getRemoteStore() instanceof WebDavStore)) {
|
2010-10-23 21:03:29 -04:00
|
|
|
setMessage(R.string.account_setup_check_settings_check_outgoing_msg);
|
|
|
|
}
|
2010-03-03 23:00:30 -05:00
|
|
|
Transport transport = Transport.getInstance(mAccount);
|
2008-11-01 17:32:06 -04:00
|
|
|
transport.close();
|
|
|
|
transport.open();
|
|
|
|
transport.close();
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mDestroyed) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCanceled) {
|
2008-11-01 17:32:06 -04:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setResult(RESULT_OK);
|
|
|
|
finish();
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (final AuthenticationFailedException afe) {
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Error while testing settings", afe);
|
2008-11-01 17:32:06 -04:00
|
|
|
showErrorDialog(
|
2009-11-21 17:45:39 -05:00
|
|
|
R.string.account_setup_failed_dlg_auth_message_fmt,
|
|
|
|
afe.getMessage() == null ? "" : afe.getMessage());
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (final CertificateValidationException cve) {
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Error while testing settings", cve);
|
2009-11-21 17:45:39 -05:00
|
|
|
acceptKeyDialog(
|
|
|
|
R.string.account_setup_failed_dlg_certificate_message_fmt,
|
|
|
|
cve);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (final Throwable t) {
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Error while testing settings", t);
|
2008-11-01 17:32:06 -04:00
|
|
|
showErrorDialog(
|
2009-11-21 17:45:39 -05:00
|
|
|
R.string.account_setup_failed_dlg_server_message_fmt,
|
|
|
|
(t.getMessage() == null ? "" : t.getMessage()));
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
|
2009-11-21 17:45:39 -05:00
|
|
|
}
|
|
|
|
.start();
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onDestroy() {
|
2008-11-01 17:32:06 -04:00
|
|
|
super.onDestroy();
|
|
|
|
mDestroyed = true;
|
|
|
|
mCanceled = true;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private void setMessage(final int resId) {
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (mDestroyed) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mMessageView.setText(getString(resId));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private void showErrorDialog(final int msgResId, final Object... args) {
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (mDestroyed) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mProgressBar.setIndeterminate(false);
|
|
|
|
new AlertDialog.Builder(AccountSetupCheckSettings.this)
|
2009-11-21 17:45:39 -05:00
|
|
|
.setTitle(getString(R.string.account_setup_failed_dlg_title))
|
|
|
|
.setMessage(getString(msgResId, args))
|
|
|
|
.setCancelable(true)
|
|
|
|
.setNegativeButton(
|
|
|
|
getString(R.string.account_setup_failed_dlg_continue_action),
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
mCanceled = false;
|
2009-11-21 17:45:39 -05:00
|
|
|
setResult(RESULT_OK);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setPositiveButton(
|
|
|
|
getString(R.string.account_setup_failed_dlg_edit_details_action),
|
2011-02-06 17:09:48 -05:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2009-11-21 17:45:39 -05:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
private void acceptKeyDialog(final int msgResId, final Object... args) {
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (mDestroyed) {
|
2008-12-02 19:04:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
final X509Certificate[] chain = TrustManagerFactory.getLastCertChain();
|
|
|
|
String exMessage = "Unknown Error";
|
2009-11-21 17:45:39 -05:00
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
Exception ex = ((Exception)args[0]);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (ex != null) {
|
|
|
|
if (ex.getCause() != null) {
|
|
|
|
if (ex.getCause().getCause() != null) {
|
2009-11-21 17:45:39 -05:00
|
|
|
exMessage = ex.getCause().getCause().getMessage();
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2009-11-21 17:45:39 -05:00
|
|
|
exMessage = ex.getCause().getMessage();
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2009-11-21 17:45:39 -05:00
|
|
|
exMessage = ex.getMessage();
|
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
|
2008-12-02 19:04:24 -05:00
|
|
|
mProgressBar.setIndeterminate(false);
|
|
|
|
StringBuffer chainInfo = new StringBuffer(100);
|
2011-04-04 10:47:56 -04:00
|
|
|
MessageDigest sha1 = null;
|
|
|
|
try {
|
|
|
|
sha1 = MessageDigest.getInstance("SHA-1");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Error while initializing MessageDigest", e);
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
for (int i = 0; i < chain.length; i++) {
|
2009-11-21 17:45:39 -05:00
|
|
|
// display certificate chain information
|
2008-12-02 19:04:24 -05:00
|
|
|
chainInfo.append("Certificate chain[" + i + "]:\n");
|
|
|
|
chainInfo.append("Subject: " + chain[i].getSubjectDN().toString() + "\n");
|
|
|
|
chainInfo.append("Issuer: " + chain[i].getIssuerDN().toString() + "\n");
|
2011-04-04 10:47:56 -04:00
|
|
|
if (sha1 != null) {
|
|
|
|
sha1.reset();
|
|
|
|
try {
|
|
|
|
char[] sha1sum = Hex.encodeHex(sha1.digest(chain[i].getEncoded()));
|
|
|
|
chainInfo.append("Fingerprint (SHA-1): " + new String(sha1sum) + "\n");
|
|
|
|
} catch (CertificateEncodingException e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Error while encoding certificate", e);
|
|
|
|
}
|
|
|
|
}
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
new AlertDialog.Builder(AccountSetupCheckSettings.this)
|
2009-11-21 17:45:39 -05:00
|
|
|
.setTitle(getString(R.string.account_setup_failed_dlg_invalid_certificate_title))
|
|
|
|
//.setMessage(getString(R.string.account_setup_failed_dlg_invalid_certificate)
|
2011-02-06 17:09:48 -05:00
|
|
|
.setMessage(getString(msgResId, exMessage)
|
2009-11-21 17:45:39 -05:00
|
|
|
+ " " + chainInfo.toString()
|
|
|
|
)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setPositiveButton(
|
|
|
|
getString(R.string.account_setup_failed_dlg_invalid_certificate_accept),
|
2011-02-06 17:09:48 -05:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
try {
|
2009-11-21 17:45:39 -05:00
|
|
|
String alias = mAccount.getUuid();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCheckIncoming) {
|
2009-11-21 17:45:39 -05:00
|
|
|
alias = alias + ".incoming";
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mCheckOutgoing) {
|
2009-11-21 17:45:39 -05:00
|
|
|
alias = alias + ".outgoing";
|
|
|
|
}
|
|
|
|
TrustManagerFactory.addCertificateChain(alias, chain);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (CertificateException e) {
|
2009-11-21 17:45:39 -05:00
|
|
|
showErrorDialog(
|
|
|
|
R.string.account_setup_failed_dlg_certificate_message_fmt,
|
|
|
|
e.getMessage() == null ? "" : e.getMessage());
|
|
|
|
}
|
|
|
|
AccountSetupCheckSettings.actionCheckSettings(AccountSetupCheckSettings.this, mAccount,
|
|
|
|
mCheckIncoming, mCheckOutgoing);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(
|
|
|
|
getString(R.string.account_setup_failed_dlg_invalid_certificate_reject),
|
2011-02-06 17:09:48 -05:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2009-11-21 17:45:39 -05:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onActivityResult(int reqCode, int resCode, Intent data) {
|
2009-11-21 17:45:39 -05:00
|
|
|
setResult(resCode);
|
|
|
|
finish();
|
2008-12-02 19:04:24 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-11-21 17:45:39 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private void onCancel() {
|
2008-11-01 17:32:06 -04:00
|
|
|
mCanceled = true;
|
|
|
|
setMessage(R.string.account_setup_check_settings_canceling_msg);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onClick(View v) {
|
|
|
|
switch (v.getId()) {
|
|
|
|
case R.id.cancel:
|
|
|
|
onCancel();
|
|
|
|
break;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|