From 4efdf9d30b2889cdf7ceba5ece410383043f4c0d Mon Sep 17 00:00:00 2001 From: GiorgioNatili Date: Fri, 17 May 2013 12:33:31 +0200 Subject: [PATCH] Update to be compliant with Cordova 2.7 --- .gitignore | 3 + src/android/plugin/PushPlugin.java | 233 ++++++++++++++++++----------- 2 files changed, 146 insertions(+), 90 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d7c787b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +src/.DS_Store +src/android/.DS_Store \ No newline at end of file diff --git a/src/android/plugin/PushPlugin.java b/src/android/plugin/PushPlugin.java index ec8aaa69..7d03b47e 100644 --- a/src/android/plugin/PushPlugin.java +++ b/src/android/plugin/PushPlugin.java @@ -1,9 +1,9 @@ package com.plugin.GCM; - -//import java.io.*; -//import java.util.*; - +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; import org.json.JSONArray; import org.json.JSONException; @@ -11,100 +11,153 @@ import android.util.Log; -import org.apache.cordova.api.Plugin; -import org.apache.cordova.api.PluginResult; -import org.apache.cordova.api.PluginResult.Status; +import org.apache.cordova.api.CallbackContext; +import org.apache.cordova.api.CordovaPlugin; +import org.apache.cordova.CordovaWebView; import com.google.android.gcm.*; /** * @author awysocki - * */ -public class PushPlugin extends Plugin { - - public static final String ME="PushPlugin"; - - public static final String REGISTER="register"; - public static final String UNREGISTER="unregister"; - - public static Plugin gwebView; - private static String gECB; - private static String gSenderID; - - @SuppressWarnings("deprecation") -@Override - public PluginResult execute(String action, JSONArray data, String callbackId) - { - - PluginResult result = null; - - Log.v(ME + ":execute", "action=" + action); - - if (REGISTER.equals(action)) { - - Log.v(ME + ":execute", "data=" + data.toString()); - - try { - - JSONObject jo= new JSONObject(data.toString().substring(1, data.toString().length()-1)); - - gwebView = this; - - Log.v(ME + ":execute", "jo=" + jo.toString()); - - gECB = (String)jo.get("ecb"); - gSenderID = (String)jo.get("senderID"); - - Log.v(ME + ":execute", "ECB="+gECB+" senderID="+gSenderID ); - - GCMRegistrar.register(this.ctx.getContext(), gSenderID); - - - Log.v(ME + ":execute", "GCMRegistrar.register called "); - - result = new PluginResult(Status.OK); - } - catch (JSONException e) { - Log.e(ME, "Got JSON Exception " - + e.getMessage()); - result = new PluginResult(Status.JSON_EXCEPTION); - } - } - else if (UNREGISTER.equals(action)) { - - GCMRegistrar.unregister(this.ctx.getContext()); - Log.v(ME + ":" + UNREGISTER, "GCMRegistrar.unregister called "); - - } - else - { - result = new PluginResult(Status.INVALID_ACTION); - Log.e(ME, "Invalid action : "+action); +public class PushPlugin extends CordovaPlugin { + + public static final String ME = "PushPlugin"; + + public static final String REGISTER = "register"; + public static final String UNREGISTER = "unregister"; + public static final String EXIT = "exit"; + + private static CordovaPlugin gwebView; + private static CordovaWebView staticWebView; + private static String gECB; + private static String gSenderID; + + + @SuppressWarnings("deprecation") + @Override + public boolean execute(String action, JSONArray data, CallbackContext callbak) { + + Log.v(ME + ":execute", "action=" + action); + JSONObject result; + + if (REGISTER.equals(action)) { + + Log.v(ME + ":execute", "data=" + data.toString()); + + try { + + JSONObject jo = new JSONObject(data.toString().substring(1, data.toString().length() - 1)); + + gwebView = this; + staticWebView = webView; + + Log.v(ME + ":execute", "jo=" + jo.toString()); + + gECB = (String) jo.get("ecb"); + gSenderID = (String) jo.get("senderID"); + + Log.v(ME + ":execute", "ECB=" + gECB + " senderID=" + gSenderID); + + GCMRegistrar.register(cordova.getActivity(), gSenderID); + + + Log.v(ME + ":execute", "GCMRegistrar.register called "); + + callbak.success(jo); + + return true; + + } catch (JSONException e) { + + Log.e(ME, "Got JSON Exception " + e.getMessage()); + callbak.error(e.getMessage()); + + } + + // if a notification was touched while we were completely exited, process it now + try { + + BufferedReader inputReader = new BufferedReader(new InputStreamReader(cordova.getActivity().getApplicationContext().openFileInput("cached_payload"))); + String inputString; + StringBuffer stringBuffer = new StringBuffer(); + + while ((inputString = inputReader.readLine()) != null) { + + stringBuffer.append(inputString); + } + + // surface the cached payload + JSONObject jsonObj = new JSONObject(stringBuffer.toString()); + sendJavascript(jsonObj); + cordova.getActivity().getApplicationContext().getFileStreamPath("cached_payload").delete(); + + callbak.success(jsonObj); + + return true; + + } catch (FileNotFoundException fnf) { + + Log.e("REGISTER", fnf.getMessage()); + callbak.error(fnf.getMessage()); + + + } catch (IOException io) { + + io.printStackTrace(); + callbak.error(io.getMessage()); + + + } catch (JSONException j) { + + j.printStackTrace(); + callbak.error(j.getMessage()); + + } + + PushHandlerActivity.EXITED = false; + + } else if (UNREGISTER.equals(action)) { + + GCMRegistrar.unregister(this.cordova.getActivity()); + + Log.v(ME + ":" + UNREGISTER, "GCMRegistrar.unregister called "); + + callbak.success("Unregistration Success"); + + return true; + + } else { + + Log.e(ME, "Invalid action : " + action); + + callbak.error("Unregistration Failed"); + + return false; + } + + return false; } - - return result; - } - - - public static void sendJavascript( JSONObject _json ) - { - String _d = "javascript:"+gECB+"(" + _json.toString() + ")"; + + public static void sendJavascript(JSONObject _json) { + + String _d = "javascript:" + gECB + "(" + _json.toString() + ")"; Log.v(ME + ":sendJavascript", _d); - - if (gECB != null ) { - gwebView.sendJavascript( _d ); + + if (gECB != null) { + + staticWebView.sendJavascript(_d); + } - } - - - /** - * Gets the Directory listing for file, in JSON format - * @param file The file for which we want to do directory listing - * @return JSONObject representation of directory list. e.g {"filename":"/sdcard","isdir":true,"children":[{"filename":"a.txt","isdir":false},{..}]} - * @throws JSONException - */ - - + } + + + public void onDestroy() { + super.onDestroy(); + + // let the service know we are exiting so it can cache the next notification payload. + PushHandlerActivity.EXITED = true; + GCMRegistrar.onDestroy(cordova.getActivity()); + } }