highgic / PermissionsDispatcher

Provides simple annotation-based API to handle runtime permissions in Marshmallow.

Home Page:https://speakerdeck.com/hotchemi/permissionsdispatcher

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PermissionsDispatcher

Build Status Download

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.

Usage

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

1. Attach annotations

There are only few annotations.

NOTE: Annotated methods must be package private or above.

Must

  • @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.

Option

  • @ShowsRationale: Register a method which explains why the permission is needed. An annotated method is called when shouldShowRequestPermissionRationale returns true.
    • You can use @ShowsRationales for multiple requests.
  • @DeniedPermission: Register a method called when user deny a permission.
    • You can use @DeniedPermissions for multiple requests.
@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();
    }
}

2. Delegate the work to generated methods

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.

Download

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'
}

Support

PermissionsDispatcher supports API level over 4 and uses support v4 rev.23.

License

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.

About

Provides simple annotation-based API to handle runtime permissions in Marshmallow.

https://speakerdeck.com/hotchemi/permissionsdispatcher

License:Apache License 2.0


Languages

Language:Java 100.0%