|
|
|
@ -4,7 +4,6 @@ import android.app.*
|
|
|
|
|
import android.content.Context |
|
|
|
|
import android.content.Intent |
|
|
|
|
import android.content.pm.PackageManager |
|
|
|
|
import android.graphics.Color |
|
|
|
|
import android.hardware.Sensor |
|
|
|
|
import android.hardware.SensorEvent |
|
|
|
|
import android.hardware.SensorEventListener |
|
|
|
@ -100,7 +99,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager |
|
|
|
|
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) |
|
|
|
|
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) |
|
|
|
|
// setupActivityRecognitionUpdates() |
|
|
|
|
setupActivityRecognitionUpdates() |
|
|
|
|
|
|
|
|
|
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
|
mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager |
|
|
|
@ -120,6 +119,12 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun getActivityTransitionPendingIntent(): PendingIntent { |
|
|
|
|
val intent = Intent(this, ActivityTransitionIntentService::class.java) |
|
|
|
|
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun setupActivityRecognitionUpdates() { |
|
|
|
|
// ActivityRecognition |
|
|
|
|
val transitions = mutableListOf<ActivityTransition>() |
|
|
|
@ -128,35 +133,46 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
.setActivityType(DetectedActivity.IN_VEHICLE) |
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) |
|
|
|
|
.build() |
|
|
|
|
transitions += |
|
|
|
|
/*transitions += |
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
.setActivityType(DetectedActivity.IN_VEHICLE) |
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT) |
|
|
|
|
.build() |
|
|
|
|
.build()*/ |
|
|
|
|
transitions += |
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
.setActivityType(DetectedActivity.ON_BICYCLE) |
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) |
|
|
|
|
.build() |
|
|
|
|
transitions += |
|
|
|
|
/*transitions += |
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
.setActivityType(DetectedActivity.ON_BICYCLE) |
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT) |
|
|
|
|
.build() |
|
|
|
|
.build()*/ |
|
|
|
|
transitions += |
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
.setActivityType(DetectedActivity.WALKING) |
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) |
|
|
|
|
.build() |
|
|
|
|
transitions += |
|
|
|
|
/*transitions += |
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
.setActivityType(DetectedActivity.WALKING) |
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT) |
|
|
|
|
.build()*/ |
|
|
|
|
transitions += |
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
.setActivityType(DetectedActivity.STILL) |
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) |
|
|
|
|
.build() |
|
|
|
|
val request = ActivityTransitionRequest(transitions) |
|
|
|
|
|
|
|
|
|
val pendingIntent = getActivityTransitionPendingIntent() |
|
|
|
|
mActivityRecognitionClient = ActivityRecognition.getClient(applicationContext) |
|
|
|
|
mActivityRecognitionClient.requestActivityTransitionUpdates(request, null) |
|
|
|
|
val task = |
|
|
|
|
mActivityRecognitionClient.requestActivityTransitionUpdates(request, pendingIntent) |
|
|
|
|
|
|
|
|
|
task.addOnSuccessListener { log("ACT_TRANSITION: success listener") } |
|
|
|
|
task.addOnFailureListener { exception -> |
|
|
|
|
log("ACT_TRANSITION: failed $exception") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onDestroy() { |
|
|
|
@ -310,7 +326,8 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
val (bytes, error) = result |
|
|
|
|
if (bytes != null) { |
|
|
|
|
log("[response bytes] ${String(bytes)}") |
|
|
|
|
val notification = createNotification("[req] ${gmtTime} - ${String(bytes)}") |
|
|
|
|
val notification = |
|
|
|
|
createNotification("[req] ${gmtTime} - ${String(bytes)}") |
|
|
|
|
nm.notify(notificationId, notification) |
|
|
|
|
} else { |
|
|
|
|
log("[response error] ${error?.message}") |
|
|
|
@ -330,13 +347,13 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
val channel = NotificationChannel( |
|
|
|
|
notificationChannelId, |
|
|
|
|
"SL notifications channel", |
|
|
|
|
NotificationManager.IMPORTANCE_DEFAULT |
|
|
|
|
NotificationManager.IMPORTANCE_LOW |
|
|
|
|
).let { |
|
|
|
|
it.description = "SL Service channel" |
|
|
|
|
it.enableLights(true) |
|
|
|
|
it.lightColor = Color.RED |
|
|
|
|
it.enableVibration(true) |
|
|
|
|
it.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400) |
|
|
|
|
it.enableLights(false) |
|
|
|
|
//it.lightColor = Color.RED |
|
|
|
|
it.enableVibration(false) |
|
|
|
|
//it.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400) |
|
|
|
|
it |
|
|
|
|
} |
|
|
|
|
nm.createNotificationChannel(channel) |
|
|
|
|