rx-android-permissions


Source link: https://github.com/beworker/rx-android-permissions

Simple RxJava library for observing and requesting Android runtime permissions introduced in Android 6.0.

Observing permissions

Sometimes it makes sense to ask user for absolutely required permissions in a separate onboarding step. Simple code down below shows either main or onboarding fragment depending on permissions granted, without asking user for those permissions. This is pure observe case.

public class MainActivity extends AppCompatActivity {

@Override public void onStart() {

super.onStart();

mSubscription = RxPermissions.get(this)

.observe(Manifest.permission.WRITE_EXTERNAL_STORAGE,

Manifest.permission.READ_EXTERNAL_STORAGE)

.subscribe(granted -> {

 if (granted) {

  // you can pass to main fragment

 
}
 else {

  // you can open onboarding fragment 

 
}

}
);

  
}

 @Override public void onStop() {

mSubsrciption.unsubsribe();

mSubsrciption = null;

super.onStop();

  
}
 
}
 

Note: Returned observable does never complete, so be sure to unsubscribe properly.

Requesting permissions

Requesting permissions requires additional binding to component (an activity or a fragment) which can ask user for permissions and receives result. Then you can request permissions. Here is how you do it.

public class OnboardingFragment extends Fragment {

  private static final int REQ_PERMISSIONS = 101;

private final PermissionsRequester mPermissionsRequester = new PermissionsRequester() {

@Override public void performRequestPermissions(String[] permissions) {

 // forward request to the system by calling fragment's method

 requestPermissions(permissions, REQ_PERMISSIONS);

}

  
}
;

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

if (requestCode == REQ_PERMISSIONS) {

 // forward response back to requester class for further processing

 mPermissionsRequester.onRequestPermissionsResult(permissions, grantResults);

}

  
}

 @Override public void onViewCreated(View view, Bundle savedInstanceState) {

super.onViewCreated(view, savedInstanceState);

mSubscription = RxPermissions.get(getActivity())

 .request(mPermissionsRequester,

  Manifest.permission.WRITE_EXTERNAL_STORAGE,

 Manifest.permission.READ_EXTERNAL_STORAGE)

 .subscribe(granted -> {

  if (granted) {

// permissions were granted, observable completes

  
}
 else {

// user denied request

  
}

 
}
);

  
}

 @Override public void onDestroyView() {

mSubsrciption.unsubsribe();

mSubsrciption = null;

super.onDestroyView();

  
}

 public void onButtonClicked(View view) {

mPermissionsRequester.request();

  
}
 
}

Observable returned by RxPermissions.request() method does only complete when all requested permissions are granted. Until then it emitts false every time user denies a permission request. You can show user a better explanation of why the app needs these permissions and ask for permissions again. Use PermissionsRequester.request() method for triggering permissions request dialog once again.

Tests & stability

Although this library has unit tests covering whole functionality described above, there is still no productive apps using it yet.

Credits

This library was inspired by https://github.com/tbruyelle/RxPermissions, but it uses a bit different design allowing pure observation of permissions and follow up permissions request without necessity to create new observable.

License

Copyright (c) 2015, 2016 Sergej Shafarenka, halfbit.de  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. 

Resources

Android Block style Button.

BlockButton is an extension of LinearLayout (thinking of extending RelativeLayout too..) hence, orientation should be set.

A tool to analysis Android/Java project dependencies.

DroidBot is a lightweight test input generator for Android.

It can send random or scripted input events to an Android app, achieve higher test coverage more quickly, and generate a UI transition graph (UTG) after testing.

A wonderful library to display 2D fancy carousels for Android.

A library of classes to create every kind of gauges.

A library used to make good looking curved SeekBar.

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