-
Notifications
You must be signed in to change notification settings - Fork 5
/
sharedstorage.py
359 lines (325 loc) · 13.3 KB
/
sharedstorage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from kivy.logger import Logger
from android import mActivity, autoclass, cast, api_version
from os.path import splitext,join, basename, exists
from os import mkdir, remove
from shutil import copyfile
FileOutputStream = autoclass('java.io.FileOutputStream')
FileInputStream = autoclass('java.io.FileInputStream')
Environment = autoclass('android.os.Environment')
MediaStoreMediaColumns = autoclass('android.provider.MediaStore$MediaColumns')
ContentUris = autoclass('android.content.ContentUris')
MimeTypeMap = autoclass('android.webkit.MimeTypeMap')
StreamCopy = None
if api_version > 28:
FileUtils = autoclass('android.os.FileUtils')
MediaStoreFiles = autoclass('android.provider.MediaStore$Files')
MediaStoreDownloads = autoclass('android.provider.MediaStore$Downloads')
MediaStoreAudioMedia = autoclass('android.provider.MediaStore$Audio$Media')
MediaStoreImagesMedia =autoclass('android.provider.MediaStore$Images$Media')
MediaStoreVideoMedia = autoclass('android.provider.MediaStore$Video$Media')
ContentValues = autoclass('android.content.ContentValues')
else:
try:
StreamCopy = autoclass('org.kivy.sharedstorage.StreamCopy')
except:
pass
# Source https://github.com/Android-for-Python/androidsatorage4kivy
class SharedStorage:
###################
# Public methods
###################
def copy_to_shared(self, private_file, collection = None, filepath = None):
if private_file == None or not exists(private_file):
return None
file_name = basename(private_file)
MIME_type = self.get_file_MIME_type(file_name)
auto_collection = self._get_auto_collection(MIME_type)
if not self._legal_collection(auto_collection, collection):
collection = auto_collection
path = [collection, self.get_app_title()]
if filepath:
sfp = filepath.split('/')
file_name = sfp[-1]
for f in sfp[:-1]:
path.append(f)
if api_version > 28:
sub_directory = ''
for d in path:
sub_directory = join(sub_directory,d)
uri = self._get_uri(join(sub_directory, file_name))
context = mActivity.getApplicationContext()
try:
cr = context.getContentResolver()
ws = None
if uri:
try:
ws = cr.openOutputStream(uri,"rwt")
except:
Logger.info('File replace permission not granted.')
Logger.info('A new file version will be created.')
uri = None
if not ws:
cv = ContentValues()
cv.put(MediaStoreMediaColumns.DISPLAY_NAME, file_name)
cv.put(MediaStoreMediaColumns.MIME_TYPE, MIME_type)
cv.put(MediaStoreMediaColumns.RELATIVE_PATH, sub_directory)
root_uri = self._get_root_uri(path[0], MIME_type)
uri = cr.insert(root_uri, cv)
ws = cr.openOutputStream(uri)
# copy file contents
rs = FileInputStream(private_file)
FileUtils.copy(rs,ws)
ws.flush()
ws.close()
rs.close()
except Exception as e:
Logger.warning('SharedStorage.copy_to_shared():')
Logger.warning(str(e))
uri = None
return uri
else:
root_directory = self._get_legacy_storage_location()
if root_directory == None:
return None
sub_directory = root_directory
for d in path:
sub_directory = join(sub_directory,d)
if not exists(sub_directory):
mkdir(sub_directory)
public_path = join(sub_directory, file_name)
self.delete_shared(public_path)
copyfile(private_file, public_path)
return public_path
def copy_from_shared(self, shared_file):
if shared_file == None:
return None
if api_version > 28:
uri = self._get_uri(shared_file)
cache_file = self._copy_uri_to_cache(uri)
else:
if type(shared_file) == str:
cache_file = self._copy_file_to_cache(shared_file)
else:
cache_file = self._copy_uri_to_cache(shared_file)
return cache_file
def delete_shared(self, shared_file):
if shared_file == None:
return False
if api_version > 28 or type(shared_file) == 'android.net.Uri':
uri = self._get_uri(shared_file)
if not uri:
return False
context = mActivity.getApplicationContext()
try:
return context.getContentResolver().delete(uri,None,None) == 1
except:
Logger.info('File delete permission not granted, ignored.')
return False
elif type(shared_file) == str:
path = join(self._get_legacy_storage_location(), shared_file)
if exists(path):
remove(path)
return True
return False
###################
# Public utilities
###################
def get_cache_dir(self):
context = mActivity.getApplicationContext()
result = context.getExternalCacheDir()
if not result:
return None
new_file_loc = str(result.toString())
if not new_file_loc:
return None
new_file_loc = join(new_file_loc,"FromSharedStorage")
if not exists(new_file_loc):
mkdir(new_file_loc)
return new_file_loc
def get_app_title(self):
context = mActivity.getApplicationContext()
appinfo = context.getApplicationInfo()
if appinfo.labelRes:
name = context.getString(appinfo.labelRes)
else:
name = appinfo.nonLocalizedLabel.toString()
return name
def get_file_MIME_type(self,file_name):
MIME_type = 'application/unknown'
try :
file_ext_no_dot = ''
file_ext = splitext(file_name)[1]
if file_ext:
file_ext_no_dot = file_ext[1:]
if file_ext_no_dot:
lower_ext = file_ext_no_dot.lower()
mtm = MimeTypeMap.getSingleton()
MIME_type = mtm.getMimeTypeFromExtension(lower_ext)
if not MIME_type:
MIME_type = 'application/' + file_ext_no_dot
except Exception as e:
Logger.warning('SharedStorage.get_file_MIME_type():')
Logger.warning(str(e))
return MIME_type
###################
# Private utilities
###################
def _get_auto_collection(self, MIME_type):
root, ext = MIME_type.split('/')
root = root.lower()
if root == 'image':
root_dir = Environment.DIRECTORY_PICTURES
elif root == 'video':
root_dir = Environment.DIRECTORY_MOVIES
elif root == 'audio':
root_dir = Environment.DIRECTORY_MUSIC
else:
root_dir = Environment.DIRECTORY_DOCUMENTS
return root_dir
def _get_root_uri(self, root_directory, MIME_type):
if root_directory == Environment.DIRECTORY_DOWNLOADS:
root_uri = MediaStoreDownloads.EXTERNAL_CONTENT_URI
else:
root, ext = MIME_type.split('/')
root = root.lower()
if root == 'image':
root_uri = MediaStoreImagesMedia.EXTERNAL_CONTENT_URI
elif root == 'video':
root_uri = MediaStoreVideoMedia.EXTERNAL_CONTENT_URI
elif root == 'audio':
root_uri = MediaStoreAudioMedia.EXTERNAL_CONTENT_URI
else:
root_uri = MediaStoreFiles.getContentUri('external')
return root_uri
def _get_uri(self, shared_file):
if type(shared_file) == str:
shared_file = shared_file
if 'file://' in shared_file or 'content://' in shared_file:
return None
else:
uri = cast('android.net.Uri',shared_file)
try:
if uri.getScheme().lower() == 'content':
return uri
else:
return None
except:
return None
file_name = basename(shared_file)
MIME_type = self.get_file_MIME_type(file_name)
path = shared_file.split('/')
if len(path) < 1:
return None
root = path[0]
self.selection = MediaStoreMediaColumns.DISPLAY_NAME+"=? AND "
if api_version > 28:
location = ''
for d in path[:-1]:
location = join(location,d)
self.selection = self.selection +\
MediaStoreMediaColumns.RELATIVE_PATH+"=?"
self.args = [file_name, location+'/']
else:
self.selection = self.selection + MediaStoreMediaColumns.DATA+"=?"
self.args = [file_name, shared_file]
root_uri = self._get_root_uri(root, MIME_type)
context = mActivity.getApplicationContext()
cursor = context.getContentResolver().query(root_uri, None,
self.selection,
self.args, None)
fileUri = None
if cursor:
while cursor.moveToNext():
dn = MediaStoreMediaColumns.DISPLAY_NAME
index = cursor.getColumnIndex(dn)
fileName = cursor.getString(index)
if file_name == fileName:
id_index = cursor.getColumnIndex(MediaStoreMediaColumns._ID)
id = cursor.getLong(id_index)
fileUri = ContentUris.withAppendedId(root_uri,id)
break
cursor.close()
return fileUri
def _copy_uri_to_cache(self, uri):
if not uri:
return uri
uri = cast('android.net.Uri',uri)
if uri.getScheme().lower() == 'file':
return self._copy_file_to_cache(uri.getPath())
context = mActivity.getApplicationContext()
cursor = context.getContentResolver().query(uri, None,
None, None, None)
if not cursor:
return None
dn = MediaStoreMediaColumns.DISPLAY_NAME
nameIndex = cursor.getColumnIndex(dn)
cursor.moveToFirst()
file_name = cursor.getString(nameIndex)
cache_dir = self.get_cache_dir()
if not cache_dir:
return None
cache_file= join(cache_dir, file_name)
if exists(cache_file):
remove(cache_file)
cr = context.getContentResolver()
try:
rs = cr.openInputStream(uri)
ws = FileOutputStream(cache_file)
if api_version > 28:
# fastest
FileUtils.copy(rs,ws)
elif StreamCopy:
# pretty fast
StreamCopy(rs, ws)
else:
# slow
jbytes = bytearray(2048)
while True:
num = rs.read(jbytes)
if num == -1:
break
ws.write(jbytes, 0, num)
ws.close()
rs.close()
except Exception as e:
Logger.warning('SharedStorage._copy_uri_to_cache():')
Logger.warning(str(e))
cursor.close()
return cache_file
def _copy_file_to_cache(self, shared_file):
path = join(self._get_legacy_storage_location(), shared_file)
cache_file = None
if exists(path):
cache_dir = self.get_cache_dir()
if not cache_dir:
return None
cache_file = join(cache_dir, basename(path))
copyfile(path, cache_file)
return cache_file
def _get_legacy_storage_location(self):
root = Environment.getExternalStorageDirectory()
root_dir = str(root.getAbsolutePath())
if exists(root_dir):
return root_dir
return None
def _legal_collection(self, auto_collection, collection):
# Too many rules, dudes.
if collection == None:
return False
elif collection == Environment.DIRECTORY_DOWNLOADS:
return True
elif auto_collection == Environment.DIRECTORY_MUSIC:
return collection in [Environment.DIRECTORY_ALARMS,
Environment.DIRECTORY_AUDIOBOOKS,
Environment.DIRECTORY_MUSIC,
Environment.DIRECTORY_NOTIFICATIONS,
Environment.DIRECTORY_PODCASTS,
Environment.DIRECTORY_RECORDINGS,
Environment.DIRECTORY_RINGTONES]
elif auto_collection == Environment.DIRECTORY_PICTURES:
return collection in [Environment.DIRECTORY_DCIM,
Environment.DIRECTORY_PICTURES,
Environment.DIRECTORY_SCREENSHOTS]
elif auto_collection == Environment.DIRECTORY_MOVIES:
return collection in [Environment.DIRECTORY_MOVIES]
return collection in [Environment.DIRECTORY_DOCUMENTS]