Skip to content

armgit / core

Public Imported from GitHub
0

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

Updated Android to API 26 sdk. Made location in background possible for API 26

Michael Rademakerdce4605466f4
diff --git a/console/android/build.gradle b/console/android/build.gradle
index f856e66cd..027f0e29b 100644
--- a/console/android/build.gradle
+++ b/console/android/build.gradle
@@ -8,7 +8,7 @@ buildscript {
}
}
dependencies {
- classpath 'com.android.tools.build:gradle:3.0.1'
+ classpath 'com.android.tools.build:gradle:3.1.2'
}
}
diff --git a/console/android/gradle.properties b/console/android/gradle.properties
index 4caffda8e..4a3e59e38 100644
--- a/console/android/gradle.properties
+++ b/console/android/gradle.properties
@@ -10,9 +10,9 @@ androidMinVersion = 21
androidTargetVersion = 25
androidGradleVersion = 2.2.3
-androidBuildToolsVersion = 25.0.2
-androidCompileVersion = 25
-androidSupportVersion = 25.1.1
+androidBuildToolsVersion = 27.0.3
+androidCompileVersion = 26
+androidSupportVersion = 26.1.0
okhttp3Version = 3.6.0
retrofitVersion = 2.2.0
diff --git a/console/android/src/main/AndroidManifest.xml b/console/android/src/main/AndroidManifest.xml
index d30903779..88f050bad 100644
--- a/console/android/src/main/AndroidManifest.xml
+++ b/console/android/src/main/AndroidManifest.xml
@@ -1,40 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.openremote.android">
+ package="org.openremote.android">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+ <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
+ <uses-permission android:name="android.permission.INTERNET"/>
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
+ <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
+ <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
+ android:name=".MainApplication"
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
- android:theme="@style/OpenRemoteTheme"
- android:name=".MainApplication">
+ android:theme="@style/OpenRemoteTheme">
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
- android:resource="@drawable/ic_launcher" />
+ android:resource="@drawable/ic_launcher"/>
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
- android:resource="@color/light_green" />
+ android:resource="@color/light_green"/>
- <activity android:name=".MainActivity"
+ <activity
+ android:name=".MainActivity"
android:configChanges="orientation|screenSize"
- android:windowSoftInputMode="adjustResize"
- android:label="@string/app_name">
+ android:label="@string/app_name"
+ android:windowSoftInputMode="adjustResize">
<intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
+ <action android:name="android.intent.action.MAIN"/>
+ <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
@@ -56,8 +57,11 @@
</intent-filter>
</service>
- <service android:name=".service.GeofenceTransitionsIntentService"/>
-
+ <service
+ android:name=".service.GeofenceTransitionsIntentService"/>
+ <service
+ android:name=".service.GeofenceProvider$LocationService"
+ android:exported="false"/>
</application>
diff --git a/console/android/src/main/java/org/openremote/android/MainActivity.java b/console/android/src/main/java/org/openremote/android/MainActivity.java
index 6c95c8190..d7187aa1e 100644
--- a/console/android/src/main/java/org/openremote/android/MainActivity.java
+++ b/console/android/src/main/java/org/openremote/android/MainActivity.java
@@ -77,7 +77,7 @@ public class MainActivity extends Activity {
}
};
- protected BroadcastReceiver actionReceiver = new BroadcastReceiver() {
+ protected BroadcastReceiver actionReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra("action")) {
@@ -168,6 +168,8 @@ public class MainActivity extends Activity {
openIntentUrl(getIntent());
errorViewHolder = new ErrorViewHolder(findViewById(R.id.errorView));
+
+ registerReceiver(actionReceiver, new IntentFilter(ACTION_BROADCAST));
}
@Override
@@ -197,7 +199,6 @@ public class MainActivity extends Activity {
super.onResume();
registerReceiver(connectivityChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
registerReceiver(onDownloadCompleteReciever, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
- registerReceiver(actionReceiver, new IntentFilter(ACTION_BROADCAST));
}
@Override
@@ -205,7 +206,13 @@ public class MainActivity extends Activity {
super.onPause();
unregisterReceiver(connectivityChangeReceiver);
unregisterReceiver(onDownloadCompleteReciever);
+ }
+
+ @Override
+ protected void onDestroy() {
unregisterReceiver(actionReceiver);
+
+ super.onDestroy();
}
@Override
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 a80e20464..8c714de62 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
@@ -2,17 +2,23 @@ package org.openremote.android.service
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.annotation.SuppressLint
-import android.app.Activity
-import android.app.PendingIntent
+import android.app.*
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.Intent
import android.content.pm.PackageManager
+import android.graphics.Color
+import android.os.Build
+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,15 +47,12 @@ class GeofenceProvider(val activity: Activity) {
// addGeofences() and removeGeofences().
PendingIntent.getService(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
- private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
- private lateinit var locationCallback: LocationCallback
var baseURL: String? = null
var consoleId: String? = null
fun initialize(): Map<String, Any> {
- fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(activity)
geofencingClient = LocationServices.getGeofencingClient(activity)
var inputStream: ObjectInputStream? = null
@@ -83,24 +86,12 @@ class GeofenceProvider(val activity: Activity) {
.putString(consoleIdKey, this.consoleId)
.apply()
- //Force location updates
- val locationRequest = LocationRequest().apply {
- interval = 60000//1min
- fastestInterval = 10000//10sec
- priority = LocationRequest.PRIORITY_HIGH_ACCURACY
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ activity.startForegroundService(Intent(activity, LocationService::class.java))
+ } else {
+ activity.startService(Intent(activity, LocationService::class.java))
}
- locationCallback = object : LocationCallback() {
- override fun onLocationResult(locationResult: LocationResult?) {
- locationResult ?: return
- Log.i("GeofenceProvider", "Location received: $locationResult")
- }
- }
-
- fusedLocationProviderClient.requestLocationUpdates(locationRequest,
- locationCallback,
- null)
-
fetchGeofences()
return hashMapOf(
@@ -118,7 +109,7 @@ class GeofenceProvider(val activity: Activity) {
.apply()
baseURL = null
consoleId = null
- fusedLocationProviderClient.removeLocationUpdates(locationCallback)
+ activity.stopService(Intent(activity, LocationService::class.java))
clearAllRegions()
}
@@ -144,16 +135,17 @@ class GeofenceProvider(val activity: Activity) {
val geofencingRequest = GeofencingRequest.Builder()
.addGeofence(geofence)
- .setInitialTrigger(Geofence.GEOFENCE_TRANSITION_ENTER)
+ .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.build()
geofencingClient.addGeofences(geofencingRequest, geofencePendingIntent).run {
addOnSuccessListener {
+ Log.i("GeofenceProvider", "Geofence added: lat${geofenceDefinition.lat}/lng${geofenceDefinition.lng}/rad${geofenceDefinition.radius}")
geoPostUrls.put(geofenceDefinition.id, geofenceDefinition.postUrl)
}
addOnFailureListener { exception ->
- print(exception)
+ Log.e("GeofenceProvider", exception.message, exception)
}
}
}
@@ -181,6 +173,7 @@ class GeofenceProvider(val activity: Activity) {
val response = URL("$baseURL/$geofenceFetchEndpoint$consoleId").readText()
val geofences = ObjectMapper().readValue(response, Array<GeofenceDefinition>::class.java)
clearAllRegions()
+ Log.i("fetchGeofences", "${geofences.size} geofences")
geofences.forEach {
addGeofence(it)
geoPostUrls[it.id] = it.postUrl
@@ -199,4 +192,74 @@ class GeofenceProvider(val activity: Activity) {
var radius: Float = 0.0F
var postUrl: String = ""
}
+
+ class LocationService : Service() {
+
+ private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
+ private lateinit var locationCallback: LocationCallback
+
+ override fun onCreate() {
+ super.onCreate()
+
+ fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(applicationContext)
+
+ locationCallback = object : LocationCallback() {
+ override fun onLocationResult(locationResult: LocationResult?) {
+ locationResult ?: return
+ Log.i("GeofenceProvider", "Location received: lat${locationResult.lastLocation.latitude}/lng${locationResult.lastLocation.longitude}")
+ }
+ }
+ }
+
+ @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)
+ }
+
+ //Force location updates
+ val locationRequest = LocationRequest().apply {
+ interval = 60000//1min
+ fastestInterval = 10000//10sec
+ priority = LocationRequest.PRIORITY_HIGH_ACCURACY
+ }
+
+ fusedLocationProviderClient.requestLocationUpdates(locationRequest,
+ locationCallback,
+ null)
+
+ return super.onStartCommand(intent, flags, startId)
+ }
+
+ override fun onDestroy() {
+ fusedLocationProviderClient.removeLocationUpdates(locationCallback)
+
+ super.onDestroy()
+ }
+
+ override fun onBind(intent: Intent?): IBinder? {
+ return null
+ }
+
+ @RequiresApi(Build.VERSION_CODES.O)
+ private fun createNotificationChannel(): String {
+ val channelId = "OR_EIND"
+ val channelName = "OR Eindhoven Location Service"
+ val channel = NotificationChannel(channelId,
+ channelName, NotificationManager.IMPORTANCE_HIGH)
+ channel.lightColor = Color.BLUE
+ channel.importance = NotificationManager.IMPORTANCE_NONE
+ channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
+ val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
+ service!!.createNotificationChannel(channel)
+ return channelId
+ }
+ }
}
\ No newline at end of file
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 de781e644..06828b31c 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
@@ -17,24 +17,17 @@ class GeofenceTransitionsIntentService : IntentService("or-geofence") {
private val LOG = Logger.getLogger(GeofenceTransitionsIntentService::class.java.name)
private val restApiResource: RestApiResource by lazy {
Retrofit.Builder()
- .baseUrl(applicationContext.getString(R.string.OR_BASE_SERVER))
+ .baseUrl(getString(R.string.OR_BASE_SERVER))
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(JacksonConverterFactory.create())
.build().create(RestApiResource::class.java);
}
- override fun onCreate() {
- super.onCreate()
-
-
- }
-
override fun onHandleIntent(intent: Intent?) {
-
val geofencingEvent = GeofencingEvent.fromIntent(intent)
if (geofencingEvent.hasError()) {
- LOG.warning("Error handling geofence event")
+ Log.w("Geofence", "Error handling geofence event")
return
}
@@ -60,9 +53,9 @@ class GeofenceTransitionsIntentService : IntentService("or-geofence") {
try {
val response = restApiResource
- .updateAssetAction(application.getString(R.string.OR_REALM), null, consoleId, "location", ObjectMapper().writeValueAsString(postJson))
+ .updateAssetAction(getString(R.string.OR_REALM), null, consoleId, "location", ObjectMapper().writeValueAsString(postJson))
.execute()
- LOG.fine("Location posted to server: response=" + response.code());
+ Log.i("Geofence", "Location posted to server: response=" + response.code());
} catch (exception: Exception) {
Log.e("Geofence", exception.message, exception)
}