Android 6.0 Elegant Privilege Request Encapsulation

Keywords: Android github

Android Elegant Privilege Request Encapsulation

Let's first look at how the code is written without encapsulation. Here we first need to determine whether there is a specific permission. If there is a specific operation, otherwise we need to request permission. The final result of the request can only be processed in onRequest Permissions Result. There are several disadvantages in this way. First, your judgment logic is repetitive. Second, once you have new permissions to add, you need to modify the code in onRequest Permissions Result. According to the open-close principle of object-oriented basic principles, we try to add new code when the requirements change. It is to modify the original code.

//Request permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
    callPhone();
} else {
    //Request permission
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, CODE_CALL_PHONE);
}

//Processing permission callback
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == CODE_CALL_PHONE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        callPhone();
    }
}

The following is the encapsulated permission call. First, we request permission. The specific result will be called back to our defined function. Its function needs to be identified by annotations. Then it will be handed directly to our encapsulated Permission Handler in onRequest Permission Result, which will help us complete the function's return. The logical code of our judgment is omitted from the adjustment of work. Most importantly, there is no need to write logical validation or modify the content of onRequest Permissions Result, and all permission requests are processed uniformly.

//Request permission
PermissionHandler.requestPermissions(this, CODE_CALL_PHONE, Manifest.permission.CALL_PHONE);

//---------------------------------------------------------------- CALL_PHONE permission callback - ---------------------------------------------------------------------------------------------------
@HandlePermission(type = HandlePermission.SUCCESSFUL, requestCode = CODE_CALL_PHONE)//Successful callback of permission application
public void callPhone() {
    Toast.makeText(this, "The request of CALL_PHONE is successful!", Toast.LENGTH_SHORT).show();
}

@HandlePermission(type = HandlePermission.FAILED, requestCode = CODE_CALL_PHONE)//Callback for Failed Permission Application
private void callPhoneFailed() {
    Toast.makeText(this, "The request of CALL_PHONE is failed!", Toast.LENGTH_SHORT).show();
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionHandler.onRequestPermissionsResult(this, requestCode, permissions, grantResults);//Processing result
}

The specific process is as follows:

Last

Github: https://github.com/qylost/And...

Posted by akano on Sat, 21 Sep 2019 00:13:01 -0700