Git Product home page Git Product logo

nammu's Introduction

Nammu - Runtime Permission Helper

Speed up your work with new Runtime Permissions introduced in Android 6.0 Marshmallow. This lib allows you to monitor permissions, check them in background and as well ask for a permission in easy way (callback).

What are Runtime Permissions?

Google docs is here. TLDR: like old-loved permissions that were ask during installation but this time they are more dynamic (should be ask only when they are needed) and can be revoked by user at any time.

Source of all evil

Why should I care?

Because your user can revoke most essential part of your app and quite probably there will be a lot of app crashes. Current solution you can see here - Google sample basically there is a lot that happens with Activity that is used to check and grant permissions. Also permissions rights are checked many times in the code.

Monitor permissions

To keep track of access to particular permissions, all you need is init Nammu Nammu.init(Context); (pass Application Context, not Activity Context) and call permissionCompare(PermissionListener) to compare lists of granted permissions with previous method call. If you want only to update granted permission lists (without checking if anything changed) use refreshMonitoredList(). PermissionListener offers a callback when permissions was changed, removed, or added. It is recommended to do on app start to check if user didn't removed any permissions and open our app again.

Also you can add Permission to ignored list to ignore its changes in access - use ignorePermission(String permission).

Easy asking for permissions

It removes a bit of boiler plate to keep request id, and thus simplify your code inside Activity class. call Nammu.askForPermission(Activity, PermissionString , PermissionCallback) which offers a nice callback with either success or fail method. To use this only thing you need to add is in your Activity that you are using.

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    Nammu.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

Extras

You can get list of monitored Permissions -Nammu.getPrevPermissions(), list of currently granted permissions - getGrantedPermissions(), and few less important.

How to import it?

It is available on jitpack.io by adding this to your build.gradle:

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

dependencies {
    compile 'com.github.tajchert:nammu:1.5.1'
}

AndroidX, Kotlin...

Android X was added in 1.3.0, last build without it is 1.2.4, Kotlin since 1.4.0.

nammu's People

Contributors

amitkot avatar bendikv avatar christiandeange avatar programmerr47 avatar psygik avatar tajchert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nammu's Issues

Nammu.askForPermission(Fragment fragment, String[] permissions... is private access.

This method is helpful as well. Why it has to be private. Unlike the other askForPermission for activity, it allow both single and multi permissions request. Is there any concern behind?

private static void askForPermission(Fragment fragment, String[] permissions, PermissionCallback permissionCallback) {
        if (permissionCallback != null) {
            if (hasPermission(fragment.getActivity(), (String[])permissions)) {
                permissionCallback.permissionGranted();
            } else {
                PermissionRequest permissionRequest = new PermissionRequest(new ArrayList(Arrays.asList(permissions)), permissionCallback);
                permissionRequests.add(permissionRequest);
                fragment.requestPermissions(permissions, permissionRequest.getRequestCode());
            }
        }
    }

Nammu.askForPermission fragment not available in 1.1.1

in version 1.1.1 the only method signatures for askForPermission are the following

askForPermission(Activity activity, String permission, PermissionCallback permissionCallback)
askForPermission(Activity activity, String[] permissions, PermissionCallback permissionCallback)

There is no askForPermission for Fragment as shown in the sample app

sdk:minSdkVersion

Error:Execution failed for task ':processDebugManifest'.

Manifest merger failed : uses-sdk:minSdkVersion 14 cannot be smaller than version 15 declared in library [com.github.tajchert:nammu:1.0.0]

Can this library have minSdkVersion be 14 or lower?

result of askForPermission

The functions permissionGranted() and permissionRefused() aren't executed when the permission is granted or refused for the first time.
They will only execute after restarting the app.
Is there any way to get them executed without restarting the app.

Differnciate between Permission asking 1st time or never ask again.

How to differentiate between the permission that I am calling is first time or user have selected on the never asked again.
In sample you have mentioned this thing.

@OnClick(R.id.buttonLocation) public void clickButtLocation() { if (Nammu.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION)) { boolean hasAccess = Tools.accessLocation(this); Toast.makeText(this, "Access granted fine= " + hasAccess, Toast.LENGTH_SHORT).show(); } else { if (Nammu.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { //User already refused to give us this permission or removed it //Now he/she can mark "never ask again" (sic!) Snackbar.make(mLayout, "Here we explain user why we need to know his/her location.", Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() { @Override public void onClick(View view) { Nammu.askForPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION, permissionLocationCallback); } }).show(); } else { //First time asking for permission // or phone doesn't offer permission // or user marked "never ask again" Nammu.askForPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION, permissionLocationCallback); } } }

Here in comments you have mentioned
First time asking permission
or user marked "never ask again."

I want to know when user selected never asked again and proceed further

PermissionCallback called permissionGranted but it isn't

When you ask a permission, android shows a popup, if at this moment app go to background and than back to foreground
PermissionCallback will trigger permissionsGranted but actually not. It happens because at this moment will call onRequestPermissionsResult with empty grantResults.

How we can see, method verifyPermissions return true by default, and how result wrong trigger of permissionGranted.
I think this method should return false by default.

fun verifyPermissions(grantResults: IntArray): Boolean {
    for (result in grantResults) {
      if (result != PackageManager.PERMISSION_GRANTED) {
        return false
      }
    }
    return true
  }

Upload to Maven/Jcenter

Will be done as soon as official API 23 (or so for Android M) is release - since then some thing might change as this is only a preview.

Also other problem is lack of targetSdkVersion="android-MNC" on Maven etc. which is not recognized.

When to check for the permissions

Very nice idea to put all granted permissions into one collection.

I am just wondering 'when' to check the permissions. Gathering data during onCreate in Application class results with a fixed collection. My idea would be to check for permissions using ActvityLifecycleMonitor after every onResume ?

Handling Scope Storage for Android 11 and above

Will this lib be further developed so that it also works with Android 11, where the authorization concept is completely different?

Will this lib be further developed so that it also works with Android 11, where the authorization concept is completely different?
I also use this lib, but the permissions don't work on smartphones with Android 11 and higher.

Camera background permission check

Nammu.checkCamera() is there but is kept private as this results in crash in sample app when Camera in Fragment is launched:

java.lang.RuntimeException: set display orientation failed
            at android.hardware.Camera.setDisplayOrientation(Native Method)
            at pl.tajchert.nammusample.camera.CameraPreview.surfaceChanged(CameraPreview.java:97)
            at android.view.SurfaceView.updateWindow(SurfaceView.java:591)
            at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:176)
            at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2030)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)

To fix this probably there is needed some release call or use of Camera2 API.
I couldn't find any method that doesn't open Camera preview and require permission to use a test (so only async methods which are not perfect).

For now Camera permission can be check only with Activity instance with new runtime permission API.

Help needed - if you know solution to that :)

SDK is out!

Hey, now that the sdk is out, could you release it on jcenter?

Cheers,

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.