|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
package co.zzyzx.sensorlogger |
|
|
|
|
|
|
|
|
|
//import com.github.kittinunf.fuel.core.extensions.jsonBody |
|
|
|
|
import android.app.* |
|
|
|
|
import android.content.Context |
|
|
|
|
import android.content.Intent |
|
|
|
@ -19,6 +18,7 @@ import android.os.IBinder
|
|
|
|
|
import android.os.PowerManager |
|
|
|
|
import android.provider.Settings |
|
|
|
|
import android.widget.Toast |
|
|
|
|
import co.zzyzx.sensorlogger.db.RecordRepository |
|
|
|
|
import com.github.kittinunf.fuel.Fuel |
|
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
|
import kotlinx.coroutines.GlobalScope |
|
|
|
@ -29,32 +29,11 @@ import java.util.*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun Double.format(digits: Int) = java.lang.String.format("%.${digits}f", this) |
|
|
|
|
fun Float.format(digits: Int) = java.lang.String.format("%.${digits}f", this) |
|
|
|
|
const val SENSOR_DELAY = SensorManager.SENSOR_DELAY_GAME |
|
|
|
|
|
|
|
|
|
class EndlessService : Service(), SensorEventListener, LocationListener { |
|
|
|
|
|
|
|
|
|
override fun onLocationChanged(result: Location) { |
|
|
|
|
val txt = |
|
|
|
|
"coords: ${result.longitude.format(4)}, ${result.latitude.format(4)} - ${result.time.toDouble().format( |
|
|
|
|
0 |
|
|
|
|
)}" |
|
|
|
|
log(txt) |
|
|
|
|
|
|
|
|
|
val notification = createNotification(txt) |
|
|
|
|
nm.notify(notificationId, notification) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onProviderEnabled(p0: String?) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onProviderDisabled(p0: String?) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private var wakeLock: PowerManager.WakeLock? = null |
|
|
|
|
private var isServiceStarted = false |
|
|
|
|
private lateinit var nm: NotificationManager |
|
|
|
@ -63,7 +42,10 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
|
|
|
|
|
private lateinit var mSensorManager: SensorManager |
|
|
|
|
private lateinit var mAccelerometer: Sensor |
|
|
|
|
private lateinit var mLocationManager: LocationManager |
|
|
|
|
private lateinit var mGyroscope: Sensor |
|
|
|
|
|
|
|
|
|
private var mLocationManager: LocationManager? = null // need to do this because of permission |
|
|
|
|
private lateinit var recRepo: RecordRepository |
|
|
|
|
|
|
|
|
|
override fun onBind(intent: Intent): IBinder? { |
|
|
|
|
log("Some component want to bind with the service") |
|
|
|
@ -87,7 +69,8 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL) |
|
|
|
|
mSensorManager.registerListener(this, mAccelerometer, SENSOR_DELAY) |
|
|
|
|
mSensorManager.registerListener(this, mGyroscope, SENSOR_DELAY) |
|
|
|
|
|
|
|
|
|
// by returning this we make sure the service is restarted if the system kills the service |
|
|
|
|
return START_STICKY |
|
|
|
@ -96,35 +79,40 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
override fun onCreate() { |
|
|
|
|
super.onCreate() |
|
|
|
|
log("The service has been created".toUpperCase()) |
|
|
|
|
// database init |
|
|
|
|
recRepo = RecordRepository(applicationContext) |
|
|
|
|
|
|
|
|
|
// notification init |
|
|
|
|
val notification = createNotification() |
|
|
|
|
notification.flags = Notification.FLAG_ONGOING_EVENT |
|
|
|
|
startForeground(notificationId, notification) |
|
|
|
|
|
|
|
|
|
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager |
|
|
|
|
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) |
|
|
|
|
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) |
|
|
|
|
|
|
|
|
|
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
|
mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager |
|
|
|
|
mLocationManager.requestLocationUpdates( |
|
|
|
|
mLocationManager?.requestLocationUpdates( |
|
|
|
|
LocationManager.PASSIVE_PROVIDER, |
|
|
|
|
2000, // 2-sec |
|
|
|
|
2.toFloat(), // 2-meter |
|
|
|
|
this |
|
|
|
|
) |
|
|
|
|
mLocationManager.requestLocationUpdates( |
|
|
|
|
mLocationManager?.requestLocationUpdates( |
|
|
|
|
LocationManager.GPS_PROVIDER, |
|
|
|
|
2000, // 2-sec |
|
|
|
|
2.toFloat(), // 2-meter |
|
|
|
|
this |
|
|
|
|
) |
|
|
|
|
log("ok permission to get location updates") |
|
|
|
|
// log("ok permission to get location updates") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onDestroy() { |
|
|
|
|
super.onDestroy() |
|
|
|
|
mSensorManager.unregisterListener(this) |
|
|
|
|
mLocationManager.removeUpdates(this) |
|
|
|
|
mLocationManager?.removeUpdates(this) |
|
|
|
|
log("The service has been destroyed".toUpperCase()) |
|
|
|
|
Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show() |
|
|
|
|
} |
|
|
|
@ -175,6 +163,39 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
nm.cancelAll() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onLocationChanged(result: Location) { |
|
|
|
|
val notiText = "coords: ${result.longitude.format(4)}, ${result.latitude.format(4)}" |
|
|
|
|
val txt = arrayOf( |
|
|
|
|
result.time, |
|
|
|
|
result.longitude.format(7), |
|
|
|
|
result.latitude.format(7), |
|
|
|
|
result.speed, |
|
|
|
|
result.altitude, |
|
|
|
|
result.bearing, |
|
|
|
|
result.accuracy, |
|
|
|
|
result.provider, |
|
|
|
|
result.isFromMockProvider |
|
|
|
|
).joinToString(",") |
|
|
|
|
|
|
|
|
|
recRepo.addNewRecord("location", txt) |
|
|
|
|
val notification = createNotification(notiText) |
|
|
|
|
nm.notify(notificationId, notification) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onProviderEnabled(p0: String?) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onProviderDisabled(p0: String?) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onAccuracyChanged(p0: Sensor?, p1: Int) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
@ -185,7 +206,6 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
|
|
|
|
|
when (evt.sensor?.type) { |
|
|
|
|
Sensor.TYPE_ACCELEROMETER -> { |
|
|
|
|
|
|
|
|
|
val accelLin = arrayOf(0.toDouble(), 0.toDouble(), 0.toDouble()) |
|
|
|
|
val alpha = 0.8f |
|
|
|
|
accelGravity[0] = alpha * accelGravity[0] + (1 - alpha) * evt.values[0] |
|
|
|
@ -194,13 +214,20 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
|
|
|
|
|
accelLin[0] = evt.values[0] - accelGravity[0] |
|
|
|
|
accelLin[1] = evt.values[1] - accelGravity[1] |
|
|
|
|
accelLin[2] = evt.values[2] - accelGravity[2] |
|
|
|
|
|
|
|
|
|
val txt = |
|
|
|
|
"[accel] (${accelLin[0].format(3)}, ${accelLin[1].format(3)}, ${accelLin[2].format( |
|
|
|
|
3 |
|
|
|
|
)})" |
|
|
|
|
// val notification = createNotification(txt) |
|
|
|
|
// nm.notify(notificationId, notification) |
|
|
|
|
val txt = arrayOf( |
|
|
|
|
accelLin[0].format(3), |
|
|
|
|
accelLin[1].format(3), |
|
|
|
|
accelLin[2].format(3) |
|
|
|
|
).joinToString(",") |
|
|
|
|
recRepo.addNewRecord("accelerometer", txt) |
|
|
|
|
} |
|
|
|
|
Sensor.TYPE_GYROSCOPE -> { |
|
|
|
|
val txt = arrayOf( |
|
|
|
|
evt.values[0].format(3), |
|
|
|
|
evt.values[1].format(3), |
|
|
|
|
evt.values[2].format(3) |
|
|
|
|
).joinToString(",") |
|
|
|
|
recRepo.addNewRecord("gyroscope", txt) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|