From b66535a17d7f85b65089beecf9da2a3219159847 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Sat, 6 Nov 2010 02:10:51 +0800 Subject: [PATCH] Introduce PhonegapActivity class to separate plugin development from base Phonegap. --- framework/src/com/phonegap/AccelListener.java | 3 +- framework/src/com/phonegap/AudioHandler.java | 2 +- .../src/com/phonegap/CameraLauncher.java | 2 +- .../src/com/phonegap/CompassListener.java | 3 +- framework/src/com/phonegap/Device.java | 3 +- framework/src/com/phonegap/DroidGap.java | 8 ++-- framework/src/com/phonegap/GpsListener.java | 6 ++- .../src/com/phonegap/NetworkListener.java | 6 ++- .../src/com/phonegap/NetworkManager.java | 3 +- framework/src/com/phonegap/Notification.java | 9 ++-- framework/src/com/phonegap/TempListener.java | 3 +- framework/src/com/phonegap/api/IPlugin.java | 3 +- .../com/phonegap/api/PhonegapActivity.java | 43 +++++++++++++++++++ framework/src/com/phonegap/api/Plugin.java | 6 +-- .../src/com/phonegap/api/PluginManager.java | 9 ++-- 15 files changed, 80 insertions(+), 29 deletions(-) mode change 100644 => 100755 framework/src/com/phonegap/TempListener.java create mode 100755 framework/src/com/phonegap/api/PhonegapActivity.java diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index f33dde84..bcd100e4 100755 --- a/framework/src/com/phonegap/AccelListener.java +++ b/framework/src/com/phonegap/AccelListener.java @@ -13,6 +13,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import com.phonegap.api.PhonegapActivity; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; @@ -60,7 +61,7 @@ public class AccelListener extends Plugin implements SensorEventListener { * * @param ctx The context of the main Activity. */ - public void setContext(DroidGap ctx) { + public void setContext(PhonegapActivity ctx) { super.setContext(ctx); this.sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); } diff --git a/framework/src/com/phonegap/AudioHandler.java b/framework/src/com/phonegap/AudioHandler.java index e8813bf7..5e995805 100755 --- a/framework/src/com/phonegap/AudioHandler.java +++ b/framework/src/com/phonegap/AudioHandler.java @@ -20,7 +20,7 @@ import android.content.Context; import android.media.AudioManager; /** - * This class called by DroidGap to play and record audio. + * This class called by PhonegapActivity to play and record audio. * The file can be local or over a network using http. * * Audio formats supported (tested): diff --git a/framework/src/com/phonegap/CameraLauncher.java b/framework/src/com/phonegap/CameraLauncher.java index 50ad55ae..62c42fcf 100755 --- a/framework/src/com/phonegap/CameraLauncher.java +++ b/framework/src/com/phonegap/CameraLauncher.java @@ -100,7 +100,7 @@ public class CameraLauncher extends Plugin { /** * Take a picture with the camera. * When an image is captured or the camera view is cancelled, the result is returned - * in DroidGap.onActivityResult, which forwards the result to this.onActivityResult. + * in PhonegapActivity.onActivityResult, which forwards the result to this.onActivityResult. * * The image can either be returned as a base64 string or a URI that points to the file. * To display base64 string in an img tag, set the source to: diff --git a/framework/src/com/phonegap/CompassListener.java b/framework/src/com/phonegap/CompassListener.java index 1f2a2994..77ba96b5 100755 --- a/framework/src/com/phonegap/CompassListener.java +++ b/framework/src/com/phonegap/CompassListener.java @@ -12,6 +12,7 @@ import java.util.List; import org.json.JSONArray; import org.json.JSONException; +import com.phonegap.api.PhonegapActivity; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; @@ -55,7 +56,7 @@ public class CompassListener extends Plugin implements SensorEventListener { * * @param ctx The context of the main Activity. */ - public void setContext(DroidGap ctx) { + public void setContext(PhonegapActivity ctx) { super.setContext(ctx); this.sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); } diff --git a/framework/src/com/phonegap/Device.java b/framework/src/com/phonegap/Device.java index b450e1ae..71684fd7 100755 --- a/framework/src/com/phonegap/Device.java +++ b/framework/src/com/phonegap/Device.java @@ -11,6 +11,7 @@ import java.util.TimeZone; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import com.phonegap.api.PhonegapActivity; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; import android.content.Context; @@ -35,7 +36,7 @@ public class Device extends Plugin { * * @param ctx The context of the main Activity. */ - public void setContext(DroidGap ctx) { + public void setContext(PhonegapActivity ctx) { super.setContext(ctx); Device.uuid = getUuid(); } diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index ffd257b3..62b416a5 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -9,10 +9,7 @@ package com.phonegap; -import com.phonegap.api.Plugin; -import com.phonegap.api.PluginManager; -import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; @@ -37,6 +34,9 @@ import android.webkit.GeolocationPermissions.Callback; import android.webkit.WebSettings.LayoutAlgorithm; import android.widget.ImageView; import android.widget.LinearLayout; +import com.phonegap.api.Plugin; +import com.phonegap.api.PluginManager; +import com.phonegap.api.PhonegapActivity; /** * This class is the main Android activity that represents the PhoneGap @@ -58,7 +58,7 @@ import android.widget.LinearLayout; * } * } */ -public class DroidGap extends Activity { +public class DroidGap extends PhonegapActivity { private static final String LOG_TAG = "DroidGap"; diff --git a/framework/src/com/phonegap/GpsListener.java b/framework/src/com/phonegap/GpsListener.java index 14a29f1e..767a489d 100755 --- a/framework/src/com/phonegap/GpsListener.java +++ b/framework/src/com/phonegap/GpsListener.java @@ -7,6 +7,8 @@ */ package com.phonegap; +import com.phonegap.api.PhonegapActivity; + import android.content.Context; import android.location.Location; import android.location.LocationManager; @@ -19,7 +21,7 @@ import android.os.Bundle; */ public class GpsListener implements LocationListener { - private DroidGap mCtx; // DroidGap object + private PhonegapActivity mCtx; // PhonegapActivity object private LocationManager mLocMan; // Location manager object private GeoListener owner; // Geolistener object (parent) @@ -35,7 +37,7 @@ public class GpsListener implements LocationListener { * @param interval * @param m */ - public GpsListener(DroidGap ctx, int interval, GeoListener m) { + public GpsListener(PhonegapActivity ctx, int interval, GeoListener m) { this.owner = m; this.mCtx = ctx; this.mLocMan = (LocationManager) this.mCtx.getSystemService(Context.LOCATION_SERVICE); diff --git a/framework/src/com/phonegap/NetworkListener.java b/framework/src/com/phonegap/NetworkListener.java index 3ec107ce..6c225f2e 100755 --- a/framework/src/com/phonegap/NetworkListener.java +++ b/framework/src/com/phonegap/NetworkListener.java @@ -7,6 +7,8 @@ */ package com.phonegap; +import com.phonegap.api.PhonegapActivity; + import android.content.Context; import android.location.Location; import android.location.LocationManager; @@ -15,7 +17,7 @@ import android.os.Bundle; public class NetworkListener implements LocationListener { - private DroidGap mCtx; // DroidGap object + private PhonegapActivity mCtx; // PhonegapActivity object private LocationManager mLocMan; // Location manager object private GeoListener owner; // Geolistener object (parent) @@ -31,7 +33,7 @@ public class NetworkListener implements LocationListener { * @param interval * @param m */ - public NetworkListener(DroidGap ctx, int interval, GeoListener m) { + public NetworkListener(PhonegapActivity ctx, int interval, GeoListener m) { this.owner = m; this.mCtx = ctx; this.mLocMan = (LocationManager) this.mCtx.getSystemService(Context.LOCATION_SERVICE); diff --git a/framework/src/com/phonegap/NetworkManager.java b/framework/src/com/phonegap/NetworkManager.java index 4ff6c971..d7dfa6dc 100755 --- a/framework/src/com/phonegap/NetworkManager.java +++ b/framework/src/com/phonegap/NetworkManager.java @@ -12,6 +12,7 @@ import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; +import com.phonegap.api.PhonegapActivity; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; @@ -38,7 +39,7 @@ public class NetworkManager extends Plugin { * * @param ctx The context of the main Activity. */ - public void setContext(DroidGap ctx) { + public void setContext(PhonegapActivity ctx) { super.setContext(ctx); this.sockMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); } diff --git a/framework/src/com/phonegap/Notification.java b/framework/src/com/phonegap/Notification.java index ed5786d5..360ea617 100755 --- a/framework/src/com/phonegap/Notification.java +++ b/framework/src/com/phonegap/Notification.java @@ -10,6 +10,7 @@ package com.phonegap; import org.json.JSONArray; import org.json.JSONException; import com.phonegap.api.Plugin; +import com.phonegap.api.PhonegapActivity; import com.phonegap.api.PluginResult; import android.app.AlertDialog; import android.app.ProgressDialog; @@ -172,7 +173,7 @@ public class Notification extends Plugin { */ public synchronized void alert(final String message, final String title, final String buttonLabel, final String callbackId) { - final DroidGap ctx = this.ctx; + final PhonegapActivity ctx = this.ctx; final Notification notification = this; Runnable runnable = new Runnable() { @@ -208,7 +209,7 @@ public class Notification extends Plugin { */ public synchronized void confirm(final String message, final String title, String buttonLabels, final String callbackId) { - final DroidGap ctx = this.ctx; + final PhonegapActivity ctx = this.ctx; final Notification notification = this; final String[] fButtons = buttonLabels.split(","); @@ -272,7 +273,7 @@ public class Notification extends Plugin { this.spinnerDialog = null; } final Notification notification = this; - final DroidGap ctx = this.ctx; + final PhonegapActivity ctx = this.ctx; Runnable runnable = new Runnable() { public void run() { notification.spinnerDialog = ProgressDialog.show(ctx, title , message, true, true, @@ -308,7 +309,7 @@ public class Notification extends Plugin { this.progressDialog = null; } final Notification notification = this; - final DroidGap ctx = this.ctx; + final PhonegapActivity ctx = this.ctx; Runnable runnable = new Runnable() { public void run() { notification.progressDialog = new ProgressDialog(ctx); diff --git a/framework/src/com/phonegap/TempListener.java b/framework/src/com/phonegap/TempListener.java old mode 100644 new mode 100755 index fc0a3a7a..a8abab47 --- a/framework/src/com/phonegap/TempListener.java +++ b/framework/src/com/phonegap/TempListener.java @@ -11,6 +11,7 @@ import java.util.List; import org.json.JSONArray; +import com.phonegap.api.PhonegapActivity; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; @@ -37,7 +38,7 @@ public class TempListener extends Plugin implements SensorEventListener { * * @param ctx The context of the main Activity. */ - public void setContext(DroidGap ctx) { + public void setContext(PhonegapActivity ctx) { super.setContext(ctx); this.sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); } diff --git a/framework/src/com/phonegap/api/IPlugin.java b/framework/src/com/phonegap/api/IPlugin.java index f36d00b2..b3b3a1da 100755 --- a/framework/src/com/phonegap/api/IPlugin.java +++ b/framework/src/com/phonegap/api/IPlugin.java @@ -8,7 +8,6 @@ package com.phonegap.api; import org.json.JSONArray; -import com.phonegap.DroidGap; import android.content.Intent; import android.webkit.WebView; @@ -43,7 +42,7 @@ public interface IPlugin { * * @param ctx The context of the main Activity. */ - void setContext(DroidGap ctx); + void setContext(PhonegapActivity ctx); /** * Sets the main View of the application, this is the WebView within which diff --git a/framework/src/com/phonegap/api/PhonegapActivity.java b/framework/src/com/phonegap/api/PhonegapActivity.java new file mode 100755 index 00000000..dcadfa2b --- /dev/null +++ b/framework/src/com/phonegap/api/PhonegapActivity.java @@ -0,0 +1,43 @@ +/* + * PhoneGap is available under *either* the terms of the modified BSD license *or* the + * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text. + * + * Copyright (c) 2005-2010, Nitobi Software Inc. + * Copyright (c) 2010, IBM Corporation + */ +package com.phonegap.api; + +import android.app.Activity; +import android.content.Intent; + +/** + * The Phonegap activity abstract class that is extended by DroidGap. + * It is used to isolate plugin development, and remove dependency on entire Phonegap library. + */ +public abstract class PhonegapActivity extends Activity { + + /** + * Add a class that implements a service. + * + * @param serviceType + * @param className + */ + abstract public void addService(String serviceType, String className); + + /** + * Send JavaScript statement back to JavaScript. + * + * @param message + */ + abstract public void sendJavascript(String statement); + + /** + * Launch an activity for which you would like a result when it finished. When this activity exits, + * your onActivityResult() method will be called. + * + * @param command The command object + * @param intent The intent to start + * @param requestCode The request code that is passed to callback to identify the activity + */ + abstract public void startActivityForResult(Plugin command, Intent intent, int requestCode); +} diff --git a/framework/src/com/phonegap/api/Plugin.java b/framework/src/com/phonegap/api/Plugin.java index f22e4972..a527159a 100755 --- a/framework/src/com/phonegap/api/Plugin.java +++ b/framework/src/com/phonegap/api/Plugin.java @@ -8,7 +8,7 @@ package com.phonegap.api; import org.json.JSONArray; -import com.phonegap.DroidGap; + import android.content.Intent; import android.webkit.WebView; @@ -20,7 +20,7 @@ import android.webkit.WebView; public abstract class Plugin implements IPlugin { public WebView webView; // WebView object - public DroidGap ctx; // DroidGap object + public PhonegapActivity ctx; // PhonegapActivity object /** * Executes the request and returns PluginResult. @@ -48,7 +48,7 @@ public abstract class Plugin implements IPlugin { * * @param ctx The context of the main Activity. */ - public void setContext(DroidGap ctx) { + public void setContext(PhonegapActivity ctx) { this.ctx = ctx; } diff --git a/framework/src/com/phonegap/api/PluginManager.java b/framework/src/com/phonegap/api/PluginManager.java index 80328b3b..ad1cf20b 100755 --- a/framework/src/com/phonegap/api/PluginManager.java +++ b/framework/src/com/phonegap/api/PluginManager.java @@ -14,7 +14,6 @@ import org.json.JSONArray; import org.json.JSONException; import android.webkit.WebView; -import com.phonegap.DroidGap; /** * PluginManager is exposed to JavaScript in the PhoneGap WebView. @@ -27,7 +26,7 @@ public final class PluginManager { private HashMap plugins = new HashMap(); private HashMap services = new HashMap(); - private final DroidGap ctx; + private final PhonegapActivity ctx; private final WebView app; /** @@ -36,7 +35,7 @@ public final class PluginManager { * @param app * @param ctx */ - public PluginManager(WebView app, DroidGap ctx) { + public PluginManager(WebView app, PhonegapActivity ctx) { this.ctx = ctx; this.app = app; } @@ -76,7 +75,7 @@ public final class PluginManager { } if (isPhoneGapPlugin(c)) { final Plugin plugin = this.addPlugin(clazz, c); - final DroidGap ctx = this.ctx; + final PhonegapActivity ctx = this.ctx; runAsync = async && !plugin.isSynch(action); if (runAsync) { // Run this on a different thread so that this one can return back to JS @@ -195,7 +194,7 @@ public final class PluginManager { try { Plugin plugin = (Plugin)clazz.newInstance(); this.plugins.put(className, plugin); - plugin.setContext((DroidGap)this.ctx); + plugin.setContext(this.ctx); plugin.setView(this.app); return plugin; }