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

add test to know if location is mocked #18

Open
wants to merge 6 commits 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: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ xmlns:android="http://schemas.android.com/apk/res/android"

<!-- android -->
<platform name="android">
<framework src="com.google.android.gms:play-services-location:+" />
<preference name="GMS_VERSION" default="11.0.1"/>
<framework src="com.google.android.gms:play-services-location:$GMS_VERSION" />

<js-module src="www/Coordinates.js" name="Coordinates">
<clobbers target="cordova.plugins.locationServices.Coordinates" />
Expand Down
10 changes: 8 additions & 2 deletions src/android/CordovaLocationServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
Expand Down Expand Up @@ -101,8 +102,6 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int

mCbContext.sendPluginResult(result);
}

mCbContext = null;
}

@Override
Expand Down Expand Up @@ -230,6 +229,13 @@ public JSONObject returnLocationJSON(Location loc) {
: null) : null));
o.put("velocity", loc.getSpeed());
o.put("timestamp", loc.getTime());
boolean mocked = false;
if (Build.VERSION.SDK_INT >= 18) {
mocked = loc.isFromMockProvider();
} else {
mocked = !Settings.Secure.getString(cordova.getActivity().getApplicationContext().getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");
}
o.put("mocked", mocked);
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down
8 changes: 7 additions & 1 deletion www/Coordinates.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
* @param {Object} head
* @param {Object} vel
* @param {Object} altacc
* @param {Object} mocked
* @constructor
*/
var Coordinates = function(lat, lng, alt, acc, head, vel, altacc) {
var Coordinates = function(lat, lng, alt, acc, head, vel, altacc, mocked) {
/**
* The latitude of the position.
*/
Expand Down Expand Up @@ -64,6 +65,11 @@ var Coordinates = function(lat, lng, alt, acc, head, vel, altacc) {
* The altitude accuracy of the position.
*/
this.altitudeAccuracy = (altacc !== undefined) ? altacc : null;

/**
* If the position was mocked
*/
this.mocked = (mocked !== undefined) ? mocked : null;
};

module.exports = Coordinates;
6 changes: 4 additions & 2 deletions www/LocationServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ var LocationServicesWithoutPermission = {
accuracy: p.accuracy,
heading: p.heading,
velocity: p.velocity,
altitudeAccuracy: p.altitudeAccuracy
altitudeAccuracy: p.altitudeAccuracy,
mocked: p.mocked
}, p.timestamp);
LocationServicesWithoutPermission.lastPosition = pos;
successCallback(pos);
Expand Down Expand Up @@ -208,7 +209,8 @@ var LocationServicesWithoutPermission = {
accuracy: p.accuracy,
heading: p.heading,
velocity: p.velocity,
altitudeAccuracy: p.altitudeAccuracy
altitudeAccuracy: p.altitudeAccuracy,
mocked: p.mocked
}, p.timestamp);
LocationServicesWithoutPermission.lastPosition = pos;
successCallback(pos);
Expand Down
2 changes: 1 addition & 1 deletion www/Position.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Coordinates = require('./Coordinates');

var Position = function(coords, timestamp) {
if (coords) {
this.coords = new Coordinates(coords.latitude, coords.longitude, coords.altitude, coords.accuracy, coords.heading, coords.velocity, coords.altitudeAccuracy);
this.coords = new Coordinates(coords.latitude, coords.longitude, coords.altitude, coords.accuracy, coords.heading, coords.velocity, coords.altitudeAccuracy, coords.mocked);
} else {
this.coords = new Coordinates();
}
Expand Down