

GitHub - permissions-dispatcher/PermissionsDispatcher: Simple annotation-based A...
source link: https://github.com/permissions-dispatcher/PermissionsDispatcher
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PermissionsDispatcher
- Fully Kotlin/Java support
- Special permissions support
- 100% reflection-free
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:
- If you're using jCenter we've moved on to MavenCentral, see migration guide.
- 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
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.
Recommend
-
180
ENTRUST (Laravel 5 Package) Entrust is a succinct and flexible way to add Role-based Permissions to Laravel 5. If you are looking for the Laravel 4 version, take a look
-
207
dispatch.vim Leverage the power of Vim's compiler plugins without being bound by synchronicity. Kick off builds and test suites using one of several asynchronous adapters (including tmux, screen, iTerm, Windows, and a headless mode), and...
-
87
symfony/event-dispatcher: The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them.
-
43
Simple and light-weight event dispatcher for Kotlin
-
45
Posts in this series: A Primer
-
128
README.md Brigadier Brigadier is a command parser & dispatcher, designed and developed for Minecraft: Java Edition and now freely available for use...
-
17
java-version.com: What's new in Java 16? 15? Keep up to date! Basic Spring MVC example with Thymeleaf and no XML (annotation based conf...
-
9
Quantum Library : A scalable C++ coroutine framework Quantum is a full-featured and powerful C++ framework build on top of the Boost...
-
8
Multi-Cluster Application Dispatcher The multi-cluster-app-dispatcher is a Kubernetes controller providing mechanisms for applications to manage batch jobs in a single or mult-cluster environment. The multi-clust...
-
3
Laravel Safe Dispatcher for Queues For Laravel, it has the Queues feature, all cool and easy to use, right? But what if it fails to dispatch a job? Then you have no idea for: What was the data inside t...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK