Multi-platform (Android specifically) MediaGoblin client

main.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. '''
  2. Goblinoid: Experience all of MediaGoblin on an Android Device
  3. Copyright (C) 2015 Dylan Jeffers
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. '''
  15. from __future__ import print_function
  16. from kivy.app import App
  17. from kivy.core.window import Window
  18. from kivy.utils import platform
  19. from pypump.exception import PyPumpException
  20. from goblinoid.services.login_service import user_logged_on, load_user
  21. from goblinoid.services.pump_service import PumpService
  22. from goblinoid.root.goblinscreenmanager import GoblinScreenManager
  23. if platform() == 'android':
  24. from jnius import autoclass
  25. from jnius import cast
  26. __version__ = '0.1.3-alpha7'
  27. class MediaGoblinApp(App):
  28. def build(self, **kwargs):
  29. self.user_dir = self.get_user_dir()
  30. self.pump_service = PumpService()
  31. self.sm = self.init_screen_manager()
  32. # NOTE: this is a poor hotfix to avoid the common
  33. # PyPumpException.
  34. if user_logged_on(self.user_dir):
  35. try:
  36. pump = load_user(self.user_dir)
  37. self.pump_service.pump = pump
  38. self.open_view('action', False)
  39. except PyPumpException, e:
  40. print('Error: {0}, trying once more...'.format(e))
  41. # NOTE: eventually add a toast to alert user of issue
  42. try:
  43. pump = load_user(self.user_dir)
  44. self.pump_service.pump = pump
  45. self.open_view('action', False)
  46. except PyPumpException, e:
  47. print('Error: {0}, transitioning to login'.format(e))
  48. self.open_view('login', True)
  49. else:
  50. self.open_view('login', False)
  51. self.bind(on_start=self.post_build_init)
  52. return self.sm
  53. def init_screen_manager(self):
  54. from goblinoid.root.screenprovider import ScreenManagerProvider
  55. from goblinoid.root.screenstack import ScreenStack
  56. from goblinoid.root.screenprovider import LoginProvider, ActionProvider
  57. screen_providers = dict([
  58. ('login', (LoginProvider, 1)),
  59. ('action', (ActionProvider, 2))
  60. ])
  61. screen_provider = ScreenManagerProvider(
  62. screen_providers=screen_providers)
  63. return RootScreenManager(
  64. screen_stack=ScreenStack(),
  65. screen_provider=screen_provider)
  66. def post_build_init(self, ev):
  67. if platform == 'android':
  68. Window.softinput_mode = 'below_target'
  69. win = self._app_window
  70. win.bind(on_keyboard=self._key_handler)
  71. def _key_handler(self, *args):
  72. key = args[1]
  73. if key in (1000, 27):
  74. self.dispatch_event('on_back_pressed')
  75. return True
  76. def update_pump_service(self, pump):
  77. self.pump_service.update_pump(pump)
  78. def open_view(self, class_name, previous):
  79. if self.sm.current_screen is None:
  80. self.sm.open_screen(class_name=class_name, name=class_name)
  81. else:
  82. self.sm.replace_screen(class_name=class_name, name=class_name)
  83. def get_pump_service(self):
  84. print('printing the pump model: {0}'.format(self.pump_service))
  85. return self.pump_service
  86. def on_pause(self):
  87. return True
  88. def on_resume(self):
  89. pass
  90. def dispatch_event(self, event, **kwargs):
  91. result = self.sm.current_screen.dispatch(event, **kwargs)
  92. self.handle_dispatch_result(event, result)
  93. def handle_dispatch_result(self, event, result):
  94. if event == 'on_back_pressed':
  95. if not result:
  96. self.send_app_to_background()
  97. def send_app_to_background(self):
  98. if platform == 'android':
  99. PythonActivity = autoclass('org.renpy.android.PythonActivity')
  100. Intent = autoclass('android.content.Intent')
  101. intent = Intent()
  102. intent.setAction(Intent.ACTION_MAIN)
  103. intent.addCategory(Intent.CATEGORY_HOME)
  104. currentActivity = cast(
  105. 'android.app.Activity',
  106. PythonActivity.mActivity)
  107. currentActivity.startActivity(intent)
  108. else:
  109. App.get_running_app().stop()
  110. def get_user_dir(self):
  111. if platform == 'android':
  112. from jnius import autoclass, cast
  113. PythonActivity = autoclass('org.renpy.android.PythonActivity')
  114. current_activity = cast('android.app.Activity', PythonActivity.mActivity)
  115. user_dir = current_activity.getApplicationContext().getFilesDir().getPath()
  116. return user_dir
  117. else:
  118. return self.user_data_dir
  119. class RootScreenManager(GoblinScreenManager):
  120. pass
  121. if __name__ == '__main__':
  122. MediaGoblinApp().run()