diff --git a/framework/src/org/apache/cordova/DroidGap.java b/framework/src/org/apache/cordova/DroidGap.java index b36320fa..354a484c 100755 --- a/framework/src/org/apache/cordova/DroidGap.java +++ b/framework/src/org/apache/cordova/DroidGap.java @@ -19,6 +19,8 @@ package org.apache.cordova; import java.util.HashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import org.apache.cordova.api.IPlugin; import org.apache.cordova.api.LOG; @@ -142,6 +144,8 @@ public class DroidGap extends Activity implements CordovaInterface { protected LinearLayout root; protected boolean cancelLoadUrl = false; protected ProgressDialog spinnerDialog = null; + private final ExecutorService threadPool = Executors.newCachedThreadPool(); + // The initial URL for our app // ie http://server/path/index.html#abc?query @@ -1051,4 +1055,9 @@ public class DroidGap extends Activity implements CordovaInterface { } return null; } + + @Override + public ExecutorService getThreadPool() { + return threadPool; + } } diff --git a/framework/src/org/apache/cordova/api/CordovaInterface.java b/framework/src/org/apache/cordova/api/CordovaInterface.java index 93b31a02..5a052c41 100755 --- a/framework/src/org/apache/cordova/api/CordovaInterface.java +++ b/framework/src/org/apache/cordova/api/CordovaInterface.java @@ -22,6 +22,8 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; +import java.util.concurrent.ExecutorService; + /** * The Cordova activity abstract class that is extended by DroidGap. * It is used to isolate plugin development, and remove dependency on entire Cordova library. @@ -67,5 +69,9 @@ public interface CordovaInterface { * @return Object or null */ public Object onMessage(String id, Object data); - + + /** + * Returns a shared thread pool that can be used for background tasks. + */ + public ExecutorService getThreadPool(); } diff --git a/framework/src/org/apache/cordova/api/LegacyContext.java b/framework/src/org/apache/cordova/api/LegacyContext.java index 073ba941..0d222813 100644 --- a/framework/src/org/apache/cordova/api/LegacyContext.java +++ b/framework/src/org/apache/cordova/api/LegacyContext.java @@ -29,6 +29,8 @@ import android.content.res.AssetManager; import android.content.res.Resources; import android.util.Log; +import java.util.concurrent.ExecutorService; + @Deprecated public class LegacyContext implements CordovaInterface { private static final String LOG_TAG = "Deprecation Notice"; @@ -145,4 +147,10 @@ public class LegacyContext implements CordovaInterface { Log.i(LOG_TAG, "Replace ctx.unbindService() with cordova.getActivity().unbindService()"); this.cordova.getActivity().unbindService(conn); } + + @Override + public ExecutorService getThreadPool() { + Log.i(LOG_TAG, "Replace ctx.getThreadPool() with cordova.getThreadPool()"); + return this.cordova.getThreadPool(); + } } diff --git a/framework/src/org/apache/cordova/api/PluginManager.java b/framework/src/org/apache/cordova/api/PluginManager.java index aaf614d7..589b1037 100755 --- a/framework/src/org/apache/cordova/api/PluginManager.java +++ b/framework/src/org/apache/cordova/api/PluginManager.java @@ -47,7 +47,6 @@ public class PluginManager { private final CordovaInterface ctx; private final CordovaWebView app; - private final ExecutorService execThreadPool = Executors.newCachedThreadPool(); // Flag to track first time through private boolean firstRun; @@ -226,7 +225,7 @@ public class PluginManager { runAsync = async && !plugin.isSynch(action); if (runAsync) { // Run this on a different thread so that this one can return back to JS - execThreadPool.execute(new Runnable() { + ctx.getThreadPool().execute(new Runnable() { public void run() { try { // Call execute on the plugin so that it can do it's thing