Android permission application package processing framework. Test support 4.0+. The project originated from the formal processing of Android permissions, did not find a simple, can meet the rejected permissions automatically to the system Settings processing framework, according to their own programming habits to build a familiar wheel is quite good. The first time I used Android Studio, IT was great to code in Eclipse. From an early Android beginner who loves HTML+CSS to build user interfaces.

Project USES XXPermissions the permissions list, domestic handset permissions Settings page jump list, draw lessons from the permissions of these results onRequestPermissionsResult reception mode.

Github:github.com/xiangyuecn/…

features

  1. A function call that handles all issues with permission requests, repeatedly asking for denied permissions, and permanently asking for denied permissions (remember to select, never ask again) opens App authorization system Settings.
  2. Functional call, the code is concise and clear, read the source code without jumping to jump.
  3. Explicit authorization result callback, either with (True) or without (False).
  4. Default 0 interface, callers do not need to know, provide, import any interface including files, but can be deeply customized.
  5. Neutral, default is not bow although j-type, there is leeway to treat denied permission scenarios.
  6. Theoretically any Version of Android is supported (but only tested on 4.0-9.0 emulators).

demo

You can directly compile the test_app project for testing, or download. Assets /test_app-debug-xxx.apk test installation package.

Quick to use

Direct copy lib_comm/SRC/main/Java/ecomm lib_comm/permission inside the file to your program can be used inside.

The sample code

// If you need to obtain camera and recording permissions, implement abstract classes directly in the place where the call logic can be simple and intuitive

new UsesPermission(MainActivity.this, Permission.CAMERA, Permission.RECORD_AUDIO){
    @Override
    protected void onTrue(@NonNull ArrayList<String> lowerPermissions) {
        // Get all permissions to execute this function,
    }

    @Override
    protected void onFalse(@NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
        // Execute this function if not fully authorized
    }

    Either onTrue or onFalse will be called once
    // Or just implement the following method, which is called once no matter how many permissions are granted

    @Override
    protected void onComplete(@NonNull ArrayList<String> resolvePermissions, @NonNull ArrayList<String> lowerPermissions, @NonNull ArrayList<String> rejectFinalPermissions, @NonNull ArrayList<String> rejectPermissions, @NonNull ArrayList<String> invalidPermissions) {
        // Complete the callback, which may be all authorized, all unauthorized, or partially authorized
        / / by resolvePermissions. The contains (Permission. XXX) to determine whether authority authorized}};Copy the code

Default behavior logic

  1. Initiate the permission application directly
    • Common permission (permission not permanently rejected) If the permission is rejected (not permanently rejected), the system will arrange a pop-up application (in case of user misselection).
    • If the authorization result is permanently rejected, this part of permissions will pop up together with the permission application in the second step and jump to the App authorization system Settings.
  2. Flick the box to process the permissions that are permanently rejected (flick the box once is enough) and jump to the App authorization system setting interface.

You can refer to rewriting onTips method to modify this behavior, do not play prompt or multiple play prompt authorization.

UsesPermission class document

import ecomm.lib_comm.permission.Permission;

This class has only one external constructor, new directly invokes the authorization request. You only need to override the corresponding functions of this class to control authorization request behavior. No method of external control is provided.

The constructor

UsesPermission(activity,permissions,defaultTips=””)

Calling the constructor immediately invokes the authorization request, requesting the permissions in the Permissions list.

During the request process, the prompt information and authorization request process will be controlled according to the result returned by onTips. DefaultTips is the default prompt information used by the default implementation of onTips, which is a string of “” by default. Refer to onTips for specific logic.

Callback classes override functions

These functions are optional to override and provide an empty implementation by default.

void onTrue(lowerPermissions)

In any case, either onTrue or onFalse will definitely call back. Note: Method calls within a method that are related to the VERSION of the API should determine the VERSION of the API, as all requests for higher version permissions in lower version apis are allowed.

LowerPermissions: If a user processes a higher version of a new permission that is not supported on a lower version API, the permission is ignored and allowed. In this case, the permission will be added to this parameter.

void onFalse(rejectFinalPermissions,rejectPermissions,invalidPermissions)

Unauthorized callback, either True or False will be called, right

RejectFinalPermissions: The list of permanently rejected permissions, a subset of rejectPermissions, with an empty array indicating no such item.

RejectPermissions: List of rejected permissions, with an empty array indicating no such item.

InvalidPermissions: A list of permissions that are not declared in the manifest and will not appear in rejectFinalPermissions.

void onComplete(resolvePermissions,lowerPermissions,rejectFinalPermissions,rejectPermissions,invalidPermissions)

The authorization completion callback is called after either onTrue or onFalse is called.

ResolvePermissions: List of authorized permissions.

LowerPermissions: Refer to onTrue, a subset of resolvePermissions.

See onFalse for the rest of the parameters.

Control classes can override functions

These functions are used to control authorization behavior and all provide default implementations.

String onTips(viewTipsCount,permissions,isFinal)

One of the most core, logically complex methods in the entire class (although the default implementation is only 3 lines of code). This method controls the entire permission request process, whether to click the prompt or not, and whether to try to reapply.

This method is called back before an authorization request is issued to generate a setup prompt and also to determine whether to prompt and authorize. If null is returned, the next operation is not performed. The return string is prompted to request authorization. Note: this method will be called multiple times with different permissions; These permissions will not be invoked by default if the user clicks cancel (you can override onCancelTips to change this behavior).

The default behavior is:

  1. Initiate the permission application directlyviewTipsCount=0
    • Common permission (not permanently rejected) If the application is rejected (not permanently rejected), a pop-up application is arranged (to prevent false selection).
    • If the result is permanently rejected, this part of the permission will be processed together with the subsequent permanent permission request.
  2. Flick the box to process the permissions that are permanently rejected (flick the box once is enough) and jump to the App authorization system setting interfaceviewTipsCount=1+
@param viewTipsCount 0-n Indicates the number of times to prepare the pop-up prompt box. 0: indicates the boot prompt before application. Null is returned to indicate that the prompt is not played and authorization is directly invoked. Note: 0 contains all permissions (excluding permissions with custom authorization requests). There is no way to distinguish whether permissions are permanently denied. 1+: the permission application is permanently rejected, or the common permission application was rejected in the last round. Note: if the value is 1, the permission with a custom authorization request will be prompted and the authorization request will be invoked even if null is returned. @param isFinal Whether this list of permissions is permanently denied,trueIs that,falseIs not permanently rejected @returnA prompt message is displayed. If the return value is null, no application is performed. Automatically generates a reasonable prompt when it is an empty string. String contents Support specific placeholders: {Auto}: replaces rejected permission Names with automatically generated prompt content. For example:'xx"{Names}"xx'= >'xx 'permission name 1, permission name 2, permission name 3"xx'
Copy the code

String onCancelTips(viewCancelCount,permissions,isFinal)

Do not recommend rewriting, cancel also play prompt is not friendly, pop up prompt, the user clicked the cancellation of the additional prompt information. Return NULL No longer calls the prompt completely, the default is null. This callback is used exactly the same as onTips, except that it only works when cancel is clicked. Note: This method should be overridden with care, so that viewCancelCount returns null after a maximum of several times to avoid the problem of being unable to cancel the forever popup.

ViewCancelCount 1-n The number of times the current request has been cancelled.

String onTipsDialogView(tips,isCancel,viewTipsCount,permissions,isFinal,okCall,cancelCall)

Authorization prompt pop-up, rewrite this method to customize the pop-up behavior, the default use of AlertDialog dialog. You only need to call either okCall or cancelCall. How to display the interface and how many interfaces to display -> optional.

The Permission class document

import ecomm.lib_comm.permission.Permission;

This class encapsulates the dangerous permission list of version 8.0 and provides mapping of the corresponding permission names.

Permissions list

It is recommended to use the permissions defined in this class, such as Permission.camera. If use the Manifest. Permission. CAMERA can also, they are equivalent.

A static method

String QueryName(permission)

The name of the query Permission, for example, permission. CAMERA, is CAMERA.

String QueryNames(permissions)

Obtain the list of permission names, such as permission name 1, permission name 2, and permission name 3. This method is used to easily generate authorization messages.

Best practices

Use the default implementation

Do not override onTips as the default implementation.

When requesting authorization, a request will pop up directly. If the user clicks reject (not permanent), a prompt will be played to prevent the user from missing the request. If a permission is permanently denied, the system prompts you to go to system Settings.

Prompt before authorization

Pop the prompt before invoking authorization, otherwise the behavior is the same as the default implementation. Rewrite the onTips method:

@Override
protected String onTips(int viewTipsCount, @NonNull ArrayList<String> permissions, boolean isFinal) {
    if(viewTipsCount<=1) {
        return "";
    }
    return null;
}
Copy the code

Does not authorize forever flick hint

If the unauthorized function is not available, you can continue to play the prompt until the user clicks cancel (you can override onCancelTips to make it impossible to cancel, but it is not recommended). Rewrite the onTips method:

protected String onTips(int viewTipsCount, @NonNull ArrayList<String> permissions, boolean isFinal) {
    if(viewTipsCount>0) {// If you need a warning before authorization is invoked, just drop the if
        return "";
    }
    return null;
}
Copy the code

Silent Mode Authorization

If the permission is denied, no prompt is displayed and the permission system setting page is not redirected. It is only used to invoke authorization requests. Except for permission Settings that require boot (such as installation, suspension window permission). Rewrite the onTips method:

protected String onTips(int viewTipsCount, @NonNull ArrayList<String> permissions, boolean isFinal) {
    return null;
}
Copy the code

Customize the prompt interface

By default, the system AlertDialog is used when playing prompt, and onTipsDialogView method can be rewritten to use its own prompt interface. For example, HiPermission has this nice interface:

This library does not provide interface implementation, need to implement itself.

More to achieve

See MainActivity in the test_app directory for the implemented code.

Related source code please go to Github to check, if this library helps you, please Star.