diff cagou/core/platform_/android.py @ 401:788e05d1e2bf

android: store and restore last selected widget
author Goffi <goffi@goffi.org>
date Wed, 12 Feb 2020 20:02:58 +0100
parents 71f51198478c
children b0af45a92055
line wrap: on
line diff
--- a/cagou/core/platform_/android.py	Sun Feb 09 23:47:29 2020 +0100
+++ b/cagou/core/platform_/android.py	Wed Feb 12 20:02:58 2020 +0100
@@ -32,6 +32,7 @@
 from kivy.uix.label import Label
 from sat.core.i18n import _
 from sat.core import log as logging
+from sat.tools.common import data_format
 from sat_frontends.tools import jid
 from cagou.core.constants import Const as C
 from cagou.core import dialog
@@ -88,7 +89,9 @@
 
         activity.bind(on_new_intent=self.on_new_intent)
         self.cache.append((self.on_new_intent, mActivity.getIntent()))
+        self.last_selected_wid = None
         host.addListener('profilePlugged', self.onProfilePlugged)
+        host.addListener('selected', self.onSelectedWidget)
         local_dir = Path(host.getConfig('', 'local_dir')).resolve()
         self.tmp_dir = local_dir / 'tmp'
         # we assert to avoid disaster if `/ 'tmp'` is removed by mistake on the line
@@ -169,6 +172,28 @@
         request_permissions(PERMISSION_MANDATORY, callback=self.permission_cb)
         return False
 
+    def privateDataGetCb(self, data_s, profile):
+        data = data_format.deserialise(data_s, type_check=None)
+        if data is not None:
+            log.debug(f"restoring previous widget {data}")
+            try:
+                name = data['name']
+                target = data['target']
+            except KeyError as e:
+                log.error(f"Bad data format for selected widget: {e}\ndata={data}")
+                return
+            if target:
+                target = jid.JID(data['target'])
+            plugin_info = G.host.getPluginInfo(name=name)
+            if plugin_info is None:
+                log.warning("Can't restore unknown plugin: {name}")
+                return
+            factory = plugin_info['factory']
+            G.host.switchWidget(
+                None,
+                factory(plugin_info, target=target, profiles=[profile])
+            )
+
     def onProfilePlugged(self, profile):
         log.debug("ANDROID profilePlugged")
         G.host.bridge.setParam(
@@ -179,6 +204,60 @@
             method(*args)
         del self.cache
         G.host.removeListener("profilePlugged", self.onProfilePlugged)
+        # we restore the stored widget if any
+        # user will then go back to where they was when the frontend was closed
+        G.host.bridge.privateDataGet(
+            "cagou", "selected_widget", profile,
+            callback=partial(self.privateDataGetCb, profile=profile),
+            errback=partial(
+                G.host.errback,
+                title=_("can't get selected widget"),
+                message=_("error while retrieving selected widget: {msg}"))
+        )
+
+    def onSelectedWidget(self, wid):
+        """Store selected widget in backend, to restore it on next startup"""
+        if self.last_selected_wid == None:
+            self.last_selected_wid = wid
+            # we skip the first selected widget, as we'll restore stored one if possible
+            return
+
+        self.last_selected_wid = wid
+
+        try:
+            plugin_info = wid.plugin_info
+        except AttributeError:
+            log.warning(f"No plugin info found for {wid}, can't store selected widget")
+            return
+
+        try:
+            profile = next(iter(wid.profiles))
+        except (AttributeError, StopIteration):
+            profile = None
+
+        if profile is None:
+            try:
+                profile = next(iter(G.host.profiles))
+            except StopIteration:
+                log.debug("No profile plugged yet, can't store selected widget")
+                return
+        try:
+            target = wid.target
+        except AttributeError:
+            target = None
+
+        data = {
+            "name": plugin_info["name"],
+            "target": target,
+        }
+
+        G.host.bridge.privateDataSet(
+            "cagou", "selected_widget", data_format.serialise(data), profile,
+            errback=partial(
+                G.host.errback,
+                title=_("can set selected widget"),
+                message=_("error while setting selected widget: {msg}"))
+        )
 
     def on_pause(self):
         G.host.sync = False