Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add openDocument #110

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ internal class DocumentFileApi(private val plugin: SharedStoragePlugin) :
result.notSupported(call.method, API_21)
}
}
OPEN_DOCUMENT ->
if (Build.VERSION.SDK_INT >= API_21) {
openDocument(call, result)
}
OPEN_DOCUMENT_TREE ->
if (Build.VERSION.SDK_INT >= API_21) {
openDocumentTree(call, result)
Expand Down Expand Up @@ -250,6 +254,33 @@ internal class DocumentFileApi(private val plugin: SharedStoragePlugin) :
}
}

@RequiresApi(API_21)
private fun openDocument(call: MethodCall, result: MethodChannel.Result) {

val initialUri = call.argument<String>("initialUri")

val intent =
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)

if (initialUri != null) {
val tree = DocumentFile.fromTreeUri(plugin.context, Uri.parse(initialUri))
if (Build.VERSION.SDK_INT >= API_26) {
putExtra(DocumentsContract.EXTRA_INITIAL_URI, tree?.uri)
}
}

type = call.argument<String>("mimeType") ?: "*/*"
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, call.argument<Boolean>("multiple") ?: false)
}

if (pendingResults[OPEN_DOCUMENT_CODE] != null) return

pendingResults[OPEN_DOCUMENT_CODE] = Pair(call, result)

plugin.binding?.activity?.startActivityForResult(intent, OPEN_DOCUMENT_CODE)
}

@RequiresApi(API_21)
private fun openDocumentTree(call: MethodCall, result: MethodChannel.Result) {
val grantWritePermission = call.argument<Boolean>("grantWritePermission")!!
Expand Down Expand Up @@ -394,6 +425,26 @@ internal class DocumentFileApi(private val plugin: SharedStoragePlugin) :
pendingResults.remove(OPEN_DOCUMENT_TREE_CODE)
}
}
OPEN_DOCUMENT_CODE -> {
val pendingResult = pendingResults[OPEN_DOCUMENT_CODE] ?: return false

try {
// if data.clipData not null, uriList from data.clipData, else uriList is data.data
val uriList = data?.clipData?.let {
(0 until it.itemCount).map { i -> it.getItemAt(i).uri }
} ?: data?.data?.let { listOf(it) }

if (uriList != null) {
pendingResult.second.success(uriList.map { "$it" })

return true
}

pendingResult.second.success(null)
} finally {
pendingResults.remove(OPEN_DOCUMENT_CODE)
}
}
}

return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const val DOCUMENTS_CONTRACT_EXTRA_INITIAL_URI =
/**
* Available DocumentFile Method Channel APIs
*/
const val OPEN_DOCUMENT = "openDocument"
const val OPEN_DOCUMENT_TREE = "openDocumentTree"
const val PERSISTED_URI_PERMISSIONS = "persistedUriPermissions"
const val RELEASE_PERSISTABLE_URI_PERMISSION = "releasePersistableUriPermission"
Expand Down Expand Up @@ -54,3 +55,4 @@ const val GET_DOCUMENT_CONTENT = "getDocumentContent"
* Intent Request Codes
*/
const val OPEN_DOCUMENT_TREE_CODE = 10
const val OPEN_DOCUMENT_CODE = 11
20 changes: 20 additions & 0 deletions lib/src/saf/saf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ Future<Uri?> openDocumentTree({
return selectedDirectoryUri?.apply((e) => Uri.parse(e));
}

/// [Refer to details](https://developer.android.com/reference/android/content/Intent#ACTION_OPEN_DOCUMENT).
Future<List<Uri>?> openDocument({
Uri? initialUri,
String mimeType = '*/*',
bool multiple = false,
}) async {
const kOpenDocument = 'openDocument';

final args = <String, dynamic>{
if (initialUri != null) 'initialUri': '$initialUri',
'mimeType': mimeType,
'multiple': multiple,
};

final selectedUriList =
await kDocumentFileChannel.invokeListMethod(kOpenDocument, args);

return selectedUriList?.apply((e) => e.map((e) => Uri.parse(e as String)).toList());
}

/// {@template sharedstorage.saf.persistedUriPermissions}
/// Returns an `List<Uri>` with all persisted [Uri]
///
Expand Down