Allow to choose foreground location servcie manualy

This commit is contained in:
estevez-dev
2020-07-20 15:21:31 +03:00
parent d2d037e468
commit f87cff7a7e
9 changed files with 64 additions and 48 deletions

View File

@ -103,13 +103,16 @@ public class LocationUpdatesService extends Service {
private void requestLocationUpdates() {
long requestInterval = LocationUtils.getLocationUpdateIntervals(getApplicationContext());
int priority = LocationUtils.getLocationUpdatesPriority(getApplicationContext());
Log.i(TAG, "Requesting location updates. Every " + requestInterval + "ms with priority of " + priority);
int priority;
if (requestInterval >= 600000) {
mLocationRequest.setFastestInterval(60000);
priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
} else {
priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
}
mLocationRequest.setPriority(priority);
mLocationRequest.setInterval(requestInterval);
/*if (priority == 102 && requestInterval > 60000) {
mLocationRequest.setFastestInterval(30000);
}*/
Log.i(TAG, "Requesting location updates. Every " + requestInterval + "ms with priority of " + priority);
startForeground(LocationUtils.SERVICE_NOTIFICATION_ID, LocationUtils.getNotification(this, null, LocationUtils.SERVICE_NOTIFICATION_CHANNEL_ID));
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,

View File

@ -98,12 +98,8 @@ public class LocationUpdatesWorker extends ListenableWorker {
};
LocationRequest locationRequest = new LocationRequest();
int accuracy = LocationUtils.getLocationUpdatesPriority(getApplicationContext());
locationRequest.setPriority(accuracy);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
/*if (accuracy == 102) {
locationRequest.setFastestInterval(1000);
}*/
try {
fusedLocationClient.requestLocationUpdates(locationRequest,
callback, Looper.myLooper());

View File

@ -22,7 +22,6 @@ class LocationUtils {
static final String KEY_REQUESTING_LOCATION_UPDATES = "flutter.location-updates-state";
static final String KEY_LOCATION_UPDATE_INTERVAL = "flutter.location-updates-interval";
static final String KEY_LOCATION_UPDATE_PRIORITY = "flutter.location-updates-priority";
static final String KEY_LOCATION_SHOW_NOTIFICATION = "flutter.location-updates-show-notification";
static final String WORKER_NOTIFICATION_CHANNEL_ID = "location_worker";
@ -50,10 +49,6 @@ class LocationUtils {
return context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).getLong(KEY_LOCATION_UPDATE_INTERVAL, DEFAULT_LOCATION_UPDATE_INTERVAL_MS);
}
static int getLocationUpdatesPriority(Context context) {
return context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).getInt(KEY_LOCATION_UPDATE_PRIORITY, 100);
}
static boolean showNotification(Context context) {
return context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).getBoolean(KEY_LOCATION_SHOW_NOTIFICATION, true);
}
@ -65,10 +60,9 @@ class LocationUtils {
.apply();
}
static void setLocationUpdatesSettings(Context context, int priority, long interval, boolean showNotification) {
static void setLocationUpdatesSettings(Context context, long interval, boolean showNotification) {
context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE)
.edit()
.putInt(KEY_LOCATION_UPDATE_PRIORITY, priority)
.putBoolean(KEY_LOCATION_SHOW_NOTIFICATION, showNotification)
.putLong(KEY_LOCATION_UPDATE_INTERVAL, interval)
.apply();

View File

@ -14,6 +14,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import io.flutter.plugin.common.MethodChannel;
@ -67,12 +68,14 @@ public class MainActivity extends FlutterActivity {
case "startLocationService":
try {
locationUpdatesInterval = ((Number)call.argument("location-updates-interval")).longValue();
if (locationUpdatesInterval >= LocationUtils.MIN_WORKER_LOCATION_UPDATE_INTERVAL_MS) {
locationUpdatesType = LocationUtils.LOCATION_UPDATES_WORKER;
} else {
boolean useForegroundService = (boolean)call.argument("foreground-location-tracking");
if (useForegroundService) {
locationUpdatesType = LocationUtils.LOCATION_UPDATES_SERVICE;
} else {
locationUpdatesType = LocationUtils.LOCATION_UPDATES_WORKER;
}
LocationUtils.setLocationUpdatesSettings(this, (int)call.argument("location-updates-priority"), locationUpdatesInterval, (boolean)call.argument("location-updates-show-notification"));
LocationUtils.setLocationUpdatesSettings(this, locationUpdatesInterval, (boolean)call.argument("location-updates-show-notification"));
if (isNoLocationPermissions()) {
requestLocationPermissions();
} else {