Skip to content

Commit

Permalink
perf(android): stop autoconnect service when unused
Browse files Browse the repository at this point in the history
If auto connect is disabled, there is no reason to keep the background
service running.
  • Loading branch information
Oppzippy committed Apr 18, 2024
1 parent b9e8b9d commit 54dc310
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ package com.oppzippy.openscq30
import android.app.Application
import android.content.Intent
import com.oppzippy.openscq30.features.autoconnect.AutoConnectService
import com.oppzippy.openscq30.features.preferences.Preferences
import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject

@HiltAndroidApp
class OpenSCQ30Application : Application() {

@Inject
lateinit var preferences: Preferences

init {
Native.initialize()
}

override fun onCreate() {
super.onCreate()
startService(Intent(this, AutoConnectService::class.java))
if (preferences.autoConnect) {
startService(Intent(this, AutoConnectService::class.java))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.oppzippy.openscq30.features.preferences.Preferences
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class AutoStartReceiver : BroadcastReceiver() {
@Inject
lateinit var preferences: Preferences

override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == Intent.ACTION_BOOT_COMPLETED) {
Log.d("BootReceiver", "starting background service")
context?.applicationContext?.startService(
Intent(context, AutoConnectService::class.java),
)
if (preferences.autoConnect) {
Log.d("AutoStartReceiver", "starting background service")
context?.applicationContext?.startService(
Intent(context.applicationContext, AutoConnectService::class.java),
)
} else {
Log.d(
"AutoStartReceiver",
"not starting background service on boot since auto connect is disabled",
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ import com.oppzippy.openscq30.features.soundcoredevice.service.DeviceService

class BluetoothConnectionReceiver(private val preferences: Preferences) : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("BluetoothConnectionReceiver", "got onReceive")
if (!preferences.autoConnect) {
Log.w(
"BluetoothConnectionReceiver",
"Got device connected event, but auto connect is disabled. This service should not be running.",
)
return
}

if (context != null &&
intent != null &&
intent.action == BluetoothDevice.ACTION_ACL_CONNECTED &&
preferences.autoConnect
intent.action == BluetoothDevice.ACTION_ACL_CONNECTED
) {
val device = IntentCompat.getParcelableExtra(
intent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.oppzippy.openscq30.ui.settings

import android.content.Context
import android.content.Intent
import androidx.lifecycle.ViewModel
import com.oppzippy.openscq30.features.autoconnect.AutoConnectService
import com.oppzippy.openscq30.features.preferences.Preferences
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class SettingsViewModel @Inject constructor(
@ApplicationContext private val context: Context,
private val preferences: Preferences,
) : ViewModel() {
private val _autoConnect = MutableStateFlow(preferences.autoConnect)
Expand All @@ -17,5 +22,11 @@ class SettingsViewModel @Inject constructor(
fun setAutoConnect(value: Boolean) {
_autoConnect.value = value
preferences.autoConnect = value
val intent = Intent(context, AutoConnectService::class.java)
if (value) {
context.startService(intent)
} else {
context.stopService(intent)
}
}
}

0 comments on commit 54dc310

Please sign in to comment.