RxPermissions README

The project's README from github.com/tbruyelle/RxPermissions. As one markdown file: README.md.

RxPermissions

BuildVersion Build Status

This library allows the usage of RxJava with the new Android M permission model.

Setup

To use this library your minSdkVersion must be >= 14.

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.tbruyelle:rxpermissions:0.12'
}

Usage

Create a RxPermissions instance :

final RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity or Fragment instance

NOTE: new RxPermissions(this) the this parameter can be a FragmentActivity or a Fragment. If you are using RxPermissions inside of a fragment you should pass the fragment instance(new RxPermissions(this)) as constructor parameter rather than new RxPermissions(fragment.getActivity()) or you could face a java.lang.IllegalStateException: FragmentManager is already executing transactions.

Example : request the CAMERA permission (with Retrolambda for brevity, but not required)

// Must be done during an initialization phase like onCreate
rxPermissions
    .request(Manifest.permission.CAMERA)
    .subscribe(granted -> {
        if (granted) { // Always true pre-M
           // I can control the camera now
        } else {
           // Oups permission denied
        }
    });

If you need to trigger the permission request from a specific event, you need to setup your event as an observable inside an initialization phase.

You can use JakeWharton/RxBinding to turn your view to an observable (not included in the library).

Example :

// Must be done during an initialization phase like onCreate
RxView.clicks(findViewById(R.id.enableCamera))
    .compose(rxPermissions.ensure(Manifest.permission.CAMERA))
    .subscribe(granted -> {
        // R.id.enableCamera has been clicked
    });

If multiple permissions at the same time, the result is combined :

rxPermissions
    .request(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(granted -> {
        if (granted) {
           // All requested permissions are granted
        } else {
           // At least one permission is denied
        }
    });

You can also observe a detailed result with requestEach or ensureEach :

rxPermissions
    .requestEach(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 2 Permission objects
        if (permission.granted) {
           // `permission.name` is granted !
        } else if (permission.shouldShowRequestPermissionRationale) {
           // Denied permission without ask never again
        } else {
           // Denied permission with ask never again
           // Need to go to the settings
        }
    });

You can also get combined detailed result with requestEachCombined or ensureEachCombined :

rxPermissions
    .requestEachCombined(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 1 Permission object
        if (permission.granted) {
           // All permissions are granted !
        } else if (permission.shouldShowRequestPermissionRationale)
           // At least one denied permission without ask never again
        } else {
           // At least one denied permission with ask never again
           // Need to go to the settings
        }
    });

Look at the sample app for more.

Important read

As mentioned above, because your app may be restarted during the permission request, the request must be done during an initialization phase. This may be Activity.onCreate, or View.onFinishInflate, but not pausing methods like onResume, because you'll potentially create an infinite request loop, as your requesting activity is paused by the framework during the permission request.

If not, and if your app is restarted during the permission request (because of a configuration change for instance), the user's answer will never be emitted to the subscriber.

You can find more details about that here.

Status

This library is still beta, so contributions are welcome. I'm currently using it in production since months without issue.

Benefits

License

Copyright (C) 2015 Thomas Bruyelle

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.

RxPermissions (tbruyelle/RxPermissions)

permissions
Searching Database…

10.4K ·GitHub
archived
What is this?An open-source Android library that handles runtime permissions through RxJava2, wrapping the Android M permission model in observables.
Is this a library I add to my app, or a standalone tool?It is a Java library you add to your Android app, released under Apache-2.0. Your app must have minSdkVersion 14 or higher.
How do I get an RxPermissions instance set up?Create one with new RxPermissions(this) from a FragmentActivity or Fragment. Inside a fragment pass the fragment instance itself, not fragment.getActivity(), or you can hit an IllegalStateException.
Does my project need to already use RxJava2?Yes — the whole API is built on RxJava2 observables: you request permissions by subscribing rather than handling callbacks.
Can I trigger a permission request from anywhere, like onResume?No — requests must run in an initialization phase like Activity.onCreate or View.onFinishInflate, not pausing methods like onResume. Otherwise you risk an infinite request loop, and if the app restarts mid-request the user's answer is never emitted.
Can I request several permissions at once and still see each individual result?Yes. Multiple permissions can be requested together with combined results; requestEach and ensureEach emit a detailed result per permission, and requestEachCombined or ensureEachCombined give combined detailed results.
Can I fire the request from a button click?Yes, but set the event up as an observable during an initialization phase — JakeWharton's RxBinding can turn views into observables — and chain the permission request onto it.
Does it do anything special on Android versions before Marshmallow?Yes: on pre-M SDKs the observer automatically receives a granted result, so you avoid framework-version checks in your code.
Is it production-ready?The README still labels it beta, but the author used it in production for months without issue. Note the GitHub repository has since been archived, so expect no further maintenance.
Creator
tbruyelle
Size
0.6 MB
Stars
10.4K
Age
11y
Commits · 90 Days