ratelimit update checking

This commit is contained in:
Chloe committed 2025-12-26 19:05:33 -07:00
1 parent 92d876560a
commit b7d3d3e730
5 files changed
+41 -6

No files matched your search

@@ -7,6 +7,7 @@ import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.DocumentsContract
import android.text.format.DateUtils
import android.view.WindowManager
import android.widget.Toast
import androidx.activity.ComponentActivity
@@ -58,6 +59,7 @@ import com.geode.launcher.utils.PreferenceUtils
import java.net.ConnectException
import java.net.UnknownHostException
import kotlin.math.roundToInt
import kotlin.time.Clock
class SettingsActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@@ -435,6 +437,22 @@ fun SettingsScreen(
title = stringResource(R.string.preference_update_automatically_name),
preferenceKey = PreferenceUtils.Key.UPDATE_AUTOMATICALLY,
)
val lastUpdateCheck by PreferenceUtils.useLongPreference(PreferenceUtils.Key.LAST_UPDATE_CHECK_TIME)
val checkAgoString = remember(lastUpdateCheck) {
if (lastUpdateCheck != 0L) {
val now = Clock.System.now().toEpochMilliseconds()
DateUtils.getRelativeTimeSpanString(
lastUpdateCheck,
now,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE
)
} else {
"never"
}
}
OptionsCard(
title = {
OptionsTitle(
@@ -444,7 +462,9 @@ fun SettingsScreen(
painterResource(R.drawable.icon_update),
contentDescription = null
)
}
},
description = stringResource(R.string.preference_check_for_updates_description, checkAgoString)
.takeIf { lastUpdateCheck != 0L }
)
},
modifier = Modifier
@@ -957,7 +957,7 @@ fun SettingsCard(title: String, description: String? = null, icon: (@Composable
@Composable
fun OptionsTitle(modifier: Modifier = Modifier, title: String, description: String? = null, icon: (@Composable () -> Unit)? = null) {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
if (icon != null) {
icon()
}
@@ -22,6 +22,8 @@ import okio.buffer
import okio.source
import java.io.File
import java.io.InterruptedIOException
import kotlin.time.Clock
import kotlin.time.Duration.Companion.minutes
private const val TAG_LATEST = "latest"
private const val TAG_BETA = "prerelease"
@@ -245,6 +247,16 @@ class ReleaseManager private constructor(
}
private suspend fun checkForNewRelease(isManual: Boolean = false) {
val sharedPreferences = PreferenceUtils.get(applicationContext)
val lastCheckTime = sharedPreferences.getLong(PreferenceUtils.Key.LAST_UPDATE_CHECK_TIME)
// only check for updates if it's been over 30 minutes since last check
val checkMinTime = Clock.System.now().minus(30.minutes).toEpochMilliseconds()
if (!isManual && lastCheckTime > checkMinTime) {
_uiState.value = ReleaseManagerState.Finished()
return
}
val release = try {
getLatestRelease()
} catch (e: Exception) {
@@ -263,6 +275,9 @@ class ReleaseManager private constructor(
checkLauncherUpdate(launcherRelease)
}
val currentTime = Clock.System.now().toEpochMilliseconds()
sharedPreferences.setLong(PreferenceUtils.Key.LAST_UPDATE_CHECK_TIME, currentTime)
if (release == null) {
downloadLauncherUpdateIfNecessary()
@@ -270,8 +285,6 @@ class ReleaseManager private constructor(
return
}
val sharedPreferences = PreferenceUtils.get(applicationContext)
val currentVersion = sharedPreferences.getLong(PreferenceUtils.Key.CURRENT_VERSION_TIMESTAMP)
val latestVersion = release.getDescriptor()
@@ -150,7 +150,8 @@ class PreferenceUtils(private val sharedPreferences: SharedPreferences) {
DISABLE_USER_THEME,
SELECTED_ICON,
DISABLE_REDESIGN,
WAIT_PERIOD
WAIT_PERIOD,
LAST_UPDATE_CHECK_TIME
}
private fun defaultValueForBooleanKey(key: Key): Boolean {
@@ -203,6 +204,7 @@ class PreferenceUtils(private val sharedPreferences: SharedPreferences) {
Key.DISABLE_USER_THEME -> "PreferenceDisableUserTheme"
Key.SELECTED_ICON -> "PreferenceSelectedIcon"
Key.WAIT_PERIOD -> "PreferenceWaitPeriod"
Key.LAST_UPDATE_CHECK_TIME -> "PreferenceLastUpdateCheckTime"
}
}
+1 -1
View File
@@ -227,7 +227,7 @@
<string name="preference_architecture">%1$s (%2$s)</string>
<string name="preference_check_for_updates_button">Check for updates</string>
<string name="preference_check_for_updates_description">Current loader version: %1$s</string>
<string name="preference_check_for_updates_description">Last checked %1$s</string>
<string name="preference_check_for_updates_failed">Update failed. See logs for more details.</string>
<string name="preference_check_for_updates_none_found">No updates found.</string>
<string name="preference_check_for_updates_success">Geode has been updated!</string>