From 73032f4657a77f733a348fe3c2c1fa783d335ff0 Mon Sep 17 00:00:00 2001 From: Mihai Preda Date: Tue, 6 Oct 2009 18:54:51 +0200 Subject: [PATCH] Email: exit AsyncTask.onPostExecute() if the result is null or isCancelled(). Bug: 2167479 --- .../android/email/activity/MailboxList.java | 17 +++++++++++-- .../email/activity/MessageCompose.java | 24 ++++++++++++------- .../android/email/activity/MessageList.java | 11 ++++++++- .../android/email/activity/MessageView.java | 22 +++++++++++++++++ 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/src/com/android/email/activity/MailboxList.java b/src/com/android/email/activity/MailboxList.java index 7a8b43c52..36eda04c1 100644 --- a/src/com/android/email/activity/MailboxList.java +++ b/src/com/android/email/activity/MailboxList.java @@ -102,6 +102,7 @@ public class MailboxList extends ListActivity implements OnItemClickListener, On // DB access private long mAccountId; private LoadMailboxesTask mLoadMailboxesTask; + private AsyncTask mLoadAccountNameTask; /** * Open a specific account. @@ -147,7 +148,7 @@ public class MailboxList extends ListActivity implements OnItemClickListener, On ((TextView)findViewById(R.id.title_left_text)).setText(R.string.mailbox_list_title); // Go to the database for the account name - new AsyncTask() { + mLoadAccountNameTask = new AsyncTask() { @Override protected String doInBackground(Void... params) { String result = null; @@ -166,6 +167,13 @@ public class MailboxList extends ListActivity implements OnItemClickListener, On @Override protected void onPostExecute(String result) { + /* doInBackground() returns null if the account name can't be retrieved from DB. + * so we can't use null test for cancellation, instead use isCancelled(). + */ + if (isCancelled()) { + return; + } + // result is null if account name can't be retrieved or query exception if (result == null) { // something is wrong with this account finish(); @@ -199,6 +207,11 @@ public class MailboxList extends ListActivity implements OnItemClickListener, On mLoadMailboxesTask.cancel(true); mLoadMailboxesTask = null; } + if (mLoadAccountNameTask != null && + mLoadAccountNameTask.getStatus() != LoadMailboxesTask.Status.FINISHED) { + mLoadAccountNameTask.cancel(true); + mLoadAccountNameTask = null; + } } public void onItemClick(AdapterView parent, View view, int position, long id) { @@ -331,7 +344,7 @@ public class MailboxList extends ListActivity implements OnItemClickListener, On @Override protected void onPostExecute(Cursor cursor) { - if (cursor.isClosed()) { + if (cursor == null || cursor.isClosed()) { return; } MailboxList.this.mListAdapter.changeCursor(cursor); diff --git a/src/com/android/email/activity/MessageCompose.java b/src/com/android/email/activity/MessageCompose.java index b3906cc65..c82e1105c 100644 --- a/src/com/android/email/activity/MessageCompose.java +++ b/src/com/android/email/activity/MessageCompose.java @@ -537,7 +537,7 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus protected Object[] doInBackground(Long... messageIds) { Message message = Message.restoreMessageWithId(MessageCompose.this, messageIds[0]); if (message == null) { - return null; + return new Object[] {null, null}; } long accountId = message.mAccountKey; Account account = Account.restoreAccountWithId(MessageCompose.this, accountId); @@ -561,7 +561,7 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus } } catch (RuntimeException e) { Log.d(Email.LOG_TAG, "Exception while loading message body: " + e); - return null; + return new Object[] {null, null}; } return new Object[]{message, account}; } @@ -569,6 +569,12 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus @Override protected void onPostExecute(Object[] messageAndAccount) { if (messageAndAccount == null) { + return; + } + + final Message message = (Message) messageAndAccount[0]; + final Account account = (Account) messageAndAccount[1]; + if (message == null && account == null) { // Something unexpected happened: // the message or the body couldn't be loaded by SQLite. // Bail out. @@ -577,8 +583,6 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus finish(); return; } - final Message message = (Message) messageAndAccount[0]; - final Account account = (Account) messageAndAccount[1]; if (ACTION_EDIT_DRAFT.equals(mAction)) { mDraft = message; @@ -590,10 +594,11 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus } @Override protected void onPostExecute(Attachment[] attachments) { - if (attachments != null) { - for (Attachment attachment : attachments) { - addAttachment(attachment); - } + if (attachments == null) { + return; + } + for (Attachment attachment : attachments) { + addAttachment(attachment); } } }.execute(message.mId); @@ -831,6 +836,9 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus @Override protected void onPostExecute(Void dummy) { + if (isCancelled()) { + return; + } // Don't display the toast if the user is just changing the orientation if (!send && (getChangingConfigurations() & ActivityInfo.CONFIG_ORIENTATION) == 0) { Toast.makeText(MessageCompose.this, R.string.message_saved_toast, diff --git a/src/com/android/email/activity/MessageList.java b/src/com/android/email/activity/MessageList.java index 7e018d7e9..ea4629ddd 100644 --- a/src/com/android/email/activity/MessageList.java +++ b/src/com/android/email/activity/MessageList.java @@ -909,6 +909,9 @@ public class MessageList extends ListActivity implements OnItemClickListener, On @Override protected void onPostExecute(Integer listFooterMode) { + if (listFooterMode == null) { + return; + } finishFooterView(listFooterMode); } } @@ -1016,6 +1019,9 @@ public class MessageList extends ListActivity implements OnItemClickListener, On @Override protected void onPostExecute(Long mailboxId) { + if (mailboxId == null) { + return; + } if (mailboxId != Mailbox.NO_MAILBOX) { mMailboxId = mailboxId; mSetTitleTask = new SetTitleTask(mMailboxId); @@ -1062,7 +1068,7 @@ public class MessageList extends ListActivity implements OnItemClickListener, On @Override protected void onPostExecute(Cursor cursor) { - if (cursor.isClosed()) { + if (cursor == null || cursor.isClosed()) { return; } MessageList.this.mListAdapter.changeCursor(cursor); @@ -1138,6 +1144,9 @@ public class MessageList extends ListActivity implements OnItemClickListener, On @Override protected void onPostExecute(String[] names) { + if (names == null) { + return; + } if (names[0] != null) { mRightTitle.setText(names[0]); } diff --git a/src/com/android/email/activity/MessageView.java b/src/com/android/email/activity/MessageView.java index b0f2d157a..0000443b2 100644 --- a/src/com/android/email/activity/MessageView.java +++ b/src/com/android/email/activity/MessageView.java @@ -982,6 +982,9 @@ public class MessageView extends Activity implements OnClickListener { @Override protected void onPostExecute(Integer icon) { + if (icon == null) { + return; + } updateSenderPresence(icon); } } @@ -1055,6 +1058,9 @@ public class MessageView extends Activity implements OnClickListener { @Override protected void onPostExecute(Cursor cursor) { + if (cursor == null) { + return; + } // remove the reference to ourselves so another one can be launched MessageView.this.mLoadPrevNextTask = null; @@ -1097,6 +1103,19 @@ public class MessageView extends Activity implements OnClickListener { @Override protected void onPostExecute(Message message) { + /* doInBackground() may return null result (due to restoreMessageWithId()) + * and in that situation we want to Activity.finish(). + * + * OTOH we don't want to Activity.finish() for isCancelled() because this + * would introduce a surprise side-effect to task cancellation: every task + * cancelation would also result in finish(). + * + * Right now LoadMesageTask is cancelled not only from onDestroy(), + * and it would be a bug to also finish() the activity in that situation. + */ + if (isCancelled()) { + return; + } if (message == null) { if (mId != Long.MIN_VALUE) { finish(); @@ -1166,6 +1185,9 @@ public class MessageView extends Activity implements OnClickListener { @Override protected void onPostExecute(Attachment[] attachments) { + if (attachments == null) { + return; + } boolean htmlChanged = false; for (Attachment attachment : attachments) { if (mHtmlText != null && attachment.mContentId != null