Giter Site home page Giter Site logo

radhica / permissionsdispatcher Goto Github PK

View Code? Open in Web Editor NEW

This project forked from permissions-dispatcher/permissionsdispatcher

0.0 2.0 0.0 2.22 MB

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

Home Page: http://hotchemi.github.io/PermissionsDispatcher

License: Apache License 2.0

Java 100.00%

permissionsdispatcher's Introduction

PermissionsDispatcher

Build Status Download

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.

Usage

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

1. Attach annotations

There are only few annotations.

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.

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();
    }

    // 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 to generated class

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);
    // NOTE: delegate the permission handling to generated method
    cameraButton.setOnClickListener(v ->
      MainActivityPermissionsDispatcher.showCameraWithCheck(this));
}

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

Download

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.2.1'
  apt 'com.github.hotchemi:permissionsdispatcher-processor:1.2.1'
}

Support

PermissionsDispatcher supports API level over 4, using 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.

permissionsdispatcher's People

Contributors

aurae avatar intrications avatar mannodermaus avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.