diff 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
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_android.py	Sat Jan 25 21:08:40 2020 +0100
+++ b/sat/plugins/plugin_misc_android.py	Sat Jan 25 21:08:41 2020 +0100
@@ -26,6 +26,7 @@
 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 @@
 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 @@
         # 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 @@
             actions=["android.net.conn.CONNECTIVITY_CHANGE"])
         self.br.start()
 
-
     @property
     def state(self):
         return self._state
@@ -276,6 +286,25 @@
 
         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):