comparison sat/plugins/plugin_misc_android.py @ 3124:b86060901278

plugin android: added a `profileAutoconnectGet` method: this method will be called by frontend to know which profile to connect automatically (when the frontend has been closed and is reopened, this will avoid to have to go through the profileManager again).
author Goffi <goffi@goffi.org>
date Sat, 25 Jan 2020 21:08:41 +0100
parents 7f7cdc6ecfd8
children c3ce8c997fdf
comparison
equal deleted inserted replaced
3123:130f9cb6e0ab 3124:b86060901278
24 from sat.core.i18n import _, D_ 24 from sat.core.i18n import _, D_
25 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
26 from sat.core.log import getLogger 26 from sat.core.log import getLogger
27 from sat.core import exceptions 27 from sat.core import exceptions
28 from sat.memory import params 28 from sat.memory import params
29 from twisted.internet import defer
29 from twisted.internet import reactor 30 from twisted.internet import reactor
30 from twisted.internet import protocol 31 from twisted.internet import protocol
31 from twisted.internet import error as int_error 32 from twisted.internet import error as int_error
32 33
33 log = getLogger(__name__) 34 log = getLogger(__name__)
34 35
35 PLUGIN_INFO = { 36 PLUGIN_INFO = {
36 C.PI_NAME: "Android ", 37 C.PI_NAME: "Android",
37 C.PI_IMPORT_NAME: "android", 38 C.PI_IMPORT_NAME: "android",
38 C.PI_TYPE: C.PLUG_TYPE_MISC, 39 C.PI_TYPE: C.PLUG_TYPE_MISC,
39 C.PI_RECOMMENDATIONS: ["XEP-0352"], 40 C.PI_RECOMMENDATIONS: ["XEP-0352"],
40 C.PI_MAIN: "AndroidPlugin", 41 C.PI_MAIN: "AndroidPlugin",
41 C.PI_HANDLER: "no", 42 C.PI_HANDLER: "no",
167 raise e 168 raise e
168 # we set a low priority because we want the notification to be sent after all 169 # we set a low priority because we want the notification to be sent after all
169 # plugins have done their job 170 # plugins have done their job
170 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000) 171 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000)
171 172
173 # profiles autoconnection
174 host.bridge.addMethod(
175 "profileAutoconnectGet",
176 ".plugin",
177 in_sign="",
178 out_sign="s",
179 method=self._profileAutoconnectGet,
180 async_=True,
181 )
182
172 # audio manager, to get ring status 183 # audio manager, to get ring status
173 self.am = activity.getSystemService(Context.AUDIO_SERVICE) 184 self.am = activity.getSystemService(Context.AUDIO_SERVICE)
174 185
175 # sound notification 186 # sound notification
176 media_dir = Path(host.memory.getConfig("", "media_dir")) 187 media_dir = Path(host.memory.getConfig("", "media_dir"))
192 callback=lambda *args, **kwargs: reactor.callLater(0, 203 callback=lambda *args, **kwargs: reactor.callLater(0,
193 self.onConnectivityChange), 204 self.onConnectivityChange),
194 actions=["android.net.conn.CONNECTIVITY_CHANGE"]) 205 actions=["android.net.conn.CONNECTIVITY_CHANGE"])
195 self.br.start() 206 self.br.start()
196 207
197
198 @property 208 @property
199 def state(self): 209 def state(self):
200 return self._state 210 return self._state
201 211
202 @state.setter 212 @state.setter
273 if not self.cagou_active: 283 if not self.cagou_active:
274 # we only send notification is the frontend is not displayed 284 # we only send notification is the frontend is not displayed
275 post_treat.addCallback(self._notifyMessage, client) 285 post_treat.addCallback(self._notifyMessage, client)
276 286
277 return True 287 return True
288
289 # Profile autoconnection
290
291 def _profileAutoconnectGet(self):
292 return defer.ensureDeferred(self.profileAutoconnectGet())
293
294 async def profileAutoconnectGet(self):
295 """Return profile to connect automatically by frontend, if any"""
296 autoconnect_dict = await self.host.memory.storage.getIndParamValues(
297 category='Connection', name='autoconnect_backend',
298 )
299 profiles_autoconnect = [p for p, v in autoconnect_dict.items() if C.bool(v)]
300 if not profiles_autoconnect:
301 return None
302 if len(profiles_autoconnect) > 1:
303 log.warning(
304 f"More that one profiles with backend autoconnection set found, picking "
305 f"up first one (full list: {profiles_autoconnect!r})")
306 return profiles_autoconnect[0]
278 307
279 # CSI 308 # CSI
280 309
281 def _setInactive(self): 310 def _setInactive(self):
282 self._csi_timer = None 311 self._csi_timer = None