diff --git a/android/build.gradle b/android/build.gradle index 8ad2194..7f8e851 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -28,12 +28,27 @@ apply plugin: 'kotlin-android' android { compileSdkVersion 33 + if (project.android.hasProperty('namespace')) { + namespace 'io.alexrintt.sharedstorage' + } + + sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { minSdkVersion 19 } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_18 + targetCompatibility JavaVersion.VERSION_18 + } + + kotlinOptions { + jvmTarget = "18" // Explicitly match Java's version + } + } dependencies { diff --git a/android/gradle.properties b/android/gradle.properties index 94adc3a..842dce9 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,3 +1,4 @@ org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true +kotlin.jvm.target.validation.mode = IGNORE \ No newline at end of file diff --git a/android/src/main/kotlin/io/alexrintt/sharedstorage/deprecated/DocumentFileApi.kt b/android/src/main/kotlin/io/alexrintt/sharedstorage/deprecated/DocumentFileApi.kt index fd17f6f..e29cd11 100644 --- a/android/src/main/kotlin/io/alexrintt/sharedstorage/deprecated/DocumentFileApi.kt +++ b/android/src/main/kotlin/io/alexrintt/sharedstorage/deprecated/DocumentFileApi.kt @@ -289,10 +289,23 @@ internal class DocumentFileApi(private val plugin: SharedStoragePlugin) : result.notSupported(CHILD, API_21, mapOf("uri" to uri)) } } + CAN_OPEN_DOCUMENT_TREE -> { + result.success(canOpenDocumentTree()) + } else -> result.notImplemented() } } + private fun canOpenDocumentTree(): Boolean { + return try { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + val activities = plugin.context.packageManager.queryIntentActivities(intent, 0) + activities.isNotEmpty() + } catch (e: Exception) { + false + } + } + @RequiresApi(API_21) private fun openDocument(call: MethodCall, result: MethodChannel.Result) { val initialUri = call.argument("initialUri") diff --git a/android/src/main/kotlin/io/alexrintt/sharedstorage/deprecated/lib/StorageAccessFrameworkConstant.kt b/android/src/main/kotlin/io/alexrintt/sharedstorage/deprecated/lib/StorageAccessFrameworkConstant.kt index 62af0c2..0fa9b62 100644 --- a/android/src/main/kotlin/io/alexrintt/sharedstorage/deprecated/lib/StorageAccessFrameworkConstant.kt +++ b/android/src/main/kotlin/io/alexrintt/sharedstorage/deprecated/lib/StorageAccessFrameworkConstant.kt @@ -39,6 +39,7 @@ const val COPY = "copy" const val LAST_MODIFIED = "lastModified" const val GET_DOCUMENT_THUMBNAIL = "getDocumentThumbnail" const val CHILD = "child" +const val CAN_OPEN_DOCUMENT_TREE = "canOpenDocumentTree" /** * Available DocumentFileHelper Method Channel APIs diff --git a/lib/src/common/functional_extender.dart b/lib/src/common/functional_extender.dart index 7e0bd65..c9abffc 100644 --- a/lib/src/common/functional_extender.dart +++ b/lib/src/common/functional_extender.dart @@ -25,7 +25,13 @@ extension FunctionalExtender on T? { T? takeIf(bool Function(T) f) { final T? self = this; - return self != null && f(self) ? self : null; + if (self == null) return null; + + if (f(self)) { + return self; + } else { + return null; + } } } diff --git a/lib/src/saf/saf.dart b/lib/src/saf/saf.dart index a0dd059..b3c45d3 100644 --- a/lib/src/saf/saf.dart +++ b/lib/src/saf/saf.dart @@ -134,6 +134,18 @@ Future canRead(Uri uri) async => kDocumentFileChannel Future canWrite(Uri uri) async => kDocumentFileChannel .invokeMethod('canWrite', {'uri': '$uri'}); +/// {@template sharedstorage.saf.canOpenDocumentTree} +/// Check if the device supports Storage Access Framework (SAF) and can open document trees. +/// +/// Returns `true` if the device has apps that can handle ACTION_OPEN_DOCUMENT_TREE intents, +/// `false` otherwise. This is useful to determine SAF support before attempting to use +/// document tree operations. +/// +/// [Refer to details](https://developer.android.com/reference/android/content/Intent#ACTION_OPEN_DOCUMENT_TREE). +/// {@endtemplate} +Future canOpenDocumentTree() async => kDocumentFileChannel + .invokeMethod('canOpenDocumentTree'); + /// {@template sharedstorage.saf.getDocumentThumbnail} /// Equivalent to `DocumentsContract.getDocumentThumbnail`. ///