Reactive Permissions


Source link: https://github.com/maxcruz/reactive_permissions

Reactive Permissions

Deal with Android M permissions in runtime in a simple way with reactive programming. This library was developed in Kotlin but can interoperate with Java.

  • You may define the permissions that you need (one or many at once). Each permission must be set in the Manifest

  • First verify if the permission is already granted, otherwise request to the user

  • Each permission may contain a string resource explaining why your app requires this permission. If the user denied the permission, the explanation is displayed with the possibility to retry or skip. Explanation can be omitted if the context is clear.

  • If some permissions are very important for your app functionality, you can define that can't continue without this. When an essential permission is denied, a special block dialog is displayed with retry or close options.

  • If the user choose the option "never ask again", the retry button in the block dialog opens the preferences of the app.

Flow

Example

View an example in a simple activity here

Setup

Make sure you have configured the repository jcenter(), it is almost always there

Add this to the gradle dependencies for your module:

  • For RxJava 1.x
compile 'com.github.maxcruz:reactive-permissions:1.5'
  • For RxJava 2.x
compile 'com.github.maxcruz:reactive-permissions:2.1'

Usage for Kotlin, read here for Java implementation

Define permissions as needed:

val camera = Permission(

Manifest.permission.CAMERA, // Permission constant to request

R.string.rationale_camera, // String resource with rationale explanation

true // Define if the app can continue without the permission )  val location = Permission(

Manifest.permission.ACCESS_FINE_LOCATION,

R.string.rationale_location,

false // If the user deny this permission, block the app )
val contacts = Permission(

Manifest.permission.READ_CONTACTS,

null, // The context is clear and isn't needed explanation for this permission

true )  // Put all permissions to evaluate in a single array  val permissions = listOf(location, camera, contacts)

Create the library object for the request

// Define a code to request the permissions private val REQUEST_CODE = 10 // Instantiate the library val reactive: ReactivePermissions = ReactivePermissions(this, REQUEST_CODE)

Subscribe to observe results Pair<String, Boolean>

reactive.observeResultPermissions().subscribe {
 event ->
  if (event.second) {

Toast.makeText(this, "${
event.first
}
 GRANTED :-)", Toast.LENGTH_SHORT).show()
  
}
 else {

Toast.makeText(this, "${
event.first
}
 DENIED :-(", Toast.LENGTH_SHORT).show()
  
}
 
}

Evaluate the defined permissions. Call evaluate after of register the observer

reactive.evaluate(permissions)

In the activity, receive the response from the user and pass to the lib

override fun onRequestPermissionsResult(code: Int, permissions: Array<String>, results: IntArray) {

if (code == REQUEST_CODE)

  reactive.receive(permissions, results) 
}

License

Copyright (C) 2016 Max Cruz Copyright (C) 2007 The Android Open Source Project  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.txt  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. 

Resources

Official port of ExoPlayer for Amazon devices.

Sample code for creating a 360 video player on Android. Creates an OpenGL ES scene that renders a sphere textured with video frames from MediaPlayer playback.

An helper to circle reveal/unreveal a view easily, with translations and child animations.

Android Section Adapter allows you to easily create a RecyclerView.Adapter which will automatically sort your data into sections and add a header above each section.

A simple project that shows how to remove all the boilerplate code and speed up your work with new Runtime Permissions introduced in Android M.

This plugin add an inspection that check if Activity implements following method.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes