Mercurial > libervia-desktop-kivy
comparison cagou/plugins/plugin_transfer_android_gallery.py @ 126:cd99f70ea592
global file reorganisation:
- follow common convention by puttin cagou in "cagou" instead of "src/cagou"
- added VERSION in cagou with current version
- updated dates
- moved main executable in /bin
- moved buildozer files in root directory
- temporary moved platform to assets/platform
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 05 Apr 2018 17:11:21 +0200 |
parents | src/cagou/plugins/plugin_transfer_android_gallery.py@e89350dd3eca |
children | 1b835bcfa663 |
comparison
equal
deleted
inserted
replaced
125:b6e6afb0dc46 | 126:cd99f70ea592 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
5 # Copyright (C) 2016-2018 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 sat.core import log as logging | |
22 log = logging.getLogger(__name__) | |
23 from sat.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": _(u"gallery"), | |
41 "main": "AndroidGallery", | |
42 "platforms": ('android',), | |
43 "external": True, | |
44 "description": _(u"upload a photo from photo gallery"), | |
45 "icon_medium": u"{media}/icons/muchoslava/png/gallery_50.png", | |
46 } | |
47 | |
48 | |
49 class AndroidGallery(object): | |
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 # TODO: move file dump to a thread or use async callbacks during file writting | |
62 if requestCode == PHOTO_GALLERY and resultCode == RESULT_OK: | |
63 if data is None: | |
64 log.warning(u"No data found in activity result") | |
65 self.cancel_cb(self, None) | |
66 return | |
67 uri = data.getData() | |
68 | |
69 # we get filename in the way explained at https://developer.android.com/training/secure-file-sharing/retrieve-info.html | |
70 cursor = mActivity.getContentResolver().query(uri, None, None, None, None ) | |
71 name_idx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) | |
72 cursor.moveToFirst() | |
73 filename = cursor.getString(name_idx) | |
74 | |
75 # we save data in a temporary file that we send to callback | |
76 # the file will be removed once upload is done (or if an error happens) | |
77 input_stream = mActivity.getContentResolver().openInputStream(uri) | |
78 tmp_dir = tempfile.mkdtemp() | |
79 tmp_file = os.path.join(tmp_dir, filename) | |
80 def cleaning(): | |
81 os.unlink(tmp_file) | |
82 os.rmdir(tmp_dir) | |
83 log.debug(u'temporary file cleaned') | |
84 buff = bytearray(4096) | |
85 with open(tmp_file, 'wb') as f: | |
86 while True: | |
87 ret = input_stream.read(buff, 0, 4096) | |
88 if ret != -1: | |
89 f.write(buff) | |
90 else: | |
91 break | |
92 input_stream.close() | |
93 self.callback(tmp_file, cleaning) | |
94 else: | |
95 self.cancel_cb(self, None) |