Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to be compliant with Cordova 2.7 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
src/.DS_Store
src/android/.DS_Store
233 changes: 143 additions & 90 deletions src/android/plugin/PushPlugin.java
Original file line number Diff line number Diff line change
@@ -1,110 +1,163 @@
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;
import org.json.JSONObject;

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