Skip to content

Commit 11efe48

Browse files
committed
fix R8 engine optimization causing bugs and crashes
Change-Id: I77829af08494a4cde78ca80511823cf58bbb7edf Signed-off-by: AbdAlMoniem AlHifnawy <hifnawy_moniem@hotmail.com>
1 parent ecc7540 commit 11efe48

File tree

5 files changed

+64
-53
lines changed

5 files changed

+64
-53
lines changed

app/src/main/java/com/hifnawy/caffeinate/CaffeinateApplication.kt

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.hifnawy.caffeinate.view.CheckBoxItem
1616
import com.hifnawy.caffeinate.view.Widget
1717
import java.util.Locale
1818
import kotlin.time.Duration
19+
import kotlin.time.Duration.Companion.seconds
1920
import timber.log.Timber as Log
2021

2122
/**
@@ -53,6 +54,41 @@ class CaffeinateApplication : Application() {
5354
*/
5455
private val sharedPreferences by lazy { SharedPrefsManager(this) }
5556

57+
/**
58+
* Returns the current locale set by the user in the system settings.
59+
*
60+
* If the device is running Android 13 (API level 33) or later, the current locale is retrieved
61+
* from the [resources.configuration.locales][android.content.res.Configuration.getLocales] list. Otherwise, the current locale is retrieved
62+
* from the [getDefault][Locale.getDefault] method.
63+
*
64+
* @return the current locale set by the user in the system settings.
65+
*
66+
* @see android.content.res.Configuration
67+
* @see Locale.getDefault
68+
*/
69+
private val currentLocale: Locale
70+
get() = when {
71+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> resources.configuration.locales[0]
72+
else -> Locale.getDefault()
73+
}
74+
75+
/**
76+
* A list of [CheckBoxItem]s that represent the timeout durations the user can select from.
77+
*
78+
* This list is stored in the app's SharedPreferences and is loaded when the app is launched.
79+
*
80+
* The list of timeout durations is used to populate the [RecyclerView][androidx.recyclerview.widget.RecyclerView] in the "Choose timeout" dialog,
81+
* which is shown when the user clicks the "Choose timeout" button in the app's UI.
82+
*
83+
* The list is also used to determine the next timeout duration to use when the current timeout duration is finished.
84+
*
85+
* @see CheckBoxItem
86+
* @see android.content.SharedPreferences
87+
* @see androidx.recyclerview.widget.RecyclerView
88+
*/
89+
val timeoutCheckBoxes
90+
get() = sharedPreferences.timeoutCheckBoxes
91+
5692
/**
5793
* The first timeout duration that was selected by the user.
5894
*
@@ -62,7 +98,7 @@ class CaffeinateApplication : Application() {
6298
*
6399
* @return [Duration] the first timeout duration that was selected by the user, or [Duration.INFINITE] if no timeout duration was selected.
64100
*/
65-
val firstTimeout: Duration
101+
val firstTimeout
66102
get() = timeoutCheckBoxes.first { checkBoxItem -> checkBoxItem.isChecked }.duration
67103

68104
/**
@@ -73,7 +109,7 @@ class CaffeinateApplication : Application() {
73109
*
74110
* @return [Duration] the last timeout duration that was selected by the user.
75111
*/
76-
val lastTimeout: Duration
112+
val lastTimeout
77113
get() = timeoutCheckBoxes.last { checkBoxItem -> checkBoxItem.isChecked }.duration
78114

79115
/**
@@ -84,15 +120,14 @@ class CaffeinateApplication : Application() {
84120
*
85121
* @return [Duration] the previously selected timeout duration.
86122
*/
87-
val prevTimeout: Duration
88-
get() {
89-
val timeoutCheckBox = timeoutCheckBoxes.first { timeoutCheckBox -> timeoutCheckBox.duration == timeout }
123+
val prevTimeout
124+
get() = timeoutCheckBoxes.first { timeoutCheckBox -> timeoutCheckBox.duration == timeout }.let { timeoutCheckBox ->
90125
val index = timeoutCheckBoxes.indexOf(timeoutCheckBox)
91126
var prevIndex = (index - 1 + timeoutCheckBoxes.size) % timeoutCheckBoxes.size
92127

93128
while (!timeoutCheckBoxes[prevIndex].isChecked) prevIndex = (prevIndex - 1 + timeoutCheckBoxes.size) % timeoutCheckBoxes.size
94129

95-
return timeoutCheckBoxes[prevIndex].duration
130+
timeoutCheckBoxes[prevIndex].duration
96131
}
97132

98133
/**
@@ -104,15 +139,14 @@ class CaffeinateApplication : Application() {
104139
*
105140
* @return [Duration] the next timeout duration that will be used when the KeepAwakeService is running.
106141
*/
107-
val nextTimeout: Duration
108-
get() {
109-
val timeoutCheckBox = timeoutCheckBoxes.first { timeoutCheckBox -> timeoutCheckBox.duration == timeout }
142+
val nextTimeout
143+
get() = timeoutCheckBoxes.first { timeoutCheckBox -> timeoutCheckBox.duration == timeout }.let { timeoutCheckBox ->
110144
val index = timeoutCheckBoxes.indexOf(timeoutCheckBox)
111145
var nextIndex = (index + 1) % timeoutCheckBoxes.size
112146

113147
while (!timeoutCheckBoxes[nextIndex].isChecked) nextIndex = (nextIndex + 1) % timeoutCheckBoxes.size
114148

115-
return timeoutCheckBoxes[nextIndex].duration
149+
timeoutCheckBoxes[nextIndex].duration
116150
}
117151

118152
/**
@@ -126,7 +160,7 @@ class CaffeinateApplication : Application() {
126160
* @see com.hifnawy.caffeinate.controller.KeepAwakeService
127161
* @see SharedPrefsManager.timeouts
128162
*/
129-
var timeout: Duration = sharedPreferences.timeouts.first()
163+
var timeout = 0.seconds
130164

131165
/**
132166
* A list of observers that are notified whenever the status of the KeepAwakeService changes.
@@ -178,23 +212,6 @@ class CaffeinateApplication : Application() {
178212
}
179213
}
180214

181-
/**
182-
* A list of [CheckBoxItem]s that represent the timeout durations the user can select from.
183-
*
184-
* This list is stored in the app's SharedPreferences and is loaded when the app is launched.
185-
*
186-
* The list of timeout durations is used to populate the [RecyclerView][androidx.recyclerview.widget.RecyclerView] in the "Choose timeout" dialog,
187-
* which is shown when the user clicks the "Choose timeout" button in the app's UI.
188-
*
189-
* The list is also used to determine the next timeout duration to use when the current timeout duration is finished.
190-
*
191-
* @see CheckBoxItem
192-
* @see android.content.SharedPreferences
193-
* @see androidx.recyclerview.widget.RecyclerView
194-
*/
195-
lateinit var timeoutCheckBoxes: MutableList<CheckBoxItem>
196-
private set
197-
198215
/**
199216
* The context of the application localized to the user's current locale.
200217
*
@@ -237,24 +254,6 @@ class CaffeinateApplication : Application() {
237254
Log.d("observers notified!")
238255
}
239256

240-
/**
241-
* Returns the current locale set by the user in the system settings.
242-
*
243-
* If the device is running Android 13 (API level 33) or later, the current locale is retrieved
244-
* from the [resources.configuration.locales][android.content.res.Configuration.getLocales] list. Otherwise, the current locale is retrieved
245-
* from the [getDefault][Locale.getDefault] method.
246-
*
247-
* @return the current locale set by the user in the system settings.
248-
*
249-
* @see android.content.res.Configuration
250-
* @see Locale.getDefault
251-
*/
252-
private val currentLocale: Locale
253-
get() = when {
254-
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> resources.configuration.locales[0]
255-
else -> Locale.getDefault()
256-
}
257-
258257
/**
259258
* Applies the locale configuration set by the user.
260259
*
@@ -274,8 +273,6 @@ class CaffeinateApplication : Application() {
274273
@Suppress("AppBundleLocaleChanges")
275274
configuration.setLocale(currentLocale)
276275
localizedApplicationContext = createConfigurationContext(configuration)
277-
278-
timeoutCheckBoxes = sharedPreferences.timeoutCheckBoxes
279276
}
280277

281278
/**

app/src/main/java/com/hifnawy/caffeinate/controller/SharedPrefsManager.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ class SharedPrefsManager(private val caffeinateApplication: CaffeinateApplicatio
230230
*
231231
* @return [List] A list of [Duration] objects representing the available timeouts.
232232
*/
233-
// val timeouts by lazy { listOf(30.seconds, 5.minutes, 10.minutes, 15.minutes, 30.minutes, 60.minutes, 120.minutes, 240.minutes, 480.minutes, Duration.INFINITE) }
234-
val timeouts by lazy { listOf(30.seconds, 5.minutes, 10.minutes, 15.minutes, 30.minutes, 60.minutes, Duration.INFINITE) }
233+
private val timeouts by lazy { listOf(30.seconds, 5.minutes, 10.minutes, 15.minutes, 30.minutes, 60.minutes, Duration.INFINITE) }
235234

236235
/**
237236
* Retrieves or sets whether all necessary permissions are granted.

app/src/main/java/com/hifnawy/caffeinate/view/CheckBoxAdapter.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.view.ViewGroup
1010
import android.view.animation.AnticipateOvershootInterpolator
1111
import android.widget.CheckBox
1212
import android.widget.ImageView
13+
import androidx.annotation.Keep
1314
import androidx.recyclerview.widget.RecyclerView
1415
import com.hifnawy.caffeinate.R
1516
import com.hifnawy.caffeinate.databinding.TimeoutCheckboxItemBinding
@@ -40,22 +41,27 @@ import kotlin.time.Duration
4041
*
4142
* @author AbdAlMoniem AlHifnawy
4243
*/
44+
@Keep
4345
data class CheckBoxItem(
4446
/**
4547
* the text displayed in the CheckBox
4648
*/
49+
@Keep
4750
var text: String,
4851
/**
4952
* the checked state of the CheckBox
5053
*/
54+
@Keep
5155
var isChecked: Boolean,
5256
/**
5357
* the enabled state of the CheckBox. When the enabled state is false, the CheckBox is grayed out.
5458
*/
59+
@Keep
5560
var isEnabled: Boolean = false,
5661
/**
5762
* the duration associated with the item, which is used to sort the items in ascending order.
5863
*/
64+
@Keep
5965
var duration: Duration,
6066
) : Serializable
6167

app/src/main/java/com/hifnawy/caffeinate/view/WidgetConfigurationActivity.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import android.view.View
88
import android.widget.FrameLayout
99
import android.widget.RemoteViews
1010
import androidx.activity.enableEdgeToEdge
11+
import androidx.annotation.Keep
1112
import androidx.appcompat.app.AppCompatActivity
1213
import com.google.android.material.card.MaterialCardView
1314
import com.hifnawy.caffeinate.CaffeinateApplication
1415
import com.hifnawy.caffeinate.R
15-
import com.hifnawy.caffeinate.databinding.ActivityWidgetConfigurationBinding
1616
import com.hifnawy.caffeinate.controller.ServiceStatus
1717
import com.hifnawy.caffeinate.controller.ServiceStatusObserver
18+
import com.hifnawy.caffeinate.controller.SharedPrefsManager
19+
import com.hifnawy.caffeinate.databinding.ActivityWidgetConfigurationBinding
1820
import com.hifnawy.caffeinate.utils.ActivityExtensionFunctions.setActivityTheme
1921
import com.hifnawy.caffeinate.utils.MutableListExtensionFunctions.addObserver
2022
import com.hifnawy.caffeinate.utils.MutableListExtensionFunctions.removeObserver
21-
import com.hifnawy.caffeinate.controller.SharedPrefsManager
2223
import java.io.Serializable
2324
import timber.log.Timber as Log
2425

@@ -211,4 +212,10 @@ class WidgetConfigurationActivity : AppCompatActivity(), ServiceStatusObserver {
211212
*
212213
* @author AbdAlMoniem AlHifnawy
213214
*/
214-
data class WidgetConfiguration(val widgetId: Int, val showBackground: Boolean) : Serializable
215+
@Keep
216+
data class WidgetConfiguration(
217+
@Keep
218+
val widgetId: Int,
219+
@Keep
220+
val showBackground: Boolean
221+
) : Serializable

fastlane/metadata/android/en-US/changelogs/34.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* fix R8 engine optimization causing bugs and crashes
2+
>
13
* privacy policy colors now match the app's theme
24
>
35
* preparing for play store release

0 commit comments

Comments
 (0)