106 lines
4.3 KiB
Kotlin
106 lines
4.3 KiB
Kotlin
package de.nicolasklier.livebeat
|
|
|
|
import android.Manifest
|
|
import android.annotation.SuppressLint
|
|
import android.app.*
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.IntentFilter
|
|
import android.content.pm.PackageManager
|
|
import android.graphics.Color
|
|
import android.location.LocationManager
|
|
import android.os.BatteryManager
|
|
import android.os.IBinder
|
|
import android.provider.Settings
|
|
import android.util.Log
|
|
import androidx.core.app.ActivityCompat
|
|
import androidx.core.app.NotificationCompat
|
|
import com.rabbitmq.client.Channel
|
|
import com.rabbitmq.client.Connection
|
|
import com.squareup.moshi.JsonAdapter
|
|
import com.squareup.moshi.Moshi
|
|
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
|
|
|
class TrackerService : Service() {
|
|
|
|
val conn = arrayOfNulls<Connection>(1)
|
|
val channel = arrayOfNulls<Channel>(1)
|
|
|
|
override fun onBind(intent: Intent): IBinder? {
|
|
return null
|
|
}
|
|
|
|
@SuppressLint("CheckResult")
|
|
private fun subscribeToEvents() {
|
|
HttpRequests.sse("http://192.168.178.26/user/events?token=${MainActivity.USER?.eventToken}")
|
|
}
|
|
|
|
@SuppressLint("HardwareIds")
|
|
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
|
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
|
|
// Android id as unique identifier
|
|
val androidId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
|
locationManager.requestLocationUpdates("gps", 10000, 0f
|
|
) { location ->
|
|
Log.i("Location", "Location is: " + location.latitude + " | " + location.longitude)
|
|
|
|
/* Get battery */
|
|
val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { ifilter ->
|
|
this@TrackerService.registerReceiver(null, ifilter)
|
|
}
|
|
val batteryPct: Float? = batteryStatus?.let { intent ->
|
|
val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
|
|
val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
|
|
level * 100 / scale.toFloat()
|
|
}
|
|
|
|
val beat = Beat(
|
|
androidId,
|
|
arrayOf(location.latitude, location.longitude, location.altitude, location.accuracy.toDouble(), location.speed.toDouble()),
|
|
batteryPct?.toInt(), location.time)
|
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
|
val jsonAdapter: JsonAdapter<Beat> = moshi.adapter(Beat::class.java)
|
|
|
|
Thread(Runnable {
|
|
channel[0]?.basicPublish("", "tracker", null, jsonAdapter.toJson(beat).toByteArray())
|
|
}).start()
|
|
}
|
|
}
|
|
|
|
subscribeToEvents()
|
|
startForeground()
|
|
return super.onStartCommand(intent, flags, startId)
|
|
}
|
|
|
|
private fun startForeground() {
|
|
val noticicationIntent = Intent(this, MainActivity::class.java)
|
|
val pendingIntent = PendingIntent.getActivity(this, 0, noticicationIntent, 0)
|
|
val chan = NotificationChannel(
|
|
NOTIF_CHANNEL_ID,
|
|
"Tracker",
|
|
NotificationManager.IMPORTANCE_LOW)
|
|
chan.lightColor = Color.BLACK
|
|
chan.lockscreenVisibility = Notification.VISIBILITY_SECRET
|
|
val manager = (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
|
|
manager.createNotificationChannel(chan)
|
|
startForeground(NOTIF_ID, NotificationCompat.Builder(this, NOTIF_CHANNEL_ID)
|
|
.setOngoing(true)
|
|
.setContentTitle("Livebeat")
|
|
.setContentText("Tracker is running")
|
|
.setContentIntent(pendingIntent)
|
|
.setCategory(Notification.CATEGORY_SERVICE)
|
|
.setPriority(NotificationManager.IMPORTANCE_LOW)
|
|
.setChannelId(NOTIF_CHANNEL_ID)
|
|
.setColorized(true)
|
|
.setColor(Color.BLACK)
|
|
.build())
|
|
}
|
|
|
|
companion object {
|
|
private const val NOTIF_ID = 1
|
|
private const val NOTIF_CHANNEL_ID = "LiveTracker"
|
|
}
|
|
} |