comparison sat/plugins/plugin_misc_android.py @ 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 5546613f5007
children 0c54970d8e6e
comparison
equal deleted inserted replaced
2871:9a019db21f3c 2872:6b00f88316bf
34 34
35 PLUGIN_INFO = { 35 PLUGIN_INFO = {
36 C.PI_NAME: "Android ", 36 C.PI_NAME: "Android ",
37 C.PI_IMPORT_NAME: "android", 37 C.PI_IMPORT_NAME: "android",
38 C.PI_TYPE: C.PLUG_TYPE_MISC, 38 C.PI_TYPE: C.PLUG_TYPE_MISC,
39 C.PI_RECOMMENDATIONS: [u"XEP-0352"],
39 C.PI_MAIN: "AndroidPlugin", 40 C.PI_MAIN: "AndroidPlugin",
40 C.PI_HANDLER: "no", 41 C.PI_HANDLER: "no",
41 C.PI_DESCRIPTION: D_( 42 C.PI_DESCRIPTION: D_(
42 """Manage Android platform specificities, like pause or notifications""" 43 """Manage Android platform specificities, like pause or notifications"""
43 ), 44 ),
44 } 45 }
45 46
46 if sys.platform != "android": 47 if sys.platform != "android":
47 raise exceptions.CancelError(u"this module is not needed on this platform") 48 raise exceptions.CancelError(u"this module is not needed on this platform")
48 49
50
49 from plyer import notification, vibrator 51 from plyer import notification, vibrator
52
53 #: delay between a pause event and sending the inactive indication to server, in seconds
54 #: we don't send the indication immediately because user can be just checking something
55 #: quickly on an other app.
56 CSI_DELAY = 30
50 57
51 PARAM_VIBRATE_CATEGORY = "Notifications" 58 PARAM_VIBRATE_CATEGORY = "Notifications"
52 PARAM_VIBRATE_NAME = "vibrate" 59 PARAM_VIBRATE_NAME = "vibrate"
53 PARAM_VIBRATE_LABEL = D_(u"Vibrate on notifications") 60 PARAM_VIBRATE_LABEL = D_(u"Vibrate on notifications")
54 SOCKET_DIR = "/data/data/org.salutatoi.cagou/" 61 SOCKET_DIR = "/data/data/org.salutatoi.cagou/"
131 ) 138 )
132 139
133 def __init__(self, host): 140 def __init__(self, host):
134 log.info(_(u"plugin Android initialization")) 141 log.info(_(u"plugin Android initialization"))
135 self.host = host 142 self.host = host
143 self._csi = host.plugins.get(u'XEP-0352')
144 self._csi_timer = None
136 host.memory.updateParams(self.params) 145 host.memory.updateParams(self.params)
137 try: 146 try:
138 os.mkdir(SOCKET_DIR, 0700) 147 os.mkdir(SOCKET_DIR, 0700)
139 except OSError as e: 148 except OSError as e:
140 if e.errno == 17: 149 if e.errno == 17:
179 return self._state == STATE_RUNNING 188 return self._state == STATE_RUNNING
180 189
181 def _onRunning(self, previous_state): 190 def _onRunning(self, previous_state):
182 if previous_state is not None: 191 if previous_state is not None:
183 self.host.bridge.bridgeReactivateSignals() 192 self.host.bridge.bridgeReactivateSignals()
193 self.setActive()
184 194
185 def _onPaused(self, previous_state): 195 def _onPaused(self, previous_state):
186 self.host.bridge.bridgeDeactivateSignals() 196 self.host.bridge.bridgeDeactivateSignals()
197 self.setInactive()
187 198
188 def _onStopped(self, previous_state): 199 def _onStopped(self, previous_state):
189 pass 200 self.setInactive()
190 201
191 def _notifyMessage(self, mess_data, client): 202 def _notifyMessage(self, mess_data, client):
192 """Send notification when suitable 203 """Send notification when suitable
193 204
194 notification is sent if: 205 notification is sent if:
214 if not self.cagou_active: 225 if not self.cagou_active:
215 # we only send notification is the frontend is not displayed 226 # we only send notification is the frontend is not displayed
216 post_treat.addCallback(self._notifyMessage, client) 227 post_treat.addCallback(self._notifyMessage, client)
217 228
218 return True 229 return True
230
231 # CSI
232
233 def _setInactive(self):
234 self._csi_timer = None
235 for client in self.host.getClients(C.PROF_KEY_ALL):
236 self._csi.setInactive(client)
237
238 def setInactive(self):
239 if self._csi is None or self._csi_timer is not None:
240 return
241 self._csi_timer = reactor.callLater(CSI_DELAY, self._setInactive)
242
243 def setActive(self):
244 if self._csi is None:
245 return
246 if self._csi_timer is not None:
247 self._csi_timer.cancel()
248 for client in self.host.getClients(C.PROF_KEY_ALL):
249 self._csi.setActive(client)