You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bc...@apache.org on 2012/05/15 06:38:02 UTC

[15/15] Updates.

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/api/Plugin.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/api/Plugin.java b/framework/src/org/apache/cordova/api/Plugin.java
index 36faa03..f7c1865 100755
--- a/framework/src/org/apache/cordova/api/Plugin.java
+++ b/framework/src/org/apache/cordova/api/Plugin.java
@@ -22,9 +22,10 @@ import org.apache.cordova.CordovaWebView;
 import org.json.JSONArray;
 import org.json.JSONObject;
 
-import android.content.Context;
+//import android.content.Context;
 import android.content.Intent;
-import android.webkit.WebView;
+
+//import android.webkit.WebView;
 
 /**
  * Plugin interface must be implemented by any plugin classes.
@@ -33,50 +34,50 @@ import android.webkit.WebView;
  */
 public abstract class Plugin implements IPlugin {
 
-	public String id;
+    public String id;
     public CordovaWebView webView;					// WebView object
-    public Context ctx;			// CordovaActivity object
-
-	/**
-	 * Executes the request and returns PluginResult.
-	 * 
-	 * @param action 		The action to execute.
-	 * @param args 			JSONArry of arguments for the plugin.
-	 * @param callbackId	The callback id used when calling back into JavaScript.
-	 * @return 				A PluginResult object with a status and message.
-	 */
-	public abstract PluginResult execute(String action, JSONArray args, String callbackId);
-
-	/**
-	 * Identifies if action to be executed returns a value and should be run synchronously.
-	 * 
-	 * @param action	The action to execute
-	 * @return			T=returns value
-	 */
-	public boolean isSynch(String action) {
-		return false;
-	}
-
-	/**
-	 * Sets the context of the Plugin. This can then be used to do things like
-	 * get file paths associated with the Activity.
-	 * 
-	 * @param ctx The context of the main Activity.
-	 */
-	public void setContext(Context ctx) {
-		this.ctx = ctx;
-	}
-
-	/**
-	 * Sets the main View of the application, this is the WebView within which 
-	 * a Cordova app runs.
-	 * 
-	 * @param webView The Cordova WebView
-	 */
-	public void setView(CordovaWebView webView) {
-		this.webView = webView;
-	}
-	
+    public CordovaInterface ctx;			         // CordovaActivity object
+
+    /**
+     * Executes the request and returns PluginResult.
+     * 
+     * @param action 		The action to execute.
+     * @param args 			JSONArry of arguments for the plugin.
+     * @param callbackId	The callback id used when calling back into JavaScript.
+     * @return 				A PluginResult object with a status and message.
+     */
+    public abstract PluginResult execute(String action, JSONArray args, String callbackId);
+
+    /**
+     * Identifies if action to be executed returns a value and should be run synchronously.
+     * 
+     * @param action	The action to execute
+     * @return			T=returns value
+     */
+    public boolean isSynch(String action) {
+        return false;
+    }
+
+    /**
+     * Sets the context of the Plugin. This can then be used to do things like
+     * get file paths associated with the Activity.
+     * 
+     * @param ctx The context of the main Activity.
+     */
+    public void setContext(CordovaInterface ctx) {
+        this.ctx = ctx;
+    }
+
+    /**
+     * Sets the main View of the application, this is the WebView within which 
+     * a Cordova app runs.
+     * 
+     * @param webView The Cordova WebView
+     */
+    public void setView(CordovaWebView webView) {
+        this.webView = webView;
+    }
+
     /**
      * Called when the system is about to start resuming a previous activity. 
      * 
@@ -92,19 +93,19 @@ public abstract class Plugin implements IPlugin {
      */
     public void onResume(boolean multitasking) {
     }
-    
+
     /**
      * Called when the activity receives a new intent. 
      */
     public void onNewIntent(Intent intent) {
     }
-    
+
     /**
      * The final call you receive before your activity is destroyed. 
      */
     public void onDestroy() {
     }
-	
+
     /**
      * Called when a message is sent to plugin. 
      * 
@@ -133,7 +134,7 @@ public abstract class Plugin implements IPlugin {
      * @return					Return true to prevent the URL from loading. Default is false.
      */
     public boolean onOverrideUrlLoading(String url) {
-    	return false;
+        return false;
     }
 
     /**
@@ -143,7 +144,7 @@ public abstract class Plugin implements IPlugin {
      * @param statement
      */
     public void sendJavascript(String statement) {
-      webView.sendJavascript(statement);
+        this.webView.sendJavascript(statement);
     }
 
     /**
@@ -154,10 +155,10 @@ public abstract class Plugin implements IPlugin {
      * call success(...) or error(...)
      * 
      * @param pluginResult		The result to return.
-	 * @param callbackId		The callback id used when calling back into JavaScript.
+     * @param callbackId		The callback id used when calling back into JavaScript.
      */
     public void success(PluginResult pluginResult, String callbackId) {
-      webView.sendJavascript(pluginResult.toSuccessCallbackString(callbackId));
+        this.webView.sendJavascript(pluginResult.toSuccessCallbackString(callbackId));
     }
 
     /**
@@ -167,7 +168,7 @@ public abstract class Plugin implements IPlugin {
      * @param callbackId		The callback id used when calling back into JavaScript.
      */
     public void success(JSONObject message, String callbackId) {
-      webView.sendJavascript(new PluginResult(PluginResult.Status.OK, message).toSuccessCallbackString(callbackId));
+        this.webView.sendJavascript(new PluginResult(PluginResult.Status.OK, message).toSuccessCallbackString(callbackId));
     }
 
     /**
@@ -177,17 +178,17 @@ public abstract class Plugin implements IPlugin {
      * @param callbackId		The callback id used when calling back into JavaScript.
      */
     public void success(String message, String callbackId) {
-      webView.sendJavascript(new PluginResult(PluginResult.Status.OK, message).toSuccessCallbackString(callbackId));
+        this.webView.sendJavascript(new PluginResult(PluginResult.Status.OK, message).toSuccessCallbackString(callbackId));
     }
-    
+
     /**
      * Call the JavaScript error callback for this plugin.
      * 
      * @param pluginResult		The result to return.
-	 * @param callbackId		The callback id used when calling back into JavaScript.
+     * @param callbackId		The callback id used when calling back into JavaScript.
      */
     public void error(PluginResult pluginResult, String callbackId) {
-      webView.sendJavascript(pluginResult.toErrorCallbackString(callbackId));
+        this.webView.sendJavascript(pluginResult.toErrorCallbackString(callbackId));
     }
 
     /**
@@ -197,7 +198,7 @@ public abstract class Plugin implements IPlugin {
      * @param callbackId		The callback id used when calling back into JavaScript.
      */
     public void error(JSONObject message, String callbackId) {
-      webView.sendJavascript(new PluginResult(PluginResult.Status.ERROR, message).toErrorCallbackString(callbackId));
+        this.webView.sendJavascript(new PluginResult(PluginResult.Status.ERROR, message).toErrorCallbackString(callbackId));
     }
 
     /**
@@ -207,6 +208,6 @@ public abstract class Plugin implements IPlugin {
      * @param callbackId		The callback id used when calling back into JavaScript.
      */
     public void error(String message, String callbackId) {
-      webView.sendJavascript(new PluginResult(PluginResult.Status.ERROR, message).toErrorCallbackString(callbackId));
+        this.webView.sendJavascript(new PluginResult(PluginResult.Status.ERROR, message).toErrorCallbackString(callbackId));
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/api/PluginEntry.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/api/PluginEntry.java b/framework/src/org/apache/cordova/api/PluginEntry.java
index 2c4cdb8..057c005 100755
--- a/framework/src/org/apache/cordova/api/PluginEntry.java
+++ b/framework/src/org/apache/cordova/api/PluginEntry.java
@@ -20,8 +20,8 @@ package org.apache.cordova.api;
 
 import org.apache.cordova.CordovaWebView;
 
-import android.content.Context;
-import android.webkit.WebView;
+//import android.content.Context;
+//import android.webkit.WebView;
 
 /**
  * This class represents a service entry object.
@@ -69,12 +69,12 @@ public class PluginEntry {
      * 
      * @return                      The plugin object
      */
-    @SuppressWarnings("unchecked")
-    public IPlugin createPlugin(CordovaWebView webView, Context ctx) {
+    public IPlugin createPlugin(CordovaWebView webView, CordovaInterface ctx) {
         if (this.plugin != null) {
             return this.plugin;
         }
         try {
+            @SuppressWarnings("rawtypes")
             Class c = getClassByName(this.pluginClass);
             if (isCordovaPlugin(c)) {
                 this.plugin = (IPlugin) c.newInstance();
@@ -96,7 +96,7 @@ public class PluginEntry {
      * @return
      * @throws ClassNotFoundException
      */
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings("rawtypes")
     private Class getClassByName(final String clazz) throws ClassNotFoundException {
         Class c = null;
         if (clazz != null) {
@@ -112,7 +112,7 @@ public class PluginEntry {
      * @param c                     The class to check the interfaces of.
      * @return                      Boolean indicating if the class implements org.apache.cordova.api.Plugin
      */
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings("rawtypes")
     private boolean isCordovaPlugin(Class c) {
         if (c != null) {
             return org.apache.cordova.api.Plugin.class.isAssignableFrom(c) || org.apache.cordova.api.IPlugin.class.isAssignableFrom(c);

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/api/PluginManager.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/api/PluginManager.java b/framework/src/org/apache/cordova/api/PluginManager.java
index d34fe92..c931477 100755
--- a/framework/src/org/apache/cordova/api/PluginManager.java
+++ b/framework/src/org/apache/cordova/api/PluginManager.java
@@ -28,10 +28,8 @@ import org.json.JSONArray;
 import org.json.JSONException;
 import org.xmlpull.v1.XmlPullParserException;
 
-import android.content.Context;
 import android.content.Intent;
 import android.content.res.XmlResourceParser;
-import android.webkit.WebView;
 
 /**
  * PluginManager is exposed to JavaScript in the Cordova WebView.
@@ -45,7 +43,7 @@ public class PluginManager {
     // List of service entries
     private final HashMap<String, PluginEntry> entries = new HashMap<String, PluginEntry>();
 
-    private final Context ctx;
+    private final CordovaInterface ctx;
     private final CordovaWebView app;
 
     // Flag to track first time through
@@ -61,26 +59,25 @@ public class PluginManager {
      * @param app
      * @param ctx
      */
-    public PluginManager(CordovaWebView app, Context ctx) {
+    public PluginManager(CordovaWebView app, CordovaInterface ctx) {
         this.ctx = ctx;
         this.app = app;
         this.firstRun = true;
     }
 
-
-    public PluginManager(WebView mApp, CordovaInterface mCtx) throws Exception {
-      this.ctx = mCtx.getContext();
-      if(CordovaWebView.class.isInstance(mApp))
-      {
-        this.app = (CordovaWebView) mApp;
-      }
-      else
-      {
-        //Throw an exception here
-        throw new Exception();
-      }
-    }
-
+    // Called by com.phonegap.api.PluginManager only
+//    public PluginManager(WebView mApp, CordovaInterface mCtx) throws Exception {
+//        this.ctx = mCtx; //mCtx.getContext();
+//        if (CordovaWebView.class.isInstance(mApp))
+//        {
+//            this.app = (CordovaWebView) mApp;
+//        }
+//        else
+//        {
+//            //Throw an exception here
+//            throw new Exception();
+//        }
+//    }
 
     /**
      * Init when loading a new HTML page into webview.
@@ -89,9 +86,9 @@ public class PluginManager {
         LOG.d(TAG, "init()");
 
         // If first time, then load plugins from plugins.xml file
-        if (firstRun) {
+        if (this.firstRun) {
             this.loadPlugins();
-            firstRun = false;
+            this.firstRun = false;
         }
 
         // Stop plugins on current HTML page and discard plugin objects
@@ -109,11 +106,11 @@ public class PluginManager {
      * Load plugins from res/xml/plugins.xml
      */
     public void loadPlugins() {
-        int id = ctx.getResources().getIdentifier("plugins", "xml", ctx.getPackageName());
+        int id = this.ctx.getActivity().getResources().getIdentifier("plugins", "xml", this.ctx.getActivity().getPackageName());
         if (id == 0) {
-            pluginConfigurationMissing();
+            this.pluginConfigurationMissing();
         }
-        XmlResourceParser xml = ctx.getResources().getXml(id);
+        XmlResourceParser xml = this.ctx.getActivity().getResources().getXml(id);
         int eventType = -1;
         String service = "", pluginClass = "";
         boolean onload = false;
@@ -184,14 +181,13 @@ public class PluginManager {
      * 
      * @return              JSON encoded string with a response message and status.
      */
-    @SuppressWarnings("unchecked")
     public String exec(final String service, final String action, final String callbackId, final String jsonArgs, final boolean async) {
         PluginResult cr = null;
         boolean runAsync = async;
         try {
             final JSONArray args = new JSONArray(jsonArgs);
             final IPlugin plugin = this.getPlugin(service);
-            final Context ctx = this.ctx;
+            //final CordovaInterface ctx = this.ctx;
             if (plugin != null) {
                 runAsync = async && !plugin.isSynch(action);
                 if (runAsync) {
@@ -257,7 +253,7 @@ public class PluginManager {
      * @return              IPlugin or null
      */
     private IPlugin getPlugin(String service) {
-        PluginEntry entry = entries.get(service);
+        PluginEntry entry = this.entries.get(service);
         if (entry == null) {
             return null;
         }
@@ -334,6 +330,7 @@ public class PluginManager {
      * @param data              The message data
      */
     public void postMessage(String id, Object data) {
+        this.ctx.onMessage(id, data);
         for (PluginEntry entry : this.entries.values()) {
             if (entry.plugin != null) {
                 entry.plugin.onMessage(id, data);

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/c8fafa6b/framework/src/org/apache/cordova/api/PluginResult.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/api/PluginResult.java b/framework/src/org/apache/cordova/api/PluginResult.java
index c3ef6d6..8f5f7fd 100755
--- a/framework/src/org/apache/cordova/api/PluginResult.java
+++ b/framework/src/org/apache/cordova/api/PluginResult.java
@@ -21,99 +21,99 @@ package org.apache.cordova.api;
 import org.json.JSONArray;
 import org.json.JSONObject;
 
-import android.util.Log;
+//import android.util.Log;
 
 public class PluginResult {
-	private final int status;
-	private final String message;
-	private boolean keepCallback = false;
-	
-	public PluginResult(Status status) {
-		this.status = status.ordinal();
-		this.message = "'" + PluginResult.StatusMessages[this.status] + "'";
-	}
-	
-	public PluginResult(Status status, String message) {
-		this.status = status.ordinal();
-		this.message = JSONObject.quote(message);
-	}
-
-	public PluginResult(Status status, JSONArray message) {
-		this.status = status.ordinal();
-		this.message = message.toString();
-	}
-
-	public PluginResult(Status status, JSONObject message) {
-		this.status = status.ordinal();
-		this.message = message.toString();
-	}
-
-	public PluginResult(Status status, int i) {
-		this.status = status.ordinal();
-		this.message = ""+i;
-	}
-
-	public PluginResult(Status status, float f) {
-		this.status = status.ordinal();
-		this.message = ""+f;
-	}
-
-	public PluginResult(Status status, boolean b) {
-		this.status = status.ordinal();
-		this.message = ""+b;
-	}
-	
-	public void setKeepCallback(boolean b) {
-		this.keepCallback = b;
-	}
-	
-	public int getStatus() {
-		return status;
-	}
-
-	public String getMessage() {
-		return message;
-	}
-	
-	public boolean getKeepCallback() {
-		return this.keepCallback;
-	}
-	
-	public String getJSONString() {
-		return "{status:" + this.status + ",message:" + this.message + ",keepCallback:" + this.keepCallback + "}";
-	}
-	
-	public String toSuccessCallbackString(String callbackId) {
-		return "cordova.callbackSuccess('"+callbackId+"',"+this.getJSONString()+");";
-	}
-	
-	public String toErrorCallbackString(String callbackId) {
-		return "cordova.callbackError('"+callbackId+"', " + this.getJSONString()+ ");";
-	}
-	
-	public static String[] StatusMessages = new String[] {
-		"No result",
-		"OK",
-		"Class not found",
-		"Illegal access",
-		"Instantiation error",
-		"Malformed url",
-		"IO error",
-		"Invalid action",
-		"JSON error",
-		"Error"
-	};
-	
-	public enum Status {
-		NO_RESULT,
-		OK,
-		CLASS_NOT_FOUND_EXCEPTION,
-		ILLEGAL_ACCESS_EXCEPTION,
-		INSTANTIATION_EXCEPTION,
-		MALFORMED_URL_EXCEPTION,
-		IO_EXCEPTION,
-		INVALID_ACTION,
-		JSON_EXCEPTION,
-		ERROR
-	}
+    private final int status;
+    private final String message;
+    private boolean keepCallback = false;
+
+    public PluginResult(Status status) {
+        this.status = status.ordinal();
+        this.message = "'" + PluginResult.StatusMessages[this.status] + "'";
+    }
+
+    public PluginResult(Status status, String message) {
+        this.status = status.ordinal();
+        this.message = JSONObject.quote(message);
+    }
+
+    public PluginResult(Status status, JSONArray message) {
+        this.status = status.ordinal();
+        this.message = message.toString();
+    }
+
+    public PluginResult(Status status, JSONObject message) {
+        this.status = status.ordinal();
+        this.message = message.toString();
+    }
+
+    public PluginResult(Status status, int i) {
+        this.status = status.ordinal();
+        this.message = "" + i;
+    }
+
+    public PluginResult(Status status, float f) {
+        this.status = status.ordinal();
+        this.message = "" + f;
+    }
+
+    public PluginResult(Status status, boolean b) {
+        this.status = status.ordinal();
+        this.message = "" + b;
+    }
+
+    public void setKeepCallback(boolean b) {
+        this.keepCallback = b;
+    }
+
+    public int getStatus() {
+        return status;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public boolean getKeepCallback() {
+        return this.keepCallback;
+    }
+
+    public String getJSONString() {
+        return "{status:" + this.status + ",message:" + this.message + ",keepCallback:" + this.keepCallback + "}";
+    }
+
+    public String toSuccessCallbackString(String callbackId) {
+        return "cordova.callbackSuccess('" + callbackId + "'," + this.getJSONString() + ");";
+    }
+
+    public String toErrorCallbackString(String callbackId) {
+        return "cordova.callbackError('" + callbackId + "', " + this.getJSONString() + ");";
+    }
+
+    public static String[] StatusMessages = new String[] {
+            "No result",
+            "OK",
+            "Class not found",
+            "Illegal access",
+            "Instantiation error",
+            "Malformed url",
+            "IO error",
+            "Invalid action",
+            "JSON error",
+            "Error"
+    };
+
+    public enum Status {
+        NO_RESULT,
+        OK,
+        CLASS_NOT_FOUND_EXCEPTION,
+        ILLEGAL_ACCESS_EXCEPTION,
+        INSTANTIATION_EXCEPTION,
+        MALFORMED_URL_EXCEPTION,
+        IO_EXCEPTION,
+        INVALID_ACTION,
+        JSON_EXCEPTION,
+        ERROR
+    }
 }