GithubHelp home page GithubHelp logo

rajeefmk / runtimepermissionhandler Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 8.0 19.22 MB

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

License: MIT License

Java 100.00%

runtimepermissionhandler's Introduction

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.

runtimepermissionhandler's People

Contributors

rajeefmk avatar

Stargazers

Bhargav Pandya avatar Duong Tuan Vu avatar  avatar JitPack.io avatar misterfifi avatar Wade avatar  avatar

Watchers

James Cloos avatar misterfifi avatar  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.