comparison 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
comparison
equal deleted inserted replaced
400:71f51198478c 401:788e05d1e2bf
30 from android.permissions import request_permissions, Permission 30 from android.permissions import request_permissions, Permission
31 from kivy.clock import Clock 31 from kivy.clock import Clock
32 from kivy.uix.label import Label 32 from kivy.uix.label import Label
33 from sat.core.i18n import _ 33 from sat.core.i18n import _
34 from sat.core import log as logging 34 from sat.core import log as logging
35 from sat.tools.common import data_format
35 from sat_frontends.tools import jid 36 from sat_frontends.tools import jid
36 from cagou.core.constants import Const as C 37 from cagou.core.constants import Const as C
37 from cagou.core import dialog 38 from cagou.core import dialog
38 from cagou import G 39 from cagou import G
39 from .base import Platform as BasePlatform 40 from .base import Platform as BasePlatform
86 argument = '' 87 argument = ''
87 service.start(mActivity, argument) 88 service.start(mActivity, argument)
88 89
89 activity.bind(on_new_intent=self.on_new_intent) 90 activity.bind(on_new_intent=self.on_new_intent)
90 self.cache.append((self.on_new_intent, mActivity.getIntent())) 91 self.cache.append((self.on_new_intent, mActivity.getIntent()))
92 self.last_selected_wid = None
91 host.addListener('profilePlugged', self.onProfilePlugged) 93 host.addListener('profilePlugged', self.onProfilePlugged)
94 host.addListener('selected', self.onSelectedWidget)
92 local_dir = Path(host.getConfig('', 'local_dir')).resolve() 95 local_dir = Path(host.getConfig('', 'local_dir')).resolve()
93 self.tmp_dir = local_dir / 'tmp' 96 self.tmp_dir = local_dir / 'tmp'
94 # we assert to avoid disaster if `/ 'tmp'` is removed by mistake on the line 97 # we assert to avoid disaster if `/ 'tmp'` is removed by mistake on the line
95 # above 98 # above
96 assert self.tmp_dir.resolve() != local_dir 99 assert self.tmp_dir.resolve() != local_dir
167 170
168 def do_postInit(self): 171 def do_postInit(self):
169 request_permissions(PERMISSION_MANDATORY, callback=self.permission_cb) 172 request_permissions(PERMISSION_MANDATORY, callback=self.permission_cb)
170 return False 173 return False
171 174
175 def privateDataGetCb(self, data_s, profile):
176 data = data_format.deserialise(data_s, type_check=None)
177 if data is not None:
178 log.debug(f"restoring previous widget {data}")
179 try:
180 name = data['name']
181 target = data['target']
182 except KeyError as e:
183 log.error(f"Bad data format for selected widget: {e}\ndata={data}")
184 return
185 if target:
186 target = jid.JID(data['target'])
187 plugin_info = G.host.getPluginInfo(name=name)
188 if plugin_info is None:
189 log.warning("Can't restore unknown plugin: {name}")
190 return
191 factory = plugin_info['factory']
192 G.host.switchWidget(
193 None,
194 factory(plugin_info, target=target, profiles=[profile])
195 )
196
172 def onProfilePlugged(self, profile): 197 def onProfilePlugged(self, profile):
173 log.debug("ANDROID profilePlugged") 198 log.debug("ANDROID profilePlugged")
174 G.host.bridge.setParam( 199 G.host.bridge.setParam(
175 "autoconnect_backend", C.BOOL_TRUE, "Connection", -1, profile, 200 "autoconnect_backend", C.BOOL_TRUE, "Connection", -1, profile,
176 callback=lambda: log.info(f"profile {profile} autoconnection set"), 201 callback=lambda: log.info(f"profile {profile} autoconnection set"),
177 errback=lambda: log.error(f"can't set {profile} autoconnection")) 202 errback=lambda: log.error(f"can't set {profile} autoconnection"))
178 for method, *args in self.cache: 203 for method, *args in self.cache:
179 method(*args) 204 method(*args)
180 del self.cache 205 del self.cache
181 G.host.removeListener("profilePlugged", self.onProfilePlugged) 206 G.host.removeListener("profilePlugged", self.onProfilePlugged)
207 # we restore the stored widget if any
208 # user will then go back to where they was when the frontend was closed
209 G.host.bridge.privateDataGet(
210 "cagou", "selected_widget", profile,
211 callback=partial(self.privateDataGetCb, profile=profile),
212 errback=partial(
213 G.host.errback,
214 title=_("can't get selected widget"),
215 message=_("error while retrieving selected widget: {msg}"))
216 )
217
218 def onSelectedWidget(self, wid):
219 """Store selected widget in backend, to restore it on next startup"""
220 if self.last_selected_wid == None:
221 self.last_selected_wid = wid
222 # we skip the first selected widget, as we'll restore stored one if possible
223 return
224
225 self.last_selected_wid = wid
226
227 try:
228 plugin_info = wid.plugin_info
229 except AttributeError:
230 log.warning(f"No plugin info found for {wid}, can't store selected widget")
231 return
232
233 try:
234 profile = next(iter(wid.profiles))
235 except (AttributeError, StopIteration):
236 profile = None
237
238 if profile is None:
239 try:
240 profile = next(iter(G.host.profiles))
241 except StopIteration:
242 log.debug("No profile plugged yet, can't store selected widget")
243 return
244 try:
245 target = wid.target
246 except AttributeError:
247 target = None
248
249 data = {
250 "name": plugin_info["name"],
251 "target": target,
252 }
253
254 G.host.bridge.privateDataSet(
255 "cagou", "selected_widget", data_format.serialise(data), profile,
256 errback=partial(
257 G.host.errback,
258 title=_("can set selected widget"),
259 message=_("error while setting selected widget: {msg}"))
260 )
182 261
183 def on_pause(self): 262 def on_pause(self):
184 G.host.sync = False 263 G.host.sync = False
185 self._frontend_status_socket.sendall(STATE_PAUSED) 264 self._frontend_status_socket.sendall(STATE_PAUSED)
186 return True 265 return True