Skip to content

Fix deprecation warnings #1036

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

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.location.Priority;
import com.google.android.gms.location.SettingsClient;

import io.flutter.plugin.common.EventChannel.EventSink;
Expand Down Expand Up @@ -64,7 +65,7 @@ public class FlutterLocation
// Parameters of the request
private long updateIntervalMilliseconds = 5000;
private long fastestUpdateIntervalMilliseconds = updateIntervalMilliseconds / 2;
private Integer locationAccuracy = LocationRequest.PRIORITY_HIGH_ACCURACY;
private Integer locationAccuracy = Priority.PRIORITY_HIGH_ACCURACY;
private float distanceFilter = 0f;

public EventSink events;
Expand All @@ -82,12 +83,12 @@ public class FlutterLocation

public SparseArray<Integer> mapFlutterAccuracy = new SparseArray<Integer>() {
{
put(0, LocationRequest.PRIORITY_NO_POWER);
put(1, LocationRequest.PRIORITY_LOW_POWER);
put(2, LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
put(3, LocationRequest.PRIORITY_HIGH_ACCURACY);
put(4, LocationRequest.PRIORITY_HIGH_ACCURACY);
put(5, LocationRequest.PRIORITY_LOW_POWER);
put(0, Priority.PRIORITY_PASSIVE);
put(1, Priority.PRIORITY_LOW_POWER);
put(2, Priority.PRIORITY_BALANCED_POWER_ACCURACY);
put(3, Priority.PRIORITY_HIGH_ACCURACY);
put(4, Priority.PRIORITY_HIGH_ACCURACY);
put(5, Priority.PRIORITY_LOW_POWER);
}
};

Expand Down Expand Up @@ -215,6 +216,7 @@ private void sendError(String errorCode, String errorMessage, Object errorDetail
/**
* Creates a callback for receiving location events.
*/
@SuppressWarnings("deprecation")
private void createLocationCallback() {
if (mLocationCallback != null) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
Expand Down Expand Up @@ -243,7 +245,13 @@ public void onLocationResult(LocationResult locationResult) {
loc.put("satelliteNumber", location.getExtras().getInt("satellites"));
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
loc.put("elapsedRealtimeNanos", (double) location.getElapsedRealtimeNanos());

if (location.isMock()) {
loc.put("isMock", (double) 1);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
loc.put("elapsedRealtimeNanos", (double) location.getElapsedRealtimeNanos());

if (location.isFromMockProvider()) {
Expand Down Expand Up @@ -303,12 +311,15 @@ public void onLocationResult(LocationResult locationResult) {
* Sets up the location request. Android has two location request settings:
*/
private void createLocationRequest() {
mLocationRequest = LocationRequest.create();
final int priority = (this.locationAccuracy != null)
? this.locationAccuracy
: Priority.PRIORITY_HIGH_ACCURACY;

LocationRequest.Builder builder = new LocationRequest.Builder(priority, this.updateIntervalMilliseconds)
.setMinUpdateIntervalMillis(this.fastestUpdateIntervalMilliseconds)
.setMinUpdateDistanceMeters(this.distanceFilter);

mLocationRequest.setInterval(this.updateIntervalMilliseconds);
mLocationRequest.setFastestInterval(this.fastestUpdateIntervalMilliseconds);
mLocationRequest.setPriority(this.locationAccuracy);
mLocationRequest.setSmallestDisplacement(this.distanceFilter);
mLocationRequest = builder.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,14 @@ void stopListening() {
private void onChangeSettings(MethodCall call, Result result) {
try {
final Integer locationAccuracy = location.mapFlutterAccuracy.get((Integer) call.argument("accuracy"));
final Long updateIntervalMilliseconds = new Long((int) call.argument("interval"));
final Long fastestUpdateIntervalMilliseconds = updateIntervalMilliseconds / 2;
final Float distanceFilter = new Float((double) call.argument("distanceFilter"));

final Number interval = (Number) call.argument("interval");
final Long updateIntervalMilliseconds = Long.valueOf(interval != null ? interval.longValue() : 0L);

final Long fastestUpdateIntervalMilliseconds = Long.valueOf(updateIntervalMilliseconds / 2);

final Number distance = (Number) call.argument("distanceFilter");
final Float distanceFilter = Float.valueOf(distance != null ? distance.floatValue() : 0f);

location.changeSettings(locationAccuracy, updateIntervalMilliseconds, fastestUpdateIntervalMilliseconds,
distanceFilter);
Expand Down