mediaviews.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.lang import Builder
  18. from kivy.logger import Logger
  19. from kivy.clock import Clock, mainthread
  20. from kivy.properties import StringProperty, ObjectProperty, BooleanProperty
  21. from kivy.utils import platform
  22. from pypump.exception import PyPumpException
  23. import threading
  24. from goblinoid.root.goblinscreen import GoblinScreen
  25. if platform() == 'android':
  26. from jnius import cast
  27. from jnius import autoclass
  28. from android import activity
  29. from goblinoid.libs.toast.androidtoast import toast
  30. else:
  31. from goblinoid.libs.toast.kivytoast import toast
  32. Builder.load_file('goblinoid/media/mediaviews.kv')
  33. if platform() == 'android':
  34. PythonActivity = autoclass('org.renpy.android.PythonActivity')
  35. Intent = autoclass('android.content.Intent')
  36. Uri = autoclass('android.net.Uri')
  37. MediaStore_Images_Media_DATA = "_data"
  38. RESULT_LOAD_IMAGE = 1
  39. Activity = autoclass('android.app.Activity')
  40. def open_image_gallery_activity(callback, on_cancel_callback):
  41. currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
  42. def on_activity_result(request_code, result_code, intent):
  43. if request_code != RESULT_LOAD_IMAGE:
  44. Logger.warning('ignoring activity result')
  45. return
  46. if result_code == Activity.RESULT_CANCELED:
  47. Logger.warning('RESULT_CANCELLED')
  48. on_cancel_callback()
  49. return
  50. if result_code != Activity.RESULT_OK:
  51. raise NotImplementedError('UNKNOWN RESULT CODE')
  52. selectedImage = intent.getData()
  53. filePathColumn = [MediaStore_Images_Media_DATA]
  54. cursor = currentActivity.getContentResolver().query(
  55. selectedImage, filePathColumn, None, None, None)
  56. cursor.moveToFirst()
  57. columnIndex = cursor.getColumnIndex(filePathColumn[0])
  58. picturePath = cursor.getString(columnIndex)
  59. cursor.close()
  60. Logger.info('android_ui: user_select_image() selected %s', picturePath)
  61. Clock.schedule_once(lambda dt: callback(picturePath), 0)
  62. activity.bind(on_activity_result=on_activity_result)
  63. intent = Intent()
  64. intent.setAction(Intent.ACTION_PICK)
  65. intent.setData(Uri.parse('content://media/internal/images/media'))
  66. currentActivity.startActivityForResult(intent, RESULT_LOAD_IMAGE)
  67. # TODO: pass a reference to the ActionView instance so we don't have to call
  68. # parent.parent.parent
  69. class UploadView(GoblinScreen):
  70. upload_image = StringProperty('')
  71. upload_thread = ObjectProperty()
  72. cancel = BooleanProperty(False)
  73. def __init__(self, **kwargs):
  74. super(UploadView, self).__init__(transition_priority=0, **kwargs)
  75. if platform == 'android':
  76. self.init_upload_image()
  77. def init_upload_image(self):
  78. open_image_gallery_activity(
  79. self.set_upload_image, self.cancel_callback)
  80. def set_upload_image(self, imageUri):
  81. self.upload_image = imageUri
  82. def upload_media(self):
  83. title = self.ids.title.text
  84. description = self.ids.desc.text
  85. toast('media uploading...', True)
  86. self.upload_thread = UploadImage(
  87. self.upload_image,
  88. title,
  89. description,
  90. self.upload_complete)
  91. self.upload_thread.start()
  92. self.cancel_callback()
  93. def on_upload_thread(self, instance, value):
  94. print ('upload thread changed state!')
  95. def cancel_callback(self):
  96. self.cancel = True
  97. @mainthread
  98. def upload_complete(self, image_uri):
  99. toast('upload complete', True)
  100. class UploadImage(threading.Thread):
  101. def __init__(self, image_uri, title, description, callback, **kwargs):
  102. threading.Thread.__init__(self)
  103. self.image_uri = image_uri
  104. self.title = title
  105. self.description = description
  106. self.callback = callback
  107. def run(self):
  108. try:
  109. pump = App.get_running_app().pump_service.pump
  110. image = pump.Image(display_name=self.title)
  111. image.content = self.description
  112. image.from_file(self.image_uri)
  113. # callback is causing issues right now, going to forgo for a moment
  114. # self.callback(self.image_uri)
  115. except PyPumpException:
  116. print ('failed to upload image due to PyPumpException')
  117. pass
  118. self.is_running = False