More use of EmailAsyncTask

... for Welcome and EmailWidget.  So that now it's safe for onDestroy()
to be called while a task is running.  (onDestroy cancels it.)

Change-Id: I660b471465170e1d1d0ce153571fb924ae703d7d
This commit is contained in:
Makoto Onuki 2011-03-28 15:43:50 -07:00
parent de70ee5f78
commit af6079c823
2 changed files with 26 additions and 15 deletions

View File

@ -24,13 +24,13 @@ import com.android.email.service.MailService;
import com.android.emailcommon.provider.EmailContent; import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.Account; import com.android.emailcommon.provider.EmailContent.Account;
import com.android.emailcommon.provider.EmailContent.Mailbox; import com.android.emailcommon.provider.EmailContent.Mailbox;
import com.android.emailcommon.utility.EmailAsyncTask;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
/** /**
@ -39,7 +39,7 @@ import android.os.Bundle;
* *
* This class knows which activity should be launched under the current configuration (screen size) * This class knows which activity should be launched under the current configuration (screen size)
* and the number of accounts configured. So if you want to open an account or a mailbox, * and the number of accounts configured. So if you want to open an account or a mailbox,
* you should alawys do so via its static methods, such as {@link #actionOpenAccountInbox}. * you should always do so via its static methods, such as {@link #actionOpenAccountInbox}.
*/ */
public class Welcome extends Activity { public class Welcome extends Activity {
/* /*
@ -73,6 +73,8 @@ public class Welcome extends Activity {
private static final String VIEW_MAILBOX_INTENT_URL_PATH = "/view/mailbox"; private static final String VIEW_MAILBOX_INTENT_URL_PATH = "/view/mailbox";
private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker();
/** /**
* @return true if the two-pane activity should be used on the current configuration. * @return true if the two-pane activity should be used on the current configuration.
*/ */
@ -166,11 +168,13 @@ public class Welcome extends Activity {
final long mailboxId = IntentUtilities.getMailboxIdFromIntent(intent); final long mailboxId = IntentUtilities.getMailboxIdFromIntent(intent);
final long messageId = IntentUtilities.getMessageIdFromIntent(intent); final long messageId = IntentUtilities.getMessageIdFromIntent(intent);
final int debugPaneMode = getDebugPaneMode(getIntent()); final int debugPaneMode = getDebugPaneMode(getIntent());
new MainActivityLauncher(this, accountId, mailboxId, messageId, debugPaneMode).execute(); new MainActivityLauncher(this, accountId, mailboxId, messageId, debugPaneMode)
.executeParallel();
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
mTaskTracker.cancellAllInterrupt();
super.onDestroy(); super.onDestroy();
} }
@ -180,15 +184,16 @@ public class Welcome extends Activity {
* *
* if {@code account} is -1, open the default account. * if {@code account} is -1, open the default account.
*/ */
private static class MainActivityLauncher extends AsyncTask<Void, Void, Void> { private static class MainActivityLauncher extends EmailAsyncTask<Void, Void, Void> {
private final Activity mFromActivity; private final Welcome mFromActivity;
private final int mDebugPaneMode; private final int mDebugPaneMode;
private final long mAccountId; private final long mAccountId;
private final long mMailboxId; private final long mMailboxId;
private final long mMessageId; private final long mMessageId;
public MainActivityLauncher(Activity fromActivity, long accountId, long mailboxId, public MainActivityLauncher(Welcome fromActivity, long accountId, long mailboxId,
long messageId, int debugPaneMode) { long messageId, int debugPaneMode) {
super(fromActivity.mTaskTracker);
mFromActivity = fromActivity; mFromActivity = fromActivity;
mAccountId = accountId; mAccountId = accountId;
mMailboxId = mailboxId; mMailboxId = mailboxId;

View File

@ -25,6 +25,7 @@ import com.android.email.activity.Welcome;
import com.android.email.provider.WidgetProvider.WidgetService; import com.android.email.provider.WidgetProvider.WidgetService;
import com.android.emailcommon.provider.EmailContent.Mailbox; import com.android.emailcommon.provider.EmailContent.Mailbox;
import com.android.emailcommon.provider.EmailContent.Message; import com.android.emailcommon.provider.EmailContent.Message;
import com.android.emailcommon.utility.EmailAsyncTask;
import com.android.emailcommon.utility.Utility; import com.android.emailcommon.utility.Utility;
import android.app.PendingIntent; import android.app.PendingIntent;
@ -39,7 +40,6 @@ import android.database.Cursor;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.net.Uri; import android.net.Uri;
import android.net.Uri.Builder; import android.net.Uri.Builder;
import android.os.AsyncTask;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
@ -128,6 +128,8 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
/** The current view type */ /** The current view type */
/* package */ WidgetView mWidgetView = WidgetView.UNINITIALIZED_VIEW; /* package */ WidgetView mWidgetView = WidgetView.UNINITIALIZED_VIEW;
private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker();
public EmailWidget(Context context, int _widgetId) { public EmailWidget(Context context, int _widgetId) {
super(); super();
if (Email.DEBUG) { if (Email.DEBUG) {
@ -519,6 +521,7 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
if (mLoader != null) { if (mLoader != null) {
mLoader.reset(); mLoader.reset();
} }
mTaskTracker.cancellAllInterrupt();
WidgetManager.getInstance().remove(mWidgetId); WidgetManager.getInstance().remove(mWidgetId);
} }
@ -530,33 +533,36 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
* Update the widget. If the current view is invalid, switch to the next view, then update. * Update the widget. If the current view is invalid, switch to the next view, then update.
*/ */
/* package */ void validateAndUpdate() { /* package */ void validateAndUpdate() {
new WidgetUpdater(false).execute(); new WidgetUpdater(this, false).cancelPreviousAndExecuteParallel();
} }
/** /**
* Switch to the next view. * Switch to the next view.
*/ */
/* package */ void switchView() { /* package */ void switchView() {
new WidgetUpdater(true).execute(); new WidgetUpdater(this, true).cancelPreviousAndExecuteParallel();
} }
/** /**
* Update the widget. If {@code switchToNextView} is set true, or the current view is invalid, * Update the widget. If {@code switchToNextView} is set true, or the current view is invalid,
* switch to the next view. * switch to the next view.
*/ */
private class WidgetUpdater extends AsyncTask<Void, Void, WidgetView> { private static class WidgetUpdater extends EmailAsyncTask<Void, Void, WidgetView> {
private final EmailWidget mParent;
private final WidgetView mCurrentView; private final WidgetView mCurrentView;
private final boolean mSwitchToNextView; private final boolean mSwitchToNextView;
public WidgetUpdater(boolean switchToNextView) { public WidgetUpdater(EmailWidget parent, boolean switchToNextView) {
mCurrentView = mWidgetView; super(parent.mTaskTracker);
mParent = parent;
mCurrentView = mParent.mWidgetView;
mSwitchToNextView = switchToNextView; mSwitchToNextView = switchToNextView;
} }
@Override @Override
protected WidgetView doInBackground(Void... params) { protected WidgetView doInBackground(Void... params) {
if (mSwitchToNextView || !mCurrentView.isValid(mContext)) { if (mSwitchToNextView || !mCurrentView.isValid(mParent.mContext)) {
return mCurrentView.getNext(mContext); return mCurrentView.getNext(mParent.mContext);
} else { } else {
return mCurrentView; // Reload the same view. return mCurrentView; // Reload the same view.
} }
@ -565,7 +571,7 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
@Override @Override
protected void onPostExecute(WidgetView nextView) { protected void onPostExecute(WidgetView nextView) {
if (nextView != null) { if (nextView != null) {
loadView(nextView); mParent.loadView(nextView);
} }
} }
} }