|
|
@ -21,6 +21,7 @@ import android.widget.Toast |
|
|
|
import co.zzyzx.sensorlogger.db.Record |
|
|
|
import co.zzyzx.sensorlogger.db.Record |
|
|
|
import co.zzyzx.sensorlogger.db.RecordRepository |
|
|
|
import co.zzyzx.sensorlogger.db.RecordRepository |
|
|
|
import com.github.kittinunf.fuel.Fuel |
|
|
|
import com.github.kittinunf.fuel.Fuel |
|
|
|
|
|
|
|
import com.google.android.gms.location.* |
|
|
|
import java.text.SimpleDateFormat |
|
|
|
import java.text.SimpleDateFormat |
|
|
|
import java.time.Instant |
|
|
|
import java.time.Instant |
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
@ -39,6 +40,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener { |
|
|
|
private val notificationChannelId = "ENDLESS SERVICE CHANNEL" |
|
|
|
private val notificationChannelId = "ENDLESS SERVICE CHANNEL" |
|
|
|
private val notificationId = 1011 |
|
|
|
private val notificationId = 1011 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private lateinit var mActivityRecognitionClient: ActivityRecognitionClient |
|
|
|
private lateinit var mSensorManager: SensorManager |
|
|
|
private lateinit var mSensorManager: SensorManager |
|
|
|
private lateinit var mAccelerometer: Sensor |
|
|
|
private lateinit var mAccelerometer: Sensor |
|
|
|
private lateinit var mGyroscope: Sensor |
|
|
|
private lateinit var mGyroscope: Sensor |
|
|
@ -78,10 +80,8 @@ class EndlessService : Service(), SensorEventListener, LocationListener { |
|
|
|
"with a null intent. It has been probably restarted by the system." |
|
|
|
"with a null intent. It has been probably restarted by the system." |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mSensorManager.registerListener(this, mAccelerometer, SENSOR_DELAY) |
|
|
|
mSensorManager.registerListener(this, mAccelerometer, SENSOR_DELAY) |
|
|
|
mSensorManager.registerListener(this, mGyroscope, SENSOR_DELAY) |
|
|
|
mSensorManager.registerListener(this, mGyroscope, SENSOR_DELAY) |
|
|
|
|
|
|
|
|
|
|
|
// by returning this we make sure the service is restarted if the system kills the service |
|
|
|
// by returning this we make sure the service is restarted if the system kills the service |
|
|
|
return START_STICKY |
|
|
|
return START_STICKY |
|
|
|
} |
|
|
|
} |
|
|
@ -100,6 +100,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener { |
|
|
|
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager |
|
|
|
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager |
|
|
|
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) |
|
|
|
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) |
|
|
|
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) |
|
|
|
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) |
|
|
|
|
|
|
|
setupActivityRecognitionUpdates() |
|
|
|
|
|
|
|
|
|
|
|
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager |
|
|
|
mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager |
|
|
@ -119,6 +120,45 @@ class EndlessService : Service(), SensorEventListener, LocationListener { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun setupActivityRecognitionUpdates() { |
|
|
|
|
|
|
|
// ActivityRecognition |
|
|
|
|
|
|
|
val transitions = mutableListOf<ActivityTransition>() |
|
|
|
|
|
|
|
transitions += |
|
|
|
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
|
|
|
.setActivityType(DetectedActivity.IN_VEHICLE) |
|
|
|
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
transitions += |
|
|
|
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
|
|
|
.setActivityType(DetectedActivity.IN_VEHICLE) |
|
|
|
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
transitions += |
|
|
|
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
|
|
|
.setActivityType(DetectedActivity.ON_BICYCLE) |
|
|
|
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
transitions += |
|
|
|
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
|
|
|
.setActivityType(DetectedActivity.ON_BICYCLE) |
|
|
|
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
transitions += |
|
|
|
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
|
|
|
.setActivityType(DetectedActivity.WALKING) |
|
|
|
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
transitions += |
|
|
|
|
|
|
|
ActivityTransition.Builder() |
|
|
|
|
|
|
|
.setActivityType(DetectedActivity.WALKING) |
|
|
|
|
|
|
|
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
val request = ActivityTransitionRequest(transitions) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mActivityRecognitionClient = ActivityRecognition.getClient(applicationContext) |
|
|
|
|
|
|
|
mActivityRecognitionClient.requestActivityTransitionUpdates(request, null) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onDestroy() { |
|
|
|
override fun onDestroy() { |
|
|
|
super.onDestroy() |
|
|
|
super.onDestroy() |
|
|
|
mSensorManager.unregisterListener(this) |
|
|
|
mSensorManager.unregisterListener(this) |
|
|
|