Giter Club home page Giter Club logo

rafikipermissions's Introduction

topic map

APIAPIAPI

RafikiPermissions is to simplify Android runtime permission request. The name Rafiki comes from the baboon elder who manages permission in LION KING :D

Content

Setup

Adding jitpack repository in your project's build.gradle file:

allprojects {
    repositories {
        maven { url 'https://www.jitpack.io' }
    }
}

Adding the following dependency to app build.gradle file:

dependencies {
    implementation 'com.github.nasduck:RafikiPermissions:1.2.0'
}

Usage

Here's a minimum example, in which you need to take a photo which requires Manifest.permission.CAMERA

1、Declare the required permission

Declare camera permission in AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />

2、Uniformly process permission requests' results in the base activity class

public class BaseActivity extends AppCompatActivity {
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
	
        RafikiPermissions.getInstance(this).onResult(requestCode, permissions, grantResults);
    }
}

3、Request permissions

// Return true if the permission is granted
if (RafikiPermissions.getInstance(this)
        .setResultStrategy(new PermissionResultCustomStrategy(this)) // Set customized permission handling strategy, There are also another two preset strategies
        .requestCamera()) {
    // Permissions Already Granted
    ...
}

4、Implement callbacks to the results

Implement interface OnPermissionResultListener:

@Override   
public void onPermissionsResultGrant(int requestCode) {
    switch (requestCode) {
        case RafikiResultCode.RESULT_CODE_CAMERA:
            // Permissions Granted
    }
}

@Override   
public void onPermissionsResultDenied(int requestCode) {
    switch (requestCode) {
        case RafikiResultCode.RESULT_CODE_CAMERA:
            // Permissions Denied
    }
}

Permission handling strategies

Three strategies are offered:

  1. PermissionResultNothingStrategy By default. Whether permission granted or not, just do nothing.

Screenshot:
NothingStrategy

  1. PermissionResultGuideStrategy Show an additional dialog to guide users to app setting to grant permissions again if permission not granted.

Screenshot:
GuideStrategy

  1. PermissionResultCustomStrategy Customized strategy. Implement interface OnPermissionResultListener to customize permission handling

Screenshot:
CustomStrategy

Different ways to request permissions

Request one single permission

Provide .requestXX() method corresponding to each permission. For example, request for Manifest.permission.CAMERA, use requestCamera() to request camera permission directly.

Request multiple permissions

For more than one permissions,just use .addXXX method corresponding to different permission to add them one by one. Call request() in the end. The parameter passed is the user-defined requestCode(By default, RafikiResultCode.RAFIKI_PERMISSION_RESULT_CODE if not passed), to recognized this request in the callbacks:

if (RafikiPermissions.getInstance(this)
        .addReadExternalStorage()
        .addWriteExternalStorage()
        .setResultStrategy(new PermissionResultCustomStrategy(this))
        .request(RESULT_CODE)) {
    // Permissions Already Granted
    ...
}

or call addPermissions() to add a permission list directly

List<String> permissions = new ArrayList();
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        
if (RafikiPermissions.getInstance(this)
        .addPermissions(permissions)
        .setResultStrategy(new PermissionResultCustomStrategy(this))
        .request(RESULT_CODE)) {
    // Permissions Already Granted
    ...
}

Contributer

LICENSE

   Copyright (2019) Chuan Dong, Lihao Zhou

   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.

rafikipermissions's People

Contributors

dongchuan avatar redrain39 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rafikipermissions's Issues

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.