PermissionsDispatcher provides simple annotation-based API to handle new runtime permissions system.
Runtime permissions is so great for users but also the hell for developers.
You can be released from the burden that writing a boiler plate check statements whether a permission have been granted or not.
Here's a minimum example that you register MainActivity
which requires Manifest.permission.CAMERA
.
There are only few annotations.
NOTE: Annotated methods must be package private or above.
@RuntimePermissions
: Register an Activity or Fragment to handle permissions.@NeedsPermission
: Register a method which the permission is needed.- You can use
@NeedsPermissions
for multiple requests.
- You can use
@ShowsRationale
: Register a method which explains why the permission is needed. An annotated method is called whenshouldShowRequestPermissionRationale
returns true.- You can use
@ShowsRationales
for multiple requests.
- You can use
@DeniedPermission
: Register a method called when user deny a permission.- You can use
@DeniedPermissions
for multiple requests.
- You can use
@RuntimePermissions
public class MainActivity extends Activity {
@NeedsPermission(Manifest.permission.CAMERA)
void showCamera() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.sample_content_fragment, CameraPreviewFragment.newInstance())
.addToBackStack("camera")
.commitAllowingStateLoss();
}
// Option
@ShowsRationale(Manifest.permission.CAMERA)
void showRationaleForCamera() {
Toast.makeText(this, R.string.permission_camera_rationale, Toast.LENGTH_SHORT).show();
}
// Option
@DeniedPermission(Manifest.permission.CAMERA)
void showDeniedForCamera() {
Toast.makeText(this, R.string.permission_camera_denied, Toast.LENGTH_SHORT).show();
}
}
Now you can use the class that generated by annotation processing.
In this case the class name is MainActivityPermissionsDispatcher
.
It has methods ends with WithCheck
and onRequestPermissionsResult
.
Only you have to do is delegating the work to them.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button cameraButton = (Button) findViewById(R.id.button_camera);
// NOTE: delegate the permission handling to generated method
cameraButton.setOnClickListener(v ->
MainActivityPermissionsDispatcher.showCameraWithCheck(this));
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// NOTE: delegate the permission handling to generated method
MainActivityPermissionsDispatcher.
onRequestPermissionsResult(this, requestCode, grantResults);
}
If you want to know more detail, please check the sample and generated class.
Add to your project's build.gradle
file:
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
}
}
apply plugin: 'android-apt'
dependencies {
compile 'com.github.hotchemi:permissionsdispatcher:1.2.1'
apt 'com.github.hotchemi:permissionsdispatcher-processor:1.2.1'
}
PermissionsDispatcher supports API level over 4 and uses support v4 rev.23.
Copyright 2015 Shintaro Katafuchi
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.