diff --git a/android/app/build.gradle b/android/app/build.gradle index 90ccdbe..20d4935 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -50,11 +50,13 @@ android { } signingConfigs { - debug { - keyAlias keystoreProperties['debugKeyAlias'] - keyPassword keystoreProperties['debugKeyPassword'] - storeFile file(keystoreProperties['debugStoreFile']) - storePassword keystoreProperties['debugStorePassword'] + if (!System.getenv()["CI"]) { + debug { + keyAlias keystoreProperties['debugKeyAlias'] + keyPassword keystoreProperties['debugKeyPassword'] + storeFile file(keystoreProperties['debugStoreFile']) + storePassword keystoreProperties['debugStorePassword'] + } } release { keyAlias keystoreProperties['keyAlias'] diff --git a/lib/main.dart b/lib/main.dart index d362498..972c831 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -140,7 +140,7 @@ final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin(); const String appName = "HA Client"; const appVersionNumber = "0.7.0"; -const appVersionAdd = "alpha2"; +const appVersionAdd = "alpha3"; const appVersion = "$appVersionNumber-$appVersionAdd"; void main() async { diff --git a/lib/managers/location_manager.class.dart b/lib/managers/location_manager.class.dart index ffbb9a7..4fce1f1 100644 --- a/lib/managers/location_manager.class.dart +++ b/lib/managers/location_manager.class.dart @@ -66,13 +66,10 @@ class LocationManager { "httpWebHost": httpWebHost }, frequency: _updateInterval, - existingWorkPolicy: workManager.ExistingWorkPolicy.replace, + existingWorkPolicy: workManager.ExistingWorkPolicy.keep, backoffPolicy: workManager.BackoffPolicy.linear, + backoffPolicyDelay: _updateInterval, constraints: workManager.Constraints( - requiresBatteryNotLow: false, - requiresCharging: false, - requiresDeviceIdle: false, - requiresStorageNotLow: false, networkType: workManager.NetworkType.connected ) ); @@ -120,19 +117,20 @@ class LocationManager { void updateDeviceLocationIsolate() { workManager.Workmanager.executeTask((backgroundTask, data) { - print("[Location isolate] Started: $backgroundTask"); + print("[Background $backgroundTask] Started"); - print("[Location isolate] loading settings"); String webhookId = data["webhookId"]; String httpWebHost = data["httpWebHost"]; if (webhookId != null && webhookId.isNotEmpty) { - Logger.d("[Location isolate] Getting device location..."); + int battery = DateTime.now().hour; + print("[Background $backgroundTask] hour=$battery"); + String url = "$httpWebHost/api/webhook/$webhookId"; + Map headers = {}; + headers["Content-Type"] = "application/json"; + print("[Background $backgroundTask] Getting device location..."); Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) { - Logger.d("[Location isolate] Got location: ${location.latitude} ${location.longitude}. Sending home..."); - int battery = DateTime.now().hour; - String url = "$httpWebHost/api/webhook/$webhookId"; - Map headers = {}; - headers["Content-Type"] = "application/json"; + print("[Background $backgroundTask] Got location: ${location.latitude} ${location.longitude}"); + print("[Background $backgroundTask] sending data home..."); var data = { "type": "update_location", "data": { @@ -146,15 +144,56 @@ void updateDeviceLocationIsolate() { headers: headers, body: json.encode(data) ).catchError((e) { - print("[Location isolate] Error sending data: ${e.toString()}"); + print("[Background $backgroundTask] Error sending data: ${e.toString()}"); }).then((_) { - print("[Location isolate] done!"); + print("[Background $backgroundTask] Success!"); }); }).catchError((e) { - print("[Location isolate] Error getting location: ${e.toString()}"); + print("[Background $backgroundTask] Error getting current location: ${e.toString()}. Trying last known..."); + Geolocator().getLastKnownPosition(desiredAccuracy: LocationAccuracy.medium).then((location){ + print("[Background $backgroundTask] Got last known location: ${location.latitude} ${location.longitude}"); + print("[Background $backgroundTask] sending data home..."); + var data = { + "type": "update_location", + "data": { + "gps": [location.latitude, location.longitude], + "gps_accuracy": location.accuracy, + "battery": battery + } + }; + http.post( + url, + headers: headers, + body: json.encode(data) + ).catchError((e) { + print("[Background $backgroundTask] Error sending data: ${e.toString()}"); + }).then((_) { + print("[Background $backgroundTask] Success!"); + }); + }).catchError((e){ + print("[Background $backgroundTask] Error getting last known location: ${e.toString()}. Sending fake..."); + print("[Background $backgroundTask] sending data home..."); + var data = { + "type": "update_location", + "data": { + "gps": [40.34, 30.34], + "gps_accuracy": 300, + "battery": battery + } + }; + http.post( + url, + headers: headers, + body: json.encode(data) + ).catchError((e) { + print("[Background $backgroundTask] Error sending data: ${e.toString()}"); + }).then((_) { + print("[Background $backgroundTask] Success!"); + }); + }); }); } else { - print("[Location isolate] No webhook id. Aborting"); + print("[Background $backgroundTask] No webhook id. Aborting"); } return Future.value(true); }); diff --git a/pubspec.yaml b/pubspec.yaml index 2e23635..b23b552 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: hass_client description: Home Assistant Android Client -version: 0.7.0+702 +version: 0.7.0+703 environment: sdk: ">=2.0.0-dev.68.0 <3.0.0"