|
|
|
@ -13,7 +13,6 @@ import androidx.activity.result.contract.ActivityResultContract
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
|
|
import androidx.annotation.RequiresApi
|
|
|
|
import androidx.annotation.RequiresApi
|
|
|
|
import androidx.core.app.ActivityOptionsCompat
|
|
|
|
import androidx.core.app.ActivityOptionsCompat
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/77555641/saf-no-activity-found-to-handle-intent-android-intent-action-open-document-tr
|
|
|
|
// https://stackoverflow.com/questions/77555641/saf-no-activity-found-to-handle-intent-android-intent-action-open-document-tr
|
|
|
|
class OpenDocumentTreeHelper(
|
|
|
|
class OpenDocumentTreeHelper(
|
|
|
|
@ -28,38 +27,42 @@ class OpenDocumentTreeHelper(
|
|
|
|
callback,
|
|
|
|
callback,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
private val pickFileTreeLauncherQ = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
|
|
private val pickFileTreeLauncherPrimaryStorage = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
|
|
activityResultCaller.registerForActivityResult(OpenDocumentTreeContractQ(flags), callback)
|
|
|
|
activityResultCaller.registerForActivityResult(OpenDocumentTreeContractPrimaryStorage(flags), callback)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
null
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private val pickFileTreeLauncherLegacy = activityResultCaller.registerForActivityResult(
|
|
|
|
private val pickFileTreeLauncherDefault = activityResultCaller.registerForActivityResult(
|
|
|
|
contract = OpenDocumentTreeContractLegacy(flags),
|
|
|
|
contract = OpenDocumentTreeContractDefault(flags),
|
|
|
|
callback = callback,
|
|
|
|
callback = callback,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
override fun launch(input: Uri?, options: ActivityOptionsCompat?) {
|
|
|
|
override fun launch(input: Uri?, options: ActivityOptionsCompat?) {
|
|
|
|
if (pickFileTreeLauncherQ == null) {
|
|
|
|
|
|
|
|
pickFileTreeLauncherLegacy.launch(input, options)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
pickFileTreeLauncherQ.launch(input, options)
|
|
|
|
pickFileTreeLauncherDefault.launch(input, options)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTraceDebug()
|
|
|
|
if (pickFileTreeLauncherPrimaryStorage != null) {
|
|
|
|
pickFileTreeLauncherLegacy.launch(input, options)
|
|
|
|
try {
|
|
|
|
|
|
|
|
pickFileTreeLauncherPrimaryStorage.launch(input, options)
|
|
|
|
|
|
|
|
} catch (e2: Exception) {
|
|
|
|
|
|
|
|
e.addSuppressed(e2)
|
|
|
|
|
|
|
|
throw e
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw e
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun unregister() {
|
|
|
|
override fun unregister() {
|
|
|
|
pickFileTreeLauncherQ?.unregister()
|
|
|
|
pickFileTreeLauncherPrimaryStorage?.unregister()
|
|
|
|
pickFileTreeLauncherLegacy.unregister()
|
|
|
|
pickFileTreeLauncherDefault.unregister()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override val contract: ActivityResultContract<Uri?, *>
|
|
|
|
override val contract: ActivityResultContract<Uri?, *>
|
|
|
|
get() = pickFileTreeLauncherQ?.contract ?: pickFileTreeLauncherLegacy.contract
|
|
|
|
get() = pickFileTreeLauncherPrimaryStorage?.contract ?: pickFileTreeLauncherDefault.contract
|
|
|
|
|
|
|
|
|
|
|
|
private open class OpenDocumentTreeContractLegacy(
|
|
|
|
private open class OpenDocumentTreeContractDefault(
|
|
|
|
private val flags: Int,
|
|
|
|
private val flags: Int,
|
|
|
|
) : ActivityResultContracts.OpenDocumentTree() {
|
|
|
|
) : ActivityResultContracts.OpenDocumentTree() {
|
|
|
|
|
|
|
|
|
|
|
|
@ -71,9 +74,9 @@ class OpenDocumentTreeHelper(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.Q)
|
|
|
|
@RequiresApi(Build.VERSION_CODES.Q)
|
|
|
|
private class OpenDocumentTreeContractQ(
|
|
|
|
private class OpenDocumentTreeContractPrimaryStorage(
|
|
|
|
private val flags: Int,
|
|
|
|
private val flags: Int,
|
|
|
|
) : OpenDocumentTreeContractLegacy(flags) {
|
|
|
|
) : OpenDocumentTreeContractDefault(flags) {
|
|
|
|
|
|
|
|
|
|
|
|
override fun createIntent(context: Context, input: Uri?): Intent {
|
|
|
|
override fun createIntent(context: Context, input: Uri?): Intent {
|
|
|
|
val intent = (context.getSystemService(Context.STORAGE_SERVICE) as? StorageManager)
|
|
|
|
val intent = (context.getSystemService(Context.STORAGE_SERVICE) as? StorageManager)
|
|
|
|
|