fixed crash when trying to create file while system file picker is missing
This commit is contained in:
parent
27f72327c4
commit
57a3385a7c
2 changed files with 39 additions and 29 deletions
|
@ -101,7 +101,17 @@ class ActivityResultStreamHandler(private val activity: Activity, arguments: Any
|
||||||
endOfStream()
|
endOfStream()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createFile() {
|
private suspend fun safeStartActivityForResult(intent: Intent, requestCode: Int, onGranted: (uri: Uri) -> Unit, onDenied: () -> Unit) {
|
||||||
|
if (intent.resolveActivity(activity.packageManager) != null) {
|
||||||
|
MainActivity.pendingStorageAccessResultHandlers[requestCode] = PendingStorageAccessResultHandler(null, onGranted, onDenied)
|
||||||
|
activity.startActivityForResult(intent, requestCode)
|
||||||
|
} else {
|
||||||
|
MainActivity.notifyError("failed to resolve activity for intent=$intent extras=${intent.extras}")
|
||||||
|
onDenied()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun createFile() {
|
||||||
@SuppressLint("ObsoleteSdkInt")
|
@SuppressLint("ObsoleteSdkInt")
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
|
||||||
error("createFile-sdk", "unsupported SDK version=${Build.VERSION.SDK_INT}", null)
|
error("createFile-sdk", "unsupported SDK version=${Build.VERSION.SDK_INT}", null)
|
||||||
|
@ -116,12 +126,7 @@ class ActivityResultStreamHandler(private val activity: Activity, arguments: Any
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
|
fun onGranted(uri: Uri) {
|
||||||
addCategory(Intent.CATEGORY_OPENABLE)
|
|
||||||
type = mimeType
|
|
||||||
putExtra(Intent.EXTRA_TITLE, name)
|
|
||||||
}
|
|
||||||
MainActivity.pendingStorageAccessResultHandlers[MainActivity.CREATE_FILE_REQUEST] = PendingStorageAccessResultHandler(null, { uri ->
|
|
||||||
ioScope.launch {
|
ioScope.launch {
|
||||||
try {
|
try {
|
||||||
// truncate is necessary when overwriting a longer file
|
// truncate is necessary when overwriting a longer file
|
||||||
|
@ -134,13 +139,20 @@ class ActivityResultStreamHandler(private val activity: Activity, arguments: Any
|
||||||
}
|
}
|
||||||
endOfStream()
|
endOfStream()
|
||||||
}
|
}
|
||||||
}, {
|
|
||||||
success(null)
|
|
||||||
endOfStream()
|
|
||||||
})
|
|
||||||
activity.startActivityForResult(intent, MainActivity.CREATE_FILE_REQUEST)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun onDenied() {
|
||||||
|
success(null)
|
||||||
|
endOfStream()
|
||||||
|
}
|
||||||
|
|
||||||
|
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
|
||||||
|
addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
|
type = mimeType
|
||||||
|
putExtra(Intent.EXTRA_TITLE, name)
|
||||||
|
}
|
||||||
|
safeStartActivityForResult(intent, MainActivity.CREATE_FILE_REQUEST, ::onGranted, ::onDenied)
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun openFile() {
|
private suspend fun openFile() {
|
||||||
@SuppressLint("ObsoleteSdkInt")
|
@SuppressLint("ObsoleteSdkInt")
|
||||||
|
@ -178,13 +190,7 @@ class ActivityResultStreamHandler(private val activity: Activity, arguments: Any
|
||||||
addCategory(Intent.CATEGORY_OPENABLE)
|
addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
setTypeAndNormalize(mimeType ?: MimeTypes.ANY)
|
setTypeAndNormalize(mimeType ?: MimeTypes.ANY)
|
||||||
}
|
}
|
||||||
if (intent.resolveActivity(activity.packageManager) != null) {
|
safeStartActivityForResult(intent, MainActivity.OPEN_FILE_REQUEST, ::onGranted, ::onDenied)
|
||||||
MainActivity.pendingStorageAccessResultHandlers[MainActivity.OPEN_FILE_REQUEST] = PendingStorageAccessResultHandler(null, ::onGranted, ::onDenied)
|
|
||||||
activity.startActivityForResult(intent, MainActivity.OPEN_FILE_REQUEST)
|
|
||||||
} else {
|
|
||||||
MainActivity.notifyError("failed to resolve activity for intent=$intent extras=${intent.extras}")
|
|
||||||
onDenied()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun pickCollectionFilters() {
|
private fun pickCollectionFilters() {
|
||||||
|
|
|
@ -71,17 +71,7 @@ mixin PermissionAwareMixin {
|
||||||
// abort if the user cancels in Flutter
|
// abort if the user cancels in Flutter
|
||||||
if (confirmed == null || !confirmed) return false;
|
if (confirmed == null || !confirmed) return false;
|
||||||
|
|
||||||
if (!await deviceService.isSystemFilePickerEnabled()) {
|
if (!await _checkSystemFilePickerEnabled(context)) return false;
|
||||||
await showDialog(
|
|
||||||
context: context,
|
|
||||||
builder: (context) => AvesDialog(
|
|
||||||
content: Text(context.l10n.missingSystemFilePickerDialogMessage),
|
|
||||||
actions: const [OkButton()],
|
|
||||||
),
|
|
||||||
routeSettings: const RouteSettings(name: AvesDialog.warningRouteName),
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
final granted = await storageService.requestDirectoryAccess(dir.dirPath);
|
final granted = await storageService.requestDirectoryAccess(dir.dirPath);
|
||||||
if (!granted) {
|
if (!granted) {
|
||||||
|
@ -105,4 +95,18 @@ mixin PermissionAwareMixin {
|
||||||
routeSettings: const RouteSettings(name: AvesDialog.warningRouteName),
|
routeSettings: const RouteSettings(name: AvesDialog.warningRouteName),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<bool> _checkSystemFilePickerEnabled(BuildContext context) async {
|
||||||
|
if (await deviceService.isSystemFilePickerEnabled()) return true;
|
||||||
|
|
||||||
|
await showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => AvesDialog(
|
||||||
|
content: Text(context.l10n.missingSystemFilePickerDialogMessage),
|
||||||
|
actions: const [OkButton()],
|
||||||
|
),
|
||||||
|
routeSettings: const RouteSettings(name: AvesDialog.warningRouteName),
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue