From a4857091733ed2e2275c1b1896f7482e01d271e5 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sat, 25 Jan 2020 21:08:41 +0100 Subject: [PATCH] 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). --- sat/plugins/plugin_misc_android.py | 33 ++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/sat/plugins/plugin_misc_android.py b/sat/plugins/plugin_misc_android.py index a30bb120..2972dc3e 100644 --- a/sat/plugins/plugin_misc_android.py +++ b/sat/plugins/plugin_misc_android.py @@ -26,6 +26,7 @@ from sat.core.constants import Const as C from sat.core.log import getLogger from sat.core import exceptions from sat.memory import params +from twisted.internet import defer from twisted.internet import reactor from twisted.internet import protocol from twisted.internet import error as int_error @@ -33,7 +34,7 @@ from twisted.internet import error as int_error log = getLogger(__name__) PLUGIN_INFO = { - C.PI_NAME: "Android ", + C.PI_NAME: "Android", C.PI_IMPORT_NAME: "android", C.PI_TYPE: C.PLUG_TYPE_MISC, C.PI_RECOMMENDATIONS: ["XEP-0352"], @@ -169,6 +170,16 @@ class AndroidPlugin(object): # plugins have done their job host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000) + # profiles autoconnection + host.bridge.addMethod( + "profileAutoconnectGet", + ".plugin", + in_sign="", + out_sign="s", + method=self._profileAutoconnectGet, + async_=True, + ) + # audio manager, to get ring status self.am = activity.getSystemService(Context.AUDIO_SERVICE) @@ -194,7 +205,6 @@ class AndroidPlugin(object): actions=["android.net.conn.CONNECTIVITY_CHANGE"]) self.br.start() - @property def state(self): return self._state @@ -276,6 +286,25 @@ class AndroidPlugin(object): return True + # Profile autoconnection + + def _profileAutoconnectGet(self): + return defer.ensureDeferred(self.profileAutoconnectGet()) + + async def profileAutoconnectGet(self): + """Return profile to connect automatically by frontend, if any""" + autoconnect_dict = await self.host.memory.storage.getIndParamValues( + category='Connection', name='autoconnect_backend', + ) + profiles_autoconnect = [p for p, v in autoconnect_dict.items() if C.bool(v)] + if not profiles_autoconnect: + return None + if len(profiles_autoconnect) > 1: + log.warning( + f"More that one profiles with backend autoconnection set found, picking " + f"up first one (full list: {profiles_autoconnect!r})") + return profiles_autoconnect[0] + # CSI def _setInactive(self):