Java Code Examples for android.location.Location#isFromMockProvider()

The following examples show how to use android.location.Location#isFromMockProvider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocationManagerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void handleLocationChanged(Location location, boolean passive) {
    // create a working copy of the incoming Location so that the service can modify it without
    // disturbing the caller's copy
    Location myLocation = new Location(location);
    String provider = myLocation.getProvider();

    // set "isFromMockProvider" bit if location came from a mock provider. we do not clear this
    // bit if location did not come from a mock provider because passive/fused providers can
    // forward locations from mock providers, and should not grant them legitimacy in doing so.
    if (!myLocation.isFromMockProvider() && isMockProvider(provider)) {
        myLocation.setIsFromMockProvider(true);
    }

    synchronized (mLock) {
        if (isAllowedByCurrentUserSettingsLocked(provider)) {
            if (!passive) {
                // notify passive provider of the new location
                mPassiveProvider.updateLocation(myLocation);
            }
            handleLocationChangedLocked(myLocation, passive);
        }
    }
}
 
Example 2
Source File: FusedLocationModule.java    From react-native-fused-location with MIT License 6 votes vote down vote up
private WritableMap convertLocationToJSON(Location l) {
    WritableMap params = new WritableNativeMap();
    params.putDouble("latitude", l.getLatitude());
    params.putDouble("longitude", l.getLongitude());
    params.putDouble("accuracy", l.getAccuracy());
    params.putDouble("altitude", l.getAltitude());
    params.putDouble("bearing", l.getBearing());
    params.putString("provider", l.getProvider());
    params.putDouble("speed", l.getSpeed());
    params.putString("timestamp", Long.toString(l.getTime()));
    boolean isMock;
    if (android.os.Build.VERSION.SDK_INT >= 18) {
        isMock = l.isFromMockProvider();
    } else {
        isMock = !Settings.Secure.getString(getReactApplicationContext().getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");
    }
    params.putBoolean("mocked", isMock);
    return params;
}
 
Example 3
Source File: SensorNotificationService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void onLocationChanged(Location location) {
    if (DBG) Slog.d(TAG, String.format(
            "Location is (%f, %f), h %f, acc %f, mocked %b",
            location.getLatitude(), location.getLongitude(),
            location.getAltitude(), location.getAccuracy(),
            location.isFromMockProvider()));

    // lat long == 0 usually means invalid location
    if (location.getLatitude() == 0 && location.getLongitude() == 0) {
        return;
    }

    // update too often, ignore
    if (SystemClock.elapsedRealtime() - mLocalGeomagneticFieldUpdateTime < 10 * MINUTE_IN_MS) {
        return;
    }

    long time = System.currentTimeMillis();
    // Mocked location should not be used. Except in test, only use mocked location
    // Wrong system clock also gives bad values so ignore as well.
    if (useMockedLocation() == location.isFromMockProvider() || time < MILLIS_2010_1_1) {
        return;
    }

    GeomagneticField field = new GeomagneticField(
            (float) location.getLatitude(), (float) location.getLongitude(),
            (float) location.getAltitude(), time);
    if (DBG) Slog.d(TAG, String.format(
            "Nominal mag field, norm %fuT, decline %f deg, incline %f deg",
            field.getFieldStrength() / 1000, field.getDeclination(), field.getInclination()));

    try {
        SensorAdditionalInfo info = SensorAdditionalInfo.createLocalGeomagneticField(
                    field.getFieldStrength() / 1000, // convert from nT to uT
                    (float)(field.getDeclination() * Math.PI / 180), // from degree to rad
                    (float)(field.getInclination() * Math.PI / 180)); // from degree to rad
        if (info != null) {
            mSensorManager.setOperationParameter(info);
            mLocalGeomagneticFieldUpdateTime = SystemClock.elapsedRealtime();
        }
    } catch (IllegalArgumentException e) {
        Slog.e(TAG, "Invalid local geomagnetic field, ignore.");
    }
}