Add an app-wide thead pool to CordovaInterface.

This commit is contained in:
Andrew Grieve 2012-09-24 15:30:59 -04:00
parent 1bf12842ca
commit afcdccf783
4 changed files with 25 additions and 3 deletions

View File

@ -19,6 +19,8 @@
package org.apache.cordova; package org.apache.cordova;
import java.util.HashMap; 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.IPlugin;
import org.apache.cordova.api.LOG; import org.apache.cordova.api.LOG;
@ -142,6 +144,8 @@ public class DroidGap extends Activity implements CordovaInterface {
protected LinearLayout root; protected LinearLayout root;
protected boolean cancelLoadUrl = false; protected boolean cancelLoadUrl = false;
protected ProgressDialog spinnerDialog = null; protected ProgressDialog spinnerDialog = null;
private final ExecutorService threadPool = Executors.newCachedThreadPool();
// The initial URL for our app // The initial URL for our app
// ie http://server/path/index.html#abc?query // ie http://server/path/index.html#abc?query
@ -1051,4 +1055,9 @@ public class DroidGap extends Activity implements CordovaInterface {
} }
return null; return null;
} }
@Override
public ExecutorService getThreadPool() {
return threadPool;
}
} }

View File

@ -22,6 +22,8 @@ import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import java.util.concurrent.ExecutorService;
/** /**
* The Cordova activity abstract class that is extended by DroidGap. * The Cordova activity abstract class that is extended by DroidGap.
* It is used to isolate plugin development, and remove dependency on entire Cordova library. * 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 * @return Object or null
*/ */
public Object onMessage(String id, Object data); public Object onMessage(String id, Object data);
/**
* Returns a shared thread pool that can be used for background tasks.
*/
public ExecutorService getThreadPool();
} }

View File

@ -29,6 +29,8 @@ import android.content.res.AssetManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.util.Log; import android.util.Log;
import java.util.concurrent.ExecutorService;
@Deprecated @Deprecated
public class LegacyContext implements CordovaInterface { public class LegacyContext implements CordovaInterface {
private static final String LOG_TAG = "Deprecation Notice"; 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()"); Log.i(LOG_TAG, "Replace ctx.unbindService() with cordova.getActivity().unbindService()");
this.cordova.getActivity().unbindService(conn); this.cordova.getActivity().unbindService(conn);
} }
@Override
public ExecutorService getThreadPool() {
Log.i(LOG_TAG, "Replace ctx.getThreadPool() with cordova.getThreadPool()");
return this.cordova.getThreadPool();
}
} }

View File

@ -47,7 +47,6 @@ public class PluginManager {
private final CordovaInterface ctx; private final CordovaInterface ctx;
private final CordovaWebView app; private final CordovaWebView app;
private final ExecutorService execThreadPool = Executors.newCachedThreadPool();
// Flag to track first time through // Flag to track first time through
private boolean firstRun; private boolean firstRun;
@ -226,7 +225,7 @@ public class PluginManager {
runAsync = async && !plugin.isSynch(action); runAsync = async && !plugin.isSynch(action);
if (runAsync) { if (runAsync) {
// Run this on a different thread so that this one can return back to JS // 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() { public void run() {
try { try {
// Call execute on the plugin so that it can do it's thing // Call execute on the plugin so that it can do it's thing