exodus-android-app icon indicating copy to clipboard operation
exodus-android-app copied to clipboard

Fix null show on permissions without label and description

Open Jean-BaptisteC opened this issue 2 years ago • 3 comments

  • Some improvements
  • Created new variable name because for permissions without label and description. After used replace(with regex) method on it, it takes sometimes value null (Detected on Android 8, no problem on Android 12)

Do you have an idea to delete permissions null with Label from packageManager ? I have tried with method removeAll on permsList before SortBy but, i think i have forgotten something.

Jean-BaptisteC avatar Jul 26 '22 15:07 Jean-BaptisteC

In Kotlin, a nullable variable is represented by a ?. Here you only check that the string is equal to the string "null", but not that the variable itself is null.

To replace the anonymous variable it by name or permissionName that is not null, you have to use the convention that allows to get the variable from the forEach. permissionList.forEach { permissionName -> ... }

Also, the definition of the label variable is dangerous here because there is no ? before the toString(), which means that Kotlin will try to transform it into a string even if it is a null variable.

val label = permInfo?.loadLabel(packageManager).toString() => label type is String, maybe "null" as a string, null isn't allowed

val label = permInfo?.loadLabel(packageManager)?.toString() => label type is String?, null is allowed

Also, one of the basic rules in dev is to never copy and paste code because if you do, the code can be replaced by a kind of encapsulation. i.e. if you want to change a regex, you have to do it in every place, if it's in a variable, you only do it once.

Another thing, there is no need of a mutable permissionList.

Here is a piece of code I haven't tested that encapsulates things better. You can see the ?.let which allows to manage the null variable. label variable is now known as not null. the ?: run allows to manage the other case, a bit like an else, I don't know what the need or the problem really is in your case but this is my modification :

    private fun getPermissionList(
        permissionList: List<String>,
        packageManager: PackageManager
    ): List<Permission> {
        val permsList = mutableListOf<Permission>()
        permissionList.forEach { permissionName ->
            var permInfo: PermissionInfo? = null
            try {
                permInfo = packageManager.getPermissionInfo(
                    permissionName,
                    PackageManager.GET_META_DATA
                )
            } catch (exception: PackageManager.NameNotFoundException) {
                Log.d(TAG, "Unable to find info about $permissionName")
            }

            // Encapsulate regex modification
            val permissionString = permissionName.replace("[^>]*[a-z][.]".toRegex(), "")

            // Labels and desc can be null for undocumented permissions, filter them out
            permInfo?.loadLabel(packageManager)?.let { label ->
                permsList.add(
                    Permission(
                        permissionString,
                        label.toString(),
                        permInfo.loadDescription(packageManager)?.toString() ?: "",
                    )
                )
            } ?: run {
                permsList.add(
                    Permission(
                        permissionString,
                        permissionName,
                    )
                )
            }
        }
        permsList.sortBy { it.permission }
        return permsList
    }

J-Jamet avatar Aug 02 '22 16:08 J-Jamet

Thanks for review, you can merge your changes on permission branch. null permission is always present on app, but i think is specially only on Huawei app.

permissionString = null permissionName = Null

Jean-BaptisteC avatar Aug 02 '22 16:08 Jean-BaptisteC

Test PASS ✅ But i have errors on Android Studio with import import androidx.test.ext.junit.runners.AndroidJUnit4 and @RunWith(AndroidJUnit4::class)

@J-Jamet Can you check errors ?

Jean-BaptisteC avatar Aug 15 '22 16:08 Jean-BaptisteC

Errors is fixed on Android Studio

Jean-BaptisteC avatar Aug 23 '22 15:08 Jean-BaptisteC