@@ -50,7 +50,8 @@ class PermissionManager(val activity: Activity) {
50
50
*/
51
51
@PermissionDispatcherDsl
52
52
infix fun buildRequestResultsDispatcher (init : RequestResultsDispatcher .() -> Unit ) {
53
- dispatcher = RequestResultsDispatcher (this ).apply (init )
53
+ dispatcher = RequestResultsDispatcher (this )
54
+ dispatcher.apply (init )
54
55
}
55
56
56
57
/* *
@@ -63,35 +64,11 @@ class PermissionManager(val activity: Activity) {
63
64
* [RequestResultsDispatcher] is dispatched.
64
65
*
65
66
* @param requestCode The request code associated to the permissions
66
- * @param comingFromRationale true if the method is called from the rationale, defaults to false
67
+ *
68
+ * @throws UnhandledRequestCodeException if the request code is not handled by the dispatcher
67
69
*/
68
- fun checkRequestAndDispatch (requestCode : Int , comingFromRationale : Boolean = false) {
69
- val permissions =
70
- dispatcher.getPermissions(requestCode) ? : throw UnhandledRequestCodeException (
71
- requestCode
72
- )
73
- val permissionsNotGranted = permissions.filter { permission ->
74
- ActivityCompat .checkSelfPermission(
75
- activity, permission
76
- ) != PackageManager .PERMISSION_GRANTED
77
-
78
- }.toTypedArray()
79
-
80
- if (permissionsNotGranted.isEmpty()) {
81
- // All permissions are granted
82
- dispatcher.getOnGranted(requestCode)?.invoke()
83
- } else {
84
- // Some permissions are not granted
85
- val shouldShowRationale = permissionsNotGranted.any { permission ->
86
- shouldShowRequestPermissionRationale(activity, permission)
87
- }
88
-
89
- if (shouldShowRationale && ! comingFromRationale) {
90
- dispatchRationale(permissionsNotGranted, requestCode)
91
- } else {
92
- ActivityCompat .requestPermissions(activity, permissionsNotGranted, requestCode)
93
- }
94
- }
70
+ infix fun checkRequestAndDispatch (requestCode : Int ) {
71
+ checkRequestAndDispatch(requestCode, false )
95
72
}
96
73
97
74
/* *
@@ -103,41 +80,63 @@ class PermissionManager(val activity: Activity) {
103
80
* If all the permissions are already granted, the action associated to onGranted in the
104
81
* [RequestResultsDispatcher] is dispatched.
105
82
*
106
- * @param requestCode The request code associated to the permissions
107
- */
108
- infix fun checkRequestAndDispatch (requestCode : Int ) {
109
- checkRequestAndDispatch(requestCode, false )
110
- }
111
-
112
- /* *
113
- * Checks whether a set of permissions is granted or not.
83
+ * Note: `comingFromRationale` is used internally to avoid showing the rationale dialog in loop.
114
84
*
115
- * @param permissions The permissions to be checked
85
+ * @param requestCode The request code associated to the permissions
86
+ * @param comingFromRationale true if the method is called from the rationale, defaults to false
116
87
*
117
- * @return true if all permissions are granted, false otherwise
88
+ * @throws UnhandledRequestCodeException if the request code is not handled by the dispatcher
118
89
*/
119
- fun checkPermissionsGranted (permissions : Array <out String >): Boolean {
120
- for (permission in permissions) {
121
- if (! checkPermissionGranted(activity, permission)) return false
90
+ internal fun checkRequestAndDispatch (requestCode : Int , comingFromRationale : Boolean = false) {
91
+ val permissionsNotGranted = dispatcher.getPermissions(requestCode)?.filter { permission ->
92
+ ActivityCompat .checkSelfPermission(
93
+ activity, permission
94
+ ) != PackageManager .PERMISSION_GRANTED
95
+ }?.toTypedArray() ? : throw UnhandledRequestCodeException (requestCode, activity)
96
+
97
+ if (permissionsNotGranted.isEmpty()) {
98
+ // All permissions are granted
99
+ dispatcher.dispatchOnGranted(requestCode)
100
+ } else {
101
+ // Some permissions are not granted
102
+ dispatchSomePermissionsNotGranted(
103
+ permissionsNotGranted,
104
+ requestCode,
105
+ comingFromRationale
106
+ )
122
107
}
123
- return true
124
108
}
125
109
126
110
/* *
127
- * Checks whether the permissions for a given request code are granted or not.
128
- * Throws an [UnhandledRequestCodeException] if the request code is not handled.
111
+ * Dispatches the case in which some permissions are not granted
129
112
*
130
- * @param requestCode The request code associated to the permissions to be checked
131
- * @return true if all permissions are granted, false otherwise
113
+ * @param permissionsNotGranted
114
+ * @param requestCode
115
+ * @param comingFromRationale
132
116
*/
133
- fun checkPermissionsGranted (requestCode : Int ): Boolean {
134
- val permissions =
135
- dispatcher.getPermissions(requestCode) ? : throw UnhandledRequestCodeException (
136
- requestCode
137
- )
138
- return checkPermissionsGranted(permissions)
117
+ private fun dispatchSomePermissionsNotGranted (
118
+ permissionsNotGranted : Array <out String >,
119
+ requestCode : Int ,
120
+ comingFromRationale : Boolean
121
+ ) {
122
+ // if not coming from rationale, gets the list of permissions that require rationale to be shown
123
+ val permissionsRequiringRationale =
124
+ if (! comingFromRationale) getPermissionsRequiringRationale(permissionsNotGranted) else emptyList()
125
+
126
+ // if some permissions require rationale, show rationale, otherwise ask for permissions
127
+ // Note: if coming from rationale, the list will be empty, so the else branch will be executed
128
+ if (permissionsRequiringRationale.isNotEmpty()) {
129
+ dispatcher.showRationale(requestCode, permissionsRequiringRationale)
130
+ } else {
131
+ ActivityCompat .requestPermissions(activity, permissionsNotGranted, requestCode)
132
+ }
139
133
}
140
134
135
+ private fun getPermissionsRequiringRationale (permissionsNotGranted : Array <out String >) =
136
+ permissionsNotGranted.filter { permission ->
137
+ shouldShowRequestPermissionRationale(activity, permission)
138
+ }.toList()
139
+
141
140
/* *
142
141
* Checks the result of a permission request, and dispatches the appropriate action.
143
142
* The actions can be defined by the user by using the [buildRequestResultsDispatcher] function
@@ -152,42 +151,12 @@ class PermissionManager(val activity: Activity) {
152
151
dispatcher.dispatchAction(requestCode, grantResults)?.invoke()
153
152
}
154
153
155
- /* *
156
- * Checks whether a permission is granted in the context.
157
- *
158
- * @param context The context to be used for checking the permission
159
- * @param permission The permission to be checked
160
- *
161
- * @return true if the permission is granted, false otherwise
162
- */
163
- private fun checkPermissionGranted (context : Context , permission : String ): Boolean {
164
- return ActivityCompat .checkSelfPermission(
165
- context, permission
166
- ) == PackageManager .PERMISSION_GRANTED
167
- }
168
-
169
- /* *
170
- * Dispatches the rationale action associated to the request code, if any.
171
- *
172
- * @param permissionsNotGranted The permissions that are not granted
173
- * @param requestCode The request code associated to the permissions
174
- */
175
- private fun dispatchRationale (permissionsNotGranted : Array <out String >, requestCode : Int ) {
176
- /* if a rationale is defined, dispatch it, otherwise it calls checkRequestAndDispatch with
177
- * comingFromRationale = true */
178
- val toInvoke = dispatcher.getOnShowRationale(requestCode) ? : fun (
179
- _: List <String >, requestCode : Int
180
- ) {
181
- checkRequestAndDispatch(requestCode, true )
182
- }
183
- toInvoke.invoke(permissionsNotGranted.toList(), requestCode)
184
- }
185
154
}
186
155
187
156
/* *
188
157
* Exception thrown when the request code is not handled by the [RequestResultsDispatcher] object.
189
158
*/
190
- class UnhandledRequestCodeException (requestCode : Int ) : Throwable() {
159
+ class UnhandledRequestCodeException (requestCode : Int , context : Context ) : Throwable() {
191
160
override val message: String =
192
- " Request code $requestCode is not handled by the RequestResultsDispatcher object. Please add a withRequestCode block to the buildRequestResultsDispatcher function. "
161
+ context.getString( R .string.unhandled_request_code_exception_message, requestCode)
193
162
}
0 commit comments