@@ -3,11 +3,9 @@ package androidstudio.tools.missed.features.permission.domain.usecase.fetchall
3
3
import androidstudio.tools.missed.features.permission.domain.usecase.entity.PermissionStateModel
4
4
import androidstudio.tools.missed.manager.adb.command.PermissionAdbCommands
5
5
import androidstudio.tools.missed.manager.device.DeviceManager
6
- import androidstudio.tools.missed.manager.resource.ResourceManager
7
6
import kotlinx.coroutines.flow.flow
8
7
9
8
class FetchAllPermissionsUseCaseImpl (
10
- private val resourceManager : ResourceManager ,
11
9
private val deviceManager : DeviceManager
12
10
) : FetchAllPermissionsUseCase {
13
11
@@ -16,7 +14,6 @@ class FetchAllPermissionsUseCaseImpl(
16
14
17
15
packageId.let {
18
16
val allRuntimePermissionDeviceSupported = getAllRuntimePermissionDeviceSupported()
19
-
20
17
deviceManager.executeShellCommand(
21
18
PermissionAdbCommands .AllPermissionInPackageIdInstalled (packageId)
22
19
).onSuccess { result ->
@@ -28,61 +25,98 @@ class FetchAllPermissionsUseCaseImpl(
28
25
}
29
26
}
30
27
31
- private suspend fun getAllRuntimePermissionDeviceSupported (): Result < ArrayList <String > > {
28
+ private suspend fun getAllRuntimePermissionDeviceSupported (): ArrayList <String > {
32
29
val allRuntimePermissionDeviceSupported = deviceManager.executeShellCommand(
33
30
PermissionAdbCommands .AllRuntimePermissionDeviceSupported ()
34
31
)
35
-
36
- return if (allRuntimePermissionDeviceSupported.isSuccess) {
37
- val array = ArrayList <String >()
38
- allRuntimePermissionDeviceSupported.getOrNull()?.split(" \n " )?.let { array.addAll(it) }
39
- Result .success(array)
40
- } else {
41
- Result .failure(
42
- allRuntimePermissionDeviceSupported.exceptionOrNull()
43
- ? : Exception (resourceManager.string(" failedToGetPermissions" ))
44
- )
32
+ val array = ArrayList <String >()
33
+ if (allRuntimePermissionDeviceSupported.isSuccess) {
34
+ allRuntimePermissionDeviceSupported
35
+ .getOrNull()?.split(" \n " )?.let { permissionsText ->
36
+ permissionsText.forEach {
37
+ array.add(it.trim())
38
+ }
39
+ }
45
40
}
41
+ return array
46
42
}
47
43
48
44
@Suppress(" ExpressionBodySyntax" )
49
45
private fun processPermissionResult (
50
46
result : String ,
51
- allRuntimePermissionInDevice : Result < ArrayList <String > >
47
+ allRuntimePermissionDeviceSupported : ArrayList <String >
52
48
): ArrayList <PermissionStateModel > {
53
49
// Sample result
54
- // android.permission.WAKE_LOCK: granted=false
55
- // android.permission.CAMERA: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
56
-
57
- val permissions = ArrayList <PermissionStateModel >()
50
+ // requested permissions:
51
+ // android.permission.FOREGROUND_SERVICE
52
+ // android.permission.INTERNET
53
+ // android.permission.CAMERA
54
+ // ...
55
+ // ...
56
+ // install permissions:
57
+ // android.permission.NFC: granted=true
58
+ // android.permission.INTERNET: granted=true
59
+ // android.permission.BLUETOOTH_ADMIN: granted=true
60
+ // gids=[3002, 3003, 3001]
61
+ // runtime permissions:
62
+ // android.permission.ACCESS_FINE_LOCATION: granted=true
63
+ // android.permission.CAMERA: granted=true
64
+ // disabledComponents:
65
+ // ...
66
+ // enabledComponents:
67
+ // ...
68
+ // mSkippingApks:
58
69
59
- result.split(" \n " ).reversed().forEach { line ->
60
- val permissionArray = line.split(" :" )
70
+ val requestedPermissions = result
71
+ .substringAfter(" requested permissions:" )
72
+ .substringBefore(" install permissions:" )
73
+ .trim()
74
+ .split(" \n " )
75
+ .map { line ->
76
+ val isRuntime = allRuntimePermissionDeviceSupported.any { it.trim() == line.trim() }
77
+ PermissionStateModel (name = line.trim(), isGranted = false , isRuntime = isRuntime)
78
+ }
79
+ .toCollection(ArrayList ())
61
80
62
- if (permissionArray.getOrNull(0 )?.isNotEmpty() == true &&
63
- permissionArray.getOrNull(1 )?.isNotEmpty() == true &&
64
- permissionArray.getOrNull(1 )?.contains(" granted=" ) == true
65
- ) {
66
- val permissionName = permissionArray.getOrNull(0 )
67
- var isRunTimePermission = false
81
+ val installPermissions = result
82
+ .substringAfter(" install permissions:" )
83
+ .substringBefore(" runtime permissions:" )
84
+ .split(" \n " )
85
+ .filter { it.contains(" granted=" ) }
86
+ .map { line ->
87
+ val (permissionName, isGranted) = line.split(" :" )
88
+ PermissionStateModel (
89
+ name = permissionName.trim() ? : " -" ,
90
+ isGranted = isGranted.contains(" granted=true" ),
91
+ isRuntime = false
92
+ )
93
+ }
94
+ .toCollection(ArrayList ())
68
95
69
- allRuntimePermissionInDevice.onSuccess { runtimePermissions ->
70
- if (permissionName in runtimePermissions) {
71
- isRunTimePermission = true
72
- }
96
+ val runtimePermissions = result
97
+ .substringAfter(" runtime permissions:" )
98
+ .substringBefore(" mSkippingApks:" )
99
+ .split(" \n " )
100
+ .filter { it.contains(" granted=" ) }
101
+ .map { line ->
102
+ val (permissionName, isGranted) = line.split(" :" )
103
+ PermissionStateModel (
104
+ name = permissionName.trim() ? : " -" ,
105
+ isGranted = isGranted.contains(" granted=true" ),
106
+ isRuntime = true
107
+ )
108
+ }
109
+ .toCollection(ArrayList ())
73
110
74
- permissions.add(
75
- PermissionStateModel (
76
- name = permissionName ? : " -" ,
77
- isGranted = permissionArray.getOrNull(1 )?.contains(" granted=true" ) ? : false ,
78
- isRuntime = isRunTimePermission
79
- )
80
- )
81
- }.onFailure {
82
- throw Throwable (it.message)
83
- }
111
+ requestedPermissions.forEachIndexed { index, permission ->
112
+ installPermissions.find { it.name == permission.name }?.let {
113
+ requestedPermissions[index] = it
114
+ }
115
+ runtimePermissions.find { it.name == permission.name }?.let {
116
+ requestedPermissions[index] = it
84
117
}
85
118
}
86
- return permissions
119
+
120
+ return requestedPermissions
87
121
}
88
122
}
0 commit comments