diff --git a/app/src/google/java/org/traccar/client/GooglePositionProvider.java b/app/src/google/java/org/traccar/client/GooglePositionProvider.java new file mode 100644 index 0000000..cb1e20b --- /dev/null +++ b/app/src/google/java/org/traccar/client/GooglePositionProvider.java @@ -0,0 +1,84 @@ +/* + * Copyright 2019 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.client; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.location.Location; + +import com.google.android.gms.location.FusedLocationProviderClient; +import com.google.android.gms.location.LocationCallback; +import com.google.android.gms.location.LocationRequest; +import com.google.android.gms.location.LocationResult; +import com.google.android.gms.location.LocationServices; +import com.google.android.gms.tasks.OnSuccessListener; + +public class GooglePositionProvider extends PositionProvider { + + private FusedLocationProviderClient fusedLocationClient; + + public GooglePositionProvider(Context context, PositionListener listener) { + super(context, listener); + fusedLocationClient = LocationServices.getFusedLocationProviderClient(context); + } + + @SuppressLint("MissingPermission") + public void startUpdates() { + LocationRequest locationRequest = new LocationRequest(); + locationRequest.setPriority(getPriority(preferences.getString(MainFragment.KEY_ACCURACY, "medium"))); + locationRequest.setInterval(distance > 0 || angle > 0 ? MINIMUM_INTERVAL : interval); + fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null); + } + + public void stopUpdates() { + fusedLocationClient.removeLocationUpdates(locationCallback); + } + + @SuppressLint("MissingPermission") + public void requestSingleLocation() { + fusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Location location) { + if (location != null) { + listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel(context))); + } + } + }); + } + + private static int getPriority(String accuracy) { + switch (accuracy) { + case "high": + return LocationRequest.PRIORITY_HIGH_ACCURACY; + case "low": + return LocationRequest.PRIORITY_LOW_POWER; + default: + return LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY; + } + } + + private LocationCallback locationCallback = new LocationCallback() { + @Override + public void onLocationResult(LocationResult locationResult) { + if (locationResult != null) { + for (Location location : locationResult.getLocations()) { + processLocation(location); + } + } + } + }; + +} diff --git a/app/src/google/java/org/traccar/client/PositionProviderFactory.java b/app/src/google/java/org/traccar/client/PositionProviderFactory.java index e39c2f4..e221a16 100644 --- a/app/src/google/java/org/traccar/client/PositionProviderFactory.java +++ b/app/src/google/java/org/traccar/client/PositionProviderFactory.java @@ -20,7 +20,7 @@ import android.content.Context; public class PositionProviderFactory { public static PositionProvider create(Context context, PositionProvider.PositionListener listener) { - return new AndroidPositionProvider(context, listener); + return new GooglePositionProvider(context, listener); } } diff --git a/app/src/main/java/org/traccar/client/AndroidPositionProvider.java b/app/src/main/java/org/traccar/client/AndroidPositionProvider.java index 86d1556..842a2e8 100644 --- a/app/src/main/java/org/traccar/client/AndroidPositionProvider.java +++ b/app/src/main/java/org/traccar/client/AndroidPositionProvider.java @@ -1,3 +1,18 @@ +/* + * Copyright 2019 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.client; import android.annotation.SuppressLint; @@ -7,7 +22,6 @@ import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Looper; -import android.widget.Toast; public class AndroidPositionProvider extends PositionProvider implements LocationListener { @@ -26,7 +40,7 @@ public class AndroidPositionProvider extends PositionProvider implements Locatio locationManager.requestLocationUpdates( provider, distance > 0 || angle > 0 ? MINIMUM_INTERVAL : interval, 0, this); } catch (RuntimeException e) { - Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show(); + listener.onPositionError(e); } }