exodus-android-app
exodus-android-app copied to clipboard
Fix null show on permissions without label and description
- Some improvements
- Created new variable
name
because for permissions without label and description. After usedreplace
(with regex) method onit
,it
takes sometimes valuenull
(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.
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
}
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
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 ?
Errors is fixed on Android Studio