From 2fb296b7a8e1f3abd5d61e6598a51e6dc0b46db6 Mon Sep 17 00:00:00 2001 From: estevez-dev Date: Wed, 8 Jul 2020 11:12:30 +0300 Subject: [PATCH] Resolves #575 Location settings --- .../hassclient/LocationUtils.java | 11 +++- .../hassclient/MainActivity.java | 51 +++++++++++-------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/android/app/src/main/java/com/keyboardcrumbs/hassclient/LocationUtils.java b/android/app/src/main/java/com/keyboardcrumbs/hassclient/LocationUtils.java index 41508b9..59cbb8a 100644 --- a/android/app/src/main/java/com/keyboardcrumbs/hassclient/LocationUtils.java +++ b/android/app/src/main/java/com/keyboardcrumbs/hassclient/LocationUtils.java @@ -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(); diff --git a/android/app/src/main/java/com/keyboardcrumbs/hassclient/MainActivity.java b/android/app/src/main/java/com/keyboardcrumbs/hassclient/MainActivity.java index a598237..a631177 100644 --- a/android/app/src/main/java/com/keyboardcrumbs/hassclient/MainActivity.java +++ b/android/app/src/main/java/com/keyboardcrumbs/hassclient/MainActivity.java @@ -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");