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

Commit

Permalink
Update to v1.9; Published to Play Store
Browse files Browse the repository at this point in the history
  • Loading branch information
nazmulidris committed Jun 9, 2020
1 parent 6b425a2 commit 0ad6957
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 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 10
versionName "1.8"
versionCode 11
versionName "1.9"
}
buildTypes {
release {
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/com/r3bl/stayawake/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ class MainActivity : AppCompatActivity() {
//hideStatusBar();
loadAndApplyFonts()
formatMessages()
coldStart(this, false)
if (loadSharedPreferences(this).autoStartEnabled) coldStart(this, false)
loadSharedPreferences(this)
setupCheckbox()
setupSpinner(typeNotoSansRegular)
}

private fun setupCheckbox() {
checkbox_prefs_auto_start.isChecked = MyTileServiceSettings.autoStartEnabled
d(TAG, "setupCheckbox: set checkbox state to: ${MyTileServiceSettings.autoStartEnabled}")
loadSharedPreferences(this).autoStartEnabled.let { autoStartEnabled ->
checkbox_prefs_auto_start.isChecked = autoStartEnabled
d(TAG, "setupCheckbox: set checkbox state to: $autoStartEnabled")
}
}

private fun loadAndApplyFonts() {
Expand Down Expand Up @@ -112,7 +114,7 @@ class MainActivity : AppCompatActivity() {
adapter = myAdapter

// Restore saved selection.
val savedTimeoutInSec: Long = MyTileServiceSettings.timeoutNotChargingSec
val savedTimeoutInSec: Long = loadSharedPreferences(this@MainActivity).timeoutNotChargingSec
val savedTimeoutInMin: Long = TimeUnit.MINUTES.convert(savedTimeoutInSec, TimeUnit.SECONDS)
val position = myAdapter.getPosition(savedTimeoutInMin.toString())
if (position != -1) {
Expand Down Expand Up @@ -155,7 +157,7 @@ class MainActivity : AppCompatActivity() {

private fun formatMessages() {
// Add actual minutes to string template.
val hours = TimeUnit.SECONDS.toMinutes(MyTileServiceSettings.timeoutNotChargingSec)
val hours = TimeUnit.SECONDS.toMinutes(loadSharedPreferences(this).timeoutNotChargingSec)
text_introduction_content.text = getString(R.string.introduction_body, hours)

// Linkify github link.
Expand Down
19 changes: 8 additions & 11 deletions app/src/main/java/com/r3bl/stayawake/MyTileService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ class MyTileService : TileService() {
}
else {
// Run down the countdown timer.
myTimeRunning_sec++
if (myTimeRunning_sec >= MyTileServiceSettings.timeoutNotChargingSec) {
myTimeRunning_sec += DELAY_UNIT.convert(DELAY_RECURRING, TimeUnit.SECONDS)
if (myTimeRunning_sec >= loadSharedPreferences(this).timeoutNotChargingSec) {
// Timer has run out.
if (isCharging(this)) {
d(TAG, "recurringTask: timer ended but phone is charging")
Expand Down Expand Up @@ -333,7 +333,7 @@ class MyTileService : TileService() {
else {
tile.state = Tile.STATE_ACTIVE
tile.icon = myIconEyeOpen
val timeRemaining = MyTileServiceSettings.timeoutNotChargingSec - myTimeRunning_sec
val timeRemaining = loadSharedPreferences(this).timeoutNotChargingSec - myTimeRunning_sec
val formatTime = formatTime(timeRemaining)
tile.label = getString(R.string.tile_active_text, formatTime)
}
Expand All @@ -343,7 +343,7 @@ class MyTileService : TileService() {
return if (time_sec <= 60) { // less than 1 min.
String.format("%ds", time_sec)
}
else if (time_sec > 60 && time_sec < 3600) { // less than 60 min.
else if (time_sec in 61..3599) { // less than 60 min.
val minutes = TimeUnit.SECONDS.toMinutes(time_sec)
String.format("%dm:%ds", minutes, time_sec - minutes * 60)
}
Expand All @@ -360,8 +360,8 @@ class MyTileService : TileService() {

companion object {
const val TAG = "SA_MyService"
const val DELAY_INITIAL = 0
const val DELAY_RECURRING = 1
const val DELAY_INITIAL: Long = 0
const val DELAY_RECURRING: Long = 5
val DELAY_UNIT = TimeUnit.SECONDS

fun startService(context: Context) {
Expand Down Expand Up @@ -396,14 +396,11 @@ class MyTileService : TileService() {

/**
* @param forceIfNotCharging If you want to start the service regardless of whether the device is currently charging
* or not then pass true here, otherwise, the service will not start if the device isn't
* charging.
* or not then pass true here, otherwise, the service will not start if the device isn't charging.
*/
fun coldStart(context: Context, forceIfNotCharging: Boolean) {
// Check if charging, and start it.
if (forceIfNotCharging || isCharging(context)) {
startService(context)
}
if (forceIfNotCharging || isCharging(context)) startService(context)
}

/**
Expand Down
50 changes: 28 additions & 22 deletions app/src/main/java/com/r3bl/stayawake/MyTileServiceSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,43 @@ import android.util.Log
import java.util.concurrent.TimeUnit

object MyTileServiceSettings {
var autoStartEnabled: Boolean = true
var timeoutNotChargingSec: Long = TimeUnit.SECONDS.convert(10,
TimeUnit.MINUTES)
private var DEFAULT_AUTO_START_ENABLED: Boolean = true
private var DEFAULT_TIMEOUT_NOT_CHARGING_SEC: Long = TimeUnit.SECONDS.convert(10, TimeUnit.MINUTES)

fun loadSharedPreferences(context: Context) = with(context) {
data class SettingsImmutable(val autoStartEnabled: Boolean, val timeoutNotChargingSec: Long)

data class SettingsMutable(var autoStartEnabled: Boolean? = null, var timeoutNotChargingSec: Long? = null)

fun loadSharedPreferences(context: Context): SettingsImmutable = with(context) {
with(PreferenceManager.getDefaultSharedPreferences(this)) {
autoStartEnabled = getBoolean(getString(R.string.prefs_auto_start_enabled), autoStartEnabled)
timeoutNotChargingSec = getLong(getString(R.string.prefs_timeout_not_charging_sec), timeoutNotChargingSec)
Log.d(MyTileService.TAG,
"loadSharedPreferences: $debugString")
return SettingsImmutable(
getBoolean(getString(R.string.prefs_auto_start_enabled), DEFAULT_AUTO_START_ENABLED),
getLong(getString(R.string.prefs_timeout_not_charging_sec), DEFAULT_TIMEOUT_NOT_CHARGING_SEC)
).apply {
Log.d(MyTileService.TAG, "loadSharedPreferences: $this")
}
}
}

private fun saveSharedPreferences(context: Context) = with(context) {
fun saveSharedPreferencesAfterRunningLambda(context: Context, lambda: SettingsMutable.() -> Unit) {
val settings = SettingsMutable()
lambda(settings)
saveSharedPreferences(context, settings)
}

private fun saveSharedPreferences(context: Context, settingsMutable: SettingsMutable) = with(context) {
with(PreferenceManager.getDefaultSharedPreferences(this)) {
with(edit()) {
putBoolean(getString(R.string.prefs_auto_start_enabled), autoStartEnabled)
putLong(getString(R.string.prefs_timeout_not_charging_sec), timeoutNotChargingSec)
// Only save fields that have been set.
settingsMutable.autoStartEnabled?.apply {
putBoolean(getString(R.string.prefs_auto_start_enabled), this)
}
settingsMutable.timeoutNotChargingSec?.apply {
putLong(getString(R.string.prefs_timeout_not_charging_sec), this)
}
Log.d(MyTileService.TAG, "saveSharedPreferences, $settingsMutable")
commit()
}
}
}

fun saveSharedPreferencesAfterRunningLambda(context: Context, lambda: MyTileServiceSettings.() -> Unit) {
lambda(this)
saveSharedPreferences(context)
Log.d(MyTileService.TAG,
"saveSharedPreferences, $debugString")
}

private val debugString: String
get() = "autoStartEnabled = $autoStartEnabled, timeoutNotChargingSec = $timeoutNotChargingSec"

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PowerConnectionReceiver(private val myContext: Context) : BroadcastReceive
}

private fun onPowerConnected() {
if (MyTileServiceSettings.autoStartEnabled) {
if (MyTileServiceSettings.loadSharedPreferences(myContext).autoStartEnabled) {
MyTileService.startService(myContext)
val message = "onReceive: PowerConnectionReceiver ACTION_POWER_CONNECTED ... Start Service"
// showToast(myContext, message)
Expand All @@ -61,7 +61,7 @@ class PowerConnectionReceiver(private val myContext: Context) : BroadcastReceive
}

private fun onPowerDisconnected() {
if (MyTileServiceSettings.autoStartEnabled) {
if (MyTileServiceSettings.loadSharedPreferences(myContext).autoStartEnabled) {
MyTileService.stopService(myContext)
val message = "onReceive: PowerConnectionReceiver ACTION_POWER_DISCONNECTED ... Stop Service"
// showToast(myContext, message)
Expand Down

0 comments on commit 0ad6957

Please sign in to comment.