/
PermissionsService.kt
308 lines (264 loc) · 12.2 KB
/
PermissionsService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package expo.modules.permissions
import android.Manifest
import android.annotation.TargetApi
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.facebook.react.modules.core.PermissionAwareActivity
import org.unimodules.core.ModuleRegistry
import org.unimodules.core.Promise
import org.unimodules.core.interfaces.ActivityProvider
import org.unimodules.core.interfaces.InternalModule
import org.unimodules.core.interfaces.LifecycleEventListener
import org.unimodules.core.interfaces.services.UIManager
import org.unimodules.interfaces.permissions.Permissions
import org.unimodules.interfaces.permissions.PermissionsResponse
import org.unimodules.interfaces.permissions.PermissionsResponseListener
import org.unimodules.interfaces.permissions.PermissionsStatus
private const val PERMISSIONS_REQUEST: Int = 13
private const val PREFERENCE_FILENAME = "expo.modules.permissions.asked"
internal const val ERROR_TAG = "E_PERMISSIONS"
open class PermissionsService(val context: Context) : InternalModule, Permissions, LifecycleEventListener {
private var mActivityProvider: ActivityProvider? = null
// state holders for asking for writing permissions
private var mWriteSettingsPermissionBeingAsked = false // change this directly before calling corresponding startActivity
private var mAskAsyncListener: PermissionsResponseListener? = null
private var mAskAsyncRequestedPermissions: Array<out String>? = null
private lateinit var mAskedPermissionsCache: SharedPreferences
private fun didAsk(permission: String): Boolean = mAskedPermissionsCache.getBoolean(permission, false)
private fun addToAskedPermissionsCache(permissions: Array<out String>) {
with(mAskedPermissionsCache.edit()) {
permissions.forEach { putBoolean(it, true) }
apply()
}
}
override fun getExportedInterfaces(): List<Class<out Any>> = listOf(Permissions::class.java)
@Throws(IllegalStateException::class)
override fun onCreate(moduleRegistry: ModuleRegistry) {
mActivityProvider = moduleRegistry.getModule(ActivityProvider::class.java)
?: throw IllegalStateException("Couldn't find implementation for ActivityProvider.")
moduleRegistry.getModule(UIManager::class.java).registerLifecycleEventListener(this)
mAskedPermissionsCache = context.applicationContext.getSharedPreferences(PREFERENCE_FILENAME, Context.MODE_PRIVATE)
}
override fun getPermissionsWithPromise(promise: Promise, vararg permissions: String) {
getPermissions(PermissionsResponseListener { permissionsMap: MutableMap<String, PermissionsResponse> ->
val areAllGranted = permissionsMap.all { (_, response) -> response.status == PermissionsStatus.GRANTED }
val areAllDenied = permissionsMap.all { (_, response) -> response.status == PermissionsStatus.DENIED }
val canAskAgain = permissionsMap.all { (_, response) -> response.canAskAgain }
promise.resolve(Bundle().apply {
putString(PermissionsResponse.EXPIRES_KEY, PermissionsResponse.PERMISSION_EXPIRES_NEVER)
putString(PermissionsResponse.STATUS_KEY, when {
areAllGranted -> PermissionsStatus.GRANTED.status
areAllDenied -> PermissionsStatus.DENIED.status
else -> PermissionsStatus.UNDETERMINED.status
})
putBoolean(PermissionsResponse.CAN_ASK_AGAIN_KEY, canAskAgain)
putBoolean(PermissionsResponse.GRANTED_KEY, areAllGranted)
})
}, *permissions)
}
override fun askForPermissionsWithPromise(promise: Promise, vararg permissions: String) {
askForPermissions(PermissionsResponseListener {
getPermissionsWithPromise(promise, *permissions)
}, *permissions)
}
override fun getPermissions(responseListener: PermissionsResponseListener, vararg permissions: String) {
responseListener.onResult(parseNativeResult(permissions, permissions.map {
if (isPermissionGranted(it)) {
PackageManager.PERMISSION_GRANTED
} else {
PackageManager.PERMISSION_DENIED
}
}.toIntArray()))
}
@Throws(IllegalStateException::class)
override fun askForPermissions(responseListener: PermissionsResponseListener, vararg permissions: String) {
if (permissions.contains(Manifest.permission.WRITE_SETTINGS) && isRuntimePermissionsAvailable()) {
val permissionsToAsk = permissions.toMutableList().apply { remove(Manifest.permission.WRITE_SETTINGS) }.toTypedArray()
val newListener = PermissionsResponseListener {
val status = if (hasWriteSettingsPermission()) {
PackageManager.PERMISSION_GRANTED
} else {
PackageManager.PERMISSION_DENIED
}
it[Manifest.permission.WRITE_SETTINGS] = getPermissionResponseFromNativeResponse(Manifest.permission.WRITE_SETTINGS, status)
responseListener.onResult(it)
}
if (!hasWriteSettingsPermission()) {
if (mAskAsyncListener != null) {
throw IllegalStateException("Another permissions request is in progress. Await the old request and then try again.")
}
mAskAsyncListener = newListener
mAskAsyncRequestedPermissions = permissionsToAsk
addToAskedPermissionsCache(arrayOf(Manifest.permission.WRITE_SETTINGS))
askForWriteSettingsPermissionFirst()
} else {
askForManifestPermissions(permissionsToAsk, newListener)
}
} else {
askForManifestPermissions(permissions, responseListener)
}
}
override fun hasGrantedPermissions(vararg permissions: String): Boolean {
return permissions.all { isPermissionGranted(it) }
}
/**
* Checks whether given permission is present in AndroidManifest or not.
*/
override fun isPermissionPresentInManifest(permission: String): Boolean {
try {
context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_PERMISSIONS)?.run {
return requestedPermissions.contains(permission)
}
return false
} catch (e: PackageManager.NameNotFoundException) {
return false
}
}
/**
* Checks status for Android built-in permission
*
* @param permission [android.Manifest.permission]
*/
private fun isPermissionGranted(permission: String): Boolean {
return when (permission) {
// we need to handle this permission in different way
Manifest.permission.WRITE_SETTINGS -> hasWriteSettingsPermission()
else -> getManifestPermission(permission) == PackageManager.PERMISSION_GRANTED
}
}
/**
* Gets status for Android built-in permission
*
* @param permission [android.Manifest.permission]
*/
private fun getManifestPermission(permission: String): Int {
mActivityProvider?.currentActivity?.let {
if (it is PermissionAwareActivity) {
return ContextCompat.checkSelfPermission(it, permission)
}
}
// We are in the headless mode. So, we ask current context.
return getManifestPermissionFromContext(permission)
}
protected open fun getManifestPermissionFromContext(permission: String): Int {
return ContextCompat.checkSelfPermission(context, permission)
}
private fun canAskAgain(permission: String): Boolean {
return mActivityProvider?.currentActivity?.let {
ActivityCompat.shouldShowRequestPermissionRationale(it, permission)
} ?: false
}
private fun parseNativeResult(permissionsString: Array<out String>, grantResults: IntArray): Map<String, PermissionsResponse> {
return HashMap<String, PermissionsResponse>().apply {
grantResults.zip(permissionsString).forEach { (result, permission) ->
this[permission] = getPermissionResponseFromNativeResponse(permission, result)
}
}
}
private fun getPermissionResponseFromNativeResponse(permission: String, result: Int): PermissionsResponse {
val status = when {
result == PackageManager.PERMISSION_GRANTED -> PermissionsStatus.GRANTED
didAsk(permission) -> PermissionsStatus.DENIED
else -> PermissionsStatus.UNDETERMINED
}
return PermissionsResponse(
status,
if (status == PermissionsStatus.DENIED) {
canAskAgain(permission)
} else {
true
}
)
}
protected open fun askForManifestPermissions(permissions: Array<out String>, listener: PermissionsResponseListener) {
if (!isRuntimePermissionsAvailable()) {
// It's not possible to ask for the permissions in the runtime.
// We return to the user the permissions status, which was granted during installation.
addToAskedPermissionsCache(permissions)
val permissionsResult = permissions.map { getManifestPermission(it) }.toIntArray()
listener.onResult(parseNativeResult(permissions, permissionsResult))
return
}
delegateRequestToActivity(permissions, listener)
}
/**
* Asks for Android built-in permission
* According to Android documentation [android.Manifest.permission.WRITE_SETTINGS] need to be handled in different way
*
* @param permissions [android.Manifest.permission]
*/
protected fun delegateRequestToActivity(permissions: Array<out String>, listener: PermissionsResponseListener) {
addToAskedPermissionsCache(permissions)
mActivityProvider?.currentActivity?.run {
if (this is PermissionAwareActivity) {
requestPermissions(permissions, PERMISSIONS_REQUEST) { requestCode, receivePermissions, grantResults ->
return@requestPermissions if (requestCode == PERMISSIONS_REQUEST) {
listener.onResult(parseNativeResult(receivePermissions, grantResults))
true
} else {
listener.onResult(parseNativeResult(receivePermissions, IntArray(receivePermissions.size) { PackageManager.PERMISSION_DENIED }))
false
}
}
} else {
listener.onResult(parseNativeResult(permissions, IntArray(permissions.size) { PackageManager.PERMISSION_DENIED }))
}
}
}
/**
* Asking for [android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS] via separate activity
* WARNING: has to be asked first among all permissions being asked in request
* Scenario that forces this order:
* 1. user asks for "systemBrightness" (actual [android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS]) and for some other permission (e.g. [android.Manifest.permission.CAMERA])
* 2. first goes ACTION_MANAGE_WRITE_SETTINGS that moves app into background and launches system-specific fullscreen activity
* 3. upon user action system resumes app and [onHostResume] is being called for the first time and logic for other permission is invoked
* 4. other permission invokes other system-specific activity that is visible as dialog what moves app again into background
* 5. upon user action app is restored and [onHostResume] is being called again, but no further action is invoked and promise is resolved
*/
@TargetApi(Build.VERSION_CODES.M)
private fun askForWriteSettingsPermissionFirst() {
Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS).apply {
data = Uri.parse("package:${context.packageName}")
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}.let {
mWriteSettingsPermissionBeingAsked = true
context.startActivity(it)
}
}
private fun hasWriteSettingsPermission(): Boolean {
return if (isRuntimePermissionsAvailable()) {
Settings.System.canWrite(context.applicationContext)
} else {
true
}
}
private fun isRuntimePermissionsAvailable() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
override fun onHostResume() {
if (!mWriteSettingsPermissionBeingAsked) {
return
}
mWriteSettingsPermissionBeingAsked = false
// cleanup
val askAsyncListener = mAskAsyncListener!!
val askAsyncRequestedPermissions = mAskAsyncRequestedPermissions!!
mAskAsyncListener = null
mAskAsyncRequestedPermissions = null
if (askAsyncRequestedPermissions.isNotEmpty()) {
// invoke actual asking for permissions
askForManifestPermissions(askAsyncRequestedPermissions, askAsyncListener)
} else {
// user asked only for Manifest.permission.WRITE_SETTINGS
askAsyncListener.onResult(mutableMapOf())
}
}
override fun onHostPause() = Unit
override fun onHostDestroy() = Unit
}