GithubHelp home page GithubHelp logo

knowbody / android-permissions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from g4rb4g3/android-permissions

0.0 1.0 0.0 198 KB

An android library that makes it really easy to deal with dynamic permissions. Based on the context, library automatically decides whether to show a dialog (in case app is in foreground) or a notification (in case permission is required by a background service).

License: MIT License

Java 100.00%

android-permissions's Introduction

android-permissions Build & Publish

An android library that makes it really easy to deal with dynamic permissions. Based on the context, library automatically decides whether to show a dialog (in case app is in foreground) or a notification (in case permission is required by a background service).

After migration of artifacts to Maven Central, the newer builds would be published under group id io.github.nishkarsh. To include into the project, please select a particular published version and follow the instructions provided for the dependency management tool used: https://search.maven.org/artifact/io.github.nishkarsh/android-permissions

How does it work?

  • Get an instance of PermissionManager using PermissionManager#getInstance()
  • Invoke PermissionManager#checkPermissions(Collection<String> permissions, PermissionRequestListener listener)
  • If permissions are already granted by user, PermissionRequestListener#onPermissionGranted() is called directly.
  • In case permissions are not yet provided, user is asked for permissions (a dialog in case app is in foreground, a notification in case it's in background). Asking for permissions from a Service follows the same principle.
  • If user taps on Deny for any of the permissions asked by the app, PermissionRequestListener#onPermissionDenied(DeniedPermissions deniedPermissions) is called.
  • If user taps on Allow for all the permissions asked by the app, PermissionRequestListener#onPermissionGranted() is called. This behaviour is per set of permissions asked.
  • If Don't ask again is checked by the user in the permission dialog displayed, DeniedPermission#shouldShowRationale() returns false.

Customising notification

The notification shown to ask for permissions when the app is in background, could be customised using NotificationSettings. An instance of NotificationSettings can be created using NotificationsSettings.Builder to customise the title, message and/or a small icon for the notification.

PermissionManager instance = PermissionManager.getInstance(this);
NotificationSettings.Builder builder = new NotificationSettings.Builder();

NotificationSettings notificationSettings = builder
                .withTitle(R.string.title_action_needed)
                .withMessage(R.string.message_permission_required)
                .withSmallIcon(R.drawable.app_icon).build();

instance.setNotificationSettings(notificationSettings);

In the above example, the R.string.title_action_needed, R.string.message_permission_required and R.drawable.app_icon are the custom resources created in the project.

Checking for specific permissions

PermissionManager permissionManager = PermissionManager.getInstance(context);
permissionManager.checkPermissions(singleton(Manifest.permission.CAMERA), new PermissionManager.PermissionRequestListener() {
                @Override
                public void onPermissionGranted() {
                    Toast.makeText(context, "Permissions Granted", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onPermissionDenied(DeniedPermissions deniedPermissions) {
                    String deniedPermissionsText = "Denied: " + Arrays.toString(deniedPermissions.toArray());
                    Toast.makeText(context, deniedPermissionsText, Toast.LENGTH_SHORT).show();
                    
                    for (DeniedPermission deniedPermission : deniedPermissions) {
                        if(deniedPermission.shouldShowRationale()) {
                            // Display a rationale about why this permission is required
                        }
                    }
                }
            });

Suggestions for Usage

Permissions must be separated per functionality. After getting the instance of PermissionManager, the call to PermissionManager#checkPermissions(Collection<String> permissions, PermissionRequestListener listener) must be made for each set of permissions that are required for specific features.

For example, if you need to access location for scanning beacons and need to access contacts and storage to perform another operation, ask for location permission and other permissions separately. This would help you focus only on a particular functionality at a time.

Including into project

Gradle: implementation 'io.github.nishkarsh:android-permissions:2.0.54'

Add android-permissions as dependency inside app module build.gradle under dependencies block. Your app level build.gradle should look like:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'io.github.nishkarsh:android-permissions:2.1.6'
}

android-permissions's People

Contributors

g4rb4g3 avatar nishkarsh avatar stromme-grammarly avatar zimmi avatar

Watchers

 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.