PermissionX is an open source project that originated in my new book, Line 1, Version 3, which was published this year. Its primary purpose was to teach readers how to develop and publish an open source library. However, as I realized that PermissionX was not only valuable to learn from, but also useful for real projects, I went through several iterations of PermissionX, and it has become a very stable and convenient library for permission requests.

In version 1.3.0, PermissionX supports the ability to customize permission notification dialogs, which has long been criticized as ugly dialogs. Now feel free to customize any dialog box that fits your project’S UI style. See the PermissionX blockbuster update in this article for details on how to do this, including support for custom permission notification dialog boxes.

Today, WE are pleased to announce that PermissionX has released its latest version 1.4.0, which will make the entire permission request process a better user experience. A better user experience usually leads to higher permission request approval rates, so this is a significant release update.

So let’s take a look at what’s new with PermissionX 1.4.0.

Support DialogFragment

As mentioned earlier, in the previous release, PermissionX introduced the ability to customize permission notification dialogs, giving developers the freedom to customize the dialogs as they wish. For example, IN my last article, I gave an example of the following effect picture:

The permission notification Dialog box is implemented based on the Dialog class. The developers need to inherit the RationaleDialog provided by PermissionX and implement the necessary methods when customizing the Dialog box.

I personally don’t see any problem with this implementation. PermissionX controls the entire permission request process, and the developer has free control over the PRESENTATION of the UI, which is perfect.

However, a friend on GitHub raised a question about this solution because he wanted to use DialogFragment to customize permission notification dialogs, and PermissionX’s restrictions meant he could only use dialogs.

DialogFragment is also a Dialog box implementation scheme recommended by Google. Although I think DialogFragment can achieve all functions with Dialog, it does seem a little unfriendly to use Dialog forcibly.

So I started thinking about completely rewriting the implementation of custom dialogs, so that instead of forcing a fixed implementation, I would just provide interface standards, and the implementation would be entirely up to the developer.

Trust me, I did. My idea at the time was that PermissionX would only define the necessary interface to display Dialog boxes, close Dialog boxes, etc. There was no restriction on the implementation. You could use Dialog boxes, dialogFragments, even PopupWindow, or completely custom controls.

However, after actually using this scheme, I found it was not a good choice. Since PermissionX controls the entire permission request process internally, developers don’t need to worry about the internal logic, just customize the interface. However, after the implementation of the dialog box is relaxed, developers need to be responsible for their implementation of the dialog box, you need to consider the user clicks ok button to request permission again, you need to consider the user clicks cancel button to call back the result of the request, you need to consider how to prevent the loss of the permission request event when the dialog box cancels, You need to think about how to prevent window leaks when you rotate vertically and horizontally.

I can’t help but wonder, is PermissionX really working?

So I ended up rolling back all the code I had written, rejecting the solution because I didn’t want to burden developers with the flexibility they never knew they would need.

Although the above scheme is denied, the demand for DialogFragment actually exists. So I ended up doing what I had done before, adding a Rational Fragment class that inherits from DialogFragment and defines the necessary methods required by PermissionX. Developers in the use of DialogFragment custom dialog box will be the same several necessary methods can be implemented. The overall implementation process is exactly the same as version 1.3.0.

It may seem a little clunky, but trust me, it’s probably the best solution. Otherwise your PermissionX will become very unstable, struggling with frequent crashes and lost permission events.

So much for DialogFragment support. But that’s not the point of this article.

The best user experience ever

Why do so many people want to customize permission alert dialogs? Because PermissionX’s default permission alert dialog is ugly.

For example, we call the following code to apply for permission:

PermissionX.init(this) .permissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.RECORD_AUDIO) .onExplainRequestReason { scope, DeniedList - > val message = "PermissionX need you agree to the following permissions can be normal use" scope. ShowRequestReasonDialog (deniedList, the message, "sure," Request {allGranted, grantedList, deniedList -> if (allGranted) {Toast. MakeText (activity, "All permissions approved ", Toast.length_short).show()} else {toast.maketext (activity, "You rejected the following permissions: $deniedList", toast.length_short).show()}}Copy the code

When a request for permission is denied, PermissionX will bring up the permission notification dialog shown in the figure below.

This dialog worked fine in our test application, but certainly not in our online project because the experience was not user-friendly.

In order to make the permission notification dialog more beautiful, PermissionX introduced the custom dialog style function in version 1.3.0, and I also demonstrated the implementation process of a custom dialog box. The final dialog box effect is as follows:

We can see that with the use of custom dialog, we can freely control the elements and content on the interface, and the user experience is significantly improved.

However, even so, some friends left comments saying the dialog was too ugly (one person said it was ugly and 42 liked it).

But this ugly I do not carry the pot, I just to demonstrate the implementation process of custom dialog box, specific dialog box ugly and beauty or in your own hands.

Now that everything seems to be supported, what has been optimized in PermissionX 1.4.0?

Most importantly, the default permission alert dialog has been greatly improved in version 1.4.0.

This may not seem like a big deal, but on the contrary, optimizing the default permission alert dialog is important, which greatly improves the user experience of PermissionX and greatly reduces the cost of using PermissionX, since customizing dialogs is generally cumbersome.

Now, as soon as you upgrade to version 1.4.0, you will get a completely different permission notification dialog with the same code as above, as shown below:

It can be seen that the current dialog box is undoubtedly better than the previous dialog box in terms of user experience, and the user will see such an interface more pleasing to the eye.

So how does PermissionX do it?

First, PermissionX learns which groups these permissions belong to based on the deniedList passed in by the developer in the showRequestReasonDialog() method. In fact, the interface does not need to display all the permissions in the deniedList, but only show the name of the permission group to apply for, which can make the interface more simplified.

It is important to note that on Android 9 and below, we can use the system API to automatically obtain a certain permission group, as shown in the code below:

context.packageManager.getPermissionInfo(permission, 0).group

Copy the code

Google has disabled this feature since Android 10, so in later versions you will need to manually set which permission group each runtime permission corresponds to. This is a fairly tedious task, but fortunately PermissionX handles this functionality internally.

In addition, in order to enrich the interface elements, we also add the corresponding icon in front of the name of each permission group. The icon can be obtained by the following code:

context.packageManager.getPermissionGroupInfo(permissionGroup, 0).icon

Copy the code

The result is a rich and pleasing permission alert dialog.

However, PermissionX does much more than that. As we all know, Starting with Android 10, Google has introduced dark themes, and a good App should automatically switch to dark as soon as the user turns on dark themes.

PermissionX also ADAPTS this feature. After the phone is enabled with dark theme mode, the permission alert dialog box looks like this:

The colors on the dialogs have been carefully adjusted by me to make them look very comfortable regardless of whether they are dark or light themes.

In addition, if you want to make an overseas version of your App, PermissionX also has native support. You just need to make sure that the text you pass to PermissionX is translated. The rest of the text will be displayed according to the language of the current phone system.

For example, we switch the language of the mobile phone system to English, and then use the following code for permission request:

PermissionX.init(this)
    .permissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.RECORD_AUDIO)
    .onExplainRequestReason { scope, deniedList ->
        val message = "PermissionX needs following permissions to continue"
        scope.showRequestReasonDialog(deniedList, message, "Allow", "Deny")
    }
    .request { allGranted, grantedList, deniedList ->
        ...
    }

Copy the code

The final permission alert dialog box looks like the following:

Here’s another example of the Japanese version:

So far, PermissionX has basically achieved a two-tier standard of providing the simplest API for developers and the best user experience for users.

In addition we can also through the combination of a explainReasonBeforeRequest () method, let permissions remind dialog before starting the request permissions, it can be done to explain why application, to perform the function of request permissions. In general, such permission requests are more user-friendly and more likely to be granted.

PermissionX.init(this)
    .permissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.RECORD_AUDIO)
    .explainReasonBeforeRequest()
    ...

Copy the code

Finally, let’s go through the entire permission request process with an animation.

Custom TintColor

Although the colors used in the default permission alert dialog are carefully adjusted by me, they may not be appropriate for your project. Since each project will have its own theme color, it may be more appropriate to set a color that matches the theme of the project rather than using the default color.

Currently, the default permission notification dialog box, the permission group icon, OK, cancel button, use blue. If you feel that the color doesn’t match the theme of your project, you can set it to any color you want.

The setDialogTintColor() method is simply attached to the PermissionX request chain, as shown below:

PermissionX.init(this)
    .permissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.RECORD_AUDIO)
    .setDialogTintColor(Color.parseColor("#008577"), Color.parseColor("#83e8dd"))
    ...

Copy the code

Here the setDialogTintColor() method takes two arguments, the first of which is to set the color value for the light-colored theme and the second is to set the color value for the dark-colored theme. Since you want to customize the color values, you must take into account both light and dark themes.

Now, after re-running the program, the dialog box under the light theme and the dark theme should look like the following:

       

As you can see, the color of the permission group icon, ok, and cancel buttons on the dialog box have been changed to make PermissionX more appropriate to the theme of your project.

How to upgrade

PermissionX < span style = “box-sizing: border-box; color: RGB (51, 51, 51); line-height: 22px; font-size: 14px! Important; word-break: inherit! Important;”

dependencies { ... Implementation 'com. Permissionx. Guolindev: permissionx: 1.4.0'}Copy the code

If your project has not been upgraded to AndroidX, you can use the same version of permission-support.

dependencies { ... Implementation 'com. Permissionx. Guolindev: permission - support: 1.4.0'}Copy the code

The project home address for PermissionX is:

Github.com/guolindev/P…

In addition, this article focuses on the new features of PermissionX version 1.4.0. If you are new to PermissionX, you can learn about it step by step through my PermissionX Permissions series, which explains how to use it in great detail.

If you want to learn about Kotlin and the latest on Android, check out my new book, Line 1, Version 3. Click here for more details.

Pay attention to my technical public account “Guo Lin”, there are high-quality technical articles pushed every week.

This article was written by me last year and is now transferred to the nuggets.