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() { private void requestLocationUpdates() {
long requestInterval = LocationUtils.getLocationUpdateIntervals(getApplicationContext()); long requestInterval = LocationUtils.getLocationUpdateIntervals(getApplicationContext());
int priority = LocationUtils.getLocationUpdatesPriority(getApplicationContext()); int priority;
Log.i(TAG, "Requesting location updates. Every " + requestInterval + "ms with priority of " + priority); if (requestInterval >= 600000) {
mLocationRequest.setFastestInterval(60000);
priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
} else {
priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
}
mLocationRequest.setPriority(priority); mLocationRequest.setPriority(priority);
mLocationRequest.setInterval(requestInterval); mLocationRequest.setInterval(requestInterval);
/*if (priority == 102 && requestInterval > 60000) { Log.i(TAG, "Requesting location updates. Every " + requestInterval + "ms with priority of " + priority);
mLocationRequest.setFastestInterval(30000);
}*/
startForeground(LocationUtils.SERVICE_NOTIFICATION_ID, LocationUtils.getNotification(this, null, LocationUtils.SERVICE_NOTIFICATION_CHANNEL_ID)); startForeground(LocationUtils.SERVICE_NOTIFICATION_ID, LocationUtils.getNotification(this, null, LocationUtils.SERVICE_NOTIFICATION_CHANNEL_ID));
try { try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mFusedLocationClient.requestLocationUpdates(mLocationRequest,

View File

@ -98,12 +98,8 @@ public class LocationUpdatesWorker extends ListenableWorker {
}; };
LocationRequest locationRequest = new LocationRequest(); LocationRequest locationRequest = new LocationRequest();
int accuracy = LocationUtils.getLocationUpdatesPriority(getApplicationContext()); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setPriority(accuracy);
locationRequest.setInterval(5000); locationRequest.setInterval(5000);
/*if (accuracy == 102) {
locationRequest.setFastestInterval(1000);
}*/
try { try {
fusedLocationClient.requestLocationUpdates(locationRequest, fusedLocationClient.requestLocationUpdates(locationRequest,
callback, Looper.myLooper()); 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_REQUESTING_LOCATION_UPDATES = "flutter.location-updates-state";
static final String KEY_LOCATION_UPDATE_INTERVAL = "flutter.location-updates-interval"; 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 KEY_LOCATION_SHOW_NOTIFICATION = "flutter.location-updates-show-notification";
static final String WORKER_NOTIFICATION_CHANNEL_ID = "location_worker"; 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); 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) { static boolean showNotification(Context context) {
return context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).getBoolean(KEY_LOCATION_SHOW_NOTIFICATION, true); return context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE).getBoolean(KEY_LOCATION_SHOW_NOTIFICATION, true);
} }
@ -65,10 +60,9 @@ class LocationUtils {
.apply(); .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) context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE)
.edit() .edit()
.putInt(KEY_LOCATION_UPDATE_PRIORITY, priority)
.putBoolean(KEY_LOCATION_SHOW_NOTIFICATION, showNotification) .putBoolean(KEY_LOCATION_SHOW_NOTIFICATION, showNotification)
.putLong(KEY_LOCATION_UPDATE_INTERVAL, interval) .putLong(KEY_LOCATION_UPDATE_INTERVAL, interval)
.apply(); .apply();

View File

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

View File

@ -278,6 +278,7 @@ class HomeAssistant {
_rawPanels = data; _rawPanels = data;
List<Panel> dashboards = []; List<Panel> dashboards = [];
data.forEach((k,v) { data.forEach((k,v) {
Logger.d('[HA] Panel $k: title=${v['title']}; component=${v['component_name']}');
String title = v['title'] == null ? "${k[0].toUpperCase()}${k.substring(1)}" : "${v['title'][0].toUpperCase()}${v['title'].substring(1)}"; String title = v['title'] == null ? "${k[0].toUpperCase()}${k.substring(1)}" : "${v['title'][0].toUpperCase()}${v['title'].substring(1)}";
if (v['component_name'] != null && v['component_name'] == 'lovelace') { if (v['component_name'] != null && v['component_name'] == 'lovelace') {
dashboards.add( dashboards.add(

View File

@ -81,8 +81,9 @@ class AppSettings {
try { try {
await platform.invokeMethod('startLocationService', <String, dynamic>{ await platform.invokeMethod('startLocationService', <String, dynamic>{
'location-updates-interval': oldLocationTrackingInterval * 60 * 1000, 'location-updates-interval': oldLocationTrackingInterval * 60 * 1000,
'location-updates-priority': 100, //'location-updates-priority': 100,
'location-updates-show-notification': true 'location-updates-show-notification': true,
'foreground-location-tracking': false
}); });
} catch (e, stack) { } catch (e, stack) {
Logger.e("[MIGRATION] Can't start new location tracking: $e", stacktrace: stack); Logger.e("[MIGRATION] Can't start new location tracking: $e", stacktrace: stack);

View File

@ -21,7 +21,8 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
bool _locationTrackingEnabled = false; bool _locationTrackingEnabled = false;
bool _wait = false; bool _wait = false;
bool _showNotification = true; bool _showNotification = true;
int _accuracy = 100; //int _accuracy = 100;
bool _useForegroundService = false;
@override @override
void initState() { void initState() {
@ -35,9 +36,10 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
await prefs.reload(); await prefs.reload();
SharedPreferences.getInstance().then((prefs) { SharedPreferences.getInstance().then((prefs) {
setState(() { setState(() {
_accuracy = /*prefs.getInt("location-updates-priority") ??*/ 100; //_accuracy = prefs.getInt("location-updates-priority") ?? 100;
_locationTrackingEnabled = (prefs.getInt("location-updates-state") ?? 0) > 0; _locationTrackingEnabled = (prefs.getInt("location-updates-state") ?? 0) > 0;
_showNotification = prefs.getBool("location-updates-show-notification") ?? true; _showNotification = prefs.getBool("location-updates-show-notification") ?? true;
_useForegroundService = prefs.getBool("foreground-location-tracking") ?? false;
_locationInterval = Duration(milliseconds: prefs.getInt("location-updates-interval") ?? _locationInterval = Duration(milliseconds: prefs.getInt("location-updates-interval") ??
900000); 900000);
}); });
@ -69,28 +71,20 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
} }
void _decLocationInterval() { void _decLocationInterval() {
if (_locationInterval.inSeconds > 5) { if ((_useForegroundService && _locationInterval.inSeconds > 5) || (!_useForegroundService && _locationInterval.inMinutes > 15)) {
if (_locationInterval.inSeconds <= 60) { setState(() {
setState(() { if (_locationInterval.inSeconds <= 60) {
_locationInterval = _locationInterval - Duration(seconds: 5); _locationInterval = _locationInterval - Duration(seconds: 5);
}); } else if (_locationInterval.inMinutes <= 15) {
} else if (_locationInterval.inMinutes <= 15) {
setState(() {
_locationInterval = _locationInterval - Duration(minutes: 1); _locationInterval = _locationInterval - Duration(minutes: 1);
}); } else if (_locationInterval.inMinutes <= 60) {
} else if (_locationInterval.inMinutes <= 60) {
setState(() {
_locationInterval = _locationInterval - Duration(minutes: 5); _locationInterval = _locationInterval - Duration(minutes: 5);
}); } else if (_locationInterval.inHours <= 4) {
} else if (_locationInterval.inHours <= 4) {
setState(() {
_locationInterval = _locationInterval - Duration(minutes: 10); _locationInterval = _locationInterval - Duration(minutes: 10);
}); } else if (_locationInterval.inHours > 4) {
} else if (_locationInterval.inHours > 4) {
setState(() {
_locationInterval = _locationInterval - Duration(hours: 1); _locationInterval = _locationInterval - Duration(hours: 1);
}); }
} });
} }
} }
@ -99,7 +93,8 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
try { try {
await platform.invokeMethod('startLocationService', <String, dynamic>{ await platform.invokeMethod('startLocationService', <String, dynamic>{
'location-updates-interval': _locationInterval.inMilliseconds, 'location-updates-interval': _locationInterval.inMilliseconds,
'location-updates-priority': _accuracy, //'location-updates-priority': _accuracy,
'foreground-location-tracking': _useForegroundService,
'location-updates-show-notification': _showNotification 'location-updates-show-notification': _showNotification
}); });
} catch (e) { } catch (e) {
@ -144,8 +139,12 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
if (_locationTrackingEnabled) { if (_locationTrackingEnabled) {
notes.add(_getNoteWidget('* Stop location tracking to change settings', false)); notes.add(_getNoteWidget('* Stop location tracking to change settings', false));
} }
if ((_locationInterval?.inMinutes ?? 15) < 15) { if (_useForegroundService) {
notes.add(_getNoteWidget('* Notification is mandatory for location updates with interval less than every 15 minutes', false)); notes.add(_getNoteWidget('* Notification is mandatory for foreground service', false));
} else {
notes.add(_getNoteWidget('* Use foreground service for intervals less then 15 minutes', false));
}
if (_useForegroundService && _locationInterval.inMinutes < 10) {
notes.add(_getNoteWidget('* Battery consumption will be noticeable', true)); notes.add(_getNoteWidget('* Battery consumption will be noticeable', true));
} }
if (notes.isEmpty) { if (notes.isEmpty) {
@ -182,6 +181,25 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
], ],
), ),
Container(height: Sizes.rowPadding), Container(height: Sizes.rowPadding),
Row(
children: <Widget>[
Text("Use foreground service"),
Switch(
value: _useForegroundService,
onChanged: _locationTrackingEnabled ? null : (value) {
setState(() {
_useForegroundService = value;
if (!_useForegroundService && _locationInterval.inMinutes < 15) {
_locationInterval = Duration(minutes: 15);
} else if (_useForegroundService) {
_showNotification = true;
}
});
},
),
],
),
Container(height: Sizes.rowPadding),
/*Text("Accuracy:", style: Theme.of(context).textTheme.body2), /*Text("Accuracy:", style: Theme.of(context).textTheme.body2),
Container(height: Sizes.rowPadding), Container(height: Sizes.rowPadding),
DropdownButton<int>( DropdownButton<int>(
@ -231,7 +249,7 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
Text("Show notification"), Text("Show notification"),
Switch( Switch(
value: _showNotification, value: _showNotification,
onChanged: (_locationTrackingEnabled || (_locationInterval?.inMinutes ?? 0) < 15) ? null : (value) { onChanged: (_locationTrackingEnabled || _useForegroundService) ? null : (value) {
setState(() { setState(() {
_showNotification = value; _showNotification = value;
}); });

View File

@ -39,7 +39,7 @@ class Panel {
eventBus.fire(ReloadUIEvent()); eventBus.fire(ReloadUIEvent());
}); });
} else { } else {
Launcher.launchAuthenticatedWebView(context: context, url: "${AppSettings().httpWebHost}/$urlPath", title: "${this.title}"); Launcher.launchAuthenticatedWebView(context: context, url: "${AppSettings().httpWebHost}/$urlPath", title: "Back to app");
} }
} }

View File

@ -1,7 +1,7 @@
name: hass_client name: hass_client
description: Home Assistant Android Client description: Home Assistant Android Client
version: 1.3.0+1306 version: 1.3.0+1307
environment: environment: