main.py 5.3 KB

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 kivy.uix.recycleview import RecycleView
  21. from goblinoid.services.login_service import user_logged_on, load_user
  22. from goblinoid.services.pump_service import PumpService
  23. from goblinoid.root.goblinscreenmanager import GoblinScreenManager
  24. if platform == 'android':
  25. from jnius import autoclass
  26. from jnius import cast
  27. __version__ = '0.1.4'
  28. class MediaGoblinApp(App):
  29. def build(self, **kwargs):
  30. self.user_dir = self.get_user_dir()
  31. self.pump_service = PumpService()
  32. self.sm = self.init_screen_manager()
  33. # NOTE: this is a poor hotfix to avoid the common
  34. # PyPumpException.
  35. if user_logged_on(self.user_dir):
  36. try:
  37. pump = load_user(self.user_dir)
  38. self.pump_service.pump = pump
  39. self.open_view('action', False)
  40. except PyPumpException, e:
  41. print('Error: {0}, trying once more...'.format(e))
  42. # NOTE: eventually add a toast to alert user of issue
  43. try:
  44. pump = load_user(self.user_dir)
  45. self.pump_service.pump = pump
  46. self.open_view('action', False)
  47. except PyPumpException, e:
  48. print('Error: {0}, transitioning to login'.format(e))
  49. self.open_view('login', True)
  50. else:
  51. self.open_view('login', False)
  52. self.bind(on_start=self.post_build_init)
  53. return self.sm
  54. def init_screen_manager(self):
  55. from goblinoid.root.screenprovider import ScreenManagerProvider
  56. from goblinoid.root.screenstack import ScreenStack
  57. from goblinoid.root.screenprovider import LoginProvider, ActionProvider
  58. screen_providers = dict([
  59. ('login', (LoginProvider, 1)),
  60. ('action', (ActionProvider, 2))
  61. ])
  62. screen_provider = ScreenManagerProvider(
  63. screen_providers=screen_providers)
  64. return RootScreenManager(
  65. screen_stack=ScreenStack(),
  66. screen_provider=screen_provider)
  67. def post_build_init(self, ev):
  68. if platform == 'android':
  69. Window.softinput_mode = 'below_target'
  70. win = self._app_window
  71. win.bind(on_keyboard=self._key_handler)
  72. def _key_handler(self, *args):
  73. key = args[1]
  74. if key in (1000, 27):
  75. self.dispatch_event('on_back_pressed')
  76. return True
  77. def update_pump_service(self, pump):
  78. self.pump_service.update_pump(pump)
  79. def open_view(self, class_name, previous):
  80. if self.sm.current_screen is None:
  81. self.sm.open_screen(class_name=class_name, name=class_name)
  82. else:
  83. self.sm.replace_screen(class_name=class_name, name=class_name)
  84. def get_pump_service(self):
  85. print('printing the pump model: {0}'.format(self.pump_service))
  86. return self.pump_service
  87. def on_pause(self):
  88. return True
  89. def on_resume(self):
  90. pass
  91. def dispatch_event(self, event, **kwargs):
  92. result = self.sm.current_screen.dispatch(event, **kwargs)
  93. self.handle_dispatch_result(event, result)
  94. def handle_dispatch_result(self, event, result):
  95. if event == 'on_back_pressed':
  96. if not result:
  97. self.send_app_to_background()
  98. def send_app_to_background(self):
  99. if platform == 'android':
  100. PythonActivity = autoclass('org.renpy.android.PythonActivity')
  101. Intent = autoclass('android.content.Intent')
  102. intent = Intent()
  103. intent.setAction(Intent.ACTION_MAIN)
  104. intent.addCategory(Intent.CATEGORY_HOME)
  105. currentActivity = cast(
  106. 'android.app.Activity',
  107. PythonActivity.mActivity)
  108. currentActivity.startActivity(intent)
  109. else:
  110. App.get_running_app().stop()
  111. def get_user_dir(self):
  112. if platform == 'android':
  113. from jnius import autoclass, cast
  114. PythonActivity = autoclass('org.renpy.android.PythonActivity')
  115. current_activity = cast('android.app.Activity', PythonActivity.mActivity)
  116. user_dir = current_activity.getApplicationContext().getFilesDir().getPath()
  117. return user_dir
  118. else:
  119. return self.user_data_dir
  120. class RootScreenManager(GoblinScreenManager):
  121. pass
  122. if __name__ == '__main__':
  123. MediaGoblinApp().run()