Handles all the boilerplate codes associated with runtime permissions for Android 6.0 and above.

Overview

Release

Runtime Permission Handler

The library came out as an effort to reduce the boilerplate required for getting runtime permissions for Android devices running OS 6.0 and above.

The developer can now focus on handling the granted and denied state of the permission rather than dealing with the permission flow and dialogues.

Integrating into your project

This library is available in JitPack.io repository. To use it make sure that repository's url is added to the build.gradle file in your app:

repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}

dependencies {
    compile 'com.github.rajeefmk:runtimepermissionhandler:1.0.3'

    // Other dependencies your app might use
}

Usage

A sample implementation can be found here Sample app. You can also make use of the Utils.java class in the sample repo for avoiding more boilerplates.

public class MainActivity extends AppCompatActivity implements PermissionsApi.PermissionCallback {

    private static final int SMS_REQUEST = 101;
    private static final int STORAGE_REQUEST = 102;
    private static final int SOME_LIST_OF_PERMISSION = 103;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PermissionsApi.getInstance().setPermissionCallback(this);
    }

The permission can be checked by calling the method Permissionutils.hasPermission(Context, String[] of permission, permission rationale message, REQUEST CODE}.

The method returns true only if all requested permission is granted by the user. If any one permission is denied, this method will return false.

The 3rd param - permission rationale message - determines if the requested permission is optional (if message is null) or required ( message is provided).

/**
     * Permission rationale won't be shown when passed null. Its recommended to pass null for
     * those permissions which are optional.
     */
    private void onSmsPermission() {
        if (PermissionUtils.hasPermission(this, {Manifest.permission.RECEIVE_SMS}, 
        null, OPTIONAL_REQUEST))
        //Permission is already granted. Continue exuecuting your code.
    }

    private void onStoragePermission() {
        if (PermissionUtils.hasPermission(this,{Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE},
                getString(R.string.message_permission_rationale), REQUIRED_REQUEST))
            showToastMessage(getString(R.string.message_required_permission_granted));

    }

    /**
     * If a list of permissions needs to be granted, until the user grants 
     * all the permissions in the list the runtime dialogue will keep showing 
     * everytime user clicks on the button. Also, those permissions which are 
      * already granted won't be asked again.
     **/
    private void someListOfPermissions() {
        if (PermissionUtils.hasPermission(this, Utils.getPermissionList(),
                getString(R.string.message_permission_rationale), SOME_LIST_OF_PERMISSION))
            showToastMessage(getString(R.string.message_permission_list_granted));
    }

Override onRequestPermissionResult for handling permission response from user.

   
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (this.isFinishing())
            return;
       switch (requestCode) {
            case SMS_REQUEST:
                /**
                 * Passing null here instead of rationale message will result in 
                  * permission being treated as denied instead of showing rationale message
                 */
                PermissionUtils.onRequestPermissionsResult(mContext, requestCode,
                permissions, grantResults, null);
                break;
            case STORAGE_REQUEST:
                PermissionUtils.onRequestPermissionsResult(mContext, requestCode, 
                permissions, grantResults, mContext.getString(R.string.message_permission_rationale));
                break;
            case SOME_LIST_OF_PERMISSION:
                PermissionUtils.onRequestPermissionsResult(mContext, requestCode, 
                permissions, grantResults, mContext.getString(R.string.message_permission_rationale));
                break;
        }
    }

These methods are implemented for handling permission granted and denied cases.

    @Override
    public void onPermissionGranted(int requestCode) {
        switch (requestCode) {
            case SMS_REQUEST:
                showToastMessage(getString(R.string.message_optional_permission_granted));
                break;
            case STORAGE_REQUEST:
                showToastMessage(getString(R.string.message_required_permission_granted));
                break;
            case SOME_LIST_OF_PERMISSION:
                showToastMessage(getString(R.string.message_permission_list_granted));
                break;
        }
    }

    @Override
    public void onPermissionDenied(int requestCode) {
        if (requestCode == SMS_REQUEST)
            showToastMessage("Sms permission denied but can continue.");
        //Based on denied permission request code, handle the case accordingly.
    }

TODO

  • Annotation instead of interfaces
  • Support screen rotation / configuration change

Help

Bugs, Feature requests

Found a bug? Something that's missing? Feedback is an important part of improving the project, so please open an issue.

Code

Fork this project and start working on your own feature branch. When you're done, send a Pull Request to have your suggested changes merged into the master branch by the project's collaborators. Read more about the GitHub flow.

License

Copyright 2017 Muhammed Rajeef M K

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.
You might also like...
An android boilerplate project using clean architecture

Android Clean Architecture Boilerplate Welcome 👋 We hope this boilerplate is not only helpful to other developers, but also that it helps to educate

A fork of our clean architecture boilerplate using the Model-View-Intent pattern

Android Clean Architecture MVI Boilerplate Note: This is a fork of our original Clean Architecture Boilerplate, except in this repo we have switched o

Android Ptrace Inject for all ABIs and all APIs. Help you inject Shared Library on Android.

Android Ptrace Inject 中文可以参考我的注释内容进行理解 我写的注释相对来说比较全面了 How to build Make sure you have CMake and Ninja in your PATH Edit CMakeLists.txt. Set ANDROID_ND

This is a Kotlin multiplatform template project used to generate and deploy a natively compiled AWS lambda function using the custom runtime.
This is a Kotlin multiplatform template project used to generate and deploy a natively compiled AWS lambda function using the custom runtime.

Overview This is a Kotlin multiplatform template project used to generate and deploy a natively compiled AWS Lambda function using a custom runtime. U

🍼Debug Bottle is an Android runtime debug / develop tools written using kotlin language.
🍼Debug Bottle is an Android runtime debug / develop tools written using kotlin language.

🇨🇳 中文 / 🇯🇵 日本語 / 🇬🇧 English 🍼 Debug Bottle An Android debug / develop tools written using Kotlin language. All the features in Debug bottle are

An experimental tool for building console UI in Kotlin using the Jetpack Compose compiler/runtime

An experimental tool for building console UI in Kotlin using the Jetpack Compose compiler/runtime

Kotlin Multiplatform runtime infix expressions evaluator.
Kotlin Multiplatform runtime infix expressions evaluator.

Kotlin multiplatform expressions evaluator This is a kotlin multiplatform runtime infix expressions evaluator. Overview Operators The library supports

Koin Annotations - help declare Koin definition in a very fast and intuitive way, and generate all underlying Koin DSL for you

The goal of Koin Annotations project is to help declare Koin definition in a very fast and intuitive way, and generate all underlying Koin DSL for you. The goal is to help developer experience to scale and go fast 🚀 , thanks to Kotlin Compilers.

Events Calendar is a user-friendly library that helps you achieve a cool Calendar UI with events mapping. You can customise every pixel of the calendar as per your wish and still achieve in implementing all the functionalities of the native android calendar in addition with adding dots to the calendar which represents the presence of an event on the respective dates. It can be done easily, you are just a few steps away from implementing your own badass looking Calendar for your very own project!
Releases(1.0.3)
Owner
RmK
Android Dev | Volunteer | PotterHead | Remote work | Declutterism
RmK
A Kotlin library for reactive and boilerplate-free SharedPreferences in Android

KPreferences A Kotlin library for reactive and boilerplate-free Shared Preferences in Android. With KPreferences you can use Kotlin's marvelous delega

Mohamad Amin Mohamadi 19 Dec 16, 2020
An Android template project (in Kotlin) with boilerplate and current patterns.

android-starter-v4 An Android template project (in Kotlin) with boilerplate and plumbing, exploring current architecture patterns. A bit too much for

Matthias Urhahn 14 Nov 4, 2022
👋 A common toolkit (utils) ⚒️ built to help you further reduce Kotlin boilerplate code and improve development efficiency. Do you think 'kotlin-stdlib' or 'android-ktx' is not sweet enough? You need this! 🍭

Toolkit [ ?? Work in progress ⛏ ?? ??️ ?? ] Snapshot version: repositories { maven("https://s01.oss.sonatype.org/content/repositories/snapshots") }

凛 35 Jul 23, 2022
Boilerplate code for implementing MVVM in Android using Jetpack libraries, coroutines, dependency injection and local persistance

MVVM Foundation This projects aims to speed up development of Android apps by providing a solid base to extend Libraries Jetpack Fragment Material3 :

Gabriel Gonzalez 2 Nov 10, 2022
Oratio Library for Android Studio helps you simplify your Android TTS codes

Oratio Oratio is a library for Android Studio. This library is useful to a number of developers who are currently making apps using android TTS(Text-T

Jacob Lim 1 Oct 28, 2021
🔓 Kotlin version of the popular google/easypermissions wrapper library to simplify basic system permissions logic on Android M or higher.

EasyPermissions-ktx Kotlin version of the popular googlesample/easypermissions wrapper library to simplify basic system permissions logic on Android M

Madalin Valceleanu 326 Dec 23, 2022
Android Library for requesting Permissions with Kotlin Coroutines or AndroidX LiveData

PEKO PErmissions with KOtlin Android Permissions with Kotlin Coroutines or LiveData No more callbacks, builders, listeners or verbose code for request

Marko Devcic 133 Dec 14, 2022
Android-Boilerplate - Base project for android development with new technology

Android-Boilerplate Base project for android development with new technology, in

Muhammad Rizky Arifin 1 Aug 15, 2022
Order picking with QR codes

Order PiQR Order picking with QR codes Open the project in Android Studio and run. First step was to create a new simple project in Android Studio. Th

Marcel van Pinxteren 0 Dec 3, 2021
A fork of our clean architecture boilerplate, this time using the Android Architecture Components

Android Clean Architecture Components Boilerplate Note: This is a fork of our original Clean Architecture Boilerplate, except in this repo we have swi

Buffer 1.3k Jan 3, 2023