Skip to content

Commit 9c29d63

Browse files
committed
more bugfixes
1 parent 30cf653 commit 9c29d63

File tree

4 files changed

+160
-109
lines changed

4 files changed

+160
-109
lines changed

app/src/main/kotlin/com/heisenberg/lux/BootReceiver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class BootReceiver : BroadcastReceiver() {
1717
val prefs = context.getSharedPreferences(MainActivity.PREFS_NAME, Context.MODE_PRIVATE)
1818
val startAtBoot = prefs.getBoolean(MainActivity.KEY_START_AT_BOOT, false)
1919
val cameraSelection = prefs.getInt(MainActivity.KEY_CAMERA_SELECTION, MainActivity.CAMERA_NONE)
20-
val cameraSensitivity = prefs.getInt(MainActivity.KEY_CAMERA_SENSITIVITY, 50)
20+
val cameraSensitivity = prefs.getInt(MainActivity.KEY_CAMERA_SENSITIVITY, 0)
2121
val lightSensitivity = prefs.getInt(MainActivity.KEY_LIGHT_SENSITIVITY, 0)
2222

2323
val cameraEnabled = cameraSelection != MainActivity.CAMERA_NONE && cameraSensitivity > 0

app/src/main/kotlin/com/heisenberg/lux/DetectionService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class DetectionService : Service(), LifecycleOwner {
161161

162162
private fun updateDetectors() {
163163
val cameraSelection = prefs.getInt(MainActivity.KEY_CAMERA_SELECTION, MainActivity.CAMERA_NONE)
164-
val cameraSensitivity = prefs.getInt(MainActivity.KEY_CAMERA_SENSITIVITY, 50)
164+
val cameraSensitivity = prefs.getInt(MainActivity.KEY_CAMERA_SENSITIVITY, 0)
165165
val lightSensitivity = prefs.getInt(MainActivity.KEY_LIGHT_SENSITIVITY, 0)
166166

167167
// Update camera detection based on selection

app/src/main/kotlin/com/heisenberg/lux/MainActivity.kt

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ class MainActivity : AppCompatActivity() {
6464

6565
prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
6666

67-
// Load saved values (default to front camera if this is first run)
68-
val cameraSelection = prefs.getInt(KEY_CAMERA_SELECTION, getDefaultCamera())
69-
val cameraSensitivity = prefs.getInt(KEY_CAMERA_SENSITIVITY, 50)
70-
val lightSensitivity = prefs.getInt(KEY_LIGHT_SENSITIVITY, 50)
67+
// Load saved values (default to disabled on first run for safety)
68+
val cameraSelection = prefs.getInt(KEY_CAMERA_SELECTION, CAMERA_NONE)
69+
val cameraSensitivity = prefs.getInt(KEY_CAMERA_SENSITIVITY, 0)
70+
val lightSensitivity = prefs.getInt(KEY_LIGHT_SENSITIVITY, 0)
7171
val startAtBoot = prefs.getBoolean(KEY_START_AT_BOOT, false)
7272

7373
with(binding) {
@@ -252,24 +252,35 @@ class MainActivity : AppCompatActivity() {
252252

253253
override fun onResume() {
254254
super.onResume()
255-
// Notify service that MainActivity is in foreground
256-
val intent =
257-
Intent(this, DetectionService::class.java).apply {
258-
action = DetectionService.ACTION_ACTIVITY_FOREGROUND
259-
}
260-
ContextCompat.startForegroundService(this, intent)
261-
Log.i(TAG, "Activity resumed, notified service")
255+
// Only notify service if it's actually running
256+
if (isServiceRunning()) {
257+
val intent =
258+
Intent(this, DetectionService::class.java).apply {
259+
action = DetectionService.ACTION_ACTIVITY_FOREGROUND
260+
}
261+
ContextCompat.startForegroundService(this, intent)
262+
Log.i(TAG, "Activity resumed, notified service")
263+
}
262264
}
263265

264266
override fun onPause() {
265267
super.onPause()
266-
// Notify service that MainActivity is in background
267-
val intent =
268-
Intent(this, DetectionService::class.java).apply {
269-
action = DetectionService.ACTION_ACTIVITY_BACKGROUND
270-
}
271-
ContextCompat.startForegroundService(this, intent)
272-
Log.i(TAG, "Activity paused, notified service")
268+
// Only notify service if it's actually running
269+
if (isServiceRunning()) {
270+
val intent =
271+
Intent(this, DetectionService::class.java).apply {
272+
action = DetectionService.ACTION_ACTIVITY_BACKGROUND
273+
}
274+
ContextCompat.startForegroundService(this, intent)
275+
Log.i(TAG, "Activity paused, notified service")
276+
}
277+
}
278+
279+
private fun isServiceRunning(): Boolean {
280+
val cameraSelection = prefs.getInt(KEY_CAMERA_SELECTION, CAMERA_NONE)
281+
val cameraEnabled = cameraSelection != CAMERA_NONE && prefs.getInt(KEY_CAMERA_SENSITIVITY, 0) > 0
282+
val lightEnabled = prefs.getInt(KEY_LIGHT_SENSITIVITY, 0) > 0
283+
return cameraEnabled || lightEnabled
273284
}
274285

275286
override fun onDestroy() {
@@ -286,7 +297,7 @@ class MainActivity : AppCompatActivity() {
286297

287298
private fun updateService() {
288299
val cameraSelection = prefs.getInt(KEY_CAMERA_SELECTION, CAMERA_NONE)
289-
val cameraEnabled = cameraSelection != CAMERA_NONE && prefs.getInt(KEY_CAMERA_SENSITIVITY, 50) > 0
300+
val cameraEnabled = cameraSelection != CAMERA_NONE && prefs.getInt(KEY_CAMERA_SENSITIVITY, 0) > 0
290301
val lightEnabled = prefs.getInt(KEY_LIGHT_SENSITIVITY, 0) > 0
291302

292303
if (cameraEnabled || lightEnabled) {
@@ -308,7 +319,7 @@ class MainActivity : AppCompatActivity() {
308319
when (sensorType) {
309320
DetectionService.SENSOR_CAMERA -> {
310321
isCameraActive = exceeds
311-
val cameraSensitivity = prefs.getInt(KEY_CAMERA_SENSITIVITY, 50)
322+
val cameraSensitivity = prefs.getInt(KEY_CAMERA_SENSITIVITY, 0)
312323
if (cameraSensitivity == 0) {
313324
// Don't update display when disabled
314325
binding.cameraValue?.text = getString(R.string.disabled_caps)
@@ -323,7 +334,7 @@ class MainActivity : AppCompatActivity() {
323334
}
324335
DetectionService.SENSOR_LIGHT -> {
325336
isLightActive = exceeds
326-
val lightSensitivity = prefs.getInt(KEY_LIGHT_SENSITIVITY, 50)
337+
val lightSensitivity = prefs.getInt(KEY_LIGHT_SENSITIVITY, 0)
327338
if (lightSensitivity == 0) {
328339
// Don't update display when disabled
329340
binding.lightValue.text = getString(R.string.disabled_caps)

0 commit comments

Comments
 (0)