Permisos
Easy to check and request dangerous permissions on Android.
This is a library that allows you to check and request permissions by annotation or API on Android. It supports Kotlin and Java.
Using this library can greatly reduce the complexity of permission requests. You no longer need to write so much redundant template code. Let the annotations handle these tasks. You can shift your focus to the business code.
Use Annotation
Three easy steps:
- In order to let your
ActivityorFragmenthave the ability to check and request permissions through annotations, please use@Permisosto annotate yourActivityorFragment.
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import cn.nikeo.permisos.weaving.Permisos
// For Activity
@Permisos
class MainActivity : AppCompatActivity() {
}
// For Fragment
@Permisos
class MainFragment : Fragment(){
}
- Annotate
@RequiredPermissionsto a method whose return type is void(Java) or Unit(Kotlin), which means that only the relevant permission is granted, the method body of this method will be executed.
@RequiredPermissions(
requestCode = 100,
permissions = [
Manifest.permission.WRITE_EXTERNAL_STORAGE
]
)
fun writeTextToExternalStorage() {
if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
val documentsFile =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
if (!documentsFile.exists()) {
documentsFile.mkdirs()
}
File(documentsFile, "Permisos.txt").writeText(
text = """
Hi, Android, I am permisos!!
""".trimIndent(),
charset = Charsets.UTF_8
)
}
}
- In order to receive notifications of permission denied, please let the
ActivityorFragmentannotated@Permisosimplement thePermissionsDeniedHandlerinterface. When the requested permission has been denied by the user, the framework will call thedoOnPermissionsDeniedmethod of this interface. In this method, you can explain to the user that the feature is unavailable because the features requires a permission that the user has denied. At the same time, respect the user's decision. Don't link to system settings in an effort to convince the user to change their decision.
// For Activity
@Permisos
class MainActivity : AppCompatActivity(), PermissionsDeniedHandler {
override fun doOnPermissionsDenied(requestCode: Int, deniedPermissions: List<String>) {
// Explain to the user that the feature is unavailable because
// the features requires a permission that the user has denied.
// At the same time, respect the user's decision. Don't link to
// system settings in an effort to convince the user to change
// their decision.
}
}
// For Fragment
@Permisos
class MainFragment : Fragment(), PermissionsDeniedHandler {
override fun doOnPermissionsDenied(requestCode: Int, deniedPermissions: List<String>) {
// Explain to the user that the feature is unavailable because
// the features requires a permission that the user has denied.
// At the same time, respect the user's decision. Don't link to
// system settings in an effort to convince the user to change
// their decision.
}
}
Use API
Sometimes you need to use permission checking and request functions outside of Activity or Fragment, don't worry, this library can also be easily satisfied. It only takes two simple steps to complete:
- Same as the first step above。
- Pass the
ActivityorFragmentannotated with@Permisosinto your external class, and then you can useasPermissionsChecker()to convert theActivityorFragmentto thePermissionsCheckerinterface, So you can check and request permissions by thecheckPermissionsmethod ofPermissionsChecker.
class Helper(private val mainActivity: MainActivity) {
fun writeTextToExternalStorage() {
mainActivity.asPermissionsChecker().checkPermissions(
requestCode = 100,
doOnAllPermissionsGranted = {
if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
val documentsFile =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
if (!documentsFile.exists()) {
documentsFile.mkdirs()
}
File(documentsFile, "Permisos.txt").writeText(
text = """
Hi, Android, I am permisos!!
""".trimIndent(),
charset = Charsets.UTF_8
)
}
},
shouldShowRequestPermissionRationale = { deniedPermission: List<String> ->
// Explain to the user that the feature is unavailable because
// the features requires a permission that the user has denied.
// At the same time, respect the user's decision. Don't link to
// system settings in an effort to convince the user to change
// their decision.
},
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
}
}
Limit
@Permisosonly supports annotations to derived classes ofandroidx.activity.ComponentActivityorandroidx.fragment.app.Fragment.- Classes annotated by
@Permisoscannot have type parameters. - The constructor of the base class of the class annotated by
@Permisoscannot contain default parameters.
Download
Top-level build file
buildscript {
ext.permisos_version = "0.5.0"
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "cn.nikeo.permisos:permisos-gradle:$permisos_version"
}
}
App-module build file
apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'cn.nikeo.permisos'
dependencies {
kapt "cn.nikeo.permisos:permisos-compiler:$permisos_version"
}
Library-module build file
apply plugin: 'com.android.library'
apply plugin: 'kotlin-kapt'
apply plugin: 'cn.nikeo.permisos'
dependencies {
kapt "cn.nikeo.permisos:permisos-compiler:$permisos_version"
}
License
Apache License, Version 2.0, (LICENSE or https://www.apache.org/licenses/LICENSE-2.0)