This repository was archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
213 lines (184 loc) · 9.42 KB
/
main.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
from kivy.app import App
from kivy.clock import mainthread
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.logger import Logger
from os.path import exists, join
from shutil import rmtree
from textwrap import fill
from android import mActivity, autoclass, api_version
from androidstorage4kivy import SharedStorage, Chooser
from android_permissions import AndroidPermissions
Environment = autoclass('android.os.Environment')
#######################################################################
# Not a real world example, this is the unit tests.
#
# One test (8) depends on Pictures/CHART_4.jpg belonging to another app
# Expect this test to likely fail on some random device.
#######################################################################
class SharedStorageExample(App):
def build(self):
Window.bind(on_keyboard = self.quit_app)
# create chooser listener
self.chooser = Chooser(self.chooser_callback)
# cleanup from last time if Android didn't
temp = SharedStorage().get_cache_dir()
if temp and exists(temp):
rmtree(temp)
# layout
self.label = Label(text = 'Greetings Earthlings')
self.button = Button(text = 'Choose an image file',
on_press = self.chooser_start,
size_hint=(1, .15))
self.layout = BoxLayout(orientation='vertical')
self.layout.add_widget(self.label)
self.layout.add_widget(self.button)
return self.layout
def on_start(self):
self.dont_gc = AndroidPermissions(self.start_app)
def quit_app(self,window,key,*args):
if key == 27:
mActivity.finishAndRemoveTask()
return True
else:
return False
def start_app(self):
self.dont_gc = None
ss = SharedStorage()
app_title = str(ss.get_app_title())
self.label_lines = []
self.display()
self.append("Cache Dir Exists: " + str(exists(ss.get_cache_dir())))
############################################################
# copy to app shared storage copy_to_shared(private_file)
############################################################
share0 = ss.copy_to_shared('./test.txt')
share1 = ss.copy_to_shared('test.txt',
filepath = 'a/b/test1.txt')
share2 = ss.copy_to_shared('test.jpg',
filepath = join('c','test1.jpg'))
share3 = ss.copy_to_shared('test.mp3')
# For .ogg, collection depends on the Android version
# On newer devices default is DIRECTORY_MUSIC, so DIRECTORY_ALARMS
# is a legal place.
# On older devices default is DIRECTORY_DOCUMENTS, so DIRECTORY_ALARMS
# is not legal and will go to the default, see test 'path4' below.
share4 = ss.copy_to_shared('test.ogg',
collection = Environment.DIRECTORY_ALARMS)
share5 = ss.copy_to_shared('test.mp4')
# Illegal collection names for .mp4 default to the automatic
# collection name DIRECTORY_MOVIES
share6 = ss.copy_to_shared('test.mp4',
collection = 'Video',
filepath = 'renamed.mp4')
share7 = ss.copy_to_shared('test.mp4',
collection = None,
filepath = 'newname.mp4')
################################################################
# copy from app shared storage copy_from_shared(shared_file)
################################################################
path0 = ss.copy_from_shared(share0)
path1 = ss.copy_from_shared(share1)
path2 = ss.copy_from_shared(join(Environment.DIRECTORY_PICTURES,
app_title, 'c', 'test1.jpg'))
path3 = ss.copy_from_shared(share3)
# See note above about .ogg
# Not certain the change is exactly Androoid 10, but close enough.
if api_version > 29:
path4 = ss.copy_from_shared(join(Environment.DIRECTORY_ALARMS,
app_title, 'test.ogg'))
else:
path4 = ss.copy_from_shared(join(Environment.DIRECTORY_DOCUMENTS,
app_title, 'test.ogg'))
path5 = ss.copy_from_shared(share5)
path6 = ss.copy_from_shared(join(Environment.DIRECTORY_MOVIES,
app_title, 'renamed.mp4'))
path7 = ss.copy_from_shared(share7)
###############################################################
# Delete from App Shared Storage delete_shared(shared_file)
###############################################################
# Keep two (share0, share1) to test persistence between app installs
del2 = ss.delete_shared(share2)
del3 = ss.delete_shared(share3)
del4 = ss.delete_shared(share4)
del5 = ss.delete_shared(join(Environment.DIRECTORY_MOVIES,
app_title, 'test.mp4'))
del6 = ss.delete_shared(share6)
del7 = ss.delete_shared(join(Environment.DIRECTORY_MOVIES,
app_title,'newname.mp4'))
#############################################################
# Copy file created by other app to this app then delete copy
#############################################################
# on my phones CHART_4.jpg exists
path8 = ss.copy_from_shared(join(Environment.DIRECTORY_PICTURES,
'CHART_4.jpg'))
share8 = ss.copy_to_shared(path8)
del8 = ss.delete_shared(share8)
#################################
# Report Results
#################################
self.append("copy_to_shared test.txt " + str(share0 != None))
self.append("copy_to_shared a/b/test.txt: " + str(share1 != None))
self.append("copy_to_shared c/test.jpg " + str(share2 != None))
self.append("copy_to_shared test.mp3: " + str(share3 != None))
self.append("copy_to_shared test.ogg: " + str(share4 != None))
self.append("copy_to_shared test.mp4: " + str(share5 != None))
self.append("copy_to_shared renamed.mp4: " + str(share6 != None))
self.append("copy_to_shared newname.mp4: " + str(share7 != None))
self.append("copy_from_shared test.txt " + str(path0 != None and\
exists(path0)))
self.append("copy_from_shared a/b/test.txt " + str(path1 != None and\
exists(path1)))
self.append("copy_from_shared c/test1.jpg " + str(path2 != None and\
exists(path2)))
self.append("copy_from_shared test.mp3 " + str(path3 != None and\
exists(path3)))
self.append("copy_from_shared test.ogg " + str(path4 != None and\
exists(path4)))
self.append("copy_from_shared test.mp4 " + str(path5 != None and\
exists(path5)))
self.append("copy_from_shared renamed.mp4 " + str(path6 != None and\
exists(path6)))
self.append("copy_from_shared newname.mp4 " + str(path7 != None and\
exists(path6)))
self.append("deleted c/test.jpg " + str(del2))
self.append("deleted test.mp3 " + str(del3))
self.append("deleted test.ogg " + str(del4))
self.append("deleted test.mp4 " + str(del5))
self.append("deleted renamed.mp4 " + str(del6))
self.append("deleted newname.mp4 " + str(del7))
self.append("copy_from_shared other app " + str(path8 != None and\
exists(path8)))
self.append("copy_to_shared this app " + str(share8 != None))
self.append("delete copy from other " + str(del8))
self.display()
# Chooser interface
def chooser_start(self,bt):
self.chooser.choose_content("image/*")
def chooser_callback(self,uri_list):
try:
ss = SharedStorage()
for uri in uri_list:
# copy to private
path = ss.copy_from_shared(uri)
if path:
# then to app shared
shared = ss.copy_to_shared(path)
self.append("Result copied to app shared "+\
str(exists(path) and shared != None))
self.display()
except Exception as e:
Logger.warning('SharedStorageExample.chooser_callback():')
Logger.warning(str(e))
# Label text
def append(self, name):
self.label_lines.append(name)
@mainthread
def display(self):
if self.label:
self.label.text = ''
for r in self.label_lines:
self.label.text += fill(r, 40) + '\n'
SharedStorageExample().run()