[-] Remove main activity instance
This commit is contained in:
@@ -21,10 +21,6 @@ import java.util.*
|
|||||||
|
|
||||||
class MainActivity : AppCompatActivity()
|
class MainActivity : AppCompatActivity()
|
||||||
{
|
{
|
||||||
companion object {
|
|
||||||
var instance: MainActivity? = null
|
|
||||||
}
|
|
||||||
|
|
||||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||||
|
|
||||||
lateinit var binding: ActivityMainBinding
|
lateinit var binding: ActivityMainBinding
|
||||||
@@ -62,8 +58,6 @@ class MainActivity : AppCompatActivity()
|
|||||||
NotificationManager.IMPORTANCE_MIN)
|
NotificationManager.IMPORTANCE_MIN)
|
||||||
getSysServ<NotificationManager>().createNotificationChannel(chan)
|
getSysServ<NotificationManager>().createNotificationChannel(chan)
|
||||||
|
|
||||||
instance = this
|
|
||||||
|
|
||||||
if (!hasPermissions()) permissionCallback.launch(intent<ActivityPermissions>())
|
if (!hasPermissions()) permissionCallback.launch(intent<ActivityPermissions>())
|
||||||
else afterPermissions()
|
else afterPermissions()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import android.content.BroadcastReceiver
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.IntentFilter
|
import android.content.IntentFilter
|
||||||
|
import android.os.BatteryManager
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import com.influxdb.client.kotlin.InfluxDBClientKotlin
|
import com.influxdb.client.kotlin.InfluxDBClientKotlin
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@@ -22,6 +23,7 @@ import timber.log.Timber
|
|||||||
class MyService : Service()
|
class MyService : Service()
|
||||||
{
|
{
|
||||||
private lateinit var influx: InfluxDBClientKotlin
|
private lateinit var influx: InfluxDBClientKotlin
|
||||||
|
private val bm by lazy { getSysServ<BatteryManager>() }
|
||||||
|
|
||||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||||
|
|
||||||
@@ -47,11 +49,11 @@ class MyService : Service()
|
|||||||
scope.launch {
|
scope.launch {
|
||||||
channel.consumeAsFlow().collect {
|
channel.consumeAsFlow().collect {
|
||||||
try {
|
try {
|
||||||
MainActivity.instance?.addRecord(it)
|
println("Adding ${it.javaClass.simpleName} to influxdb")
|
||||||
influx add it
|
influx add it
|
||||||
}
|
}
|
||||||
catch (e: Exception) {
|
catch (e: Exception) {
|
||||||
MainActivity.instance?.addRecord(e)
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,7 +62,7 @@ class MyService : Service()
|
|||||||
private val mBatInfoReceiver: BroadcastReceiver = object : BroadcastReceiver()
|
private val mBatInfoReceiver: BroadcastReceiver = object : BroadcastReceiver()
|
||||||
{
|
{
|
||||||
override fun onReceive(ctxt: Context, intent: Intent) {
|
override fun onReceive(ctxt: Context, intent: Intent) {
|
||||||
val bi = intent.batteryInfo()
|
val bi = intent.batteryInfo(bm)
|
||||||
Timber.d("Battery info recorded: $bi")
|
Timber.d("Battery info recorded: $bi")
|
||||||
scope.launch { influx add bi }
|
scope.launch { influx add bi }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user