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 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) {
@ -60,6 +60,15 @@ class LocationUtils {
.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) {
return location == null ? "Accuracy: unknown" :
"Accuracy: " + location.getAccuracy();

View File

@ -39,35 +39,40 @@ public class MainActivity extends FlutterActivity {
Context context = getActivity();
switch (call.method) {
case "getFCMToken":
if (checkPlayServices()) {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
String token = task.getResult().getToken();
context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).edit().putString("flutter.npush-token", token).apply();
result.success(token);
} else {
Exception ex = task.getException();
if (ex != null) {
result.error("fcm_error", ex.getMessage(), null);
try {
if (checkPlayServices()) {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
String token = task.getResult().getToken();
context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).edit().putString("flutter.npush-token", token).apply();
result.success(token);
} else {
result.error("fcm_error", "Unknown", null);
}
Exception ex = task.getException();
if (ex != null) {
result.error("fcm_error", ex.getMessage(), null);
} else {
result.error("fcm_error", "Unknown", null);
}
}
});
} else {
result.error("google_play_service_error", "Google Play Services unavailable", null);
}
});
} else {
result.error("google_play_service_error", "Google Play Services unavailable", null);
}
} catch (Exception e) {
result.error("get_token_exception", e.getMessage(), e);
}
break;
case "startLocationService":
try {
locationUpdatesInterval = LocationUtils.getLocationUpdateIntervals(this);
locationUpdatesInterval = (long) call.argument("location-updates-interval") * 1000;
if (locationUpdatesInterval >= LocationUtils.MIN_WORKER_LOCATION_UPDATE_INTERVAL_MS) {
locationUpdatesType = LocationUtils.LOCATION_UPDATES_WORKER;
} else {
locationUpdatesType = LocationUtils.LOCATION_UPDATES_SERVICE;
}
LocationUtils.setLocationUpdatesSettings(this, (int)call.argument("location-updates-priority"), locationUpdatesInterval, (boolean)call.argument("location-updates-show-notification"));
if (isNoLocationPermissions()) {
requestLocationPermissions();
} else {
@ -75,12 +80,16 @@ public class MainActivity extends FlutterActivity {
}
result.success("");
} catch (Exception e) {
result.error("location_error", e.getMessage(), null);
result.error("location_error", e.getMessage(), e);
}
break;
case "stopLocationService":
stopLocationUpdates();
result.success("");
try {
stopLocationUpdates();
result.success("");
} catch (Exception e) {
result.error("location_error", e.getMessage(), e);
}
break;
case "cancelOldLocationWorker":
WorkManager.getInstance(this).cancelAllWorkByTag("haclocation");