From 69ee6a48187d7c1cde3bc3cdb292f4858d7c5c56 Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 17 Mar 2012 20:12:33 +0100 Subject: [PATCH] Avoid NullPointerException in Accounts.onCreateDialog() Also cleaned up code formatting of onCreateDialog() and onPrepareDialog(). --- src/com/fsck/k9/activity/Accounts.java | 195 ++++++++++++++----------- 1 file changed, 108 insertions(+), 87 deletions(-) diff --git a/src/com/fsck/k9/activity/Accounts.java b/src/com/fsck/k9/activity/Accounts.java index d31bced32..bb8d9a96a 100644 --- a/src/com/fsck/k9/activity/Accounts.java +++ b/src/com/fsck/k9/activity/Accounts.java @@ -918,107 +918,128 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC @Override public Dialog onCreateDialog(int id) { + // Android recreates our dialogs on configuration changes even when they have been + // dismissed. Make sure we have all information necessary before creating a new dialog. switch (id) { - case DIALOG_REMOVE_ACCOUNT: - return ConfirmationDialog.create(this, id, - R.string.account_delete_dlg_title, - getString(R.string.account_delete_dlg_instructions_fmt, - mSelectedContextAccount.getDescription()), - R.string.okay_action, - R.string.cancel_action, - new Runnable() { - @Override - public void run() { - if (mSelectedContextAccount instanceof Account) { - Account realAccount = (Account)mSelectedContextAccount; - try { - realAccount.getLocalStore().delete(); - } catch (Exception e) { - // Ignore, this may lead to localStores on sd-cards that are - // currently not inserted to be left - } - MessagingController.getInstance(getApplication()) - .notifyAccountCancel(Accounts.this, realAccount); - Preferences.getPreferences(Accounts.this).deleteAccount(realAccount); - K9.setServicesEnabled(Accounts.this); - refresh(); - } + case DIALOG_REMOVE_ACCOUNT: { + if (mSelectedContextAccount == null) { + return null; } - }); - case DIALOG_CLEAR_ACCOUNT: - return ConfirmationDialog.create(this, id, - R.string.account_clear_dlg_title, - getString(R.string.account_clear_dlg_instructions_fmt, - mSelectedContextAccount.getDescription()), - R.string.okay_action, - R.string.cancel_action, - new Runnable() { - @Override - public void run() { - if (mSelectedContextAccount instanceof Account) { - Account realAccount = (Account)mSelectedContextAccount; - mHandler.workingAccount(realAccount, R.string.clearing_account); - MessagingController.getInstance(getApplication()).clear(realAccount, null); - } + return ConfirmationDialog.create(this, id, + R.string.account_delete_dlg_title, + getString(R.string.account_delete_dlg_instructions_fmt, + mSelectedContextAccount.getDescription()), + R.string.okay_action, + R.string.cancel_action, + new Runnable() { + @Override + public void run() { + if (mSelectedContextAccount instanceof Account) { + Account realAccount = (Account) mSelectedContextAccount; + try { + realAccount.getLocalStore().delete(); + } catch (Exception e) { + // Ignore, this may lead to localStores on sd-cards that + // are currently not inserted to be left + } + MessagingController.getInstance(getApplication()) + .notifyAccountCancel(Accounts.this, realAccount); + Preferences.getPreferences(Accounts.this) + .deleteAccount(realAccount); + K9.setServicesEnabled(Accounts.this); + refresh(); + } + } + }); + } + case DIALOG_CLEAR_ACCOUNT: { + if (mSelectedContextAccount == null) { + return null; } - }); - case DIALOG_RECREATE_ACCOUNT: - return ConfirmationDialog.create(this, id, - R.string.account_recreate_dlg_title, - getString(R.string.account_recreate_dlg_instructions_fmt, - mSelectedContextAccount.getDescription()), - R.string.okay_action, - R.string.cancel_action, - new Runnable() { - @Override - public void run() { - if (mSelectedContextAccount instanceof Account) { - Account realAccount = (Account)mSelectedContextAccount; - mHandler.workingAccount(realAccount, R.string.recreating_account); - MessagingController.getInstance(getApplication()).recreate(realAccount, null); - } + return ConfirmationDialog.create(this, id, + R.string.account_clear_dlg_title, + getString(R.string.account_clear_dlg_instructions_fmt, + mSelectedContextAccount.getDescription()), + R.string.okay_action, + R.string.cancel_action, + new Runnable() { + @Override + public void run() { + if (mSelectedContextAccount instanceof Account) { + Account realAccount = (Account) mSelectedContextAccount; + mHandler.workingAccount(realAccount, + R.string.clearing_account); + MessagingController.getInstance(getApplication()) + .clear(realAccount, null); + } + } + }); + } + case DIALOG_RECREATE_ACCOUNT: { + if (mSelectedContextAccount == null) { + return null; } - }); - case DIALOG_NO_FILE_MANAGER: - return ConfirmationDialog.create(this, id, - R.string.import_dialog_error_title, - getString(R.string.import_dialog_error_message), - R.string.open_market, - R.string.close, - new Runnable() { - @Override - public void run() { - Uri uri = Uri.parse(ANDROID_MARKET_URL); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(intent); - } - }); + + return ConfirmationDialog.create(this, id, + R.string.account_recreate_dlg_title, + getString(R.string.account_recreate_dlg_instructions_fmt, + mSelectedContextAccount.getDescription()), + R.string.okay_action, + R.string.cancel_action, + new Runnable() { + @Override + public void run() { + if (mSelectedContextAccount instanceof Account) { + Account realAccount = (Account) mSelectedContextAccount; + mHandler.workingAccount(realAccount, + R.string.recreating_account); + MessagingController.getInstance(getApplication()) + .recreate(realAccount, null); + } + } + }); + } + case DIALOG_NO_FILE_MANAGER: { + return ConfirmationDialog.create(this, id, + R.string.import_dialog_error_title, + getString(R.string.import_dialog_error_message), + R.string.open_market, + R.string.close, + new Runnable() { + @Override + public void run() { + Uri uri = Uri.parse(ANDROID_MARKET_URL); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + startActivity(intent); + } + }); + } } + return super.onCreateDialog(id); } @Override public void onPrepareDialog(int id, Dialog d) { - AlertDialog alert = (AlertDialog) d; switch (id) { - case DIALOG_REMOVE_ACCOUNT: - alert.setMessage(getString(R.string.account_delete_dlg_instructions_fmt, - mSelectedContextAccount.getDescription())); - break; - case DIALOG_CLEAR_ACCOUNT: - alert.setMessage(getString(R.string.account_clear_dlg_instructions_fmt, - mSelectedContextAccount.getDescription())); - break; - case DIALOG_RECREATE_ACCOUNT: - alert.setMessage(getString(R.string.account_recreate_dlg_instructions_fmt, - mSelectedContextAccount.getDescription())); - break; - case DIALOG_NO_FILE_MANAGER: - alert.setMessage(getString(R.string.import_dialog_error_message)); - break; + case DIALOG_REMOVE_ACCOUNT: { + alert.setMessage(getString(R.string.account_delete_dlg_instructions_fmt, + mSelectedContextAccount.getDescription())); + break; + } + case DIALOG_CLEAR_ACCOUNT: { + alert.setMessage(getString(R.string.account_clear_dlg_instructions_fmt, + mSelectedContextAccount.getDescription())); + break; + } + case DIALOG_RECREATE_ACCOUNT: { + alert.setMessage(getString(R.string.account_recreate_dlg_instructions_fmt, + mSelectedContextAccount.getDescription())); + break; + } } super.onPrepareDialog(id, d);