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.
This commit is contained in:
Goffi 2019-03-25 07:08:26 +01:00
commit 598f4e465e

View file

@ -36,6 +36,7 @@ PLUGIN_INFO = {
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 @@ PLUGIN_INFO = {
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 @@ class AndroidPlugin(object):
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 @@ class AndroidPlugin(object):
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 @@ class AndroidPlugin(object):
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)