Last active
April 7, 2024 03:19
-
-
Save sowens-csd/3b43b6da120a403430d3d9fcc5fee19f to your computer and use it in GitHub Desktop.
A simple Flutter plugin example to show permission handling in Android with Kotlin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.my_permission_plugin | |
import android.Manifest | |
import android.app.Activity | |
import android.app.Application | |
import android.content.pm.PackageManager | |
import android.os.Build | |
import android.net.Uri | |
import androidx.core.app.ActivityCompat | |
import androidx.core.content.ContextCompat | |
import io.flutter.plugin.common.MethodCall | |
import io.flutter.plugin.common.MethodChannel | |
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | |
import io.flutter.plugin.common.MethodChannel.Result | |
import io.flutter.plugin.common.PluginRegistry | |
import io.flutter.plugin.common.PluginRegistry.Registrar | |
import java.util.* | |
import kotlin.collections.ArrayList | |
class MyPermissionPlugin ( activity: Activity): MethodCallHandler, PluginRegistry.RequestPermissionsResultListener { | |
val pluginActivity: Activity = activity | |
private val application: Application = activity.application | |
private val myPermissionCode = 34264 | |
private var activeResult: Result? = null | |
private var permissionGranted: Boolean = false | |
companion object { | |
@JvmStatic | |
fun registerWith(registrar: Registrar) { | |
val channel = MethodChannel(registrar.messenger(), "my_permission_plugin") | |
val permissionPlugin = MyPermissionPlugin( registrar.activity()) | |
registrar.addRequestPermissionsResultListener(permissionPlugin) | |
channel.setMethodCallHandler(permissionPlugin) | |
} | |
} | |
override fun onMethodCall(call: MethodCall, result: Result) { | |
when (call.method) { | |
"initialize" -> initialize( result ) | |
else -> result.notImplemented() | |
} | |
} | |
// Note that this method doesn't respond until the permission check is complete | |
// even if an asynchronous system dialog is required to ask for permission. This | |
// turns out to be really simple in Flutter since until the result.success|error | |
// invocation there is no response. | |
private fun initialize(result: Result) { | |
if ( null != activeResult ) { | |
result.error("alreadyInitializing", "Only one initialize call allowed at a time", null ) | |
return | |
} | |
activeResult = result | |
checkPermission( application ) | |
} | |
// If permission is already granted this completes initialization, otherwise it requests | |
// a permission check from the system. This is using the READ_EXTERNAL_STORAGE | |
// permission as an example. Change that to whatever permission(s) your app needs. | |
private fun checkPermission(context: Application) { | |
permissionGranted = ContextCompat.checkSelfPermission(context, | |
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED | |
if ( !permissionGranted ) { | |
ActivityCompat.requestPermissions(pluginActivity, | |
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), myPermissionCode ) | |
} | |
else { | |
completeInitialization() | |
} | |
} | |
// Invoked either from the permission result callback or permission check | |
private fun completeInitialization() { | |
if ( permissionGranted ) { | |
// Do some other work if needed | |
initializedSuccessfully = permissionGranted | |
} | |
// Conveniently plugin invocations are all asynchronous | |
activeResult?.success(permissionGranted) | |
activeResult = null | |
} | |
// This is invoked after the user chooses an option on the Android permission dialog | |
// Note that the code to check the grant is simplified, if you request multiple | |
// permissions you can't make the assumptions this makes. | |
override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<out String>?, | |
grantResults: IntArray?): Boolean { | |
when (requestCode) { | |
myPermissionCode -> { | |
if ( null != grantResults ) { | |
permissionGranted = grantResults.isNotEmpty() && | |
grantResults.get(0) == PackageManager.PERMISSION_GRANTED | |
} | |
completeInitialization() | |
// Only return true if handling the requestCode | |
return true | |
} | |
} | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment