Resolves #575 Location settings
This commit is contained in:
		| @@ -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(); | ||||||
|   | |||||||
| @@ -39,35 +39,40 @@ public class MainActivity extends FlutterActivity { | |||||||
|                     Context context = getActivity(); |                     Context context = getActivity(); | ||||||
|                     switch (call.method) { |                     switch (call.method) { | ||||||
|                         case "getFCMToken": |                         case "getFCMToken": | ||||||
|                             if (checkPlayServices()) { |                             try { | ||||||
|                                 FirebaseInstanceId.getInstance().getInstanceId() |                                 if (checkPlayServices()) { | ||||||
|                                         .addOnCompleteListener(task -> { |                                     FirebaseInstanceId.getInstance().getInstanceId() | ||||||
|                                             if (task.isSuccessful()) { |                                             .addOnCompleteListener(task -> { | ||||||
|                                                 String token = task.getResult().getToken(); |                                                 if (task.isSuccessful()) { | ||||||
|                                                 context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).edit().putString("flutter.npush-token", token).apply(); |                                                     String token = task.getResult().getToken(); | ||||||
|                                                 result.success(token); |                                                     context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).edit().putString("flutter.npush-token", token).apply(); | ||||||
|                                             } else { |                                                     result.success(token); | ||||||
|                                                 Exception ex = task.getException(); |  | ||||||
|                                                 if (ex != null) { |  | ||||||
|                                                     result.error("fcm_error", ex.getMessage(), null); |  | ||||||
|                                                 } else { |                                                 } 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 { |                                 } 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": | ||||||
|                             stopLocationUpdates(); |                             try { | ||||||
|                             result.success(""); |                                 stopLocationUpdates(); | ||||||
|  |                                 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"); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user