-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
FilePickerWritableImpl.kt
359 lines (319 loc) · 10.3 KB
/
FilePickerWritableImpl.kt
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
353
354
355
356
357
358
359
package codeux.design.filepicker.file_picker_writable
import android.app.Activity
import android.app.Activity.RESULT_OK
import android.content.ActivityNotFoundException
import android.content.ContentResolver
import android.content.Intent
import android.database.Cursor
import android.net.Uri
import android.provider.OpenableColumns
import androidx.annotation.MainThread
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
interface ActivityProvider : CoroutineScope {
val activity: Activity?
fun logDebug(message: String, e: Throwable? = null)
@MainThread
fun openFile(fileInfo: Map<String, String>)
@MainThread
fun handleOpenUri(uri: Uri)
}
class FilePickerWritableImpl(
private val plugin: ActivityProvider
) : PluginRegistry.ActivityResultListener, PluginRegistry.NewIntentListener {
companion object {
const val REQUEST_CODE_OPEN_FILE = 40832
const val REQUEST_CODE_CREATE_FILE = 40833
}
private var filePickerCreateFile: File? = null
private var filePickerResult: MethodChannel.Result? = null
private var isInitialized = false
private var initOpenUrl: Uri? = null
@MainThread
fun openFilePicker(result: MethodChannel.Result) {
if (filePickerResult != null) {
throw FilePickerException("Invalid lifecycle, only one call at a time.")
}
filePickerResult = result
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
}
val activity = requireActivity()
try {
activity.startActivityForResult(intent, REQUEST_CODE_OPEN_FILE)
} catch (e: ActivityNotFoundException) {
filePickerResult = null
plugin.logDebug("exception while launcing file picker", e)
result.error(
"FilePickerNotAvailable",
"Unable to start file picker, $e",
null
)
}
}
@MainThread
fun openFilePickerForCreate(result: MethodChannel.Result, path: String) {
if (filePickerResult != null) {
throw FilePickerException("Invalid lifecycle, only one call at a time.")
}
val file = File(path)
filePickerResult = result
filePickerCreateFile = file
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
// type = "application/x-keepass"
type = "*/*"
putExtra(Intent.EXTRA_TITLE, file.name)
}
val activity = requireActivity()
try {
activity.startActivityForResult(intent, REQUEST_CODE_CREATE_FILE)
} catch (e: ActivityNotFoundException) {
filePickerResult = null
plugin.logDebug("exception while launcing file picker", e)
result.error(
"FilePickerNotAvailable",
"Unable to start file picker, $e",
null
)
}
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
): Boolean {
if (!arrayOf(REQUEST_CODE_OPEN_FILE, REQUEST_CODE_CREATE_FILE).contains(
requestCode
)) {
plugin.logDebug("Unknown requestCode $requestCode - ignore")
return false
}
val result = filePickerResult ?: return false.also {
plugin.logDebug("We have no active result, so activity result was not for us.")
}
filePickerResult = null
plugin.logDebug("onActivityResult($requestCode, $resultCode, ${data?.data})")
if (resultCode == Activity.RESULT_CANCELED) {
plugin.logDebug("Activity result was canceled.")
result.success(null)
return true
} else if (resultCode != RESULT_OK) {
result.error(
"InvalidResult",
"Got invalid result $resultCode",
null
)
return true
}
try {
return when (requestCode) {
REQUEST_CODE_OPEN_FILE -> {
val fileUri = data?.data
if (fileUri != null) {
plugin.logDebug("Got result $fileUri")
plugin.launch {
handleFileUriResponse(result, fileUri)
}
} else {
plugin.logDebug("Got RESULT_OK with null fileUri?")
result.success(null)
}
true
}
REQUEST_CODE_CREATE_FILE -> {
val initialFileContent = filePickerCreateFile
?: throw FilePickerException("illegal state - filePickerCreateFile was nul")
val fileUri =
requireNotNull(data?.data) { "RESULT_OK with null file uri $data" }
plugin.logDebug("Got result $fileUri")
plugin.launch {
handleFileUriCreateResponse(
result,
fileUri,
initialFileContent
)
}
true
}
else -> {
// can never happen, we already checked the result code.
throw IllegalStateException("Unexpected requestCode $requestCode")
}
}
} catch (e: Exception) {
plugin.logDebug("Error during handling file packer result.", e)
result.error(
"FatalError",
"Error handling file picker callback. $e",
null
)
return true
}
}
@MainThread
private suspend fun handleFileUriCreateResponse(
result: MethodChannel.Result,
fileUri: Uri,
initialFileContent: File
) {
val activity = requireActivity()
val contentResolver = activity.applicationContext.contentResolver
val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.takePersistableUriPermission(fileUri, takeFlags)
writeFileWithIdentifier(result, fileUri.toString(), initialFileContent)
}
@MainThread
private suspend fun handleFileUriResponse(
result: MethodChannel.Result,
fileUri: Uri
) {
copyContentUriAndReturn(result, fileUri)
}
@MainThread
suspend fun readFileWithIdentifier(
result: MethodChannel.Result,
identifier: String
) {
copyContentUriAndReturn(result, Uri.parse(identifier))
}
@MainThread
private suspend fun copyContentUriAndReturn(
result: MethodChannel.Result,
fileUri: Uri
) {
result.success(
copyContentUriAndReturnFileInfo(fileUri)
)
}
@MainThread
private suspend fun copyContentUriAndReturnFileInfo(fileUri: Uri): Map<String, String> {
val activity = requireActivity()
val contentResolver = activity.applicationContext.contentResolver
return withContext(Dispatchers.IO) {
val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.takePersistableUriPermission(fileUri, takeFlags)
val fileName = readFileInfo(fileUri, contentResolver)
val tempFile = File.createTempFile(fileName, null, activity.cacheDir)
plugin.logDebug("Copy file $fileUri to $tempFile")
contentResolver.openInputStream(fileUri).use { input ->
requireNotNull(input)
tempFile.outputStream().use { output ->
input.copyTo(output)
}
}
mapOf(
"path" to tempFile.absolutePath,
"identifier" to fileUri.toString(),
"fileName" to fileName,
"uri" to fileUri.toString()
)
}
}
private suspend fun readFileInfo(
uri: Uri,
contentResolver: ContentResolver
): String = withContext(Dispatchers.IO) {
// The query, because it only applies to a single document, returns only
// one row. There's no need to filter, sort, or select fields,
// because we want all fields for one document.
val cursor: Cursor? = contentResolver.query(
uri, null, null, null, null, null
)
cursor?.use {
if (!it.moveToFirst()) {
throw FilePickerException("Cursor returned empty while trying to read file info for $uri")
}
// Note it's called "Display Name". This is
// provider-specific, and might not necessarily be the file name.
val displayName: String =
it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME))
plugin.logDebug("Display Name: $displayName")
displayName
} ?: throw FilePickerException("Unable to load file info from $uri")
}
fun onDetachedFromActivity(binding: ActivityPluginBinding) {
binding.removeActivityResultListener(this)
}
fun onAttachedToActivity(binding: ActivityPluginBinding) {
binding.addActivityResultListener(this)
binding.addOnNewIntentListener(this)
onNewIntent(binding.activity.intent)
}
@MainThread
suspend fun writeFileWithIdentifier(
result: MethodChannel.Result,
identifier: String,
file: File
) {
if (!file.exists()) {
throw FilePickerException("File at source not found. $file")
}
val fileUri = Uri.parse(identifier)
val activity = requireActivity()
val contentResolver = activity.contentResolver
withContext(Dispatchers.IO) {
contentResolver.openOutputStream(fileUri, "w").use { output ->
require(output != null)
file.inputStream().use { input ->
input.copyTo(output)
}
}
}
copyContentUriAndReturn(result, fileUri)
}
private fun requireActivity() = (plugin.activity
?: throw FilePickerException("Illegal state, expected activity to be there."))
private val CONTENT_PROVIDER_SCHEMES = setOf(
ContentResolver.SCHEME_CONTENT,
ContentResolver.SCHEME_FILE,
ContentResolver.SCHEME_ANDROID_RESOURCE
)
override fun onNewIntent(intent: Intent?): Boolean {
val data = intent?.data
val scheme = data?.scheme
plugin.logDebug("onNewIntent($data)")
if (data == null) {
return false
}
// if (scheme == null || !CONTENT_PROVIDER_SCHEMES.contains(scheme)) {
// plugin.logDebug("Not handling url $data (no supported scheme $CONTENT_PROVIDER_SCHEMES)")
// return false
// }
plugin.launch {
if (isInitialized) {
handleUri(data)
} else {
initOpenUrl = data
}
}
return true
}
@MainThread
suspend fun init() {
isInitialized = true
initOpenUrl?.let { uri ->
handleUri(uri)
}
initOpenUrl = null
}
@MainThread
private suspend fun handleUri(uri: Uri) {
val scheme = uri.scheme ?: return
val isFile = CONTENT_PROVIDER_SCHEMES.contains(scheme)
if (isFile) {
plugin.openFile(copyContentUriAndReturnFileInfo(uri))
} else {
plugin.handleOpenUri(uri)
}
}
}