Coder Social home page Coder Social logo

rxpermission's Introduction

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

compile 'com.vanniktech:rxpermission:0.4.0'
compile 'com.vanniktech:rxpermission:0.5.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

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.