comparison libervia/desktop_kivy/plugins/plugin_transfer_android_gallery.py @ 493:b3cedbee561d

refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 18:26:16 +0200
parents cagou/plugins/plugin_transfer_android_gallery.py@3c9ba4a694ef
children
comparison
equal deleted inserted replaced
492:5114bbb5daa3 493:b3cedbee561d
1 #!/usr/bin/env python3
2
3
4 #Libervia Desktop-Kivy
5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 from libervia.backend.core import log as logging
22 log = logging.getLogger(__name__)
23 from libervia.backend.core.i18n import _
24 import sys
25 import tempfile
26 import os
27 import os.path
28 if sys.platform=="android":
29 from jnius import autoclass
30 from android import activity, mActivity
31
32 Intent = autoclass('android.content.Intent')
33 OpenableColumns = autoclass('android.provider.OpenableColumns')
34 PHOTO_GALLERY = 1
35 RESULT_OK = -1
36
37
38
39 PLUGIN_INFO = {
40 "name": _("gallery"),
41 "main": "AndroidGallery",
42 "platforms": ('android',),
43 "external": True,
44 "description": _("upload a photo from photo gallery"),
45 "icon_medium": "{media}/icons/muchoslava/png/gallery_50.png",
46 }
47
48
49 class AndroidGallery:
50
51 def __init__(self, callback, cancel_cb):
52 self.callback = callback
53 self.cancel_cb = cancel_cb
54 activity.bind(on_activity_result=self.on_activity_result)
55 intent = Intent()
56 intent.setType('image/*')
57 intent.setAction(Intent.ACTION_GET_CONTENT)
58 mActivity.startActivityForResult(intent, PHOTO_GALLERY);
59
60 def on_activity_result(self, requestCode, resultCode, data):
61 activity.unbind(on_activity_result=self.on_activity_result)
62 # TODO: move file dump to a thread or use async callbacks during file writting
63 if requestCode == PHOTO_GALLERY and resultCode == RESULT_OK:
64 if data is None:
65 log.warning("No data found in activity result")
66 self.cancel_cb(self, None)
67 return
68 uri = data.getData()
69
70 # we get filename in the way explained at https://developer.android.com/training/secure-file-sharing/retrieve-info.html
71 cursor = mActivity.getContentResolver().query(uri, None, None, None, None )
72 name_idx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
73 cursor.moveToFirst()
74 filename = cursor.getString(name_idx)
75
76 # we save data in a temporary file that we send to callback
77 # the file will be removed once upload is done (or if an error happens)
78 input_stream = mActivity.getContentResolver().openInputStream(uri)
79 tmp_dir = tempfile.mkdtemp()
80 tmp_file = os.path.join(tmp_dir, filename)
81 def cleaning():
82 os.unlink(tmp_file)
83 os.rmdir(tmp_dir)
84 log.debug('temporary file cleaned')
85 buff = bytearray(4096)
86 with open(tmp_file, 'wb') as f:
87 while True:
88 ret = input_stream.read(buff, 0, 4096)
89 if ret != -1:
90 f.write(buff)
91 else:
92 break
93 input_stream.close()
94 self.callback(tmp_file, cleaning)
95 else:
96 self.cancel_cb(self, None)