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.Account;
import com.android.emailcommon.provider.EmailContent.Mailbox;
import com.android.emailcommon.utility.EmailAsyncTask;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.AsyncTask;
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)
* 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 {
/*
@ -73,6 +73,8 @@ public class Welcome extends Activity {
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.
*/
@ -166,11 +168,13 @@ public class Welcome extends Activity {
final long mailboxId = IntentUtilities.getMailboxIdFromIntent(intent);
final long messageId = IntentUtilities.getMessageIdFromIntent(intent);
final int debugPaneMode = getDebugPaneMode(getIntent());
new MainActivityLauncher(this, accountId, mailboxId, messageId, debugPaneMode).execute();
new MainActivityLauncher(this, accountId, mailboxId, messageId, debugPaneMode)
.executeParallel();
}
@Override
public void onDestroy() {
mTaskTracker.cancellAllInterrupt();
super.onDestroy();
}
@ -180,15 +184,16 @@ public class Welcome extends Activity {
*
* if {@code account} is -1, open the default account.
*/
private static class MainActivityLauncher extends AsyncTask<Void, Void, Void> {
private final Activity mFromActivity;
private static class MainActivityLauncher extends EmailAsyncTask<Void, Void, Void> {
private final Welcome mFromActivity;
private final int mDebugPaneMode;
private final long mAccountId;
private final long mMailboxId;
private final long mMessageId;
public MainActivityLauncher(Activity fromActivity, long accountId, long mailboxId,
public MainActivityLauncher(Welcome fromActivity, long accountId, long mailboxId,
long messageId, int debugPaneMode) {
super(fromActivity.mTaskTracker);
mFromActivity = fromActivity;
mAccountId = accountId;
mMailboxId = mailboxId;

View File

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