@@ -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