Skip to content

Commit 9693b6a

Browse files
committed
Improve update check handling
We can now check with the VPN active, as HTTP Toolkit traffic is excluded. It's useful though to ensure we don't check shortly after the first install (annoying) or every single time (also annoying). As before, this only applies to ADB-installed apps, not versions installed from the play store.
1 parent 3abfdae commit 9693b6a

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

app/src/main/java/tech/httptoolkit/android/HttpToolkitApplication.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import kotlin.coroutines.resume
2525
import kotlin.coroutines.suspendCoroutine
2626

2727
private const val VPN_START_TIME_PREF = "vpn-start-time"
28+
private const val LAST_UPDATE_CHECK_TIME_PREF = "update-check-time"
2829
private const val APP_CRASHED_PREF = "app-crashed"
2930
private const val FIRST_RUN_PREF = "is-first-run"
3031

@@ -212,18 +213,21 @@ class HttpToolkitApplication : Application() {
212213

213214
suspend fun isUpdateRequired(): Boolean {
214215
return withContext(Dispatchers.IO) {
215-
if (isVpnActive()) {
216-
Log.i(TAG, "VPN currently active, so not checking for updates for now")
217-
return@withContext false
218-
}
219-
220216
if (wasInstalledFromStore(this@HttpToolkitApplication)) {
221217
// We only check for updates for side-loaded/ADB-loaded versions. This is useful
222218
// because otherwise anything outside the play store gets no updates.
223219
Log.i(TAG, "Installed from play store, no update prompting required")
224220
return@withContext false
225221
}
226222

223+
val lastUpdateTime = prefs.getLong(LAST_UPDATE_CHECK_TIME_PREF,
224+
firstInstallTime(this@HttpToolkitApplication)
225+
)
226+
227+
if (System.currentTimeMillis() - lastUpdateTime < 1000 * 60) {
228+
return@withContext false;
229+
}
230+
227231
val httpClient = OkHttpClient()
228232
val request = Request.Builder()
229233
.url("https://api.github.com/repos/httptoolkit/httptoolkit-android/releases/latest")
@@ -258,6 +262,8 @@ class HttpToolkitApplication : Application() {
258262
else
259263
"App is up to date"
260264
)
265+
266+
prefs.edit().putLong(LAST_UPDATE_CHECK_TIME_PREF, System.currentTimeMillis()).apply()
261267
return@withContext updateAvailable && updateNotTooRecent
262268
} catch (e: Exception) {
263269
Log.w(TAG, e)
@@ -272,6 +278,10 @@ private fun wasInstalledFromStore(context: Context): Boolean {
272278
return context.packageManager.getInstallerPackageName(context.packageName) != null
273279
}
274280

281+
private fun firstInstallTime(context: Context): Long {
282+
return context.packageManager.getPackageInfo(context.packageName, 0).firstInstallTime
283+
}
284+
275285
private data class GithubRelease(
276286
val tag_name: String?,
277287
val name: String?,

0 commit comments

Comments
 (0)