TedPermission


Source link: https://github.com/ParkSangGwon/TedPermission

What is TedPermission?

After the update to Android 6.0 Marshmallow, we have to not only declare permissions in AndroidManifest.xml, but also request them at runtime. Furthermore, user can on/off permissions in application setting anytime.
When you use dangerous permissons(ex. CAMERA, READ_CONTACTS, READ_PHONE_STATE, ...), you must check and request them runtime.
( http://developer.android.com/intl/ko/guide/topics/security/permissions.html#normal-dangerous)

You can make your own permission check logic like this, but it's so complex and hard to use functions Google offers: checkSelfPermission(), requestPermissions(), onRequestPermissionsResult(), onActivityResult().

TedPermission makes it easy to check and request android permissions.

(For Korean) ?? ???? ?? ????? ????? ??? ?????
http://gun0912.tistory.com/55

Demo



  1. Request Permissions.
  2. If user denied permissions, we will show message dialog with Setting button.

Setup

Gradle

Edit root/app/build.gradle like below.

Normal

dependencies {

  compile 'gun0912.ted:tedpermission:2.1.0' 
}

RxJava1

dependencies {

  compile 'gun0912.ted:tedpermission-rx1:2.1.0' 
}

RxJava2

dependencies {

  compile 'gun0912.ted:tedpermission-rx2:2.1.0' 
}

If you think this library is useful, please press star button at upside.



How to use

Normal

-Make PermissionListener

We will use PermissionListener for Permission Result. You will get result to onPermissionGranted(), onPermissionDenied()

 PermissionListener permissionlistener = new PermissionListener() {

@Override

public void onPermissionGranted() {

 Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();

}

 @Override

public void onPermissionDenied(ArrayList<String> deniedPermissions) {

 Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();

}

 
}
;

-Start TedPermission

TedPermission class requires setPermissionListener(), setPermissions(), and check() check() will start to check permissions.

setRationaleMessage() and setDeniedMessage() are optional methods.

 TedPermission.with(this)
  .setPermissionListener(permissionlistener)
  .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
  .setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
  .check();



RxJava1

If you use RxJava1, You can use request() method instead check() When Permission check finish, you can receive tedPermissionResult instance. tedPermissionResult instance has isGranted(), getDeniedPermissions()

 TedRxPermission.with(this)

.setDeniedMessage(

 "If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")

.setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)

.request()

.subscribe(tedPermissionResult -> {

  if (tedPermissionResult.isGranted()) {

 Toast.makeText(RxJava1Activity.this, "Permission Granted", Toast.LENGTH_SHORT).show();

  
}
 else {

 Toast.makeText(RxJava1Activity.this,

  "Permission Denied\n" + tedPermissionResult.getDeniedPermissions().toString(), Toast.LENGTH_SHORT)

  .show();

  
}

}
, throwable -> {

}
, () -> {

}
);
 



RxJava2

Also RxJava2 can use request() like RxJava1

 TedRx2Permission.with(this)

.setRationaleTitle(R.string.rationale_title)

.setRationaleMessage(R.string.rationale_message) // "we need permission for read contact and find your location"

.setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)

.request()

.subscribe(tedPermissionResult -> {

  if (tedPermissionResult.isGranted()) {

 Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();

  
}
 else {

 Toast.makeText(this,

  "Permission Denied\n" + tedPermissionResult.getDeniedPermissions().toString(), Toast.LENGTH_SHORT)

  .show();

  
}

}
, throwable -> {

}
, () -> {

}
);

Customize

TedPermission support this method

  • setGotoSettingButton(boolean) (default: true)
  • setRationaleTitle(R.string.xxx or String)
  • setRationaleMessage(R.string.xxx or String)
  • setRationaleConfirmText(R.string.xxx or String) (default: confirm / ??)
  • setDeniedTitle(R.string.xxx or String)
  • setDeniedMessage(R.string.xxx or String)
  • setDeniedCloseButtonText(R.string.xxx or String) (default: close / ??)
  • setGotoSettingButtonText(R.string.xxx or String) (default: setting / ??)

Also you can use util function.

  • isGranted(Context context, String... permissions): Check permissions all granted
  • isDenied(Context context, String... permissions): Check permissions all denied
  • getDeniedPermissions(Context context, String... permissions)
  • canRequestPermission(Activity activity, String... permissions): If true you can request system popup, false mean user checked Never ask again
  • startSettingActivityForResult(Activity activity)
  • startSettingActivityForResult(Activity activity, int requestCode)



Number of Cases

  1. Check permissions -> have permissions
    : onPermissionGranted() called

  2. Check permissions -> don't have permissions
    : show request dialog

  3. show request dialog -> granted permissions
    : onPermissionGranted() called

  4. show request dialog -> denied permissions
    : show denied dialog

  5. show denied dialog -> close
    : onPermissionDenied() called

  6. show denied dialog -> setting
    : startActivityForResult() to setting activity

  7. setting activity -> onActivityResult()
    : check permission

  8. check permission -> granted permissions
    : onPermissionGranted() called

  9. check permission -> denied permissions
    : onPermissionDenied() called





License

Copyright 2017 Ted Park  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 project allows to easily setup a vertical menu for Android.

This project created to help developer to have better experience in using Realm-Java

An Android view for displaying repeated continuous side scrolling images. This can be used to create a parallax animation effect.

A Promise wrapper around RxJava's Observable. A promise represents a future value (usually of an asynchronous operation).

Postman is a java library that uses code generation to handle the details of implementing the Parcelable interface on Android.

Metajava is a collection of utilities that make writing annotation processors and code generation easier.

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