Improve the Android back button handling

This commit is contained in:
Michael Zanetti 2019-07-07 17:25:10 +02:00
parent 604475d29c
commit c1bd92ea49
2 changed files with 36 additions and 15 deletions

View File

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

View File

@ -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) {