Skip to content
Reactive permissions for Android
Java Shell
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.buildscript Do not run clean build again when deploying snapshots. (#42) May 27, 2018
code_quality_tools Finish before overriding pending transitions. (#46) Aug 11, 2018
gradle/wrapper Update dependencies to latest & greatest. (#64) Feb 23, 2019
rxpermission-testing Separate testing infrastructure into it's own module. (#65) Feb 23, 2019
rxpermission Fix NullpointerException in ShadowActivity#handleIntent. (#70) Sep 25, 2019
sample Separate testing infrastructure into it's own module. (#65) Feb 23, 2019
.gitignore Initial version. (#1) Aug 12, 2017
.travis.yml
CHANGELOG.md Prepare next development version. Jan 6, 2019
LICENSE Initial commit Aug 2, 2017
README.md Use implementation instead of compile for gradle (#68) Apr 23, 2019
build.gradle Update dependencies to latest & greatest. (#64) Feb 23, 2019
gradle.properties Prepare next development version. Jan 6, 2019
gradlew Update dependencies to latest & greatest. (#64) Feb 23, 2019
gradlew.bat Update dependencies to latest & greatest. (#64) Feb 23, 2019
settings.gradle Separate testing infrastructure into it's own module. (#65) Feb 23, 2019

README.md

RxPermission

This library wraps the Android Runtime Permissions with RxJava 2. It's based on the RxPermissions library and was adjusted with simplicity in mind. Here are a few things that are different:

  • API is really small and focused
  • Uses a shadowing Activity to request the permission which allows you to use the library within Services, Broadcastreceiver etc.
  • Supports the 'Never ask again' case

Download

implementation 'com.vanniktech:rxpermission:0.7.0'
implementation 'com.vanniktech:rxpermission:0.8.0-SNAPSHOT'

Usage

The core functionality is provided via an interface:

public interface RxPermission {
  /** Requests a single permission. */
  Single<Permission> request(String permission);

  /** Requests multiple permissions. */
  Observable<Permission> requestEach(String... permissions);

  /** Returns true when the given permission is granted. */
  boolean isGranted(String permission);

  /** Returns true when the given permission is revoked by a policy. */
  boolean isRevokedByPolicy(String permission);
}

And the Permission class:

public class Permission {
  /** The name of the permission. For instance android.permission.CAMERA */
  @NonNull public String name();

  /** The state of the permission. */
  @NonNull public State state();

  public enum State {
    /** Permission has been granted. */
    GRANTED,

    /** Permission has been denied. */
    DENIED,

    /**
     * Permission is denied.
     * Previously the requested permission was denied and never ask again was selected.
     * This means that the user hasn't seen the permission dialog.
     * The only way to let the user grant the permission is via the settings now.
     */
    DENIED_NOT_SHOWN,

    /** Permission has been revoked by a policy. */
    REVOKED_BY_POLICY
  }
}

Production

For your Android application you can get an instance of the interface via RealRxPermission.getInstance(application) and then simply use the above mentioned methods to your needs.

RealRxPermission.getInstance(application)
    .request(Manifest.permission.CAMERA)
    .subscribe();

Testing

In addition the library offers you a MockRxPermission that can be used for testing. The constructor takes a vararg of Permissions.

new MockRxPermission(Permission.denied(Manifest.permission.CAMERA))
    .request(Manifest.permission.CAMERA)
    .test()
    .assertResult(Permission.denied(Manifest.permission.CAMERA));

The Permission class provides you a few static factory methods:

/** This will create a granted Camera Permission instance. */
Permission.granted(Manifest.permission.CAMERA)

/** This will create a denied Camera Permission instance. */
Permission.denied(Manifest.permission.CAMERA)

/** This will create a denied not shown Camera Permission instance. */
Permission.deniedNotShown(Manifest.permission.CAMERA)

/** This will create a revoked by policy Camera Permission instance. */
Permission.revokedByPolicy(Manifest.permission.CAMERA)

Sample

Also checkout the sample app that shows you how to use the library.

License

Copyright (C) 2017 Vanniktech - Niklas Baudy

Licensed under the Apache License, Version 2.0

You can’t perform that action at this time.