Introduce PhonegapActivity class to separate plugin development from base Phonegap.

This commit is contained in:
Bryce Curtis 2010-11-06 02:10:51 +08:00
parent 36064c564e
commit b66535a17d
15 changed files with 80 additions and 29 deletions

View File

@ -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);
}

View File

@ -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):

View File

@ -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:

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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";

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

3
framework/src/com/phonegap/TempListener.java Normal file → Executable file
View File

@ -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);
}

View File

@ -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

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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<String, Plugin> plugins = new HashMap<String,Plugin>();
private HashMap<String, String> services = new HashMap<String,String>();
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;
}