1
0
mirror of https://github.com/godotengine/godot.git synced 2025-12-06 17:25:19 +00:00

Merge pull request #113367 from syntaxerror247/persistable_uri_perm

Android: Add method to take persistable URI permission
This commit is contained in:
Thaddeus Crews
2025-12-02 11:52:09 -06:00
3 changed files with 37 additions and 18 deletions

View File

@@ -85,28 +85,12 @@ internal class FilePicker {
for (i in 0 until clipData.itemCount) {
val uri = clipData.getItemAt(i).uri
uri?.let {
try {
context.contentResolver.takePersistableUriPermission(
it,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
} catch (e: SecurityException) {
Log.d(TAG, "Unable to persist URI: $it", e)
}
selectedFiles.add(it.toString())
}
}
} else {
val uri: Uri? = data?.data
uri?.let {
try {
context.contentResolver.takePersistableUriPermission(
it,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
} catch (e: SecurityException) {
Log.w(TAG, "Unable to persist URI: $it", e)
}
selectedFiles.add(it.toString())
}
}

View File

@@ -30,6 +30,10 @@
package org.godotengine.godot.plugin
import android.content.Intent
import android.util.Log
import androidx.core.net.toUri
import org.godotengine.godot.Godot
import org.godotengine.godot.variant.Callable
@@ -39,6 +43,8 @@ import org.godotengine.godot.variant.Callable
* @see <a href="https://docs.godotengine.org/en/latest/tutorials/platform/android/javaclasswrapper_and_androidruntimeplugin.html">Integrating with Android APIs</a>
*/
class AndroidRuntimePlugin(godot: Godot) : GodotPlugin(godot) {
private val TAG = AndroidRuntimePlugin::class.java.simpleName
override fun getPluginName() = "AndroidRuntime"
/**
@@ -68,4 +74,24 @@ class AndroidRuntimePlugin(godot: Godot) : GodotPlugin(godot) {
fun createCallableFromGodotCallable(godotCallable: Callable): java.util.concurrent.Callable<Any> {
return java.util.concurrent.Callable { godotCallable.call() }
}
/**
* Helper method to take/release persistable URI permission.
*/
@UsedByGodot
fun updatePersistableUriPermission(uriString: String, persist: Boolean): Boolean {
try {
val uri = uriString.toUri()
val contentResolver = context.contentResolver
if (persist) {
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
} else {
contentResolver.releasePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
} catch (e: RuntimeException) {
Log.d(TAG, "Error updating persistable permission: ", e)
return false
}
return true
}
}