preface
When I was working on a project, I did some work about permission processing on Android M. At that time, I felt that it was still a bit complicated, so I planned to build a library to solve this problem. However, later something else happened, and this idea was shelved. Recently some time, and began to write this library, so far finally have some outline.
FcPermissions welcomes everyone to click star or submit an issue.
The body of the
1. Effect display
I made a small demo with this library, and here’s a GIF of it in action:
2. Introduce dependencies
This library is on the JitPack, and the way it depends is the way it works:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}Copy the code
Dependencies {the compile 'com. Making. Lypeer: FcPermissions: v0.0.1'}Copy the code
It’s easy. I don’t want to talk about it.
3, the use of
FcPermissions provides three ways to request permissions, implementing an interface, inheriting abstract classes, and building a Builder. I will describe the steps in each of these three ways.
3.1. Implementation interface
The framework provides a FcPermissionsCallbacks interface that can be implemented directly in activities or fragments that require permissions. This interface has two methods that need to be implemented:
void onPermissionsGranted(int requestCode, List perms)
RequestCode is the requestCode and perms is the list of approved permissions.void onPermissionsDenied(int requestCode, List perms)
RequestCode is the requestCode and perms is the list of denied permissions.
It is noteworthy that, through this method, the right to require the onPermissionsDenied FcPermissions. () method call checkDeniedPermissionsNeverAskAgain (), To handle the situation after the user clicks “Ask no more” :
@Override
public void onPermissionsDenied(int requestCode, List 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);
}Copy the code
In addition, through this way, the right needs to be rewritten Actiivty/fragments onRequestPermissionsResult () method, the parameters into the FcPermissions processing:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
FcPermissions.onRequestPermissionsResult(requestCode , permissions , grantResults , this);
}Copy the code
In need to request a permission is callable FcPermissions. RequestPermissions () method, the specific parameter information see the comments in the code.
See mainActivity.java for demo code that requests permissions in this way.
3.2. Inherit abstract classes
You may have found that when using the above way to request permission, we need to do a little more things, so on the basis of the above way I have some encapsulation, and strive to make the user as convenient as possible.
There are three abstract classes:
- FcPermissionsActivity: inherits from AppCompatActivity.
- FcPermissionsFragment: inherited from android. Support. The v4. App. The fragments.
- FcPermissionsAppFragment: inherits from Android.app. Fragment.
You can inherit from different classes as needed to complete the operation of requesting permission. They are all abstract classes, and three methods need to override to inherit them:
void onPermissionsGranted(int requestCode, List perms)
RequestCode is the requestCode and perms is the list of approved permissions.void onPermissionDenied(int requestCode, List perms)
RequestCode is the requestCode and perms is the list of denied permissions.- String getRationale4NeverAskAgain () : the return value of this method for when the user refused to provide access and select no longer bounced after tooltips content. This method must be overridden and return a valid String value.
You then simply call the requestPermissions() method where you need to requestPermissions(note: the method here is already wrapped in the abstract class, so FcPermissions is no longer required).
As you can see, this approach requires the developer to do less, but conversely, inheritance can destroy the architecture of the original project, so it’s up to you to decide which one to choose. See mainBaseActivity.java for demo code that requests permissions in this way.
3.3, Build Builder
In addition to the above two approaches, the framework provides a way to make requests for permissions by building A Builder, but note that the core class for this approach is FcPermissionsB, not the FcPemissions class.
First and the first way is consistent, need to request permission in the Activity/fragments of rewriting onRequestPermissionsResult () method, the parameters into the FcPermissionsB processing:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
mFcPermissionsB.onRequestPermissionsResult(requestCode , permissions , grantResults , this);
}Copy the code
Then you can start building the Builder and start requesting:
private void requestCameraPermission() {
mFcPermissionsB = new FcPermissionsB.Builder(this)
.onGrantedListener(new OnPermissionsGrantedListener() {
@Override
public void onPermissionsGranted(int requestCode, List perms) {
}
})
.onDeniedListener(new OnPermissionsDeniedListener() {
@Override
public void onPermissionsDenied(int requestCode, List 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);
}Copy the code
There are only three required parameters in builder:
- Rationale4ReqPer () : Prompt asking whether to request permission.
- Rationale4NeverAskAgain () : Asks if you want to change the permission prompt in the Settings after you previously selected “No more ask”.
- RequestCode () : specifies the requestCode.
Other builder parameters are not mentioned here, basically as the name suggests also know what it does, if not to know the source code to see. See MainBuilderActivity.java for demo code that requests permissions in this way.
conclusion
This is basically the end of the usage of FcPermissions. If you find any problems in the process, please send me an issue or reply to me in the comments section.
Finally, thanks to googlesample’s easypermissions, some code for this library is referenced in the library.
FcPermissions welcomes everyone to click star or submit an issue.