This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExternalStorage.bas
154 lines (138 loc) · 5.11 KB
/
ExternalStorage.bas
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
B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=9.3
@EndOfDesignText@
'version 1.02
#Event: ExternalFolderAvailable
Sub Class_Globals
Private ion As Object
Private PersistantUri As String
Private const FileName As String = "PersistantUri"
Private ctxt As JavaObject
Private mCallback As Object
Private mEventName As String
Public Root As ExternalFile
Type ExternalFile (Name As String, Length As Long, LastModified As Long, IsFolder As Boolean, Native As JavaObject)
End Sub
Public Sub Initialize (Callback As Object, EventName As String)
mCallback = Callback
mEventName = EventName
ctxt.InitializeContext
End Sub
'Lets the user pick a folder.
'Optionally using the previous selected folder.
Public Sub SelectDir (UsePreviouslySelectedIfAvaiable As Boolean)
If UsePreviouslySelectedIfAvaiable And File.Exists(File.DirInternal, FileName) Then
PersistantUri = File.ReadString(File.DirInternal, FileName)
Dim list As List = ctxt.RunMethodJO("getContentResolver", Null).RunMethod("getPersistedUriPermissions", Null)
If list.IsInitialized Then
For Each uripermission As JavaObject In list
Dim u As Uri = uripermission.RunMethod("getUri", Null)
Dim temp As Object = u
Dim s As String = temp
If s = PersistantUri And uripermission.RunMethod("isWritePermission", Null) = True Then
Log("Can use persistant uri!")
SetPickedDir
Return
End If
Next
End If
End If
Dim i As Intent
i.Initialize("android.intent.action.OPEN_DOCUMENT_TREE", "")
i.PutExtra("android.content.extra.SHOW_ADVANCED", True)
StartActivityForResult(i)
End Sub
'List all files in the given folder.
Public Sub ListFiles (Folder As ExternalFile) As List
Dim files() As Object = Folder.Native.RunMethod("listFiles", Null)
Dim res As List
res.Initialize
For Each o As Object In files
Dim f As JavaObject = o
res.Add(DocumentFileToExternalFile(f))
Next
Return res
End Sub
'Finds the file with the given name.
'Returns an uninitialized ExternalFile if not found.
Public Sub FindFile (Parent As ExternalFile, Name As String) As ExternalFile
Dim f As JavaObject = Parent.Native.RunMethod("findFile", Array(Name))
Return DocumentFileToExternalFile(f)
End Sub
'Creates a new file.
Public Sub CreateNewFile (Parent As ExternalFile, Name As String) As ExternalFile
Return DocumentFileToExternalFile(Parent.Native.RunMethod("createFile", Array("", Name)))
End Sub
'Deletes the file.
Public Sub DeleteFile (EF As ExternalFile) As Boolean
If EF.IsInitialized = False Then Return False
Return EF.Native.RunMethod("delete", Null)
End Sub
'Open an output stream that writes to the file.
Public Sub OpenOutputStream(EF As ExternalFile) As OutputStream
Return ctxt.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(EF.Native.RunMethod("getUri", Null)))
End Sub
'Open an input stream that reads from the file.
Public Sub OpenInputStream(EF As ExternalFile) As InputStream
Return ctxt.RunMethodJO("getContentResolver", Null).RunMethod("openInputStream", Array(EF.Native.RunMethod("getUri", Null)))
End Sub
'Finds the file with the given name. If not found creates a new file.
Public Sub FindFileOrCreate (Parent As ExternalFile, Name As String) As ExternalFile
Dim f As ExternalFile = FindFile(Parent, Name)
If f.IsInitialized = False Then
Return CreateNewFile(Parent, Name)
Else
Return f
End If
End Sub
Private Sub DocumentFileToExternalFile (DocumentFile As JavaObject) As ExternalFile
Dim ef As ExternalFile
If DocumentFile.IsInitialized = False Then
Return ef
End If
ef.Initialize
ef.Name = DocumentFile.RunMethod("getName", Null)
ef.Length = DocumentFile.RunMethod("length", Null)
ef.IsFolder = DocumentFile.RunMethod("isDirectory", Null)
ef.Native = DocumentFile
ef.LastModified = DocumentFile.RunMethod("lastModified", Null)
Return ef
End Sub
Private Sub SetPickedDir
Root = DocumentFileToExternalFile(GetPickedDir(PersistantUri))
CallSubDelayed(mCallback, mEventName & "_ExternalFolderAvailable")
End Sub
Private Sub ion_Event (MethodName As String, Args() As Object) As Object
If Args(0) = -1 Then 'resultCode = RESULT_OK
Dim i As Intent = Args(1)
Dim jo As JavaObject = i
Dim treeUri As Uri = jo.RunMethod("getData", Null)
Dim takeFlags As Int = Bit.And(i.Flags, 3)
ctxt.RunMethodJO("getContentResolver", Null).RunMethod("takePersistableUriPermission", Array(treeUri, takeFlags))
Dim temp As Object = treeUri
PersistantUri = temp
File.WriteString(File.DirInternal, FileName, PersistantUri)
Log(PersistantUri)
SetPickedDir
End If
Return Null
End Sub
Private Sub GetPickedDir (uri As String) As JavaObject
Dim DocumentFileStatic As JavaObject
Dim treeUri As Uri
treeUri.Parse(uri)
Dim PickedDir As JavaObject = DocumentFileStatic.InitializeStatic("android.support.v4.provider.DocumentFile").RunMethod("fromTreeUri", Array(ctxt, treeUri))
Return PickedDir
End Sub
Private Sub StartActivityForResult(i As Intent)
Dim jo As JavaObject = GetBA
ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub
Private Sub GetBA As Object
Dim jo As JavaObject = Me
Return jo.RunMethod("getBA", Null)
End Sub