comparison cagou/core/cagou_main.py @ 342:89799148f894

core: use classes and factory to handle platform specific behaviours in a generic way
author Goffi <goffi@goffi.org>
date Sat, 04 Jan 2020 16:24:57 +0100
parents d36040493434
children 7b9a0e57df53
comparison
equal deleted inserted replaced
341:89b17a841c2f 342:89799148f894
60 from kivy.uix.dropdown import DropDown 60 from kivy.uix.dropdown import DropDown
61 from kivy.uix.behaviors import ButtonBehavior 61 from kivy.uix.behaviors import ButtonBehavior
62 from kivy.core.window import Window 62 from kivy.core.window import Window
63 from kivy.animation import Animation 63 from kivy.animation import Animation
64 from kivy.metrics import dp 64 from kivy.metrics import dp
65 from kivy import utils as kivy_utils
66 from kivy.config import Config as KivyConfig
67 from .cagou_widget import CagouWidget 65 from .cagou_widget import CagouWidget
68 from .share_widget import ShareWidget 66 from .share_widget import ShareWidget
69 from . import widgets_handler 67 from . import widgets_handler
70 from .common import IconButton 68 from .common import IconButton
71 from . import menu 69 from . import menu
87 log.warning(_("Can't import plyer, some features disabled")) 85 log.warning(_("Can't import plyer, some features disabled"))
88 86
89 87
90 ## platform specific settings ## 88 ## platform specific settings ##
91 89
92 if kivy_utils.platform == "android": 90 from . import platform_
93 import socket 91 local_platform = platform_.create()
94 from .platform_ import android 92 local_platform.init_platform()
95 android.init_platform()
96 else:
97 # we don't want multi-touch emulation with mouse
98
99 # this option doesn't make sense on Android and cause troubles, so we only activate
100 # it for other platforms (cf. https://github.com/kivy/kivy/issues/6229)
101 KivyConfig.set('input', 'mouse', 'mouse,disable_multitouch')
102 93
103 94
104 ## General Configuration ## 95 ## General Configuration ##
105 96
106 # we want white background by default 97 # we want white background by default
316 return 307 return
317 308
318 def build(self): 309 def build(self):
319 Window.bind(on_keyboard=self.key_input) 310 Window.bind(on_keyboard=self.key_input)
320 wid = CagouRootWidget(Label(text="Loading please wait")) 311 wid = CagouRootWidget(Label(text="Loading please wait"))
321 if sys.platform == 'android': 312 local_platform.on_app_build(wid)
322 # we don't want menu on Android
323 wid.root_menus.height = 0
324 return wid 313 return wid
325 314
326 def showWidget(self): 315 def showWidget(self):
327 self._profile_manager = ProfileManager() 316 self._profile_manager = ProfileManager()
328 self.root.changeWidget(self._profile_manager) 317 self.root.changeWidget(self._profile_manager)
338 """ 327 """
339 return os.path.expanduser(path).format(*args, media=self.host.media_dir, **kwargs) 328 return os.path.expanduser(path).format(*args, media=self.host.media_dir, **kwargs)
340 329
341 def initFrontendState(self): 330 def initFrontendState(self):
342 """Init state to handle paused/stopped/running on mobile OSes""" 331 """Init state to handle paused/stopped/running on mobile OSes"""
343 if sys.platform == "android": 332 local_platform.on_initFrontendState()
344 # XXX: we use a separated socket instead of bridge because if we
345 # try to call a bridge method in on_pause method, the call data
346 # is not written before the actual pause
347 s = self._frontend_status_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
348 s.connect(os.path.join(android.SOCKET_DIR, android.SOCKET_FILE))
349 s.sendall(android.STATE_RUNNING)
350 333
351 def on_pause(self): 334 def on_pause(self):
352 self.host.sync = False 335 return local_platform.on_pause()
353 self._frontend_status_socket.sendall(android.STATE_PAUSED)
354 return True
355 336
356 def on_resume(self): 337 def on_resume(self):
357 self._frontend_status_socket.sendall(android.STATE_RUNNING) 338 return local_platform.on_resume()
358 self.host.sync = True
359 339
360 def on_stop(self): 340 def on_stop(self):
361 if sys.platform == "android": 341 return local_platform.on_stop()
362 self._frontend_status_socket.sendall(android.STATE_STOPPED)
363 self._frontend_status_socket.close()
364 342
365 def key_input(self, window, key, scancode, codepoint, modifier): 343 def key_input(self, window, key, scancode, codepoint, modifier):
366 if key == 27: 344 if key == 27:
367 # we disable [esc] handling, because default action is to quit app 345 # we disable [esc] handling, because default action is to quit app
368 return True 346 return True
447 if not self.tls_validation: 425 if not self.tls_validation:
448 from cagou.core import patches 426 from cagou.core import patches
449 patches.apply() 427 patches.apply()
450 log.warning("SSL certificate validation is disabled, this is unsecure!") 428 log.warning("SSL certificate validation is disabled, this is unsecure!")
451 429
452 if sys.platform == 'android': 430 local_platform.on_host_init(self)
453 android.host_init(self)
454 431
455 @property 432 @property
456 def visible_widgets(self): 433 def visible_widgets(self):
457 for w_list in self._visible_widgets.values(): 434 for w_list in self._visible_widgets.values():
458 for w in w_list: 435 for w in w_list: