expo-location 58.0.13 → 58.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/android/build.gradle +2 -2
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/expo/modules/location/next/LocationModuleNext.kt +189 -10
- package/android/src/main/java/expo/modules/location/next/LocationPermissions.kt +65 -0
- package/android/src/main/java/expo/modules/location/next/LocationRecords.kt +91 -2
- package/android/src/main/java/expo/modules/location/next/LocationTaskConsumer.kt +205 -0
- package/android/src/main/java/expo/modules/location/next/WatchPosition.kt +8 -6
- package/android/src/main/java/expo/modules/location/next/locationForegroundService/BackgroundSessionOptions.kt +62 -0
- package/android/src/main/java/expo/modules/location/next/locationForegroundService/BackgroundSessionTypes.kt +32 -0
- package/android/src/main/java/expo/modules/location/next/locationForegroundService/ForegroundServiceExceptions.kt +53 -0
- package/android/src/main/java/expo/modules/location/next/locationForegroundService/LocationForegroundService.kt +142 -0
- package/android/src/main/java/expo/modules/location/next/locationForegroundService/ServiceNotification.kt +64 -0
- package/android/src/main/java/expo/modules/location/next/locationProviders/AndroidLocationProvider.kt +115 -7
- package/android/src/main/java/expo/modules/location/next/locationProviders/FallbackLocationProvider.kt +12 -3
- package/android/src/main/java/expo/modules/location/next/locationProviders/GmsLocationProvider.kt +79 -0
- package/android/src/main/java/expo/modules/location/next/locationProviders/LocationProvider.kt +48 -0
- package/android/src/main/java/expo/modules/location/taskConsumers/LocationTaskConsumer.kt +6 -0
- package/build/next/index.d.ts +1 -1
- package/build/next/index.js.map +1 -1
- package/build/next/js/LocationProvider.d.ts +35 -0
- package/build/next/js/LocationProvider.js +35 -6
- package/build/next/js/LocationProvider.js.map +1 -1
- package/build/next/js/LocationUpdatesHandle.d.ts +45 -7
- package/build/next/js/LocationUpdatesHandle.js +43 -7
- package/build/next/js/LocationUpdatesHandle.js.map +1 -1
- package/build/next/js/Permissions.d.ts +63 -0
- package/build/next/js/Permissions.js +63 -6
- package/build/next/js/Permissions.js.map +1 -1
- package/build/next/js/PositionWatchHandle.d.ts +78 -0
- package/build/next/js/PositionWatchHandle.js +78 -11
- package/build/next/js/PositionWatchHandle.js.map +1 -1
- package/build/next/js/getPosition.d.ts +26 -1
- package/build/next/js/getPosition.js +26 -2
- package/build/next/js/getPosition.js.map +1 -1
- package/build/next/js/locationServices.d.ts +29 -0
- package/build/next/js/locationServices.js +29 -3
- package/build/next/js/locationServices.js.map +1 -1
- package/build/next/types/LocationTask.types.d.ts +20 -0
- package/build/next/types/LocationTask.types.js +5 -0
- package/build/next/types/LocationTask.types.js.map +1 -0
- package/build/next/types/Permissions.types.d.ts +68 -0
- package/build/next/types/Permissions.types.js +51 -9
- package/build/next/types/Permissions.types.js.map +1 -1
- package/build/next/types/Position.types.d.ts +203 -2
- package/build/next/types/Position.types.js +51 -7
- package/build/next/types/Position.types.js.map +1 -1
- package/build/next/types/index.d.ts +1 -0
- package/build/next/types/index.js +1 -0
- package/build/next/types/index.js.map +1 -1
- package/ios/Providers/LocationsStreamer.swift +10 -4
- package/ios/TaskConsumers/EXGeofencingTaskConsumer.m +16 -1
- package/ios/next/LocationModuleNext.swift +4 -0
- package/ios/next/LocationNextExceptions.swift +0 -8
- package/ios/next/LocationUpdatesHandle/LocationUpdatesHandle.swift +6 -2
- package/ios/next/Position/LocationUpdateDiagnostics.swift +0 -3
- package/ios/next/Position/PositionUpdatesCompatibilitySource.swift +9 -1
- package/ios/next/Position/PositionUpdatesLiveSource.swift +5 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15-sources.jar +0 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15-sources.jar.md5 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15-sources.jar.sha1 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15-sources.jar.sha256 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15-sources.jar.sha512 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.aar +0 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.aar.md5 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.aar.sha1 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.aar.sha256 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.aar.sha512 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/{58.0.13/expo.modules.location-58.0.13.module → 58.0.15/expo.modules.location-58.0.15.module} +22 -22
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.module.md5 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.module.sha1 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.module.sha256 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.module.sha512 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/{58.0.13/expo.modules.location-58.0.13.pom → 58.0.15/expo.modules.location-58.0.15.pom} +1 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.pom.md5 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.pom.sha1 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.pom.sha256 +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.15/expo.modules.location-58.0.15.pom.sha512 +1 -0
- package/package.json +2 -2
- package/plugin/build/withLocation.js +2 -0
- package/plugin/build/withLocation.js.map +1 -1
- package/plugin/src/withLocation.ts +2 -0
- package/src/next/index.ts +1 -0
- package/src/next/js/LocationProvider.ts +35 -0
- package/src/next/js/LocationUpdatesHandle.ts +45 -8
- package/src/next/js/Permissions.ts +63 -0
- package/src/next/js/PositionWatchHandle.ts +78 -0
- package/src/next/js/getPosition.ts +29 -2
- package/src/next/js/locationServices.ts +29 -0
- package/src/next/types/LocationTask.types.ts +22 -0
- package/src/next/types/Permissions.types.ts +68 -0
- package/src/next/types/Position.types.ts +203 -2
- package/src/next/types/index.ts +1 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13-sources.jar +0 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13-sources.jar.md5 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13-sources.jar.sha1 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13-sources.jar.sha256 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13-sources.jar.sha512 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.aar +0 -0
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.aar.md5 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.aar.sha1 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.aar.sha256 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.aar.sha512 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.module.md5 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.module.sha1 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.module.sha256 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.module.sha512 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.pom.md5 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.pom.sha1 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.pom.sha256 +0 -1
- package/local-maven-repo/host/exp/exponent/expo.modules.location/58.0.13/expo.modules.location-58.0.13.pom.sha512 +0 -1
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
package expo.modules.location.next
|
|
2
|
+
|
|
3
|
+
import android.app.PendingIntent
|
|
4
|
+
import android.app.job.JobParameters
|
|
5
|
+
import android.app.job.JobService
|
|
6
|
+
import android.content.Context
|
|
7
|
+
import android.content.Intent
|
|
8
|
+
import android.os.PersistableBundle
|
|
9
|
+
import expo.modules.interfaces.taskManager.TaskConsumer
|
|
10
|
+
import expo.modules.interfaces.taskManager.TaskInterface
|
|
11
|
+
import expo.modules.interfaces.taskManager.TaskManagerInterface
|
|
12
|
+
import expo.modules.interfaces.taskManager.TaskManagerUtilsInterface
|
|
13
|
+
import expo.modules.kotlin.records.Field
|
|
14
|
+
import expo.modules.kotlin.records.Record
|
|
15
|
+
import expo.modules.kotlin.sharedobjects.SharedObject
|
|
16
|
+
import expo.modules.kotlin.types.OptimizedRecord
|
|
17
|
+
import expo.modules.location.next.locationProviders.BackgroundUpdatesParameters
|
|
18
|
+
import expo.modules.location.next.locationProviders.LocationProvider
|
|
19
|
+
import expo.modules.location.next.locationProviders.ProviderResult
|
|
20
|
+
import expo.modules.location.next.locationProviders.toBackgroundUpdatesParameters
|
|
21
|
+
import java.util.concurrent.ConcurrentHashMap
|
|
22
|
+
import java.util.concurrent.atomic.AtomicInteger
|
|
23
|
+
|
|
24
|
+
abstract class LocationTaskConsumer(
|
|
25
|
+
context: Context,
|
|
26
|
+
taskManagerUtils: TaskManagerUtilsInterface?
|
|
27
|
+
) : TaskConsumer(context, taskManagerUtils) {
|
|
28
|
+
@Volatile
|
|
29
|
+
var task: TaskInterface? = null
|
|
30
|
+
|
|
31
|
+
@Volatile
|
|
32
|
+
protected var pendingIntent: PendingIntent? = null
|
|
33
|
+
|
|
34
|
+
private val currentOptions: BackgroundUpdatesParameters
|
|
35
|
+
get() = (task?.options ?: emptyMap()).toBackgroundUpdatesParameters()
|
|
36
|
+
|
|
37
|
+
override fun taskType(): String = "location"
|
|
38
|
+
|
|
39
|
+
final override fun didRegister(registeredTask: TaskInterface?) {
|
|
40
|
+
task = registeredTask ?: return
|
|
41
|
+
|
|
42
|
+
val newPendingIntent = taskManagerUtils.createTaskIntent(context, registeredTask)
|
|
43
|
+
pendingIntent = newPendingIntent
|
|
44
|
+
|
|
45
|
+
val started = requestLocationUpdates(newPendingIntent, currentOptions)
|
|
46
|
+
|
|
47
|
+
statuses.compute(registeredTask.name) { _, previousStatus ->
|
|
48
|
+
val status = previousStatus ?: BackgroundTaskStatus()
|
|
49
|
+
status.copy(
|
|
50
|
+
isRunning = started,
|
|
51
|
+
lastError = if (started) {
|
|
52
|
+
null
|
|
53
|
+
} else {
|
|
54
|
+
status.lastError
|
|
55
|
+
}
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
protected fun reportRequestFailed(cause: Throwable) {
|
|
61
|
+
val name = task?.name ?: return
|
|
62
|
+
statuses.compute(name) { _, previousStatus ->
|
|
63
|
+
(previousStatus ?: BackgroundTaskStatus())
|
|
64
|
+
.copy(isRunning = false, lastError = cause.message ?: cause.toString())
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
abstract fun requestLocationUpdates(
|
|
69
|
+
pendingIntent: PendingIntent,
|
|
70
|
+
options: BackgroundUpdatesParameters,
|
|
71
|
+
updateExisting: Boolean = false
|
|
72
|
+
): Boolean
|
|
73
|
+
|
|
74
|
+
final override fun didUnregister() {
|
|
75
|
+
task?.let { statuses.remove(it.name) }
|
|
76
|
+
task = null
|
|
77
|
+
pendingIntent?.let { stopLocationUpdates(it) }
|
|
78
|
+
pendingIntent = null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
abstract fun stopLocationUpdates(pendingIntent: PendingIntent)
|
|
82
|
+
|
|
83
|
+
final override fun setOptions(options: MutableMap<String, Any>?) {
|
|
84
|
+
val currentTask = task ?: return
|
|
85
|
+
val pendingIntentNow = pendingIntent ?: return
|
|
86
|
+
val started = requestLocationUpdates(pendingIntentNow, currentOptions, updateExisting = true)
|
|
87
|
+
|
|
88
|
+
statuses.compute(currentTask.name) { _, previousStatus ->
|
|
89
|
+
val status = previousStatus ?: BackgroundTaskStatus()
|
|
90
|
+
status.copy(
|
|
91
|
+
isRunning = started,
|
|
92
|
+
lastError = if (started) {
|
|
93
|
+
null
|
|
94
|
+
} else {
|
|
95
|
+
status.lastError
|
|
96
|
+
}
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
final override fun didReceiveBroadcast(intent: Intent?) {
|
|
102
|
+
val currentTask = task ?: return
|
|
103
|
+
val locationData = decodeBatchedPositions(intent)
|
|
104
|
+
if (locationData.data == null && locationData.error == null) {
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
locationData.data?.lastOrNull()?.let { position ->
|
|
109
|
+
statuses.compute(currentTask.name) { _, previousStatus ->
|
|
110
|
+
(previousStatus ?: BackgroundTaskStatus())
|
|
111
|
+
.copy(isRunning = true, lastFixAt = position.timestamp, lastError = null)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (LocationModuleNext.modulesStarted.get() > 0) {
|
|
116
|
+
locationData.error?.let { currentTask.execute(null, Error(it)) }
|
|
117
|
+
locationData.data?.forEach { currentTask.execute(it.toBundle(), null) }
|
|
118
|
+
} else {
|
|
119
|
+
runCatching {
|
|
120
|
+
taskManagerUtils.scheduleJob(context, currentTask, locationData.toPersistableBundleList())
|
|
121
|
+
}.onFailure { reportRequestFailed(it) }
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
abstract fun decodeBatchedPositions(intent: Intent?): BatchedPositions
|
|
125
|
+
|
|
126
|
+
final override fun didExecuteJob(jobService: JobService?, params: JobParameters?): Boolean {
|
|
127
|
+
val currentTask = task ?: return false
|
|
128
|
+
val locationData = taskManagerUtils.extractDataFromJobParams(params).toBatchedPositions()
|
|
129
|
+
val positions = locationData.data
|
|
130
|
+
|
|
131
|
+
if (positions.isNullOrEmpty()) {
|
|
132
|
+
currentTask.execute(null, Error(locationData.error)) {
|
|
133
|
+
jobService?.jobFinished(params, false)
|
|
134
|
+
}
|
|
135
|
+
return true
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
val remainingTasks = AtomicInteger(positions.size)
|
|
139
|
+
positions.forEach { position ->
|
|
140
|
+
currentTask.execute(position.toBundle(), null) {
|
|
141
|
+
if (remainingTasks.decrementAndGet() == 0) {
|
|
142
|
+
jobService?.jobFinished(params, false)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return true
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
companion object {
|
|
150
|
+
private val statuses = ConcurrentHashMap<String, BackgroundTaskStatus>()
|
|
151
|
+
|
|
152
|
+
fun statusOf(taskName: String, isRegistered: Boolean) =
|
|
153
|
+
statuses.getOrDefault(taskName, BackgroundTaskStatus()).copy(isRegistered = isRegistered)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
class LocationUpdatesHandle(
|
|
158
|
+
val taskName: String,
|
|
159
|
+
@Volatile
|
|
160
|
+
var profile: LocationProfile,
|
|
161
|
+
val locationProvider: LocationProvider
|
|
162
|
+
) : SharedObject()
|
|
163
|
+
|
|
164
|
+
@OptimizedRecord
|
|
165
|
+
data class BackgroundTaskStatus(
|
|
166
|
+
@Field val isRegistered: Boolean = false,
|
|
167
|
+
@Field val isRunning: Boolean = false,
|
|
168
|
+
@Field val lastError: String? = null,
|
|
169
|
+
@Field val lastFixAt: Double? = null
|
|
170
|
+
) : Record
|
|
171
|
+
|
|
172
|
+
class BatchedPositions(
|
|
173
|
+
@Field val data: List<Position>? = null,
|
|
174
|
+
@Field val error: String? = null
|
|
175
|
+
) : Record {
|
|
176
|
+
fun toPersistableBundleList(): List<PersistableBundle> {
|
|
177
|
+
val bundles = if (data != null) {
|
|
178
|
+
data.map { it.toPersistableBundle() }
|
|
179
|
+
} else {
|
|
180
|
+
val bundle = PersistableBundle()
|
|
181
|
+
bundle.putString("error", error)
|
|
182
|
+
listOf(bundle)
|
|
183
|
+
}
|
|
184
|
+
return bundles
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
fun List<PersistableBundle>.toBatchedPositions(): BatchedPositions = if (isEmpty()) {
|
|
189
|
+
BatchedPositions(null, "Empty persistable bundle.")
|
|
190
|
+
} else if (first().containsKey("error")) {
|
|
191
|
+
BatchedPositions(null, first().getString("error"))
|
|
192
|
+
} else {
|
|
193
|
+
BatchedPositions(
|
|
194
|
+
mapNotNull { it.toPosition() }.takeIf { it.isNotEmpty() },
|
|
195
|
+
null
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
fun getExistingOrNewLocationTaskConsumer(handle: LocationUpdatesHandle, taskManager: TaskManagerInterface): ProviderResult<Class<out TaskConsumer>> {
|
|
200
|
+
val existing = handle.locationProvider.getRegisteredTaskConsumerClass(taskManager, handle.taskName)
|
|
201
|
+
if (existing is ProviderResult.Available) {
|
|
202
|
+
return existing
|
|
203
|
+
}
|
|
204
|
+
return handle.locationProvider.getLocationTaskConsumerClass()
|
|
205
|
+
}
|
|
@@ -36,12 +36,14 @@ class PositionWatchStatus(
|
|
|
36
36
|
@Field val isHandleAlive: Boolean = false,
|
|
37
37
|
@Field val isStarted: Boolean = false,
|
|
38
38
|
@Field val isPaused: Boolean = false,
|
|
39
|
+
@Field val areUpdatesAllowed: Boolean = true,
|
|
39
40
|
@Field val isInForeground: Boolean = true
|
|
40
41
|
) : Record
|
|
41
42
|
|
|
42
43
|
class PausableWatchSession(
|
|
43
44
|
initialParameters: WatchPositionParameters,
|
|
44
|
-
private val session: PositionUpdatesSession
|
|
45
|
+
private val session: PositionUpdatesSession,
|
|
46
|
+
private var areUpdatesAllowed: Boolean
|
|
45
47
|
) {
|
|
46
48
|
|
|
47
49
|
private var activeParameters: WatchPositionParameters = initialParameters
|
|
@@ -50,13 +52,12 @@ class PausableWatchSession(
|
|
|
50
52
|
var isPaused: Boolean = false
|
|
51
53
|
var isStarted: Boolean = false
|
|
52
54
|
var isReleased: Boolean = false
|
|
53
|
-
var isInForeground: Boolean = true
|
|
54
55
|
|
|
55
56
|
private var onEvent: ((PositionChangedEvent) -> Unit)? = null
|
|
56
57
|
|
|
57
58
|
@Synchronized
|
|
58
59
|
private fun handleLocationUpdatesRequest(): Throwable? {
|
|
59
|
-
val shouldBeActive = !isPaused && isStarted && !isReleased &&
|
|
60
|
+
val shouldBeActive = !isPaused && isStarted && !isReleased && areUpdatesAllowed
|
|
60
61
|
val shouldRequestUpdates = !session.isSubscribed() && shouldBeActive
|
|
61
62
|
val onEvent = this.onEvent
|
|
62
63
|
if (shouldRequestUpdates && onEvent != null) {
|
|
@@ -100,8 +101,8 @@ class PausableWatchSession(
|
|
|
100
101
|
}
|
|
101
102
|
|
|
102
103
|
@Synchronized
|
|
103
|
-
fun
|
|
104
|
-
this.
|
|
104
|
+
fun setUpdatesAllowed(allowed: Boolean) {
|
|
105
|
+
this.areUpdatesAllowed = allowed
|
|
105
106
|
emitEventOnFailure(handleLocationUpdatesRequest())
|
|
106
107
|
}
|
|
107
108
|
|
|
@@ -138,13 +139,14 @@ class PausableWatchSession(
|
|
|
138
139
|
}
|
|
139
140
|
|
|
140
141
|
@Synchronized
|
|
141
|
-
fun status(): PositionWatchStatus {
|
|
142
|
+
fun status(isInForeground: Boolean): PositionWatchStatus {
|
|
142
143
|
return PositionWatchStatus(
|
|
143
144
|
isSubscribed = session.isSubscribed(),
|
|
144
145
|
canDeliverUpdates = session.canDeliverUpdates(),
|
|
145
146
|
isHandleAlive = !isReleased,
|
|
146
147
|
isStarted = isStarted,
|
|
147
148
|
isPaused = isPaused,
|
|
149
|
+
areUpdatesAllowed = areUpdatesAllowed,
|
|
148
150
|
isInForeground = isInForeground
|
|
149
151
|
)
|
|
150
152
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
package expo.modules.location.next.locationForegroundService
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import androidx.core.content.edit
|
|
5
|
+
import expo.modules.kotlin.records.Field
|
|
6
|
+
import expo.modules.kotlin.records.Record
|
|
7
|
+
import expo.modules.kotlin.types.OptimizedRecord
|
|
8
|
+
|
|
9
|
+
@OptimizedRecord
|
|
10
|
+
class BackgroundSessionOptions(
|
|
11
|
+
@Field val notificationTitle: String? = null,
|
|
12
|
+
@Field val notificationBody: String? = null,
|
|
13
|
+
@Field val notificationColor: Int? = null,
|
|
14
|
+
@Field val stopOnTaskRemoved: Boolean = false
|
|
15
|
+
) : Record {
|
|
16
|
+
companion object {
|
|
17
|
+
private const val PREFERENCES_NAME = "expo.modules.location.next.backgroundSession"
|
|
18
|
+
private const val KEY_REQUESTED = "requested"
|
|
19
|
+
private const val KEY_TITLE = "notificationTitle"
|
|
20
|
+
private const val KEY_BODY = "notificationBody"
|
|
21
|
+
private const val KEY_COLOR = "notificationColor"
|
|
22
|
+
private const val KEY_STOP_ON_TASK_REMOVED = "stopOnTaskRemoved"
|
|
23
|
+
|
|
24
|
+
private fun preferences(context: Context) =
|
|
25
|
+
context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE)
|
|
26
|
+
|
|
27
|
+
fun persist(context: Context, options: BackgroundSessionOptions) {
|
|
28
|
+
preferences(context).edit(commit = true) {
|
|
29
|
+
putBoolean(KEY_REQUESTED, true)
|
|
30
|
+
putString(KEY_TITLE, options.notificationTitle)
|
|
31
|
+
putString(KEY_BODY, options.notificationBody)
|
|
32
|
+
if (options.notificationColor != null) {
|
|
33
|
+
putInt(KEY_COLOR, options.notificationColor)
|
|
34
|
+
} else {
|
|
35
|
+
remove(KEY_COLOR)
|
|
36
|
+
}
|
|
37
|
+
putBoolean(KEY_STOP_ON_TASK_REMOVED, options.stopOnTaskRemoved)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fun clearPersisted(context: Context) {
|
|
42
|
+
preferences(context).edit(commit = true) { clear() }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
fun readPersisted(context: Context): BackgroundSessionOptions? {
|
|
46
|
+
val preferences = preferences(context)
|
|
47
|
+
if (!preferences.getBoolean(KEY_REQUESTED, false)) {
|
|
48
|
+
return null
|
|
49
|
+
}
|
|
50
|
+
return BackgroundSessionOptions(
|
|
51
|
+
notificationTitle = preferences.getString(KEY_TITLE, null),
|
|
52
|
+
notificationBody = preferences.getString(KEY_BODY, null),
|
|
53
|
+
notificationColor = if (preferences.contains(KEY_COLOR)) {
|
|
54
|
+
preferences.getInt(KEY_COLOR, 0)
|
|
55
|
+
} else {
|
|
56
|
+
null
|
|
57
|
+
},
|
|
58
|
+
stopOnTaskRemoved = preferences.getBoolean(KEY_STOP_ON_TASK_REMOVED, false)
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
package expo.modules.location.next.locationForegroundService
|
|
2
|
+
|
|
3
|
+
import expo.modules.kotlin.records.Field
|
|
4
|
+
import expo.modules.kotlin.records.Record
|
|
5
|
+
import expo.modules.kotlin.types.Enumerable
|
|
6
|
+
import expo.modules.kotlin.types.OptimizedRecord
|
|
7
|
+
import kotlinx.coroutines.CompletableDeferred
|
|
8
|
+
|
|
9
|
+
enum class BackgroundSessionState : Enumerable {
|
|
10
|
+
NOT_RUNNING,
|
|
11
|
+
PENDING,
|
|
12
|
+
PROMOTED
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
sealed interface ServicePromotionResult {
|
|
16
|
+
data object Promoted : ServicePromotionResult
|
|
17
|
+
data class Failed(val cause: Throwable) : ServicePromotionResult
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
sealed interface SessionState {
|
|
21
|
+
data object Idle : SessionState
|
|
22
|
+
data class Starting(val promotion: CompletableDeferred<ServicePromotionResult>) : SessionState
|
|
23
|
+
data object Promoted : SessionState
|
|
24
|
+
data object Stopping : SessionState
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@OptimizedRecord
|
|
28
|
+
class BackgroundSessionStatus(
|
|
29
|
+
@Field val state: BackgroundSessionState,
|
|
30
|
+
@Field val stopOnTaskRemoved: Boolean,
|
|
31
|
+
@Field val unthrottled: Boolean
|
|
32
|
+
) : Record
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
package expo.modules.location.next.locationForegroundService
|
|
2
|
+
|
|
3
|
+
import expo.modules.kotlin.exception.CodedException
|
|
4
|
+
|
|
5
|
+
class BackgroundSessionRequiresForegroundException :
|
|
6
|
+
CodedException(
|
|
7
|
+
"Cannot start the background session, because the app is in the background. " +
|
|
8
|
+
"Android 12 and above only allows a foreground service to start while the app is in the foreground. " +
|
|
9
|
+
"Call `ensureStarted` while the app is in the foreground - a request made from the background is " +
|
|
10
|
+
"recorded and honoured the next time the app returns to the foreground."
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
class ServicePromotionFailedException(cause: Throwable) :
|
|
14
|
+
CodedException(cause.localizedMessage, cause)
|
|
15
|
+
|
|
16
|
+
class ServicePromotionTimedOutException :
|
|
17
|
+
CodedException(
|
|
18
|
+
"The foreground service did not confirm that it was promoted in time, so the state of the " +
|
|
19
|
+
"background session is unknown. It may still promote shortly. " +
|
|
20
|
+
"Read `BackgroundSession.status()` to find out whether it did."
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
class NoNotificationIconException :
|
|
24
|
+
CodedException("No notification icon was configured.")
|
|
25
|
+
|
|
26
|
+
class ServiceNotFoundException :
|
|
27
|
+
CodedException(
|
|
28
|
+
"The location foreground service could not be started, because the system found no service to start. " +
|
|
29
|
+
"`expo.modules.location.next.locationForegroundService.LocationForegroundService` is declared in " +
|
|
30
|
+
"expo-location's own manifest, so this almost always means the app's manifest merge dropped it. " +
|
|
31
|
+
"Check for a `tools:node=\"remove\"` or `tools:replace` rule on that service and re-run " +
|
|
32
|
+
"`npx expo prebuild --clean`."
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
class ServiceDestroyedDuringPromotionException :
|
|
36
|
+
CodedException(
|
|
37
|
+
"The foreground service was destroyed before it finished promoting, so the background session did " +
|
|
38
|
+
"not start. The service was most likely stopped while starting up. " +
|
|
39
|
+
"Call `ensureStarted` again while the app is in the foreground."
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
class BackgroundSessionStoppingException :
|
|
43
|
+
CodedException(
|
|
44
|
+
"Cannot start the background session, because the previous one is still stopping. The foreground " +
|
|
45
|
+
"service is being torn down and a new one cannot start until it is gone. " +
|
|
46
|
+
"Call `ensureStarted` again in a moment."
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
class BackgroundSessionStoppedDuringPromotionException :
|
|
50
|
+
CodedException(
|
|
51
|
+
"The background session was stopped while the foreground service was starting, so the service did " +
|
|
52
|
+
"not start. Call `ensureStarted` again if you still want a background session."
|
|
53
|
+
)
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
package expo.modules.location.next.locationForegroundService
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint
|
|
4
|
+
import android.app.Service
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.content.Intent
|
|
7
|
+
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION
|
|
8
|
+
import android.os.Build
|
|
9
|
+
import android.os.IBinder
|
|
10
|
+
import androidx.annotation.ChecksSdkIntAtLeast
|
|
11
|
+
import androidx.core.app.NotificationManagerCompat
|
|
12
|
+
import androidx.core.app.ServiceCompat
|
|
13
|
+
import expo.modules.kotlin.exception.CodedException
|
|
14
|
+
import kotlinx.coroutines.CompletableDeferred
|
|
15
|
+
|
|
16
|
+
private const val LOCATION_NOTIFICATION_ID = 1492
|
|
17
|
+
|
|
18
|
+
class LocationForegroundService : Service() {
|
|
19
|
+
override fun onBind(intent: Intent?): IBinder? = null
|
|
20
|
+
|
|
21
|
+
override fun onDestroy() = synchronized(serviceLock) {
|
|
22
|
+
super.onDestroy()
|
|
23
|
+
completePromotion(SessionState.Idle, ServicePromotionResult.Failed(ServiceDestroyedDuringPromotionException()))
|
|
24
|
+
stopOnTaskRemovedOption = false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
override fun onTaskRemoved(rootIntent: Intent?) = synchronized(serviceLock) {
|
|
28
|
+
super.onTaskRemoved(rootIntent)
|
|
29
|
+
if (stopOnTaskRemovedOption) {
|
|
30
|
+
stopSelf()
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int = synchronized(serviceLock) {
|
|
35
|
+
val options = BackgroundSessionOptions.readPersisted(this)
|
|
36
|
+
if (options == null) {
|
|
37
|
+
stopSelf()
|
|
38
|
+
completePromotion(SessionState.Stopping, ServicePromotionResult.Failed(BackgroundSessionStoppedDuringPromotionException()))
|
|
39
|
+
return START_NOT_STICKY
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
val notification = buildNotification(this, options)
|
|
44
|
+
ServiceCompat.startForeground(this, LOCATION_NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_LOCATION)
|
|
45
|
+
completePromotion(SessionState.Promoted, ServicePromotionResult.Promoted)
|
|
46
|
+
stopOnTaskRemovedOption = options.stopOnTaskRemoved
|
|
47
|
+
} catch (t: Throwable) {
|
|
48
|
+
stopSelf()
|
|
49
|
+
completePromotion(SessionState.Stopping, ServicePromotionResult.Failed(t))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return START_NOT_STICKY
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
companion object {
|
|
56
|
+
// Service callbacks (e.g. onStartCommand) can happen on different thread than other methods as they are invoked by the system on the main thread
|
|
57
|
+
// and not the JS thread / AsyncFunctionQueue handler thread like the DSL methods. Use lock on operations that access the global state to disallow any races.
|
|
58
|
+
private val serviceLock = Any()
|
|
59
|
+
private var stopOnTaskRemovedOption: Boolean = false
|
|
60
|
+
|
|
61
|
+
@Volatile
|
|
62
|
+
var state: SessionState = SessionState.Idle
|
|
63
|
+
private set
|
|
64
|
+
|
|
65
|
+
private fun completePromotion(nextState: SessionState, result: ServicePromotionResult) {
|
|
66
|
+
synchronized(serviceLock) {
|
|
67
|
+
val currentState = state
|
|
68
|
+
if (currentState is SessionState.Starting) {
|
|
69
|
+
currentState.promotion.complete(result)
|
|
70
|
+
}
|
|
71
|
+
state = nextState
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fun status(): BackgroundSessionStatus = synchronized(serviceLock) {
|
|
76
|
+
val sessionState = when (state) {
|
|
77
|
+
SessionState.Promoted -> BackgroundSessionState.PROMOTED
|
|
78
|
+
is SessionState.Starting, SessionState.Stopping -> BackgroundSessionState.PENDING
|
|
79
|
+
SessionState.Idle -> BackgroundSessionState.NOT_RUNNING
|
|
80
|
+
}
|
|
81
|
+
BackgroundSessionStatus(
|
|
82
|
+
sessionState,
|
|
83
|
+
stopOnTaskRemovedOption,
|
|
84
|
+
isBackgroundLocationUnthrottled()
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@SuppressLint("MissingPermission")
|
|
89
|
+
fun startOrUpdate(
|
|
90
|
+
context: Context,
|
|
91
|
+
options: BackgroundSessionOptions,
|
|
92
|
+
updateOnly: Boolean = false
|
|
93
|
+
): SessionState {
|
|
94
|
+
val starting = synchronized(serviceLock) {
|
|
95
|
+
when (val current = state) {
|
|
96
|
+
SessionState.Promoted -> {
|
|
97
|
+
val notification = buildNotification(context, options)
|
|
98
|
+
NotificationManagerCompat.from(context).notify(LOCATION_NOTIFICATION_ID, notification)
|
|
99
|
+
stopOnTaskRemovedOption = options.stopOnTaskRemoved
|
|
100
|
+
return current
|
|
101
|
+
}
|
|
102
|
+
is SessionState.Starting -> return current
|
|
103
|
+
SessionState.Stopping -> throw BackgroundSessionStoppingException()
|
|
104
|
+
SessionState.Idle -> {
|
|
105
|
+
if (updateOnly) {
|
|
106
|
+
throw BackgroundSessionRequiresForegroundException()
|
|
107
|
+
}
|
|
108
|
+
SessionState.Starting(CompletableDeferred()).also { state = it }
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return runCatching {
|
|
114
|
+
val serviceStartIntent = Intent(context, LocationForegroundService::class.java)
|
|
115
|
+
if (context.startService(serviceStartIntent) == null) {
|
|
116
|
+
throw ServiceNotFoundException()
|
|
117
|
+
}
|
|
118
|
+
starting
|
|
119
|
+
}.getOrElse {
|
|
120
|
+
completePromotion(SessionState.Idle, ServicePromotionResult.Failed(it))
|
|
121
|
+
throw it as? CodedException ?: ServicePromotionFailedException(it)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
fun stop(context: Context) {
|
|
126
|
+
synchronized(serviceLock) {
|
|
127
|
+
if (state == SessionState.Promoted) {
|
|
128
|
+
state = SessionState.Stopping
|
|
129
|
+
}
|
|
130
|
+
BackgroundSessionOptions.clearPersisted(context)
|
|
131
|
+
}
|
|
132
|
+
context.stopService(Intent(context, LocationForegroundService::class.java))
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@get:ChecksSdkIntAtLeast(api = Build.VERSION_CODES.O)
|
|
136
|
+
val isForegroundServiceRequired: Boolean
|
|
137
|
+
get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
|
|
138
|
+
|
|
139
|
+
fun isBackgroundLocationUnthrottled(): Boolean =
|
|
140
|
+
!isForegroundServiceRequired || state == SessionState.Promoted
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
package expo.modules.location.next.locationForegroundService
|
|
2
|
+
|
|
3
|
+
import android.app.Notification
|
|
4
|
+
import android.app.PendingIntent
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.content.Intent
|
|
7
|
+
import android.content.pm.PackageManager
|
|
8
|
+
import androidx.core.app.NotificationChannelCompat
|
|
9
|
+
import androidx.core.app.NotificationCompat
|
|
10
|
+
import androidx.core.app.NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
|
|
11
|
+
import androidx.core.app.NotificationCompat.VISIBILITY_PUBLIC
|
|
12
|
+
import androidx.core.app.NotificationManagerCompat
|
|
13
|
+
|
|
14
|
+
private const val META_DATA_LOCATION_FOREGROUND_SERVICE_ICON = "expo.modules.location.foreground_service_icon"
|
|
15
|
+
private const val NOTIFICATION_CHANNEL_ID = "expo.location.next.notification.channel"
|
|
16
|
+
|
|
17
|
+
private fun resolveNotificationIcon(context: Context): Int {
|
|
18
|
+
val packageName = context.packageName
|
|
19
|
+
val metaData = runCatching {
|
|
20
|
+
context.packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA).metaData
|
|
21
|
+
}.getOrNull()
|
|
22
|
+
|
|
23
|
+
return metaData?.getInt(META_DATA_LOCATION_FOREGROUND_SERVICE_ICON, 0)?.takeIf { it != 0 }
|
|
24
|
+
?: context.resources.getIdentifier("notification_icon", "drawable", packageName).takeIf { it != 0 }
|
|
25
|
+
?: context.applicationInfo.icon.takeIf { it != 0 }
|
|
26
|
+
?: throw NoNotificationIconException()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private fun ensureNotificationChannel(context: Context, appName: String): String {
|
|
30
|
+
val channel = NotificationChannelCompat
|
|
31
|
+
.Builder(NOTIFICATION_CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW)
|
|
32
|
+
.setName(appName)
|
|
33
|
+
.setDescription("Location foreground service notification channel")
|
|
34
|
+
.setShowBadge(false)
|
|
35
|
+
.build()
|
|
36
|
+
NotificationManagerCompat.from(context).createNotificationChannel(channel)
|
|
37
|
+
return NOTIFICATION_CHANNEL_ID
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
internal fun buildNotification(context: Context, options: BackgroundSessionOptions): Notification {
|
|
41
|
+
val packageManager = context.packageManager
|
|
42
|
+
val packageName = context.packageName
|
|
43
|
+
val appName = context.applicationInfo.loadLabel(packageManager).toString()
|
|
44
|
+
|
|
45
|
+
val notificationBuilder = NotificationCompat
|
|
46
|
+
.Builder(context, ensureNotificationChannel(context, appName))
|
|
47
|
+
.setForegroundServiceBehavior(FOREGROUND_SERVICE_IMMEDIATE)
|
|
48
|
+
.setSmallIcon(resolveNotificationIcon(context))
|
|
49
|
+
.setVisibility(VISIBILITY_PUBLIC)
|
|
50
|
+
.setContentTitle(options.notificationTitle ?: appName)
|
|
51
|
+
.setOngoing(true)
|
|
52
|
+
.setCategory(Notification.CATEGORY_SERVICE)
|
|
53
|
+
options.notificationBody?.let { notificationBuilder.setContentText(it).setStyle(NotificationCompat.BigTextStyle().bigText(it)) }
|
|
54
|
+
options.notificationColor?.let { notificationBuilder.setColorized(true).setColor(it) }
|
|
55
|
+
|
|
56
|
+
packageManager.getLaunchIntentForPackage(packageName)?.let {
|
|
57
|
+
it.flags = it.flags or Intent.FLAG_ACTIVITY_SINGLE_TOP
|
|
58
|
+
val getActivityFlags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
|
59
|
+
val contentIntent = PendingIntent.getActivity(context, 0, it, getActivityFlags)
|
|
60
|
+
notificationBuilder.setContentIntent(contentIntent)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return notificationBuilder.build()
|
|
64
|
+
}
|