aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt38
-rw-r--r--app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt13
2 files changed, 32 insertions, 19 deletions
diff --git a/app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt b/app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt
index 06ef35c..fef7f4d 100644
--- a/app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt
+++ b/app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt
@@ -12,6 +12,7 @@ import android.util.Log
import android.content.SharedPreferences
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.preference.PreferenceManager
+import androidx.preference.SwitchPreferenceCompat
import dev.tablaster.dashpanel.mqtt.ConnectionData
import dev.tablaster.dashpanel.mqtt.MQTTModule
import dev.tablaster.dashpanel.mqtt.MQTTOptions
@@ -104,28 +105,31 @@ class DashPanelService : Service(), MQTTModule.MQTTListener {
wakeLock.setReferenceCounted(false)
notificationUtils = NotificationUtils(this)
- notificationUtils.startForegroundService()
+ notificationUtils.startForegroundService(this)
Log.d(TAG, "DashPanelService started")
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
- mqttOptions = MQTTOptions(prefs)
- mqttModule = MQTTModule(this, mqttOptions, this)
-
- updateInterval = prefs.getString("sensor_update_interval", "15")!!.toInt()
-
- prefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener)
-
- connectionData = ConnectionData(this)
- connectionData.observe(ProcessLifecycleOwner.get()) { connected ->
- if (connected && !isConnected) {
- mqttModule.restart()
- isConnected = true
- } else if (!connected && isConnected) {
- mqttModule.pause()
- isConnected = false
+ val mqttEnabled = prefs.getBoolean("mqtt_enalbed", false)
+ mqttOptions = MQTTOptions(prefs)
+ mqttModule = MQTTModule(this, mqttOptions, this)
+
+ updateInterval = prefs.getString("sensor_update_interval", "15")!!.toInt()
+
+ prefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener)
+
+ connectionData = ConnectionData(this)
+ if (mqttEnabled) {
+ connectionData.observe(ProcessLifecycleOwner.get()) { connected ->
+ if (connected && !isConnected) {
+ mqttModule.restart()
+ isConnected = true
+ } else if (!connected && isConnected) {
+ mqttModule.pause()
+ isConnected = false
+ }
}
+ mqttModule.start()
}
- mqttModule.start()
if (!sensorsInitialized) {
registerSensorProvider(BatterySensorProvider(this))
diff --git a/app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt b/app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt
index 48baa85..4508df7 100644
--- a/app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt
+++ b/app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt
@@ -4,6 +4,7 @@ import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
+import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
@@ -56,12 +57,20 @@ class NotificationUtils(private val context: Context) {
.build()
}
- fun startForegroundService() {
+ fun startForegroundService(service: Service) {
val notification = buildNotification(
context.getString(R.string.service_notification_title),
context.getString(R.string.service_notification_message)
)
- notificationManager.notify(NOTIFICATION_ID, notification)
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+ service.startForeground(
+ NOTIFICATION_ID,
+ notification,
+ android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
+ )
+ } else {
+ service.startForeground(NOTIFICATION_ID, notification)
+ }
}
fun updateNotification(title: String, message: String) {