package at.lockstep.player.util import android.content.Context import android.net.Uri 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" private const val INITIAL_URI_EXTRA = "android.provider.extra.INITIAL_URI" /** * 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", ) } }