FcPermissions


Source link: https://github.com/lypeer/FcPermissions

FcPermissions

FcPermissions is a library to simplify basic system permissions logic when targeting Android M or higher. Chinese README is here

##Show

Installation

You can install FcPermissions by adding the following dependency to your build.gradle:

 allprojects {

repositories {

 jcenter()

 maven {
 url "https://jitpack.io" 
}

}

 
}
dependencies {

compile 'com.github.lypeer:FcPermissions:v0.0.1'
 
}

Usage

The library applies three way to request permissions : implementing an interface , extending an abstract class and creating a builder .

Implementing an interface

Firstly , let your Activity / Fragment implement FcPermissionsCallbacks interface . There are two methods in the interface :

  • void onPermissionsGranted(int requestCode, List<String> perms) : when user grants the permissions we have requested , this method will be invoked . Perms is the list of granted permissions .
  • void onPermissionsDenied(int requestCode, List<String> perms) : when user denies the permissions we have requested , this method will be invoked . Perms is the list of denied permissions .

What you should remember is that you should invoke FcPermissions.checkDeniedPermissionsNeverAskAgain() method in the onPermissionsDenied() to handler the solution user clicks "Never Ask Again" ( in the requesting dialog ) :

@Override public void onPermissionsDenied(int requestCode, List<String> perms) {

  Toast.makeText(this, R.string.prompt_been_denied, Toast.LENGTH_LONG).show();

  FcPermissions.checkDeniedPermissionsNeverAskAgain(this,

 getString(R.string.prompt_we_need_camera),

 R.string.setting, R.string.cancel, null, perms);
 
}

Otherwise , you must override the onRequestPermissionsResult method and transport its parameters to FcPermissions.onRequestPermissionsResult() :

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

  super.onRequestPermissionsResult(requestCode, permissions, grantResults);

  FcPermissions.onRequestPermissionsResult(requestCode , permissions , grantResults , this);
 
}

At last , when you want to request permissions , you can invoke the FcPermissions.requestPermissions() method . You can find the detail about this method in the source code .

A demo using this way to request permissions is MainActivity.java .

Extending an abstract class

FcPermissions provides three abstract classes :

  • FcPermissionsActivity ?its parent is AppCompatActivity .
  • FcPermissionsFragment : its parent is android.support.v4.app.fragment .
  • FcPermissionsAppFragment : its parent is android.app.fragment .

You can choose a suitable class to extend . They are abstract class , so you must override some methods after extending them :

  • void onPermissionsGranted(int requestCode, List<String> perms)?when user grants the permissions we have requested , this method will be invoked . Perms is the list of granted permissions .
  • void onPermissionDenied(int requestCode, List<String> perms)?when user denies the permissions we have requested , this method will be invoked . Perms is the list of denied permissions .
  • String getRationale4NeverAskAgain() ?The content of the dialog which shows after user click " Never Ask Again" . Returned value must be a legal String .

when you want to request permissions , you can invoke the requestPermissions() method ( NOTFcPermissions.requestPermissions() ) .

A demo using this way to request permissions is MainBaseActivity.java .

Creating a builder

The last but not least , creating a builder to request permissions . The basic class working for this way is FcPermissionB.java ( NOT FcPermissions.java above ) .

Firstly , just like what we do in the fist way , invoke the FcPermissionsB#checkDeniedPermissionsNeverAskAgain() method in the onPermissionsDenied() to handler the solution user clicks "Never Ask Again" ( in the requesting dialog ) :

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

  super.onRequestPermissionsResult(requestCode, permissions, grantResults);

  mFcPermissionsB.onRequestPermissionsResult(requestCode , permissions , grantResults , this);
 
}

Then you can create a builder and begin to request permissions :

private void requestCameraPermission() {

  mFcPermissionsB = new FcPermissionsB.Builder(this)

 .onGrantedListener(new OnPermissionsGrantedListener() {

  @Override

  public void onPermissionsGranted(int requestCode, List<String> perms) {

}

 
}
)

 .onDeniedListener(new OnPermissionsDeniedListener() {

  @Override

  public void onPermissionsDenied(int requestCode, List<String> perms) {

}

 
}
)

 .positiveBtn4ReqPer(android.R.string.ok)

 .negativeBtn4ReqPer(R.string.cancel)

 .positiveBtn4NeverAskAgain(R.string.setting)

 .negativeBtn4NeverAskAgain(R.string.cancel)

 .rationale4ReqPer(getString(R.string.prompt_request_camara))//??

 .rationale4NeverAskAgain(getString(R.string.prompt_we_need_camera))//??

 .requestCode(RC_CAMERA)//??

 .build();

  mFcPermissionsB.requestPermissions(Manifest.permission.CAMERA);
//request permissions 
}

There are three parameters must be added to builder :

  • rationale4ReqPer() ?The content in the dialog asking whether to request permissions .
  • rationale4NeverAskAgain() ?The content of the dialog which shows after user click " Never Ask Again" .
  • requestCode() ?Request code of the request .

You can find other parameters's usage in the source code .

A demo using this way to request permissions is MainBuilderActivity.java .

Why this library ?

This library is an extension of easypermissions , but I change some code in it and create two other ways to make the work easier . So it may be a better choose for you to handler permissions .

##License

Copyright 2014-2016 lypeer.  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

A night model lib to change app's night theme easy. You do not need to restart Activity.

A messaging library for Android.

A cool customizable loading animation with Dots.

A simple and easy-to-use scroll number widget.

Simple implementations of a EditText widgets, which can help to visualise inputted text. Classes are inherited from AppCompatEditText.

FSquaDRA is a tool for detection of repackaged Android applications. The approach is based on the idea that repackaged applications want to maintain "look and feel" of the originals.

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