PermissionsDispatcher provides simple annotation-based API to handle runtime permissions in Marshmallow.
Runtime permissions is so great for users but also the hell for developers.
You can be released from the burden that writing a bunch of 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.
@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
NOTE: Annotated methods must be package private or above.
@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();
}
// this is option
@ShowsRationale(Manifest.permission.CAMERA)
void showRationaleForCamera() {
Toast.makeText(this, R.string.permission_camera_rationale, Toast.LENGTH_SHORT).show();
}
}
Now you can use the class that generated by annotation processor.
In this case the 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);
cameraButton.setOnClickListener(this);
Button contactsButton = (Button) findViewById(R.id.button_contacts);
contactsButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_camera:
// NOTE: delegate the permission handling to generated method
MainActivityPermissionsDispatcher.showCameraWithCheck(this);
break;
case R.id.button_contacts:
// NOTE: delegate the permission handling to generated method
MainActivityPermissionsDispatcher.showContactsWithCheck(this);
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] 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 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.1.2'
apt 'com.github.hotchemi:permissionsdispatcher-processor:1.1.2'
}
PermissionsDispatcher supports API level over 4, using 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.