diff --git a/resources/settings/properties.xml b/resources/settings/properties.xml
index d3ffc6d..b640dfa 100644
--- a/resources/settings/properties.xml
+++ b/resources/settings/properties.xml
@@ -24,7 +24,14 @@
+
+ 0
+
+
diff --git a/resources/settings/settings.xml b/resources/settings/settings.xml
index 6482d61..6f10416 100644
--- a/resources/settings/settings.xml
+++ b/resources/settings/settings.xml
@@ -43,6 +43,16 @@
/>
+
+
+
+
+
diff --git a/source/Alert.mc b/source/Alert.mc
index dc61cb9..70b60f9 100644
--- a/source/Alert.mc
+++ b/source/Alert.mc
@@ -132,11 +132,13 @@ class AlertDelegate extends WatchUi.InputDelegate {
function onKey(evt) {
mView.dismiss();
+ getApp().getQuitTimer().reset();
return true;
}
function onTap(evt) {
mView.dismiss();
+ getApp().getQuitTimer().reset();
return true;
}
}
diff --git a/source/ErrorView.mc b/source/ErrorView.mc
index 3b8318a..5dbadc0 100644
--- a/source/ErrorView.mc
+++ b/source/ErrorView.mc
@@ -82,6 +82,7 @@ class ErrorDelegate extends WatchUi.BehaviorDelegate {
WatchUi.BehaviorDelegate.initialize();
}
function onBack() {
+ getApp().getQuitTimer().reset();
WatchUi.popView(WatchUi.SLIDE_DOWN);
return true;
}
diff --git a/source/HomeAssistantApp.mc b/source/HomeAssistantApp.mc
index 33e4649..dc1651f 100644
--- a/source/HomeAssistantApp.mc
+++ b/source/HomeAssistantApp.mc
@@ -25,6 +25,7 @@ using Toybox.Application.Properties;
class HomeAssistantApp extends Application.AppBase {
private var mHaMenu;
+ private var quitTimer as QuitTimer;
private var strNoApiKey as Lang.String;
private var strNoApiUrl as Lang.String;
private var strNoConfigUrl as Lang.String;
@@ -52,14 +53,18 @@ class HomeAssistantApp extends Application.AppBase {
strConfigUrlNotFound = WatchUi.loadResource($.Rez.Strings.ConfigUrlNotFound);
strUnhandledHttpErr = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr);
strTrailingSlashErr = WatchUi.loadResource($.Rez.Strings.TrailingSlashErr);
+ quitTimer = new QuitTimer();
}
// onStart() is called on application start up
function onStart(state as Lang.Dictionary?) as Void {
+ quitTimer.begin();
}
// onStop() is called when your application is exiting
- function onStop(state as Lang.Dictionary?) as Void {}
+ function onStop(state as Lang.Dictionary?) as Void {
+ quitTimer.stop();
+ }
// Return the initial view of your application here
function getInitialView() as Lang.Array? {
@@ -174,6 +179,10 @@ class HomeAssistantApp extends Application.AppBase {
mNextItemToUpdate = (mNextItemToUpdate + 1) % itu.size();
}
+ function getQuitTimer() as QuitTimer{
+ return quitTimer;
+ }
+
}
function getApp() as HomeAssistantApp {
diff --git a/source/HomeAssistantConfirmation.mc b/source/HomeAssistantConfirmation.mc
index 243a685..7d05604 100644
--- a/source/HomeAssistantConfirmation.mc
+++ b/source/HomeAssistantConfirmation.mc
@@ -40,6 +40,7 @@ class HomeAssistantConfirmationDelegate extends WatchUi.ConfirmationDelegate {
}
function onResponse(response) as Lang.Boolean {
+ getApp().getQuitTimer().reset();
if (response == WatchUi.CONFIRM_YES) {
confirmMethod.invoke();
}
diff --git a/source/HomeAssistantView.mc b/source/HomeAssistantView.mc
index 6a040dd..366dcac 100644
--- a/source/HomeAssistantView.mc
+++ b/source/HomeAssistantView.mc
@@ -107,7 +107,23 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate {
Menu2InputDelegate.initialize();
}
+ function onBack() {
+ getApp().getQuitTimer().reset();
+ WatchUi.popView(WatchUi.SLIDE_RIGHT);
+ }
+
+ // Only for CheckboxMenu
+ function onDone() {
+ getApp().getQuitTimer().reset();
+ }
+
+ // Only for CustomMenu
+ function onFooter() {
+ getApp().getQuitTimer().reset();
+ }
+
function onSelect(item as WatchUi.MenuItem) as Void {
+ getApp().getQuitTimer().reset();
if (item instanceof HomeAssistantToggleMenuItem) {
var haToggleItem = item as HomeAssistantToggleMenuItem;
if (Globals.scDebug) {
@@ -147,8 +163,9 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate {
}
}
- function onBack() {
- WatchUi.popView(WatchUi.SLIDE_RIGHT);
+ // Only for CustomMenu
+ function onTitle() {
+ getApp().getQuitTimer().reset();
}
}
diff --git a/source/QuitTimer.mc b/source/QuitTimer.mc
new file mode 100644
index 0000000..fdc7d63
--- /dev/null
+++ b/source/QuitTimer.mc
@@ -0,0 +1,56 @@
+//-----------------------------------------------------------------------------------
+//
+// Distributed under MIT Licence
+// See https://github.com/house-of-abbey/GarminHomeAssistant/blob/main/LICENSE.
+//
+//-----------------------------------------------------------------------------------
+//
+// GarminHomeAssistant is a Garmin IQ application written in Monkey C and routinely
+// tested on a Venu 2 device. The source code is provided at:
+// https://github.com/house-of-abbey/GarminHomeAssistant.
+//
+// J D Abbey & P A Abbey, 28 December 2022
+//
+//
+// Description:
+//
+// Quit the application after a period of inactivity in order to save the battery.
+//
+//-----------------------------------------------------------------------------------
+
+using Toybox.Lang;
+using Toybox.Timer;
+using Toybox.Application.Properties;
+using Toybox.WatchUi;
+
+class QuitTimer extends Timer.Timer {
+ private var api_timeout;
+
+ function initialize() {
+ Timer.Timer.initialize();
+ // Timer needs delay in milliseconds.
+ api_timeout = (Properties.getValue("app_timeout") as Lang.Number) * 1000;
+ }
+
+ function exitApp() as Void {
+ if (Globals.scDebug) {
+ System.println("QuitTimer exitApp(): Exiting");
+ }
+ // This will exit the system cleanly from any point within an app.
+ System.exit();
+ }
+
+ function begin() {
+ if (api_timeout > 0) {
+ start(method(:exitApp), api_timeout, false);
+ }
+ }
+
+ function reset() {
+ if (Globals.scDebug) {
+ System.println("QuitTimer reset(): Restarted quit timer");
+ }
+ stop();
+ begin();
+ }
+}