Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

Commit

Permalink
Change implementation of auto start to be stable
Browse files Browse the repository at this point in the history
Auto starting the service in MyTileService.kt onCreate() was a problem
that led to unpredictable behaviors of the service starting itself
when not plugged in.

Auto starting the service in the MainActivity.kt onCreate() also led
to the similar issues from above.
  • Loading branch information
nazmulidris committed Jun 16, 2020
1 parent 49d98da commit 1801ecd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {
applicationId "com.r3bl.stayawake"
minSdkVersion 25
targetSdkVersion 28
versionCode 15
versionName "2.3"
versionCode 16
versionName "2.4"
}
buildTypes {
release {
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/com/r3bl/stayawake/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class MainActivity : AppCompatActivity() {
loadAndApplyFonts()
formatMessages()

handleAutoStartOfService()
//handleAutoStartOfService()

setupCheckbox()
setupSpinner(typeNotoSansRegular)
Expand All @@ -95,13 +95,13 @@ class MainActivity : AppCompatActivity() {
super.onStop()
}

private fun handleAutoStartOfService() {
if (mySettingsHolder.value.autoStartEnabled && MyTileService.isCharging(this)) {
MyTileService.fireIntentWithStartService(this)
d(TAG, "MainActivity.handleAutoStartOfService: Initiate auto start")
}
else d(TAG, "MainActivity.handleAutoStartOfService: Do not auto start")
}
// private fun handleAutoStartOfService() {
// if (mySettingsHolder.value.autoStartEnabled && MyTileService.isCharging(this)) {
// MyTileService.fireIntentWithStartService(this)
// d(TAG, "MainActivity.handleAutoStartOfService: Initiate auto start")
// }
// else d(TAG, "MainActivity.handleAutoStartOfService: Do not auto start")
// }

private fun setupCheckbox() = mySettingsHolder.value.autoStartEnabled.let { autoStartEnabled ->
checkbox_prefs_auto_start.isChecked = autoStartEnabled
Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/com/r3bl/stayawake/MyTileService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,18 @@ class MyTileService : TileService() {
myReceiver = PowerConnectionReceiver(this)
mySettingsHolder = MyTileServiceSettings.Holder(this)
MyTileServiceSettings.registerWithEventBus(this)
handleAutoStartOfService()
//handleAutoStartOfService()
d(TAG, "onCreate: ")
}

private fun handleAutoStartOfService() {
if (mySettingsHolder.value.autoStartEnabled && isCharging(this)) {
commandStart()
d(TAG, "MyTileService.handleAutoStartOfService: Initiate auto start")
}
else d(TAG, "MyTileService.handleAutoStartOfService: Do not auto start")
}
// private fun handleAutoStartOfService() {
// if (mySettingsHolder.value.autoStartEnabled && isCharging(this)) {
// showToast(applicationContext, "MyTileService: autoStartEnabled & isCharging -> auto start service")
// commandStart()
// d(TAG, "MyTileService.handleAutoStartOfService: Initiate auto start")
// }
// else d(TAG, "MyTileService.handleAutoStartOfService: Do not auto start")
// }

override fun onDestroy() {
super.onDestroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ class PowerConnectionReceiver(private val myContext: Context) : BroadcastReceive
if (mySettingsHolder.value.autoStartEnabled) {
MyTileService.fireIntentWithStartService(myContext)
val message = "onReceive: PowerConnectionReceiver ACTION_POWER_CONNECTED ... Start Service"
// showToast(myContext, message)
//showToast(myContext, message)
d(TAG, message)
}
else {
val message = "onReceive: PowerConnectionReceiver ACTION_POWER_CONNECTED ... Do not auto start"
// showToast(myContext, message)
//showToast(myContext, message)
d(TAG, message)
}
}
Expand All @@ -79,12 +79,12 @@ class PowerConnectionReceiver(private val myContext: Context) : BroadcastReceive
if (mySettingsHolder.value.autoStartEnabled) {
MyTileService.fireIntentWithStopService(myContext)
val message = "onReceive: PowerConnectionReceiver ACTION_POWER_DISCONNECTED ... Stop Service"
// showToast(myContext, message)
//showToast(myContext, message)
d(TAG, message)
}
else {
val message = "onReceive: PowerConnectionReceiver ACTION_POWER_DISCONNECTED ... Do not auto start"
// showToast(myContext, message)
//showToast(myContext, message)
d(TAG, message)
}
}
Expand Down

0 comments on commit 1801ecd

Please sign in to comment.