@@ -14,6 +14,8 @@ import com.onesignal.location.internal.common.LocationUtils
14
14
import com.onesignal.location.internal.controller.ILocationController
15
15
import com.onesignal.location.internal.permissions.ILocationPermissionChangedHandler
16
16
import com.onesignal.location.internal.permissions.LocationPermissionController
17
+ import kotlinx.coroutines.Dispatchers
18
+ import kotlinx.coroutines.withContext
17
19
18
20
internal class LocationManager (
19
21
private val _applicationService : IApplicationService ,
@@ -61,79 +63,81 @@ internal class LocationManager(
61
63
override suspend fun requestPermission (fallbackToSettings : Boolean ): Boolean {
62
64
Logging .log(LogLevel .DEBUG , " LocationManager.requestPermission()" )
63
65
64
- if (! isShared) {
65
- return false
66
- }
66
+ var result = false
67
+ withContext(Dispatchers .Main ) {
68
+ if (! isShared) {
69
+ return @withContext false
70
+ }
67
71
68
- var result: Boolean
69
- val hasFinePermissionGranted = AndroidUtils .hasPermission(LocationConstants .ANDROID_FINE_LOCATION_PERMISSION_STRING , true , _applicationService )
70
- var hasCoarsePermissionGranted: Boolean = false
71
- var hasBackgroundPermissionGranted: Boolean = false
72
+ val hasFinePermissionGranted = AndroidUtils .hasPermission(LocationConstants .ANDROID_FINE_LOCATION_PERMISSION_STRING , true , _applicationService )
73
+ var hasCoarsePermissionGranted: Boolean = false
74
+ var hasBackgroundPermissionGranted: Boolean = false
72
75
73
- if (! hasFinePermissionGranted) {
74
- hasCoarsePermissionGranted = AndroidUtils .hasPermission(LocationConstants .ANDROID_COARSE_LOCATION_PERMISSION_STRING , true , _applicationService )
75
- _capturer .locationCoarse = true
76
- }
77
-
78
- if (Build .VERSION .SDK_INT >= 29 ) {
79
- hasBackgroundPermissionGranted = AndroidUtils .hasPermission(LocationConstants .ANDROID_BACKGROUND_LOCATION_PERMISSION_STRING , true , _applicationService )
80
- }
76
+ if (! hasFinePermissionGranted) {
77
+ hasCoarsePermissionGranted = AndroidUtils .hasPermission(LocationConstants .ANDROID_COARSE_LOCATION_PERMISSION_STRING , true , _applicationService )
78
+ _capturer .locationCoarse = true
79
+ }
81
80
82
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
83
- if (! hasFinePermissionGranted && ! hasCoarsePermissionGranted) {
84
- // Permission missing on manifest
85
- Logging .error(" Location permissions not added on AndroidManifest file < M" )
86
- return false
81
+ if (Build .VERSION .SDK_INT >= 29 ) {
82
+ hasBackgroundPermissionGranted = AndroidUtils .hasPermission(LocationConstants .ANDROID_BACKGROUND_LOCATION_PERMISSION_STRING , true , _applicationService )
87
83
}
88
84
89
- startGetLocation()
90
- result = true
91
- } else { // Android 6.0+
92
- if (! hasFinePermissionGranted) {
93
- var requestPermission: String? = null
94
- var permissionList = AndroidUtils .filterManifestPermissions(
95
- listOf (
96
- LocationConstants .ANDROID_FINE_LOCATION_PERMISSION_STRING ,
97
- LocationConstants .ANDROID_COARSE_LOCATION_PERMISSION_STRING ,
98
- LocationConstants .ANDROID_BACKGROUND_LOCATION_PERMISSION_STRING
99
- ),
100
- _applicationService
101
- )
102
-
103
- if (permissionList.contains(LocationConstants .ANDROID_FINE_LOCATION_PERMISSION_STRING )) {
104
- // ACCESS_FINE_LOCATION permission defined on Manifest, prompt for permission
105
- // If permission already given prompt will return positive, otherwise will prompt again or show settings
106
- requestPermission = LocationConstants .ANDROID_FINE_LOCATION_PERMISSION_STRING
107
- } else if (permissionList.contains(LocationConstants .ANDROID_COARSE_LOCATION_PERMISSION_STRING )) {
108
- if (! hasCoarsePermissionGranted) {
109
- // ACCESS_COARSE_LOCATION permission defined on Manifest, prompt for permission
85
+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
86
+ if (! hasFinePermissionGranted && ! hasCoarsePermissionGranted) {
87
+ // Permission missing on manifest
88
+ Logging .error(" Location permissions not added on AndroidManifest file < M" )
89
+ return @withContext false
90
+ }
91
+
92
+ startGetLocation()
93
+ result = true
94
+ } else { // Android 6.0+
95
+ if (! hasFinePermissionGranted) {
96
+ var requestPermission: String? = null
97
+ var permissionList = AndroidUtils .filterManifestPermissions(
98
+ listOf (
99
+ LocationConstants .ANDROID_FINE_LOCATION_PERMISSION_STRING ,
100
+ LocationConstants .ANDROID_COARSE_LOCATION_PERMISSION_STRING ,
101
+ LocationConstants .ANDROID_BACKGROUND_LOCATION_PERMISSION_STRING
102
+ ),
103
+ _applicationService
104
+ )
105
+
106
+ if (permissionList.contains(LocationConstants .ANDROID_FINE_LOCATION_PERMISSION_STRING )) {
107
+ // ACCESS_FINE_LOCATION permission defined on Manifest, prompt for permission
110
108
// If permission already given prompt will return positive, otherwise will prompt again or show settings
111
- requestPermission = LocationConstants .ANDROID_COARSE_LOCATION_PERMISSION_STRING
112
- } else if (Build .VERSION .SDK_INT >= 29 && permissionList.contains(LocationConstants .ANDROID_BACKGROUND_LOCATION_PERMISSION_STRING )) {
113
- // ACCESS_BACKGROUND_LOCATION permission defined on Manifest, prompt for permission
114
- requestPermission = LocationConstants .ANDROID_BACKGROUND_LOCATION_PERMISSION_STRING
109
+ requestPermission = LocationConstants .ANDROID_FINE_LOCATION_PERMISSION_STRING
110
+ } else if (permissionList.contains(LocationConstants .ANDROID_COARSE_LOCATION_PERMISSION_STRING )) {
111
+ if (! hasCoarsePermissionGranted) {
112
+ // ACCESS_COARSE_LOCATION permission defined on Manifest, prompt for permission
113
+ // If permission already given prompt will return positive, otherwise will prompt again or show settings
114
+ requestPermission = LocationConstants .ANDROID_COARSE_LOCATION_PERMISSION_STRING
115
+ } else if (Build .VERSION .SDK_INT >= 29 && permissionList.contains(LocationConstants .ANDROID_BACKGROUND_LOCATION_PERMISSION_STRING )) {
116
+ // ACCESS_BACKGROUND_LOCATION permission defined on Manifest, prompt for permission
117
+ requestPermission = LocationConstants .ANDROID_BACKGROUND_LOCATION_PERMISSION_STRING
118
+ }
119
+ } else {
120
+ Logging .info(" Location permissions not added on AndroidManifest file >= M" )
115
121
}
116
- } else {
117
- Logging .info(" Location permissions not added on AndroidManifest file >= M" )
118
- }
119
122
120
- // We handle the following cases:
121
- // 1 - If needed and available then prompt for permissions
122
- // - Request permission can be ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION
123
- // 2 - If the permission were already granted then start getting location
124
- // 3 - If permission wasn't granted then trigger fail flow
125
- //
126
- // For each case, we call the prompt handlers
127
- result = if (requestPermission != null ) {
128
- _locationPermissionController .prompt(fallbackToSettings, requestPermission)
123
+ // We handle the following cases:
124
+ // 1 - If needed and available then prompt for permissions
125
+ // - Request permission can be ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION
126
+ // 2 - If the permission were already granted then start getting location
127
+ // 3 - If permission wasn't granted then trigger fail flow
128
+ //
129
+ // For each case, we call the prompt handlers
130
+ result = if (requestPermission != null ) {
131
+ _locationPermissionController .prompt(fallbackToSettings, requestPermission)
132
+ } else {
133
+ hasCoarsePermissionGranted
134
+ }
135
+ } else if (Build .VERSION .SDK_INT >= 29 && ! hasBackgroundPermissionGranted) {
136
+ result = backgroundLocationPermissionLogic(fallbackToSettings)
129
137
} else {
130
- hasCoarsePermissionGranted
138
+ result = true
139
+ startGetLocation()
131
140
}
132
- } else if (Build .VERSION .SDK_INT >= 29 && ! hasBackgroundPermissionGranted) {
133
- result = backgroundLocationPermissionLogic(fallbackToSettings)
134
- } else {
135
- result = true
136
- startGetLocation()
137
141
}
138
142
}
139
143
0 commit comments