From cc2efe690d5832a0ee15fc5047bbb97a809408d4 Mon Sep 17 00:00:00 2001 From: "user.name" Date: Tue, 12 May 2015 04:17:32 +0300 Subject: [PATCH 01/17] Added support for location requests from server. --- plugin.xml | 16 +-- .../com/plugin/gcm/GCMIntentService.java | 107 ++++++++++++++---- 2 files changed, 97 insertions(+), 26 deletions(-) diff --git a/plugin.xml b/plugin.xml index 68a46ff3..d2b2c123 100755 --- a/plugin.xml +++ b/plugin.xml @@ -37,13 +37,15 @@ - - - - - - - + + + + + + + + + diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index caee145e..5d3db09f 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -2,6 +2,14 @@ import org.json.JSONException; import org.json.JSONObject; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.client.HttpClient; +import java.io.IOException; +import org.apache.http.client.ClientProtocolException; +import java.io.UnsupportedEncodingException; +import org.json.JSONArray; import android.annotation.SuppressLint; import android.app.Notification; @@ -9,12 +17,17 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.util.Log; +import android.widget.Toast; import com.google.android.gcm.GCMBaseIntentService; + @SuppressLint("NewApi") public class GCMIntentService extends GCMBaseIntentService { @@ -31,8 +44,7 @@ public void onRegistered(Context context, String regId) { JSONObject json; - try - { + try { json = new JSONObject().put("event", "registered"); json.put("regid", regId); @@ -42,9 +54,7 @@ public void onRegistered(Context context, String regId) { // In this case this is the registration ID PushPlugin.sendJavascript( json ); - } - catch( JSONException e) - { + } catch( JSONException e) { // No message to the user is sent, JSON failed Log.e(TAG, "onRegistered: JSON exception"); } @@ -63,6 +73,10 @@ protected void onMessage(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { + if (extras.getString("type").equals("REPORT_LOCATION")) { + reportLocation(extras.getString("secret")); + } + // if we are in the foreground, just surface the payload, else post it to the statusbar if (PushPlugin.isInForeground()) { extras.putBoolean("foreground", true); @@ -79,8 +93,68 @@ protected void onMessage(Context context, Intent intent) { } } - public void createNotification(Context context, Bundle extras) - { + public static void makeRequest(JSONObject holder) { + final String url = "https://app-for-lunch.appspot.com/rpc"; + Log.i(TAG, "Making report location request."); + HttpClient httpClient = new DefaultHttpClient(); + HttpPost httpost = new HttpPost(url); + try { + Log.i(TAG, "Message to send: " + holder.toString()); + StringEntity se = new StringEntity(holder.toString()); + httpost.setEntity(se); + httpost.setHeader("Accept", "application/json"); + httpost.setHeader("Content-type", "application/json"); + try { + Log.i(TAG, "Executing."); + httpClient.execute(httpost); + } catch (ClientProtocolException e) { + Log.i(TAG, "Protocol exception."); + } catch (IOException e) { + Log.i(TAG, "IO exception."); + } + } catch (UnsupportedEncodingException e) { + Log.i(TAG, "Unsupported encoding exception."); + } + } + + private static JSONObject getJsonObject(String secret, double lat, double lng) throws JSONException { + JSONObject holder = new JSONObject(); + JSONObject params = new JSONObject(); + params.put("secret", secret); + params.put("lat", lat); + params.put("long", lng); + holder.put("method", "RPCService.UpdateLocation"); + JSONArray array = new JSONArray(); + array.put(params); + holder.put("params", array); + holder.put("id", 0); + return holder; + } + + public void reportLocation(final String secret) { + final Context context = this; + LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); + if (lastKnownLocation == null) { + Log.i(TAG, "Could not get last location by GPS."); + lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + } + if (lastKnownLocation == null) { + Log.i(TAG, "Could not get last location by network. Aborting."); + return; + } + double latitude = lastKnownLocation.getLatitude(); + double longitude = lastKnownLocation.getLongitude(); + double speed = lastKnownLocation.getSpeed(); // meter/minute + speed = (speed * 3600) / 1000; // km/minute + try { + GCMIntentService.makeRequest(GCMIntentService.getJsonObject(secret, latitude, longitude)); + } catch (JSONException e) { + e.printStackTrace(); + } + } + + public void createNotification(Context context, Bundle extras) { NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String appName = getAppName(this); @@ -98,6 +172,7 @@ public void createNotification(Context context, Bundle extras) } catch (NumberFormatException e) {} } + extras.putLong("time", System.currentTimeMillis()); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setDefaults(defaults) @@ -124,24 +199,19 @@ public void createNotification(Context context, Bundle extras) try { notId = Integer.parseInt(extras.getString("notId")); - } - catch(NumberFormatException e) { + } catch(NumberFormatException e) { Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage()); - } - catch(Exception e) { + } catch(Exception e) { Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage()); } mNotificationManager.notify((String) appName, notId, mBuilder.build()); } - private static String getAppName(Context context) - { - CharSequence appName = - context - .getPackageManager() - .getApplicationLabel(context.getApplicationInfo()); - + private static String getAppName(Context context) { + CharSequence appName = context + .getPackageManager() + .getApplicationLabel(context.getApplicationInfo()); return (String)appName; } @@ -149,5 +219,4 @@ private static String getAppName(Context context) public void onError(Context context, String errorId) { Log.e(TAG, "onError - errorId: " + errorId); } - } From 819f073d1b9cfa4a9c98d887dfd530b2a04539a9 Mon Sep 17 00:00:00 2001 From: "user.name" Date: Thu, 14 May 2015 02:50:04 +0300 Subject: [PATCH 02/17] URL is now passed in as parameter. --- plugin.xml | 14 +++++++------- src/android/com/plugin/gcm/GCMIntentService.java | 4 +++- src/android/com/plugin/gcm/PushPlugin.java | 7 +++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/plugin.xml b/plugin.xml index d2b2c123..bcfb327f 100755 --- a/plugin.xml +++ b/plugin.xml @@ -37,15 +37,15 @@ - + - - - - - - + + + + + + diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index 5d3db09f..7203446d 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -10,6 +10,7 @@ import org.apache.http.client.ClientProtocolException; import java.io.UnsupportedEncodingException; import org.json.JSONArray; +import android.content.SharedPreferences; import android.annotation.SuppressLint; import android.app.Notification; @@ -94,7 +95,8 @@ protected void onMessage(Context context, Intent intent) { } public static void makeRequest(JSONObject holder) { - final String url = "https://app-for-lunch.appspot.com/rpc"; + SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); + final string url = sharedPref.getString("reportLocationURL"); Log.i(TAG, "Making report location request."); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpost = new HttpPost(url); diff --git a/src/android/com/plugin/gcm/PushPlugin.java b/src/android/com/plugin/gcm/PushPlugin.java index 3e390856..fd02f6f6 100644 --- a/src/android/com/plugin/gcm/PushPlugin.java +++ b/src/android/com/plugin/gcm/PushPlugin.java @@ -12,6 +12,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.content.SharedPreferences; import java.util.Iterator; @@ -59,6 +60,12 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo gECB = (String) jo.get("ecb"); gSenderID = (String) jo.get("senderID"); + reportLocationURL = (String) io.get("reportLocationURL"); + + SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPref.edit(); + editor.putInt(getString("reportLocationURL"), reportLocationURL); + editor.commit(); Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID); From 8034766ec563a653156530a5f4ba4501fab970d1 Mon Sep 17 00:00:00 2001 From: "user.name" Date: Thu, 14 May 2015 03:14:06 +0300 Subject: [PATCH 03/17] Fixed bugs. --- src/android/com/plugin/gcm/GCMIntentService.java | 6 +++--- src/android/com/plugin/gcm/PushPlugin.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index 7203446d..2ae30b50 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -95,9 +95,9 @@ protected void onMessage(Context context, Intent intent) { } public static void makeRequest(JSONObject holder) { - SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); - final string url = sharedPref.getString("reportLocationURL"); - Log.i(TAG, "Making report location request."); + SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("prefs", Context.MODE_PRIVATE); + final String url = sharedPref.getString("reportLocationURL", ""); + Log.i(TAG, "Making report location request to: " + url); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpost = new HttpPost(url); try { diff --git a/src/android/com/plugin/gcm/PushPlugin.java b/src/android/com/plugin/gcm/PushPlugin.java index fd02f6f6..8ae327d8 100644 --- a/src/android/com/plugin/gcm/PushPlugin.java +++ b/src/android/com/plugin/gcm/PushPlugin.java @@ -60,11 +60,11 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo gECB = (String) jo.get("ecb"); gSenderID = (String) jo.get("senderID"); - reportLocationURL = (String) io.get("reportLocationURL"); + String reportLocationURL = (String) jo.get("reportLocationURL"); - SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); + SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("prefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); - editor.putInt(getString("reportLocationURL"), reportLocationURL); + editor.putInt("reportLocationURL", reportLocationURL); editor.commit(); Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID); From b9cc440810b6b5fcf823ac31a56a5cb58df28374 Mon Sep 17 00:00:00 2001 From: "user.name" Date: Thu, 14 May 2015 03:20:29 +0300 Subject: [PATCH 04/17] asd --- src/android/com/plugin/gcm/GCMIntentService.java | 3 ++- src/android/com/plugin/gcm/PushPlugin.java | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index 2ae30b50..ceced70d 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -11,6 +11,7 @@ import java.io.UnsupportedEncodingException; import org.json.JSONArray; import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.annotation.SuppressLint; import android.app.Notification; @@ -95,7 +96,7 @@ protected void onMessage(Context context, Intent intent) { } public static void makeRequest(JSONObject holder) { - SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("prefs", Context.MODE_PRIVATE); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(); final String url = sharedPref.getString("reportLocationURL", ""); Log.i(TAG, "Making report location request to: " + url); HttpClient httpClient = new DefaultHttpClient(); diff --git a/src/android/com/plugin/gcm/PushPlugin.java b/src/android/com/plugin/gcm/PushPlugin.java index 8ae327d8..941d061c 100644 --- a/src/android/com/plugin/gcm/PushPlugin.java +++ b/src/android/com/plugin/gcm/PushPlugin.java @@ -13,6 +13,7 @@ import org.json.JSONException; import org.json.JSONObject; import android.content.SharedPreferences; +import android.preference.PreferenceManager; import java.util.Iterator; @@ -62,9 +63,9 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo gSenderID = (String) jo.get("senderID"); String reportLocationURL = (String) jo.get("reportLocationURL"); - SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("prefs", Context.MODE_PRIVATE); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(); SharedPreferences.Editor editor = sharedPref.edit(); - editor.putInt("reportLocationURL", reportLocationURL); + editor.putString("reportLocationURL", reportLocationURL); editor.commit(); Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID); From 9939f393d2de6f61e9fbc3682a3b710505b77ff4 Mon Sep 17 00:00:00 2001 From: "user.name" Date: Thu, 14 May 2015 03:24:01 +0300 Subject: [PATCH 05/17] asd --- src/android/com/plugin/gcm/GCMIntentService.java | 2 +- src/android/com/plugin/gcm/PushPlugin.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index ceced70d..8c6fe3d9 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -96,7 +96,7 @@ protected void onMessage(Context context, Intent intent) { } public static void makeRequest(JSONObject holder) { - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); final String url = sharedPref.getString("reportLocationURL", ""); Log.i(TAG, "Making report location request to: " + url); HttpClient httpClient = new DefaultHttpClient(); diff --git a/src/android/com/plugin/gcm/PushPlugin.java b/src/android/com/plugin/gcm/PushPlugin.java index 941d061c..d20a51f2 100644 --- a/src/android/com/plugin/gcm/PushPlugin.java +++ b/src/android/com/plugin/gcm/PushPlugin.java @@ -63,7 +63,7 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo gSenderID = (String) jo.get("senderID"); String reportLocationURL = (String) jo.get("reportLocationURL"); - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("reportLocationURL", reportLocationURL); editor.commit(); From 7d41f5e5633272a0f8ac74e8068613d87ad1278a Mon Sep 17 00:00:00 2001 From: "user.name" Date: Thu, 14 May 2015 03:30:45 +0300 Subject: [PATCH 06/17] asd --- src/android/com/plugin/gcm/GCMIntentService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index 8c6fe3d9..6b97bccb 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -95,9 +95,7 @@ protected void onMessage(Context context, Intent intent) { } } - public static void makeRequest(JSONObject holder) { - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - final String url = sharedPref.getString("reportLocationURL", ""); + public static void makeRequest(String url, JSONObject holder) { Log.i(TAG, "Making report location request to: " + url); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpost = new HttpPost(url); @@ -151,7 +149,9 @@ public void reportLocation(final String secret) { double speed = lastKnownLocation.getSpeed(); // meter/minute speed = (speed * 3600) / 1000; // km/minute try { - GCMIntentService.makeRequest(GCMIntentService.getJsonObject(secret, latitude, longitude)); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + String url = sharedPref.getString("reportLocationURL", ""); + GCMIntentService.makeRequest(url, GCMIntentService.getJsonObject(secret, latitude, longitude)); } catch (JSONException e) { e.printStackTrace(); } From 38ae18720cf6d4c9635f2d7a45e12fa6d7ced09b Mon Sep 17 00:00:00 2001 From: uriva Date: Sat, 30 May 2015 14:41:11 +0300 Subject: [PATCH 07/17] Update PushPlugin.java Fixed indentation. --- src/android/com/plugin/gcm/PushPlugin.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/android/com/plugin/gcm/PushPlugin.java b/src/android/com/plugin/gcm/PushPlugin.java index d20a51f2..e7df1368 100644 --- a/src/android/com/plugin/gcm/PushPlugin.java +++ b/src/android/com/plugin/gcm/PushPlugin.java @@ -32,7 +32,7 @@ public class PushPlugin extends CordovaPlugin { private static String gECB; private static String gSenderID; private static Bundle gCachedExtras = null; - private static boolean gForeground = false; + private static boolean gForeground = false; /** * Gets the application context from cordova's main activity. @@ -61,9 +61,9 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo gECB = (String) jo.get("ecb"); gSenderID = (String) jo.get("senderID"); - String reportLocationURL = (String) jo.get("reportLocationURL"); + String reportLocationURL = (String) jo.get("reportLocationURL"); - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("reportLocationURL", reportLocationURL); editor.commit(); From 0de524dba23bfd8667afe6819c5082f029312b2d Mon Sep 17 00:00:00 2001 From: uriva Date: Sat, 30 May 2015 14:43:01 +0300 Subject: [PATCH 08/17] Update GCMIntentService.java Fixed indentation. --- .../com/plugin/gcm/GCMIntentService.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index 6b97bccb..d8b74247 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -144,17 +144,17 @@ public void reportLocation(final String secret) { Log.i(TAG, "Could not get last location by network. Aborting."); return; } - double latitude = lastKnownLocation.getLatitude(); - double longitude = lastKnownLocation.getLongitude(); - double speed = lastKnownLocation.getSpeed(); // meter/minute - speed = (speed * 3600) / 1000; // km/minute - try { - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + double latitude = lastKnownLocation.getLatitude(); + double longitude = lastKnownLocation.getLongitude(); + double speed = lastKnownLocation.getSpeed(); // meter/minute + speed = (speed * 3600) / 1000; // km/minute + try { + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); String url = sharedPref.getString("reportLocationURL", ""); - GCMIntentService.makeRequest(url, GCMIntentService.getJsonObject(secret, latitude, longitude)); - } catch (JSONException e) { - e.printStackTrace(); - } + GCMIntentService.makeRequest(url, GCMIntentService.getJsonObject(secret, latitude, longitude)); + } catch (JSONException e) { + e.printStackTrace(); + } } public void createNotification(Context context, Bundle extras) { From dc54bdd27b37285c0d740d24b4fa3a9507a57843 Mon Sep 17 00:00:00 2001 From: uriva Date: Sat, 30 May 2015 14:45:30 +0300 Subject: [PATCH 09/17] Update GCMIntentService.java Fixed indentation. --- .../com/plugin/gcm/GCMIntentService.java | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index d8b74247..d795ab66 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -73,26 +73,23 @@ protected void onMessage(Context context, Intent intent) { // Extract the payload from the message Bundle extras = intent.getExtras(); - if (extras != null) - { + if (extras != null) { if (extras.getString("type").equals("REPORT_LOCATION")) { - reportLocation(extras.getString("secret")); - } + reportLocation(extras.getString("secret")); + } // if we are in the foreground, just surface the payload, else post it to the statusbar - if (PushPlugin.isInForeground()) { + if (PushPlugin.isInForeground()) { extras.putBoolean("foreground", true); - PushPlugin.sendExtras(extras); - } - else { + PushPlugin.sendExtras(extras); + } else { extras.putBoolean("foreground", false); - - // Send a notification if there is a message - if (extras.getString("message") != null && extras.getString("message").length() != 0) { - createNotification(context, extras); - } - } - } + // Send a notification if there is a message + if (extras.getString("message") != null && extras.getString("message").length() != 0) { + createNotification(context, extras); + } + } + } } public static void makeRequest(String url, JSONObject holder) { From 59ca675881c98b3e7b7097834129c6ad1142f065 Mon Sep 17 00:00:00 2001 From: uriva Date: Sat, 30 May 2015 14:48:01 +0300 Subject: [PATCH 10/17] Update plugin.xml Alphabetized and fixed indentation. --- plugin.xml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugin.xml b/plugin.xml index bcfb327f..fa8e2746 100755 --- a/plugin.xml +++ b/plugin.xml @@ -37,15 +37,15 @@ - - - + + + + - + + - - From 5be32b8200fbaa09aef4fccd823c233eeaca30bd Mon Sep 17 00:00:00 2001 From: uriva Date: Sun, 16 Aug 2015 22:55:49 +0300 Subject: [PATCH 11/17] Update GCMIntentService.java --- src/android/com/plugin/gcm/GCMIntentService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index d795ab66..6e0cd35f 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -74,7 +74,7 @@ protected void onMessage(Context context, Intent intent) { // Extract the payload from the message Bundle extras = intent.getExtras(); if (extras != null) { - if (extras.getString("type").equals("REPORT_LOCATION")) { + if (extras.containsKey("secret") && extras.containsKey("type") && extras.getString("type").equals("REPORT_LOCATION")) { reportLocation(extras.getString("secret")); } From 56ed7b05e9124315ebcf3deab9df3a479a6c4f1c Mon Sep 17 00:00:00 2001 From: "user.name" Date: Fri, 28 Aug 2015 16:22:22 +0300 Subject: [PATCH 12/17] Support for reporting location --- src/android/com/plugin/gcm/GCMIntentService.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index 6e0cd35f..51feda62 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -74,9 +74,11 @@ protected void onMessage(Context context, Intent intent) { // Extract the payload from the message Bundle extras = intent.getExtras(); if (extras != null) { - if (extras.containsKey("secret") && extras.containsKey("type") && extras.getString("type").equals("REPORT_LOCATION")) { - reportLocation(extras.getString("secret")); - } + if (extras.containsKey("secret") && + extras.containsKey("reportLocation") && + extras.getBoolean("reportLocation")) { + reportLocation(extras.getString("secret")); + } // if we are in the foreground, just surface the payload, else post it to the statusbar if (PushPlugin.isInForeground()) { From 9159644aaa00fe6d9506c06dd2ba9d6fdf206be6 Mon Sep 17 00:00:00 2001 From: "user.name" Date: Fri, 28 Aug 2015 17:34:43 +0300 Subject: [PATCH 13/17] more printouts --- .../com/plugin/gcm/GCMIntentService.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index 51feda62..c82099b3 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -73,6 +73,13 @@ protected void onMessage(Context context, Intent intent) { // Extract the payload from the message Bundle extras = intent.getExtras(); + + for (String key : extras.keySet()) { + Object value = extras.get(key); + Log.d(TAG, String.format( + "%s %s (%s)", key, value.toString(), value.getClass().getName())); + } + if (extras != null) { if (extras.containsKey("secret") && extras.containsKey("reportLocation") && @@ -86,12 +93,13 @@ protected void onMessage(Context context, Intent intent) { PushPlugin.sendExtras(extras); } else { extras.putBoolean("foreground", false); - // Send a notification if there is a message - if (extras.getString("message") != null && extras.getString("message").length() != 0) { - createNotification(context, extras); - } - } - } + // Send a notification if there is a message + if (extras.getString("message") != null && + extras.getString("message").length() != 0) { + createNotification(context, extras); + } + } + } } public static void makeRequest(String url, JSONObject holder) { @@ -149,7 +157,7 @@ public void reportLocation(final String secret) { speed = (speed * 3600) / 1000; // km/minute try { SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - String url = sharedPref.getString("reportLocationURL", ""); + String url = sharedPref.getString("reportLocationURL", ""); GCMIntentService.makeRequest(url, GCMIntentService.getJsonObject(secret, latitude, longitude)); } catch (JSONException e) { e.printStackTrace(); From 7ae92460b82be7fe464c1d25f0f3259add82d8e6 Mon Sep 17 00:00:00 2001 From: "user.name" Date: Fri, 28 Aug 2015 18:10:51 +0300 Subject: [PATCH 14/17] changed from boolean to string --- src/android/com/plugin/gcm/GCMIntentService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index c82099b3..9fee8ffb 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -83,7 +83,7 @@ protected void onMessage(Context context, Intent intent) { if (extras != null) { if (extras.containsKey("secret") && extras.containsKey("reportLocation") && - extras.getBoolean("reportLocation")) { + extras.getString("reportLocation").equals("true")) { reportLocation(extras.getString("secret")); } From d5e494c618cbb64139f35d2487ab21547234f40e Mon Sep 17 00:00:00 2001 From: "user.name" Date: Fri, 28 Aug 2015 18:22:47 +0300 Subject: [PATCH 15/17] passing params properly --- src/android/com/plugin/gcm/GCMIntentService.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index 9fee8ffb..c2c6a71c 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -126,17 +126,11 @@ public static void makeRequest(String url, JSONObject holder) { } private static JSONObject getJsonObject(String secret, double lat, double lng) throws JSONException { - JSONObject holder = new JSONObject(); JSONObject params = new JSONObject(); params.put("secret", secret); params.put("lat", lat); - params.put("long", lng); - holder.put("method", "RPCService.UpdateLocation"); - JSONArray array = new JSONArray(); - array.put(params); - holder.put("params", array); - holder.put("id", 0); - return holder; + params.put("lng", lng); + return param; } public void reportLocation(final String secret) { From f32d466abd7360b481560892ad1539c289750953 Mon Sep 17 00:00:00 2001 From: "user.name" Date: Fri, 28 Aug 2015 18:36:11 +0300 Subject: [PATCH 16/17] asD --- src/android/com/plugin/gcm/GCMIntentService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index c2c6a71c..52d982a0 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -130,7 +130,7 @@ private static JSONObject getJsonObject(String secret, double lat, double lng) t params.put("secret", secret); params.put("lat", lat); params.put("lng", lng); - return param; + return params; } public void reportLocation(final String secret) { From 508808575adfeeb20b217d8019ef6396b8595822 Mon Sep 17 00:00:00 2001 From: "user.name" Date: Sun, 27 Sep 2015 04:40:22 +0300 Subject: [PATCH 17/17] Fixed indent --- .../com/plugin/gcm/GCMIntentService.java | 7 +- src/android/com/plugin/gcm/PushPlugin.java | 410 ++++++++---------- 2 files changed, 192 insertions(+), 225 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index 52d982a0..d66301b3 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -87,10 +87,11 @@ protected void onMessage(Context context, Intent intent) { reportLocation(extras.getString("secret")); } - // if we are in the foreground, just surface the payload, else post it to the statusbar - if (PushPlugin.isInForeground()) { + // if we are in the foreground, just surface the payload, else post it + // to the status bar. + if (PushPlugin.isInForeground()) { extras.putBoolean("foreground", true); - PushPlugin.sendExtras(extras); + PushPlugin.sendExtras(extras); } else { extras.putBoolean("foreground", false); // Send a notification if there is a message diff --git a/src/android/com/plugin/gcm/PushPlugin.java b/src/android/com/plugin/gcm/PushPlugin.java index e7df1368..78db6559 100644 --- a/src/android/com/plugin/gcm/PushPlugin.java +++ b/src/android/com/plugin/gcm/PushPlugin.java @@ -22,232 +22,198 @@ */ public class PushPlugin extends CordovaPlugin { - public static final String TAG = "PushPlugin"; - - public static final String REGISTER = "register"; - public static final String UNREGISTER = "unregister"; - public static final String EXIT = "exit"; - - private static CordovaWebView gWebView; - private static String gECB; - private static String gSenderID; - private static Bundle gCachedExtras = null; - private static boolean gForeground = false; - - /** - * Gets the application context from cordova's main activity. - * @return the application context - */ - private Context getApplicationContext() { - return this.cordova.getActivity().getApplicationContext(); - } - - @Override - public boolean execute(String action, JSONArray data, CallbackContext callbackContext) { - - boolean result = false; - - Log.v(TAG, "execute: action=" + action); - - if (REGISTER.equals(action)) { - - Log.v(TAG, "execute: data=" + data.toString()); - - try { - JSONObject jo = data.getJSONObject(0); - - gWebView = this.webView; - Log.v(TAG, "execute: jo=" + jo.toString()); - - gECB = (String) jo.get("ecb"); - gSenderID = (String) jo.get("senderID"); - String reportLocationURL = (String) jo.get("reportLocationURL"); - - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - SharedPreferences.Editor editor = sharedPref.edit(); - editor.putString("reportLocationURL", reportLocationURL); - editor.commit(); - - Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID); - - GCMRegistrar.register(getApplicationContext(), gSenderID); - result = true; - callbackContext.success(); - } catch (JSONException e) { - Log.e(TAG, "execute: Got JSON Exception " + e.getMessage()); - result = false; - callbackContext.error(e.getMessage()); - } - - if ( gCachedExtras != null) { - Log.v(TAG, "sending cached extras"); - sendExtras(gCachedExtras); - gCachedExtras = null; - } - - } else if (UNREGISTER.equals(action)) { - - GCMRegistrar.unregister(getApplicationContext()); - - Log.v(TAG, "UNREGISTER"); - result = true; - callbackContext.success(); - } else { - result = false; - Log.e(TAG, "Invalid action : " + action); - callbackContext.error("Invalid action : " + action); - } - - return result; - } - - /* - * Sends a json object to the client as parameter to a method which is defined in gECB. - */ - public static void sendJavascript(JSONObject _json) { - String _d = "javascript:" + gECB + "(" + _json.toString() + ")"; - Log.v(TAG, "sendJavascript: " + _d); - - if (gECB != null && gWebView != null) { - gWebView.sendJavascript(_d); - } - } - - /* - * Sends the pushbundle extras to the client application. - * If the client application isn't currently active, it is cached for later processing. - */ - public static void sendExtras(Bundle extras) - { - if (extras != null) { - if (gECB != null && gWebView != null) { - sendJavascript(convertBundleToJson(extras)); - } else { - Log.v(TAG, "sendExtras: caching extras to send at a later time."); - gCachedExtras = extras; - } - } - } - - @Override - public void initialize(CordovaInterface cordova, CordovaWebView webView) { - super.initialize(cordova, webView); - gForeground = true; + public static final String TAG = "PushPlugin"; + + public static final String REGISTER = "register"; + public static final String UNREGISTER = "unregister"; + public static final String EXIT = "exit"; + + private static CordovaWebView gWebView; + private static String gECB; + private static String gSenderID; + private static Bundle gCachedExtras = null; + private static boolean gForeground = false; + + /** + * Gets the application context from cordova's main activity. + * @return the application context + */ + private Context getApplicationContext() { + return this.cordova.getActivity().getApplicationContext(); + } + + @Override + public boolean execute(String action, JSONArray data, + CallbackContext callbackContext) { + boolean result = false; + Log.v(TAG, "execute: action=" + action); + if (REGISTER.equals(action)) { + Log.v(TAG, "execute: data=" + data.toString()); + try { + JSONObject jo = data.getJSONObject(0); + gWebView = this.webView; + Log.v(TAG, "execute: jo=" + jo.toString()); + gECB = (String) jo.get("ecb"); + gSenderID = (String) jo.get("senderID"); + String reportLocationURL = (String) jo.get("reportLocationURL"); + SharedPreferences sharedPref = + PreferenceManager.getDefaultSharedPreferences( + getApplicationContext()); + SharedPreferences.Editor editor = sharedPref.edit(); + editor.putString("reportLocationURL", reportLocationURL); + editor.commit(); + Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID); + GCMRegistrar.register(getApplicationContext(), gSenderID); + result = true; + callbackContext.success(); + } catch (JSONException e) { + Log.e(TAG, "execute: Got JSON Exception " + e.getMessage()); + result = false; + callbackContext.error(e.getMessage()); + } + if (gCachedExtras != null) { + Log.v(TAG, "sending cached extras"); + sendExtras(gCachedExtras); + gCachedExtras = null; + } + } else if (UNREGISTER.equals(action)) { + GCMRegistrar.unregister(getApplicationContext()); + Log.v(TAG, "UNREGISTER"); + result = true; + callbackContext.success(); + } else { + result = false; + Log.e(TAG, "Invalid action : " + action); + callbackContext.error("Invalid action : " + action); } - - @Override - public void onPause(boolean multitasking) { - super.onPause(multitasking); - gForeground = false; - final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.cancelAll(); + return result; + } + + /* + * Sends a json object to the client as parameter to a method which is defined + * in gECB. + */ + public static void sendJavascript(JSONObject _json) { + String _d = "javascript:" + gECB + "(" + _json.toString() + ")"; + Log.v(TAG, "sendJavascript: " + _d); + + if (gECB != null && gWebView != null) { + gWebView.sendJavascript(_d); } - - @Override - public void onResume(boolean multitasking) { - super.onResume(multitasking); - gForeground = true; + } + + /* + * Sends the pushbundle extras to the client application. + * If the client application isn't currently active, it is cached for later + * processing. + */ + public static void sendExtras(Bundle extras) { + if (extras != null) { + if (gECB != null && gWebView != null) { + sendJavascript(convertBundleToJson(extras)); + } else { + Log.v(TAG, "sendExtras: caching extras to send at a later time."); + gCachedExtras = extras; + } } - - @Override - public void onDestroy() { - super.onDestroy(); - gForeground = false; - gECB = null; - gWebView = null; + } + + @Override + public void initialize(CordovaInterface cordova, CordovaWebView webView) { + super.initialize(cordova, webView); + gForeground = true; + } + + @Override + public void onPause(boolean multitasking) { + super.onPause(multitasking); + gForeground = false; + final NotificationManager notificationManager = + (NotificationManager) cordova.getActivity() + .getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancelAll(); + } + + @Override + public void onResume(boolean multitasking) { + super.onResume(multitasking); + gForeground = true; + } + + @Override + public void onDestroy() { + super.onDestroy(); + gForeground = false; + gECB = null; + gWebView = null; + } + + /* + * serializes a bundle to JSON. + */ + private static JSONObject convertBundleToJson(Bundle extras) { + try { + JSONObject json; + json = new JSONObject().put("event", "message"); + + JSONObject jsondata = new JSONObject(); + Iterator it = extras.keySet().iterator(); + while (it.hasNext()) { + String key = it.next(); + Object value = extras.get(key); + + // System data from Android + if (key.equals("from") || key.equals("collapse_key")) { + json.put(key, value); + } else if (key.equals("foreground")) { + json.put(key, extras.getBoolean("foreground")); + } else if (key.equals("coldstart")) { + json.put(key, extras.getBoolean("coldstart")); + } else { + // Maintain backwards compatibility + if (key.equals("message") || key.equals("msgcnt") || + key.equals("soundname")) { + json.put(key, value); + } + + if (value instanceof String) { + // Try to figure out if the value is another JSON object + String strValue = (String)value; + if (strValue.startsWith("{")) { + try { + JSONObject json2 = new JSONObject(strValue); + jsondata.put(key, json2); + } catch (Exception e) { + jsondata.put(key, value); + } + // Try to figure out if the value is another JSON array + } else if (strValue.startsWith("[")){ + try { + JSONArray json2 = new JSONArray(strValue); + jsondata.put(key, json2); + } catch (Exception e) { + jsondata.put(key, value); + } + } else { + jsondata.put(key, value); + } + } + } + } // while + json.put("payload", jsondata); + Log.v(TAG, "extrasToJSON: " + json.toString()); + return json; + } catch( JSONException e) { + Log.e(TAG, "extrasToJSON: JSON exception"); } + return null; + } - /* - * serializes a bundle to JSON. - */ - private static JSONObject convertBundleToJson(Bundle extras) - { - try - { - JSONObject json; - json = new JSONObject().put("event", "message"); - - JSONObject jsondata = new JSONObject(); - Iterator it = extras.keySet().iterator(); - while (it.hasNext()) - { - String key = it.next(); - Object value = extras.get(key); - - // System data from Android - if (key.equals("from") || key.equals("collapse_key")) - { - json.put(key, value); - } - else if (key.equals("foreground")) - { - json.put(key, extras.getBoolean("foreground")); - } - else if (key.equals("coldstart")) - { - json.put(key, extras.getBoolean("coldstart")); - } - else - { - // Maintain backwards compatibility - if (key.equals("message") || key.equals("msgcnt") || key.equals("soundname")) - { - json.put(key, value); - } - - if ( value instanceof String ) { - // Try to figure out if the value is another JSON object - - String strValue = (String)value; - if (strValue.startsWith("{")) { - try { - JSONObject json2 = new JSONObject(strValue); - jsondata.put(key, json2); - } - catch (Exception e) { - jsondata.put(key, value); - } - // Try to figure out if the value is another JSON array - } - else if (strValue.startsWith("[")) - { - try - { - JSONArray json2 = new JSONArray(strValue); - jsondata.put(key, json2); - } - catch (Exception e) - { - jsondata.put(key, value); - } - } - else - { - jsondata.put(key, value); - } - } - } - } // while - json.put("payload", jsondata); - - Log.v(TAG, "extrasToJSON: " + json.toString()); - - return json; - } - catch( JSONException e) - { - Log.e(TAG, "extrasToJSON: JSON exception"); - } - return null; - } + public static boolean isInForeground() { + return gForeground; + } - public static boolean isInForeground() - { - return gForeground; - } - - public static boolean isActive() - { - return gWebView != null; - } + public static boolean isActive() { + return gWebView != null; + } }