Resolves #575 Location settings

This commit is contained in:
estevez-dev 2020-07-08 11:12:30 +03:00
parent e5fe853f0b
commit 2fb296b7a8
2 changed files with 40 additions and 22 deletions

View File

@ -38,7 +38,7 @@ class LocationUtils {
static final long MIN_WORKER_LOCATION_UPDATE_INTERVAL_MS = 900000; //15 minutes static final long MIN_WORKER_LOCATION_UPDATE_INTERVAL_MS = 900000; //15 minutes
static int getLocationUpdatesState(Context context) { static int getLocationUpdatesState(Context context) {
return (int) context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).getInt(KEY_REQUESTING_LOCATION_UPDATES, LOCATION_UPDATES_DISABLED); return (int) context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).getLong(KEY_REQUESTING_LOCATION_UPDATES, LOCATION_UPDATES_DISABLED);
} }
static long getLocationUpdateIntervals(Context context) { static long getLocationUpdateIntervals(Context context) {
@ -60,6 +60,15 @@ class LocationUtils {
.apply(); .apply();
} }
static void setLocationUpdatesSettings(Context context, int priority, 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/1000)
.apply();
}
static String getLocationText(Location location) { static String getLocationText(Location location) {
return location == null ? "Accuracy: unknown" : return location == null ? "Accuracy: unknown" :
"Accuracy: " + location.getAccuracy(); "Accuracy: " + location.getAccuracy();

View File

@ -39,6 +39,7 @@ public class MainActivity extends FlutterActivity {
Context context = getActivity(); Context context = getActivity();
switch (call.method) { switch (call.method) {
case "getFCMToken": case "getFCMToken":
try {
if (checkPlayServices()) { if (checkPlayServices()) {
FirebaseInstanceId.getInstance().getInstanceId() FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(task -> { .addOnCompleteListener(task -> {
@ -59,15 +60,19 @@ public class MainActivity extends FlutterActivity {
} else { } else {
result.error("google_play_service_error", "Google Play Services unavailable", null); result.error("google_play_service_error", "Google Play Services unavailable", null);
} }
} catch (Exception e) {
result.error("get_token_exception", e.getMessage(), e);
}
break; break;
case "startLocationService": case "startLocationService":
try { try {
locationUpdatesInterval = LocationUtils.getLocationUpdateIntervals(this); locationUpdatesInterval = (long) call.argument("location-updates-interval") * 1000;
if (locationUpdatesInterval >= LocationUtils.MIN_WORKER_LOCATION_UPDATE_INTERVAL_MS) { if (locationUpdatesInterval >= LocationUtils.MIN_WORKER_LOCATION_UPDATE_INTERVAL_MS) {
locationUpdatesType = LocationUtils.LOCATION_UPDATES_WORKER; locationUpdatesType = LocationUtils.LOCATION_UPDATES_WORKER;
} else { } else {
locationUpdatesType = LocationUtils.LOCATION_UPDATES_SERVICE; locationUpdatesType = LocationUtils.LOCATION_UPDATES_SERVICE;
} }
LocationUtils.setLocationUpdatesSettings(this, (int)call.argument("location-updates-priority"), locationUpdatesInterval, (boolean)call.argument("location-updates-show-notification"));
if (isNoLocationPermissions()) { if (isNoLocationPermissions()) {
requestLocationPermissions(); requestLocationPermissions();
} else { } else {
@ -75,12 +80,16 @@ public class MainActivity extends FlutterActivity {
} }
result.success(""); result.success("");
} catch (Exception e) { } catch (Exception e) {
result.error("location_error", e.getMessage(), null); result.error("location_error", e.getMessage(), e);
} }
break; break;
case "stopLocationService": case "stopLocationService":
try {
stopLocationUpdates(); stopLocationUpdates();
result.success(""); result.success("");
} catch (Exception e) {
result.error("location_error", e.getMessage(), e);
}
break; break;
case "cancelOldLocationWorker": case "cancelOldLocationWorker":
WorkManager.getInstance(this).cancelAllWorkByTag("haclocation"); WorkManager.getInstance(this).cancelAllWorkByTag("haclocation");