From c1bd92ea497308212451f43b1bb8792223b0fa5e Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sun, 7 Jul 2019 17:25:10 +0200 Subject: [PATCH] Improve the Android back button handling --- nymea-app/ui/Nymea.qml | 30 ++++++++++++++++++++++++++---- nymea-app/ui/RootItem.qml | 21 ++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/nymea-app/ui/Nymea.qml b/nymea-app/ui/Nymea.qml index 631740d1..9abc3ebb 100644 --- a/nymea-app/ui/Nymea.qml +++ b/nymea-app/ui/Nymea.qml @@ -65,10 +65,6 @@ ApplicationWindow { } property alias _discovery: discovery - onClosing: { - rootItem.handleCloseEvent(close) - } - property var supportedInterfaces: ["light", "weather", "media", "garagegate", "awning", "shutter", "blind", "powersocket", "heating", "sensor", "smartmeter", "evcharger", "accesscontrol", "button", "notifications", "inputtrigger", "outputtrigger", "gateway"] function interfaceToString(name) { switch(name) { @@ -338,6 +334,32 @@ ApplicationWindow { return s.substr(s.length-size); } + // Handle the Android close event that happens when the back button is pressed + // It's hard to handle the key press, because we might not have focus all the time + // So let's handle the window's onClosing signal instad. + // The problem is, we cannot distinguish between the back button being pressed + // or the bottom swipe gesture is being used to switch apps. Let's try to figure that out + // by checking if the app becomes inactive right after the event. If not, it's probably a back + // button press and we close ourselves. + onClosing: { + if (Qt.platform.os == "android") { + var handled = rootItem.handleAndroidBackButton(); + if (!handled) { + closeTimer.start() + } + close.accepted = false; + } + } + Timer { + id: closeTimer + interval: 300 + onTriggered: Qt.quit(); + } + Connections { + target: Qt.application + onStateChanged: closeTimer.stop() + } + KeyboardLoader { id: keyboardRect diff --git a/nymea-app/ui/RootItem.qml b/nymea-app/ui/RootItem.qml index 7dfd8f6c..c96e5340 100644 --- a/nymea-app/ui/RootItem.qml +++ b/nymea-app/ui/RootItem.qml @@ -16,8 +16,8 @@ Item { color: Material.background } - function handleCloseEvent(close) { - swipeView.currentItem.handleCloseEvent(close) + function handleAndroidBackButton() { + return swipeView.currentItem.handleAndroidBackButton() } ListModel { @@ -182,16 +182,15 @@ Item { }) } - function handleCloseEvent(close) { - if (Qt.platform.os == "android") { - // If we're connected, allow going back up to MainPage - if ((engine.jsonRpcClient.connected && pageStack.depth > 1) - // if we're not connected, only allow using the back button in wizards - || (!engine.jsonRpcClient.connected && pageStack.depth > 3)) { - close.accepted = false; - pageStack.pop(); - } + function handleAndroidBackButton() { + // If we're connected, allow going back up to MainPage + if ((engine.jsonRpcClient.connected && pageStack.depth > 1) + // if we're not connected, only allow using the back button in wizards + || (!engine.jsonRpcClient.connected && pageStack.depth > 3)) { + pageStack.pop(); + return true; } + return false; } function setupPushNotifications(askForPermissions) {