Skip to content

Commit

Permalink
Merge pull request cowbell#226 from cowbell/error_codes
Browse files Browse the repository at this point in the history
Error codes
  • Loading branch information
tsubik authored May 6, 2017
2 parents 52df0c2 + a7deb7c commit 82844c5
Show file tree
Hide file tree
Showing 15 changed files with 584 additions and 381 deletions.
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ For listening of geofence transistion you can override onTransitionReceived meth
- `TransitionType.EXIT` = 2
- `TransitionType.BOTH` = 3

## Error Codes

Both `onError` function handler and promise rejection take `error` object as an argument.

```
error: {
code: String,
message: String
}
```

Error codes:

- `UNKNOWN`
- `PERMISSION_DENIED`
- `GEOFENCE_NOT_AVAILABLE`
- `GEOFENCE_LIMIT_EXCEEDED`

## Plugin initialization

The plugin is not available until `deviceready` event is fired.
Expand Down Expand Up @@ -165,8 +183,8 @@ window.geofence.addOrUpdate({
}
}).then(function () {
console.log('Geofence successfully added');
}, function (reason) {
console.log('Adding geofence failed', reason);
}, function (error) {
console.log('Adding geofence failed', error);
});
```
Adding more geofences at once
Expand Down Expand Up @@ -243,8 +261,8 @@ window.geofence.remove(geofenceId)
.then(function () {
console.log('Geofence sucessfully removed');
}
, function (reason){
console.log('Removing geofence failed', reason);
, function (error){
console.log('Removing geofence failed', error);
});
```
Removing more than one geofence at once.
Expand All @@ -259,8 +277,8 @@ window.geofence.removeAll()
.then(function () {
console.log('All geofences successfully removed.');
}
, function (reason) {
console.log('Removing geofences failed', reason);
, function (error) {
console.log('Removing geofences failed', error);
});
```

Expand Down
3 changes: 1 addition & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ dependencies:
- echo y | android update sdk --no-ui --all --filter "extra-android-m2repository"
- echo y | android update sdk --no-ui --all --filter "extra-google-m2repository"
- if [ ! -d "$ANDROID_HOME/build-tools/24.0.3" ]; then echo y | android update sdk --no-ui --all --filter "build-tools-24.0.3"; fi
- if [ ! -d "$ANDROID_HOME/add-ons/addon-google_apis-google-22" ]; then echo y | android update sdk --no-ui --all --filter "addon-google_apis-google-22"; fi
- echo y | android update sdk --no-ui --all --filter "sys-img-armeabi-v7a-google_apis-22"
- echo y | android update sdk --no-ui --all --filter "addon-google_apis-google-22, sys-img-armeabi-v7a-addon-google_apis-google-22"
- echo 'no' | android create avd --force -n test -t "android-22" --abi armeabi-v7a --tag google_apis
- emulator -avd test -no-audio -no-window:
background: true
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
]
},
"scripts": {
"test:ios": "FIX_PARAMEDIC=true cordova-paramedic --platform ios --plugin . --verbose",
"pretest": "sed -i 's/play-services-location:+/play-services-location:9.8.0/g' plugin.xml",
"test": "cordova-paramedic --platform [email protected] --plugin . --verbose",
"posttest": "sed -i 's/play-services-location:9.8.0/play-services-location:+/g' plugin.xml"
Expand Down
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
<source-file src="src/android/AssetUtil.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/BeepHelper.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/BootReceiver.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/CommandExecutionHandler.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/GeoNotification.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/GeoNotificationManager.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/GeoNotificationNotifier.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/GeoNotificationStore.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/GeofencePlugin.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/GoogleServiceCommandExecutor.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/IGoogleServiceCommandListener.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/Gson.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/IGoogleServiceCommandListener.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/LocalStorage.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/LocalStorageDBHelper.java" target-dir="src/com/cowbell/cordova/geofence" />
<source-file src="src/android/Logger.java" target-dir="src/com/cowbell/cordova/geofence" />
Expand Down
6 changes: 5 additions & 1 deletion src/android/AbstractGoogleServiceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ public void Execute() {
}

protected void CommandExecuted() {
CommandExecuted(null);
}

protected void CommandExecuted(Object error) {
// Turn off the in progress flag and disconnect the client
connectionInProgress = false;
mGoogleApiClient.disconnect();
for (IGoogleServiceCommandListener listener : listeners) {
listener.onCommandExecuted();
listener.onCommandExecuted(error);
}
}

Expand Down
60 changes: 43 additions & 17 deletions src/android/AddGeofenceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofenceStatusCodes;
import com.google.android.gms.location.LocationServices;

import org.apache.cordova.LOG;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddGeofenceCommand extends AbstractGoogleServiceCommand {
private List<Geofence> geofencesToAdd;
Expand All @@ -24,26 +31,45 @@ public AddGeofenceCommand(Context context, PendingIntent pendingIntent,

@Override
public void ExecuteCustomCode() {
logger.log(Log.DEBUG, "Adding new geofences");
if (geofencesToAdd != null && geofencesToAdd.size() > 0) {
try {
LocationServices.GeofencingApi
.addGeofences(mGoogleApiClient, geofencesToAdd, pendingIntent)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
logger.log(Log.DEBUG, "Geofences successfully added");
logger.log(Log.DEBUG, "Adding new geofences...");
if (geofencesToAdd != null && geofencesToAdd.size() > 0) try {
LocationServices.GeofencingApi
.addGeofences(mGoogleApiClient, geofencesToAdd, pendingIntent)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
logger.log(Log.DEBUG, "Geofences successfully added");
CommandExecuted();
} else try {
Map<Integer, String> errorCodeMap = new HashMap<Integer, String>();
errorCodeMap.put(GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE, GeofencePlugin.ERROR_GEOFENCE_NOT_AVAILABLE);
errorCodeMap.put(GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES, GeofencePlugin.ERROR_GEOFENCE_LIMIT_EXCEEDED);

Integer statusCode = status.getStatusCode();
String message = "Adding geofences failed - SystemCode: " + statusCode;
JSONObject error = new JSONObject();
error.put("message", message);

if (statusCode == GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE) {
error.put("code", GeofencePlugin.ERROR_GEOFENCE_NOT_AVAILABLE);
} else if (statusCode == GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES) {
error.put("code", GeofencePlugin.ERROR_GEOFENCE_LIMIT_EXCEEDED);
} else {
logger.log(Log.DEBUG, "Adding geofences failed");
error.put("code", GeofencePlugin.ERROR_UNKNOWN);
}
CommandExecuted();

logger.log(Log.ERROR, message);
CommandExecuted(error);
} catch (JSONException exception) {
CommandExecuted(exception);
}
});
} catch (Exception exception) {
logger.log("Adding geofence failed", exception);
CommandExecuted();
}
}
});
} catch (Exception exception) {
logger.log(LOG.ERROR, "Exception while adding geofences");
exception.printStackTrace();
CommandExecuted(exception);
}
}
}
42 changes: 42 additions & 0 deletions src/android/CommandExecutionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.cowbell.cordova.geofence;

import org.apache.cordova.CallbackContext;
import org.json.JSONException;
import org.json.JSONObject;

public class CommandExecutionHandler implements IGoogleServiceCommandListener {
private CallbackContext callbackContext;

public CommandExecutionHandler(CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}

@Override
public void onCommandExecuted(Object error) {
if (error != null) try {
if (error instanceof Throwable) {
JSONObject errorObject = new JSONObject();
errorObject.put("message", ((Throwable) error).getMessage());
if (error instanceof SecurityException) {
errorObject.put("code", GeofencePlugin.ERROR_PERMISSION_DENIED);
} else {
errorObject.put("code", GeofencePlugin.ERROR_UNKNOWN);
}
callbackContext.error(errorObject);
} else if (error instanceof JSONObject) {
callbackContext.error((JSONObject) error);
} else {
JSONObject errorObject = new JSONObject();
errorObject.put("message", "Unknown error");
errorObject.put("code", GeofencePlugin.ERROR_UNKNOWN);
callbackContext.error(errorObject);
}
} catch (JSONException exception) {
callbackContext.error(exception.getMessage());
exception.printStackTrace();
}
else {
callbackContext.success();
}
}
}
15 changes: 3 additions & 12 deletions src/android/GeoNotificationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.android.gms.location.Geofence;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -77,25 +78,15 @@ public void addGeoNotifications(List<GeoNotification> geoNotifications,
newGeofences
);
if (callback != null) {
geoFenceCmd.addListener(new IGoogleServiceCommandListener() {
@Override
public void onCommandExecuted() {
callback.success();
}
});
geoFenceCmd.addListener(new CommandExecutionHandler(callback));
}
googleServiceCommandExecutor.QueueToExecute(geoFenceCmd);
}

public void removeGeoNotifications(List<String> ids, final CallbackContext callback) {
RemoveGeofenceCommand cmd = new RemoveGeofenceCommand(context, ids);
if (callback != null) {
cmd.addListener(new IGoogleServiceCommandListener() {
@Override
public void onCommandExecuted() {
callback.success();
}
});
cmd.addListener(new CommandExecutionHandler(callback));
}
for (String id : ids) {
geoNotificationStore.remove(id);
Expand Down
6 changes: 6 additions & 0 deletions src/android/GeofencePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

public class GeofencePlugin extends CordovaPlugin {
public static final String TAG = "GeofencePlugin";

public static final String ERROR_UNKNOWN = "UNKNOWN";
public static final String ERROR_PERMISSION_DENIED = "PERMISSION_DENIED";
public static final String ERROR_GEOFENCE_NOT_AVAILABLE = "GEOFENCE_NOT_AVAILABLE";
public static final String ERROR_GEOFENCE_LIMIT_EXCEEDED = "GEOFENCE_LIMIT_EXCEEDED";

private GeoNotificationManager geoNotificationManager;
private Context context;
public static CordovaWebView webView = null;
Expand Down
3 changes: 1 addition & 2 deletions src/android/GoogleServiceCommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ private void ExecuteNext() {
}

@Override
public void onCommandExecuted() {
// TODO Auto-generated method stub
public void onCommandExecuted(Object error) {
isExecuting = false;
ExecuteNext();
}
Expand Down
2 changes: 1 addition & 1 deletion src/android/IGoogleServiceCommandListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.cowbell.cordova.geofence;

public interface IGoogleServiceCommandListener {
void onCommandExecuted();
void onCommandExecuted(Object error);
}
33 changes: 6 additions & 27 deletions src/android/RemoveGeofenceCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cowbell.cordova.geofence;

import android.app.PendingIntent;
import android.content.Context;
import android.util.Log;

Expand All @@ -11,50 +10,30 @@
import java.util.List;

public class RemoveGeofenceCommand extends AbstractGoogleServiceCommand {
private PendingIntent pendingIntent;
private List<String> geofencesIds;

public RemoveGeofenceCommand(Context context, PendingIntent pendingIntent) {
super(context);
this.pendingIntent = pendingIntent;
}

public RemoveGeofenceCommand(Context context, List<String> geofencesIds) {
super(context);
this.geofencesIds = geofencesIds;
}

@Override
protected void ExecuteCustomCode() {
// TODO: refactor
if (pendingIntent != null) {
logger.log(Log.DEBUG, "Tried to remove Geofences in first if");
LocationServices.GeofencingApi
.removeGeofences(mGoogleApiClient, geofencesIds)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
logger.log(Log.DEBUG, "Geofences successfully removed");
} else {
logger.log(Log.DEBUG, "Removing geofences failed");
}
CommandExecuted();
}
});
} else if (geofencesIds != null && geofencesIds.size() > 0) {
logger.log(Log.DEBUG, "Tried to remove Geofences in 2nd if");
if (geofencesIds != null && geofencesIds.size() > 0) {
logger.log(Log.DEBUG, "Removing geofences...");
LocationServices.GeofencingApi
.removeGeofences(mGoogleApiClient, geofencesIds)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
logger.log(Log.DEBUG, "Geofences successfully removed");
CommandExecuted();
} else {
logger.log(Log.DEBUG, "Removing geofences failed");
String message = "Removing geofences failed - " + status.getStatusMessage();
logger.log(Log.ERROR, message);
CommandExecuted(new Error(message));
}
CommandExecuted();
}
});
} else {
Expand Down
Loading

0 comments on commit 82844c5

Please sign in to comment.