Merge pull request #24 from house-of-abbey/23-prevent-battery-drain-by-quitting-the-app-after-a-timeout

Initial version with "auto quit"
This commit is contained in:
Philip Abbey
2023-12-06 22:43:42 +00:00
committed by GitHub
8 changed files with 107 additions and 3 deletions

View File

@ -24,7 +24,14 @@
<!-- Best be a public URL in order to work away from your home LAN and have a trusted HTTPS certificate -->
<property id="config_url" type="string"></property>
<!--
Application timeout in seconds, except 0 for no timeout (default). After this amount of elapsed time
with no activity, exit the application.
-->
<property id="app_timeout" type="number">0</property>
<property id="types_representation" type="boolean"></property>
<property id="menu_alignment" type="boolean"></property>
</properties>

View File

@ -43,6 +43,16 @@
/>
</setting>
<setting
propertyKey="@Properties.app_timeout"
title="Timeout in seconds. Exit the application after this period of inactivity to save the device battery."
>
<settingConfig
type="numeric"
min="0"
/>
</setting>
<setting
propertyKey="@Properties.types_representation"
title="Representing types with icons (off) or with labels (on)"
@ -60,4 +70,5 @@
type="boolean"
/>
</setting>
</settings>

View File

@ -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;
}
}

View File

@ -82,6 +82,7 @@ class ErrorDelegate extends WatchUi.BehaviorDelegate {
WatchUi.BehaviorDelegate.initialize();
}
function onBack() {
getApp().getQuitTimer().reset();
WatchUi.popView(WatchUi.SLIDE_DOWN);
return true;
}

View File

@ -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<WatchUi.Views or WatchUi.InputDelegates>? {
@ -174,6 +179,10 @@ class HomeAssistantApp extends Application.AppBase {
mNextItemToUpdate = (mNextItemToUpdate + 1) % itu.size();
}
function getQuitTimer() as QuitTimer{
return quitTimer;
}
}
function getApp() as HomeAssistantApp {

View File

@ -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();
}

View File

@ -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();
}
}

56
source/QuitTimer.mc Normal file
View File

@ -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();
}
}