diff --git a/mobile/android/base/AwesomeBar.java b/mobile/android/base/AwesomeBar.java index c66ec6102a8..90693c924d5 100644 --- a/mobile/android/base/AwesomeBar.java +++ b/mobile/android/base/AwesomeBar.java @@ -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() { + (new GeckoAsyncTask(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() { + (new GeckoAsyncTask(GeckoApp.mAppContext, GeckoAppShell.getHandler()) { @Override public Void doInBackground(Void... params) { BrowserDB.removeHistoryEntry(mResolver, id); diff --git a/mobile/android/base/GeckoApp.java b/mobile/android/base/GeckoApp.java index 4e24ee4bea6..ba4ca6da2a3 100644 --- a/mobile/android/base/GeckoApp.java +++ b/mobile/android/base/GeckoApp.java @@ -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() { + (new GeckoAsyncTask(mAppContext, GeckoAppShell.getHandler()) { @Override public String doInBackground(Void... params) { return getFavicons().getFaviconUrlForPageUrl(url); diff --git a/mobile/android/base/GeckoAsyncTask.java b/mobile/android/base/GeckoAsyncTask.java index 5d351b78f3e..c8e217658d3 100644 --- a/mobile/android/base/GeckoAsyncTask.java +++ b/mobile/android/base/GeckoAsyncTask.java @@ -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 { - 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 { 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 { } } - 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 setPriority(int priority) { + public final GeckoAsyncTask 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); } diff --git a/mobile/android/base/Makefile.in b/mobile/android/base/Makefile.in index a918adee10c..38eea169a3c 100644 --- a/mobile/android/base/Makefile.in +++ b/mobile/android/base/Makefile.in @@ -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 \ diff --git a/mobile/android/base/Tab.java b/mobile/android/base/Tab.java index 0080f735146..8038329aee1 100644 --- a/mobile/android/base/Tab.java +++ b/mobile/android/base/Tab.java @@ -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() { + (new GeckoAsyncTask(GeckoApp.mAppContext, GeckoAppShell.getHandler()) { @Override public Void doInBackground(Void... params) { if (url.equals(getURL())) { diff --git a/mobile/android/base/TabsAccessor.java b/mobile/android/base/TabsAccessor.java index d04fc8ed7f1..f2c3a40083c 100644 --- a/mobile/android/base/TabsAccessor.java +++ b/mobile/android/base/TabsAccessor.java @@ -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 () { + (new GeckoAsyncTask(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> () { + (new GeckoAsyncTask>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) { @Override protected List doInBackground(Void... unused) { Uri uri = BrowserContract.Tabs.CONTENT_URI;