changeset 2872:6b00f88316bf

plugin android: use XEP-0352 to indicate (in)active state: Client State Indication plugin is used to set state when pause/resume is called. The state is changed on pause only after a delay (currently 30s), to avoid changing state if use is only checking something quickly on an other app.
author Goffi <goffi@goffi.org>
date Mon, 25 Mar 2019 07:08:26 +0100
parents 9a019db21f3c
children e1207b8ad97c
files sat/plugins/plugin_misc_android.py
diffstat 1 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_android.py	Mon Mar 25 07:08:23 2019 +0100
+++ b/sat/plugins/plugin_misc_android.py	Mon Mar 25 07:08:26 2019 +0100
@@ -36,6 +36,7 @@
     C.PI_NAME: "Android ",
     C.PI_IMPORT_NAME: "android",
     C.PI_TYPE: C.PLUG_TYPE_MISC,
+    C.PI_RECOMMENDATIONS: [u"XEP-0352"],
     C.PI_MAIN: "AndroidPlugin",
     C.PI_HANDLER: "no",
     C.PI_DESCRIPTION: D_(
@@ -46,8 +47,14 @@
 if sys.platform != "android":
     raise exceptions.CancelError(u"this module is not needed on this platform")
 
+
 from plyer import notification, vibrator
 
+#: delay between a pause event and sending the inactive indication to server, in seconds
+#: we don't send the indication immediately because user can be just checking something
+#: quickly on an other app.
+CSI_DELAY = 30
+
 PARAM_VIBRATE_CATEGORY = "Notifications"
 PARAM_VIBRATE_NAME = "vibrate"
 PARAM_VIBRATE_LABEL = D_(u"Vibrate on notifications")
@@ -133,6 +140,8 @@
     def __init__(self, host):
         log.info(_(u"plugin Android initialization"))
         self.host = host
+        self._csi = host.plugins.get(u'XEP-0352')
+        self._csi_timer = None
         host.memory.updateParams(self.params)
         try:
             os.mkdir(SOCKET_DIR, 0700)
@@ -181,12 +190,14 @@
     def _onRunning(self, previous_state):
         if previous_state is not None:
             self.host.bridge.bridgeReactivateSignals()
+        self.setActive()
 
     def _onPaused(self, previous_state):
         self.host.bridge.bridgeDeactivateSignals()
+        self.setInactive()
 
     def _onStopped(self, previous_state):
-        pass
+        self.setInactive()
 
     def _notifyMessage(self, mess_data, client):
         """Send notification when suitable
@@ -216,3 +227,23 @@
             post_treat.addCallback(self._notifyMessage, client)
 
         return True
+
+    # CSI
+
+    def _setInactive(self):
+        self._csi_timer = None
+        for client in self.host.getClients(C.PROF_KEY_ALL):
+            self._csi.setInactive(client)
+
+    def setInactive(self):
+        if self._csi is None or self._csi_timer is not None:
+            return
+        self._csi_timer = reactor.callLater(CSI_DELAY, self._setInactive)
+
+    def setActive(self):
+        if self._csi is None:
+            return
+        if self._csi_timer is not None:
+            self._csi_timer.cancel()
+        for client in self.host.getClients(C.PROF_KEY_ALL):
+            self._csi.setActive(client)