Mercurial > libervia-desktop-kivy
comparison src/cagou/plugins/plugin_upload_android_gallery.py @ 88:3dc526bb4a5a
upload: plugin android gallery, first draft:
- added "external" key in PLUGIN_INFO which indicate that a plugin doesn't create a widget when set to True
- callback and cancel_cb method in UploadMenu now use a cleaning_cb arguments which can be None. If set, the callback will be called without argument once the file is uploaded
- the AndroidGallery plugin looks for images on Android, then save the content to a temporary file an upload it, then clean the file. This plugin is only active on android platform.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 25 Dec 2016 22:53:50 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
87:17094a075fd2 | 88:3dc526bb4a5a |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
5 # Copyright (C) 2016 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"photo"), | |
41 "main": "AndroidGallery", | |
42 "platforms": ('android',), | |
43 "external": True, | |
44 "description": _(u"upload a photo from photo gallery"), | |
45 } | |
46 | |
47 | |
48 class AndroidGallery(object): | |
49 | |
50 def __init__(self, callback, cancel_cb): | |
51 self.callback = callback | |
52 self.cancel_cb = cancel_cb | |
53 activity.bind(on_activity_result=self.on_activity_result) | |
54 intent = Intent() | |
55 intent.setType('image/*') | |
56 intent.setAction(Intent.ACTION_GET_CONTENT) | |
57 mActivity.startActivityForResult(intent, PHOTO_GALLERY); | |
58 | |
59 def on_activity_result(self, requestCode, resultCode, data): | |
60 # TODO: move file dump to a thread or use async callbacks during file writting | |
61 if requestCode == PHOTO_GALLERY and resultCode == RESULT_OK: | |
62 if data is None: | |
63 log.warning(u"No data found in activity result") | |
64 self.cancel_cb(self, None) | |
65 return | |
66 uri = data.getData() | |
67 | |
68 # we get filename in the way explained at https://developer.android.com/training/secure-file-sharing/retrieve-info.html | |
69 cursor = mActivity.getContentResolver().query(uri, None, None, None, None ) | |
70 name_idx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) | |
71 cursor.moveToFirst() | |
72 filename = cursor.getString(name_idx) | |
73 | |
74 # we save data in a temporary file that we send to callback | |
75 # the file will be removed once upload is done (or if an error happens) | |
76 input_stream = mActivity.getContentResolver().openInputStream(uri) | |
77 tmp_dir = tempfile.mkdtemp() | |
78 tmp_file = os.path.join(tmp_dir, filename) | |
79 def cleaning(): | |
80 os.unlink(tmp_file) | |
81 os.rmdir(tmp_dir) | |
82 log.debug(u'temporary file cleaned') | |
83 buff = bytearray(4096) | |
84 with open(tmp_file, 'wb') as f: | |
85 while True: | |
86 ret = input_stream.read(buff, 0, 4096) | |
87 if ret != -1: | |
88 f.write(buff) | |
89 else: | |
90 break | |
91 input_stream.close() | |
92 self.callback(tmp_file, cleaning) | |
93 else: | |
94 self.cancel_cb(self, None) |