mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-24 19:08:26 +00:00
Update MediaStoreUtils
This commit is contained in:
parent
1ffe9bd83b
commit
784dd80965
@ -1,6 +1,5 @@
|
|||||||
package com.topjohnwu.magisk.core.utils
|
package com.topjohnwu.magisk.core.utils
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import android.content.ContentResolver
|
import android.content.ContentResolver
|
||||||
import android.content.ContentUris
|
import android.content.ContentUris
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
@ -40,15 +39,17 @@ object MediaStoreUtils {
|
|||||||
|
|
||||||
private val relativePath get() = relativePath(Config.downloadDir)
|
private val relativePath get() = relativePath(Config.downloadDir)
|
||||||
|
|
||||||
@RequiresApi(api = 29)
|
@RequiresApi(api = 30)
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
private fun insertFile(displayName: String): MediaStoreFile {
|
private fun insertFile(displayName: String): MediaStoreFile {
|
||||||
val values = ContentValues()
|
val values = ContentValues()
|
||||||
values.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath)
|
values.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath)
|
||||||
values.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName)
|
values.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName)
|
||||||
|
|
||||||
// before Android 11, MediaStore can not rename new file when file exists,
|
// When a file with the same name exists and was not created by us:
|
||||||
// insert will return null. use newFile() instead.
|
// - Before Android 11, insert will return null
|
||||||
|
// - On Android 11+, the system will automatically create a new name
|
||||||
|
// Thus the reason to restrict this method call to API 30+
|
||||||
val fileUri = cr.insert(tableUri, values) ?: throw IOException("Can't insert $displayName.")
|
val fileUri = cr.insert(tableUri, values) ?: throw IOException("Can't insert $displayName.")
|
||||||
|
|
||||||
val projection = arrayOf(MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATA)
|
val projection = arrayOf(MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATA)
|
||||||
@ -65,14 +66,8 @@ object MediaStoreUtils {
|
|||||||
throw IOException("Can't insert $displayName.")
|
throw IOException("Can't insert $displayName.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = 29)
|
||||||
private fun queryFile(displayName: String): UriFile? {
|
private fun queryFile(displayName: String): UriFile? {
|
||||||
if (Build.VERSION.SDK_INT < 30) {
|
|
||||||
// Fallback to file based I/O pre Android 11
|
|
||||||
val parent = File(Environment.getExternalStorageDirectory(), relativePath)
|
|
||||||
parent.mkdirs()
|
|
||||||
return LegacyUriFile(File(parent, displayName))
|
|
||||||
}
|
|
||||||
|
|
||||||
val projection = arrayOf(MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATA)
|
val projection = arrayOf(MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATA)
|
||||||
// Before Android 10, we wrote the DISPLAY_NAME field when insert, so it can be used.
|
// Before Android 10, we wrote the DISPLAY_NAME field when insert, so it can be used.
|
||||||
val selection = "${MediaStore.MediaColumns.DISPLAY_NAME} == ?"
|
val selection = "${MediaStore.MediaColumns.DISPLAY_NAME} == ?"
|
||||||
@ -92,11 +87,17 @@ object MediaStoreUtils {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun getFile(displayName: String): UriFile {
|
fun getFile(displayName: String, skipQuery: Boolean = false): UriFile {
|
||||||
return queryFile(displayName) ?:
|
if (Build.VERSION.SDK_INT < 30) {
|
||||||
/* this code path will never happen pre 29 */ insertFile(displayName)
|
// Fallback to file based I/O pre Android 11
|
||||||
|
val parent = File(Environment.getExternalStorageDirectory(), relativePath)
|
||||||
|
parent.mkdirs()
|
||||||
|
return LegacyUriFile(File(parent, displayName))
|
||||||
|
}
|
||||||
|
|
||||||
|
return if (skipQuery) insertFile(displayName)
|
||||||
|
else queryFile(displayName) ?: insertFile(displayName)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Uri.inputStream() = cr.openInputStream(this) ?: throw FileNotFoundException()
|
fun Uri.inputStream() = cr.openInputStream(this) ?: throw FileNotFoundException()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user