From 8c0540ee45b407fed138ada8b059a256019d773f Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Sat, 2 Dec 2023 19:04:53 +0000 Subject: [PATCH 1/2] Initial version with "auto quit" Quit the application after a user settable period of time based on a timeout value from the settings. --- manifest.xml | 2 +- resources/settings/properties.xml | 7 ++++ resources/settings/settings.xml | 18 ++++++++++ source/Alert.mc | 2 ++ source/ErrorView.mc | 1 + source/HomeAssistantApp.mc | 11 +++++- source/HomeAssistantConfirmation.mc | 1 + source/HomeAssistantView.mc | 21 +++++++++-- source/QuitTimer.mc | 56 +++++++++++++++++++++++++++++ 9 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 source/QuitTimer.mc diff --git a/manifest.xml b/manifest.xml index 6c011ed..3f2a12b 100644 --- a/manifest.xml +++ b/manifest.xml @@ -21,7 +21,7 @@ Use "Monkey C: Edit Application" from the Visual Studio Code command palette to update the application attributes. --> - + + + 0 + + diff --git a/resources/settings/settings.xml b/resources/settings/settings.xml index 6482d61..817f986 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(); + } +} From 42e89906f69f214d13b0dbea00f117d56bbfef7a Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Wed, 6 Dec 2023 22:09:22 +0000 Subject: [PATCH 2/2] Review comments --- manifest.xml | 2 +- resources/settings/settings.xml | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/manifest.xml b/manifest.xml index 3f2a12b..6c011ed 100644 --- a/manifest.xml +++ b/manifest.xml @@ -21,7 +21,7 @@ Use "Monkey C: Edit Application" from the Visual Studio Code command palette to update the application attributes. --> - +