Bug 778468 - Part 5: Move GeckoAsyncTask to org.mozilla.gecko.util package. r=mfinkle

--HG--
extra : rebase_source : 4aa925222ea94fd58d5b071075c0d6a7d9be9231
This commit is contained in:
Chris Peterson 2012-08-02 11:10:31 -07:00
parent 75caa1ddcf
commit 425c4af89d
6 changed files with 31 additions and 23 deletions

View File

@ -6,6 +6,7 @@
package org.mozilla.gecko;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.util.GeckoAsyncTask;
import android.app.Activity;
import android.app.AlertDialog;
@ -511,7 +512,7 @@ public class AwesomeBar extends GeckoActivity {
editPrompt.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
(new GeckoAsyncTask<Void, Void, Void>() {
(new GeckoAsyncTask<Void, Void, Void>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
@Override
public Void doInBackground(Void... params) {
String newUrl = locationText.getText().toString().trim();
@ -587,7 +588,7 @@ public class AwesomeBar extends GeckoActivity {
break;
}
case R.id.remove_history: {
(new GeckoAsyncTask<Void, Void, Void>() {
(new GeckoAsyncTask<Void, Void, Void>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
@Override
public Void doInBackground(Void... params) {
BrowserDB.removeHistoryEntry(mResolver, id);

View File

@ -13,6 +13,7 @@ import org.mozilla.gecko.gfx.LayerView;
import org.mozilla.gecko.gfx.PluginLayer;
import org.mozilla.gecko.gfx.PointUtils;
import org.mozilla.gecko.ui.PanZoomController;
import org.mozilla.gecko.util.GeckoAsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
@ -715,7 +716,7 @@ abstract public class GeckoApp
}
void handleFaviconRequest(final String url) {
(new GeckoAsyncTask<Void, Void, String>() {
(new GeckoAsyncTask<Void, Void, String>(mAppContext, GeckoAppShell.getHandler()) {
@Override
public String doInBackground(Void... params) {
return getFavicons().getFaviconUrlForPageUrl(url);

View File

@ -3,24 +3,27 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko;
package org.mozilla.gecko.util;
import org.mozilla.gecko.util.GeckoBackgroundThread;
import android.app.Activity;
import android.os.Handler;
// AsyncTask runs onPostExecute on the thread it is constructed on
// We construct these off of the main thread, and we want that to run
// on the main UI thread, so this is a convenience class to do that
public abstract class GeckoAsyncTask<Params, Progress, Result> {
public static final int PRIORITY_NORMAL = 0;
public static final int PRIORITY_HIGH = 1;
public enum Priority { NORMAL, HIGH };
private int mPriority;
private final Activity mActivity;
private final Handler mBackgroundThreadHandler;
private Priority mPriority = Priority.NORMAL;
public GeckoAsyncTask() {
mPriority = PRIORITY_NORMAL;
public GeckoAsyncTask(Activity activity, Handler backgroundThreadHandler) {
mActivity = activity;
mBackgroundThreadHandler = backgroundThreadHandler;
}
private class BackgroundTaskRunnable implements Runnable {
private final class BackgroundTaskRunnable implements Runnable {
private Params[] mParams;
public BackgroundTaskRunnable(Params... params) {
@ -29,7 +32,7 @@ public abstract class GeckoAsyncTask<Params, Progress, Result> {
public void run() {
final Result result = doInBackground(mParams);
GeckoApp.mAppContext.runOnUiThread(new Runnable() {
mActivity.runOnUiThread(new Runnable() {
public void run() {
onPostExecute(result);
}
@ -37,18 +40,19 @@ public abstract class GeckoAsyncTask<Params, Progress, Result> {
}
}
public void execute(final Params... params) {
if (mPriority == PRIORITY_HIGH)
GeckoBackgroundThread.getHandler().postAtFrontOfQueue(new BackgroundTaskRunnable(params));
public final void execute(final Params... params) {
BackgroundTaskRunnable runnable = new BackgroundTaskRunnable(params);
if (mPriority == Priority.HIGH)
mBackgroundThreadHandler.postAtFrontOfQueue(runnable);
else
GeckoBackgroundThread.getHandler().post(new BackgroundTaskRunnable(params));
mBackgroundThreadHandler.post(runnable);
}
public GeckoAsyncTask<Params, Progress, Result> setPriority(int priority) {
public final GeckoAsyncTask<Params, Progress, Result> setPriority(Priority priority) {
mPriority = priority;
return this;
}
protected abstract Result doInBackground(Params... params);
protected abstract void onPostExecute(Result result);
protected abstract void onPostExecute(Result result);
}

View File

@ -23,6 +23,7 @@ MOZGLUE_JAVA_FILES := \
UTIL_JAVA_FILES := \
ActivityResultHandler.java \
ActivityResultHandlerMap.java \
GeckoAsyncTask.java \
GeckoBackgroundThread.java \
GeckoJarReader.java \
INIParser.java \
@ -65,7 +66,6 @@ FENNEC_JAVA_FILES = \
GeckoApplication.java \
GeckoApp.java \
GeckoAppShell.java \
GeckoAsyncTask.java \
GeckoBatteryManager.java \
GeckoConnectivityReceiver.java \
GeckoEvent.java \

View File

@ -8,6 +8,7 @@ package org.mozilla.gecko;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.gfx.Layer;
import org.mozilla.gecko.mozglue.DirectBufferAllocator;
import org.mozilla.gecko.util.GeckoAsyncTask;
import org.json.JSONException;
import org.json.JSONObject;
@ -373,7 +374,7 @@ public final class Tab {
if (url == null)
return;
(new GeckoAsyncTask<Void, Void, Void>() {
(new GeckoAsyncTask<Void, Void, Void>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
@Override
public Void doInBackground(Void... params) {
if (url.equals(getURL())) {

View File

@ -5,6 +5,7 @@
package org.mozilla.gecko;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.util.GeckoAsyncTask;
import android.content.Context;
import android.database.Cursor;
@ -59,7 +60,7 @@ public final class TabsAccessor {
if (listener == null)
return;
(new GeckoAsyncTask<Void, Void, Boolean> () {
(new GeckoAsyncTask<Void, Void, Boolean>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
@Override
protected Boolean doInBackground(Void... unused) {
Uri uri = BrowserContract.Tabs.CONTENT_URI;
@ -87,7 +88,7 @@ public final class TabsAccessor {
protected void onPostExecute(Boolean availability) {
listener.areAvailable(availability);
}
}).setPriority(GeckoAsyncTask.PRIORITY_HIGH).execute();
}).setPriority(GeckoAsyncTask.Priority.HIGH).execute();
}
// This method returns all tabs from all remote clients,
@ -103,7 +104,7 @@ public final class TabsAccessor {
if (listener == null)
return;
(new GeckoAsyncTask<Void, Void, List<RemoteTab>> () {
(new GeckoAsyncTask<Void, Void, List<RemoteTab>>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
@Override
protected List<RemoteTab> doInBackground(Void... unused) {
Uri uri = BrowserContract.Tabs.CONTENT_URI;