MCPcopy
hub / github.com/permissions-dispatcher/PermissionsDispatcher

github.com/permissions-dispatcher/PermissionsDispatcher @4.8.0 sqlite

repository ↗ · DeepWiki ↗ · release 4.8.0 ↗
230 symbols 469 edges 30 files 10 documented · 4%
README

PermissionsDispatcher Build Status 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

  • Kotlin: You can pick either of ktx or kapt.
  • Java: apt

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: 4.x only supports Jetpack. If you still use appcompat 3.x is the way to go.

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

${latest.version} is Download

dependencies {
  implementation "org.permissionsdispatcher:permissionsdispatcher:${latest.version}"
  annotationProcessor "org.permissionsdispatcher:permissionsdispatcher-processor:${latest.version}"
}

With Kotlin:

apply plugin: 'kotlin-kapt'

dependencies {
  implementation "org.permissionsdispatcher:permissionsdispatcher:${latest.version}"
  kapt "org.permissionsdispatcher:permissionsdispatcher-processor:${latest.version}"
}

Snapshots of the development version are available in JFrog's snapshots repository. Add the repo below to download SNAPSHOT releases.

repositories {
  jcenter()
  maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
}

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.

Extension points exported contracts — how you extend this code

PermissionRequest (Interface)
Interface used by OnShowRationale methods to allow for continuation or cancellation of a permission request.
annotation/src/main/java/permissions/dispatcher/PermissionRequest.java
GrantableRequest (Interface)
(no doc)
annotation/src/main/java/permissions/dispatcher/GrantableRequest.java

Core symbols most depended-on inside this repo

assertMethodName
called by 4
lint/src/main/java/permissions/dispatcher/CallOnRequestPermissionsResultDetector.java
methodIdentifier
called by 2
lint/src/main/java/permissions/dispatcher/CallNeedsPermissionDetector.java
isGeneratedFiles
called by 2
lint/src/main/java/permissions/dispatcher/CallNeedsPermissionDetector.java
permissionExists
called by 1
library/src/main/java/permissions/dispatcher/PermissionUtils.java
hasSelfPermissions
called by 1
library/src/main/java/permissions/dispatcher/PermissionUtils.java
hasSelfPermission
called by 1
library/src/main/java/permissions/dispatcher/PermissionUtils.java
visitMethod
called by 1
lint/src/main/java/permissions/dispatcher/NoDelegateOnResumeDetector.java
isPublicOrProtected
called by 1
lint/src/main/java/permissions/dispatcher/NoDelegateOnResumeDetector.java

Shape

Method 200
Class 28
Interface 2

Languages

Java100%

Modules by API surface

processor/src/test/java/permissions/dispatcher/processor/ProcessorTestSuite.java86 symbols
library/src/test/java/permissions/dispatcher/ApiLevelTestSuite.java20 symbols
lint/src/main/java/permissions/dispatcher/NoDelegateOnResumeDetector.java14 symbols
lint/src/main/java/permissions/dispatcher/CallNeedsPermissionDetector.java11 symbols
lint/src/main/java/permissions/dispatcher/CallOnRequestPermissionsResultDetector.java10 symbols
lint/src/main/java/permissions/dispatcher/NoCorrespondingNeedsPermissionDetector.java9 symbols
library/src/main/java/permissions/dispatcher/PermissionUtils.java7 symbols
test/src/main/java/permissions/dispatcher/test/FragmentWithAllAnnotations.java6 symbols
test/src/main/java/permissions/dispatcher/test/ActivityWithAllAnnotations.java6 symbols
processor/src/test/java/permissions/dispatcher/processor/base/BaseTest.java6 symbols
test/src/main/java/permissions/dispatcher/test/ActivityWithWriteSettingAllAnnotations.java5 symbols
test/src/main/java/permissions/dispatcher/test/ActivityWithSystemAlertWindowAllAnnotations.java5 symbols

For agents

$ claude mcp add PermissionsDispatcher \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact