PermissionsDispatcher README

The project's README from github.com/permissions-dispatcher/PermissionsDispatcher. As one markdown file: README.md.

PermissionsDispatcher CI for pull request PermissionsDispatcher

PermissionsDispatcher provides a simple annotation-based API to handle runtime permissions.

This library lifts the burden that comes with writing a bunch of check statements whether a permission has been granted or not from you, in order to keep your code clean and safe.

Usage

Here's a minimum example, in which you register a MainActivity which requires Manifest.permission.CAMERA.

0. Prepare AndroidManifest

Add the following line to AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />

1. Attach annotations

PermissionsDispatcher introduces only a few annotations, keeping its general API concise:

NOTE: Annotated methods must not be private.

Annotation Required Description
@RuntimePermissions ✓ Register an Activity or Fragment to handle permissions
@NeedsPermission ✓ Annotate a method which performs the action that requires one or more permissions
@OnShowRationale Annotate a method which explains why the permissions are needed. It passes in a PermissionRequest object which can be used to continue or abort the current permission request upon user input. If you don't specify any argument for the method compiler will generate process${NeedsPermissionMethodName}ProcessRequest and cancel${NeedsPermissionMethodName}ProcessRequest. You can use those methods in place of PermissionRequest(ex: with DialogFragment)
@OnPermissionDenied Annotate a method which is invoked if the user doesn't grant the permissions
@OnNeverAskAgain Annotate a method which is invoked if the user chose to have the device "never ask again" about a permission
@RuntimePermissions
class MainActivity : AppCompatActivity(), View.OnClickListener {

    @NeedsPermission(Manifest.permission.CAMERA)
    fun showCamera() {
        supportFragmentManager.beginTransaction()
                .replace(R.id.sample_content_fragment, CameraPreviewFragment.newInstance())
                .addToBackStack("camera")
                .commitAllowingStateLoss()
    }

    @OnShowRationale(Manifest.permission.CAMERA)
    fun showRationaleForCamera(request: PermissionRequest) {
        showRationaleDialog(R.string.permission_camera_rationale, request)
    }

    @OnPermissionDenied(Manifest.permission.CAMERA)
    fun onCameraDenied() {
        Toast.makeText(this, R.string.permission_camera_denied, Toast.LENGTH_SHORT).show()
    }

    @OnNeverAskAgain(Manifest.permission.CAMERA)
    fun onCameraNeverAskAgain() {
        Toast.makeText(this, R.string.permission_camera_never_askagain, Toast.LENGTH_SHORT).show()
    }
}

2. Delegate to generated functions

Now generated functions become much more concise and intuitive than Java version!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById(R.id.button_camera).setOnClickListener {
            // NOTE: delegate the permission handling to generated function
            showCameraWithPermissionCheck()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        // NOTE: delegate the permission handling to generated function
        onRequestPermissionsResult(requestCode, grantResults)
    }

Check out the sample for more details.

Other features/plugins

Installation

NOTE:

To add PermissionsDispatcher to your project, include the following in your app module build.gradle file:

${latest.version} is Download

dependencies {
  implementation "com.github.permissions-dispatcher:permissionsdispatcher:${latest.version}"
  annotationProcessor "com.github.permissions-dispatcher:permissionsdispatcher-processor:${latest.version}"
}

With Kotlin:

apply plugin: 'kotlin-kapt'

dependencies {
  implementation "com.github.permissions-dispatcher:permissionsdispatcher:${latest.version}"
  kapt "com.github.permissions-dispatcher:permissionsdispatcher-processor:${latest.version}"
}

License

Copyright 2016 Shintaro Katafuchi, Marcel Schnelle, Yoshinori Isogai

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

PermissionsDispatcher (permissions-dispatcher/PermissionsDispatcher)

Searching Database…

这是什么?PermissionsDispatcher 是一个基于注解的 Android 库,可以自动生成请求和检查运行时权限所需的样板代码,因此你无需手动编写权限检查语句。
它是框架还是普通库?它是一个注解处理库:你只需在 Activity 或 Fragment 上添加几个注解,它就会在编译时为你生成权限处理代码。
如何将它添加到项目中?在应用模块的 build.gradle 文件中添加 PermissionsDispatcher,然后用 @RuntimePermissions 注解你的类,用 @NeedsPermission 注解需要权限的方法,并把调用委托给生成的函数。
我需要 Kotlin 的 ktx/kapt 还是 Java 的 apt?Kotlin 用户可以选择 ktx 或 kapt 进行注解处理,Java 用户则使用 apt。你还需要在 AndroidManifest.xml 中声明所需的权限。
安装时有哪些需要注意的坑吗?如果项目仍在使用 jCenter,你需要切换到 MavenCentral;4.x 版本只支持 Jetpack(AndroidX),如果你还在使用旧的 appcompat 库,则需要继续使用 3.x 版本。
PermissionsDispatcher 提供哪些注解?主要有 @RuntimePermissions、@NeedsPermission、@OnShowRationale、@OnPermissionDenied 和 @OnNeverAskAgain,它们共同涵盖请求权限、说明需要权限的原因,以及在权限被拒绝或选择“不再询问”时做出响应。
它使用反射吗?不使用。该库被描述为 100% 无反射,它依靠注解处理在编译时生成代码。
被注解的方法可以是 private 的吗?不可以,使用 PermissionsDispatcher 注解的方法不能是 private 的,因为生成的代码需要调用它们。
它是否支持普通运行时权限之外的特殊权限?支持。README 中提到了对“特殊权限”的专门支持以及 maxSdkVersion 选项,另外还有 IntelliJ 插件和 AndroidAnnotations 插件。
Size
2.9 MB
Stars
11.1K
Age
11y
Commits · 90 Days