Files
lockstep-player/app/src/main/java/at/lockstep/player/util/SafInitialUris.kt

42 lines
1.6 KiB
Kotlin
Raw Normal View History

package at.lockstep.player.util
2026-05-24 06:47:10 +02:00
import android.content.Context
import android.net.Uri
2026-05-24 06:47:10 +02:00
import android.os.Build
import android.os.storage.StorageManager
import android.provider.DocumentsContract
object SafInitialUris {
private const val EXTERNAL_STORAGE_AUTHORITY = "com.android.externalstorage.documents"
2026-05-24 06:47:10 +02:00
private const val INITIAL_URI_EXTRA = "android.provider.extra.INITIAL_URI"
2026-05-24 06:47:10 +02:00
/**
* Internal-storage Documents. Uses [StorageManager] on Android 10+ so the system picker
* lands in a choosable folder instead of the blocked volume root on Pixel devices.
*/
fun internalDocuments(context: Context): Uri {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val storageManager = context.getSystemService(StorageManager::class.java)
val intent = storageManager.primaryStorageVolume.createOpenDocumentTreeIntent()
val root =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(INITIAL_URI_EXTRA, Uri::class.java)
} else {
@Suppress("DEPRECATION")
intent.getParcelableExtra(INITIAL_URI_EXTRA)
}
if (root != null) {
val scheme =
root
.toString()
.replace("/root/", "/document/") + "%3A" + "Documents"
return Uri.parse(scheme)
}
}
return DocumentsContract.buildDocumentUri(
EXTERNAL_STORAGE_AUTHORITY,
"primary:Documents",
)
2026-05-24 06:47:10 +02:00
}
}