Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Improvement for background geofencing

Michael Rademaker956919d07207
diff --git a/console/android/src/main/AndroidManifest.xml b/console/android/src/main/AndroidManifest.xml
index 88f050bad..f6c532323 100644
--- a/console/android/src/main/AndroidManifest.xml
+++ b/console/android/src/main/AndroidManifest.xml
@@ -57,8 +57,14 @@
</intent-filter>
</service>
- <service
- android:name=".service.GeofenceTransitionsIntentService"/>
+ <receiver
+ android:name=".service.GeofenceTransitionsIntentService"
+ android:enabled="true"
+ android:exported="false">
+ <intent-filter>
+ <action android:name="org.openremote.android.geofence.ACTION_RECEIVE"/>
+ </intent-filter>
+ </receiver>
<service
android:name=".service.GeofenceProvider$LocationService"
android:exported="false"/>
diff --git a/console/android/src/main/java/org/openremote/android/service/GeofenceProvider.kt b/console/android/src/main/java/org/openremote/android/service/GeofenceProvider.kt
index 8c714de62..4845235bf 100644
--- a/console/android/src/main/java/org/openremote/android/service/GeofenceProvider.kt
+++ b/console/android/src/main/java/org/openremote/android/service/GeofenceProvider.kt
@@ -13,12 +13,10 @@ import android.os.IBinder
import android.support.annotation.RequiresApi
import android.support.v4.app.ActivityCompat
import android.support.v4.app.NotificationCompat
-import android.support.v4.app.NotificationCompat.PRIORITY_MIN
import android.support.v4.content.ContextCompat
import android.util.Log
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.android.gms.location.*
-import org.openremote.android.BuildConfig
import org.openremote.android.R
import java.io.*
import java.net.URL
@@ -41,11 +39,11 @@ class GeofenceProvider(val activity: Activity) {
lateinit var geofencingClient: GeofencingClient
private val geofencePendingIntent: PendingIntent by lazy {
- val intent = Intent(activity, GeofenceTransitionsIntentService::class.java)
+ val intent = Intent("org.openremote.android.geofence.ACTION_RECEIVE")
intent.putExtra(consoleIdKey, consoleId)
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
// addGeofences() and removeGeofences().
- PendingIntent.getService(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
+ PendingIntent.getBroadcast(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
var baseURL: String? = null
@@ -159,7 +157,15 @@ class GeofenceProvider(val activity: Activity) {
}
fun clearAllRegions() {
- geofencingClient.removeGeofences(geofencePendingIntent)
+ geofencingClient.removeGeofences(geoPostUrls.keys.toList()).run {
+ addOnSuccessListener {
+ Log.i("GeofenceProvider", "Geofences removed")
+ }
+
+ addOnFailureListener { exception ->
+ Log.e("GeofenceProvider", exception.message, exception)
+ }
+ }
geoPostUrls = hashMapOf()
}
@@ -213,17 +219,25 @@ class GeofenceProvider(val activity: Activity) {
@SuppressLint("MissingPermission")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- val channelId = createNotificationChannel()
- val notificationBuilder = NotificationCompat.Builder(this, channelId)
- val notification = notificationBuilder.setOngoing(true)
- .setSmallIcon(R.drawable.ic_launcher)
- .setPriority(PRIORITY_MIN)
- .setCategory(Notification.CATEGORY_SERVICE)
- .build()
- startForeground(101, notification)
+
+ val notificationBuilder = when {
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
+ val channelId = createNotificationChannel()
+ NotificationCompat.Builder(this, channelId)
+ }
+ else -> {
+ NotificationCompat.Builder(this)
+ }
}
+ val notification = notificationBuilder.setOngoing(true)
+ .setSmallIcon(R.drawable.ic_launcher)
+ .setCategory(Notification.CATEGORY_SERVICE)
+ .setContentTitle("Tracking location")
+ .build()
+
+ notification.flags = notification.flags or Notification.FLAG_ONGOING_EVENT or Notification.FLAG_FOREGROUND_SERVICE or Notification.FLAG_NO_CLEAR
+
//Force location updates
val locationRequest = LocationRequest().apply {
interval = 60000//1min
@@ -235,7 +249,9 @@ class GeofenceProvider(val activity: Activity) {
locationCallback,
null)
- return super.onStartCommand(intent, flags, startId)
+ startForeground(101, notification)
+
+ return START_REDELIVER_INTENT
}
override fun onDestroy() {
diff --git a/console/android/src/main/java/org/openremote/android/service/GeofenceTransitionsIntentService.kt b/console/android/src/main/java/org/openremote/android/service/GeofenceTransitionsIntentService.kt
index 06828b31c..616d72f7e 100644
--- a/console/android/src/main/java/org/openremote/android/service/GeofenceTransitionsIntentService.kt
+++ b/console/android/src/main/java/org/openremote/android/service/GeofenceTransitionsIntentService.kt
@@ -1,6 +1,7 @@
package org.openremote.android.service
-import android.app.IntentService
+import android.content.BroadcastReceiver
+import android.content.Context
import android.content.Intent
import android.util.Log
import com.fasterxml.jackson.databind.ObjectMapper
@@ -12,18 +13,11 @@ import retrofit2.converter.jackson.JacksonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.logging.Logger
-class GeofenceTransitionsIntentService : IntentService("or-geofence") {
+class GeofenceTransitionsIntentService : BroadcastReceiver() {
private val LOG = Logger.getLogger(GeofenceTransitionsIntentService::class.java.name)
- private val restApiResource: RestApiResource by lazy {
- Retrofit.Builder()
- .baseUrl(getString(R.string.OR_BASE_SERVER))
- .addConverterFactory(ScalarsConverterFactory.create())
- .addConverterFactory(JacksonConverterFactory.create())
- .build().create(RestApiResource::class.java);
- }
- override fun onHandleIntent(intent: Intent?) {
+ override fun onReceive(context: Context?, intent: Intent?) {
val geofencingEvent = GeofencingEvent.fromIntent(intent)
if (geofencingEvent.hasError()) {
@@ -51,13 +45,21 @@ class GeofenceTransitionsIntentService : IntentService("or-geofence") {
}
}
- try {
- val response = restApiResource
- .updateAssetAction(getString(R.string.OR_REALM), null, consoleId, "location", ObjectMapper().writeValueAsString(postJson))
- .execute()
- Log.i("Geofence", "Location posted to server: response=" + response.code());
- } catch (exception: Exception) {
- Log.e("Geofence", exception.message, exception)
- }
+ Thread({
+ try {
+ val restApiResource = Retrofit.Builder()
+ .baseUrl(context?.getString(R.string.OR_BASE_SERVER))
+ .addConverterFactory(ScalarsConverterFactory.create())
+ .addConverterFactory(JacksonConverterFactory.create())
+ .build().create(RestApiResource::class.java);
+
+ val response = restApiResource
+ .updateAssetAction(context?.getString(R.string.OR_REALM), null, consoleId, "location", ObjectMapper().writeValueAsString(postJson))
+ .execute()
+ Log.i("Geofence", "Location posted to server: response=" + response.code());
+ } catch (exception: Exception) {
+ Log.e("Geofence", exception.message, exception)
+ }
+ }).start()
}
}
\ No newline at end of file