Runtime Permission Handler


Source link: https://github.com/rajeefmk/runtimepermissionhandler/

Runtime Permission Handler

The library came out as an effort to reduce the boilerplate required for getting runtime permissions for Android devices running OS 6.0 and above.

The developer can now focus on handling the granted and denied state of the permission rather than dealing with the permission flow and dialogues.

Integrating into your project

This library is available in JitPack.io repository. To use it make sure that repository's url is added to the build.gradle file in your app:

repositories {

  mavenCentral()
  maven {
 url "https://jitpack.io" 
}
 
}
  dependencies {

  compile 'com.github.rajeefmk:runtimepermissionhandler:1.0.3'

// Other dependencies your app might use 
}

Usage

A sample implementation can be found here Sample app. You can also make use of the Utils.java class in the sample repo for avoiding more boilerplates.

public class MainActivity extends AppCompatActivity implements PermissionsApi.PermissionCallback {

private static final int SMS_REQUEST = 101;
  private static final int STORAGE_REQUEST = 102;
  private static final int SOME_LIST_OF_PERMISSION = 103;

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

PermissionsApi.getInstance().setPermissionCallback(this);

  
}

The permission can be checked by calling the method Permissionutils.hasPermission(Context, String[] of permission, permission rationale message, REQUEST CODE } .

The method returns true only if all requested permission is granted by the user. If any one permission is denied, this method will return false.

The 3rd param - permission rationale message - determines if the requested permission is optional (if message is null) or required ( message is provided).

/** 
  * Permission rationale won't be shown when passed null. Its recommended to pass null for 
  * those permissions which are optional. 
  */
  private void onSmsPermission() {

if (PermissionUtils.hasPermission(this, {
Manifest.permission.RECEIVE_SMS
}
,

 null, OPTIONAL_REQUEST))

//Permission is already granted. Continue exuecuting your code.
  
}

private void onStoragePermission() {

if (PermissionUtils.hasPermission(this,{
Manifest.permission.READ_EXTERNAL_STORAGE,

Manifest.permission.WRITE_EXTERNAL_STORAGE
}
,

  getString(R.string.message_permission_rationale), REQUIRED_REQUEST))

 showToastMessage(getString(R.string.message_required_permission_granted));

}

/** 
  * If a list of permissions needs to be granted, until the user grants  
  * all the permissions in the list the runtime dialogue will keep showing  
  * everytime user clicks on the button. Also, those permissions which are  

* already granted won't be asked again. 
  **/
  private void someListOfPermissions() {

if (PermissionUtils.hasPermission(this, Utils.getPermissionList(),

  getString(R.string.message_permission_rationale), SOME_LIST_OF_PERMISSION))

 showToastMessage(getString(R.string.message_permission_list_granted));

  
}

Override onRequestPermissionResult for handling permission response from user.


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

if (this.isFinishing())

 return;

  switch (requestCode) {

 case SMS_REQUEST:

  /** 

  * Passing null here instead of rationale message will result in  

* permission being treated as denied instead of showing rationale message 

  */

  PermissionUtils.onRequestPermissionsResult(mContext, requestCode,

  permissions, grantResults, null);

  break;

 case STORAGE_REQUEST:

  PermissionUtils.onRequestPermissionsResult(mContext, requestCode,

permissions, grantResults, mContext.getString(R.string.message_permission_rationale));

  break;

 case SOME_LIST_OF_PERMISSION:

  PermissionUtils.onRequestPermissionsResult(mContext, requestCode,

permissions, grantResults, mContext.getString(R.string.message_permission_rationale));

  break;

}

  
}
 

These methods are implemented for handling permission granted and denied cases.

 @Override
  public void onPermissionGranted(int requestCode) {

switch (requestCode) {

 case SMS_REQUEST:

  showToastMessage(getString(R.string.message_optional_permission_granted));

  break;

 case STORAGE_REQUEST:

  showToastMessage(getString(R.string.message_required_permission_granted));

  break;

 case SOME_LIST_OF_PERMISSION:

  showToastMessage(getString(R.string.message_permission_list_granted));

  break;

}

  
}

@Override
  public void onPermissionDenied(int requestCode) {

if (requestCode == SMS_REQUEST)

 showToastMessage("Sms permission denied but can continue.");

//Based on denied permission request code, handle the case accordingly.
  
}

TODO

  • Annotation instead of interfaces
  • Support screen rotation / configuration change

Help

Bugs, Feature requests

Found a bug? Something that's missing? Feedback is an important part of improving the project, so please open an issue.

Code

Fork this project and start working on your own feature branch. When you're done, send a Pull Request to have your suggested changes merged into the master branch by the project's collaborators. Read more about the GitHub flow.

License

Copyright 2017 Muhammed Rajeef M K  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

This library wraps the Location APIs in RxJava 2 Observables, Singles, Maybes and Completables. No more managing GoogleApiClients! Also, the resolution of the location settings check is optionally handled by the lib.

Prevent screen from turning off until your user is looking at the screen.

Monotweety is a simple Twitter Client just for tweeting from notification area.

It demonstrates reactive mvvm-like architecture and Conductor, an framework that allows building View-based Android applications.

Use one line to apply almost famous libraries of proguard configurations.

RxDownloader is an RxWrapped Download Manager.

Simple JUnit rule for overriding RxJava/RxAndroid schedulers during unit tests.

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