catch erros in bg check

Signed-off-by: androidacy-user <opensource@androidacy.com>
pull/89/head
androidacy-user 2 years ago
parent 2928d4ee97
commit 84bbf9f6f0

@ -113,6 +113,7 @@ class BackgroundUpdateChecker(context: Context, workerParams: WorkerParameters)
@Suppress("NAME_SHADOWING", "KotlinConstantConditions")
fun doCheck(context: Context) {
try {
// first, check if the user has enabled background update checking
if (!MainApplication.getPreferences("mmm")!!
.getBoolean("pref_background_update_check", false)
@ -211,7 +212,10 @@ class BackgroundUpdateChecker(context: Context, workerParams: WorkerParameters)
for (s in stringSet!!) {
if (s.startsWith(localModuleInfo.id)) {
version = s
if (MainApplication.forceDebugLogging) Timber.d("igV: %s", version)
if (MainApplication.forceDebugLogging) Timber.d(
"igV: %s",
version
)
break
}
}
@ -222,14 +226,18 @@ class BackgroundUpdateChecker(context: Context, workerParams: WorkerParameters)
remoteVersionCode = repoModule.moduleInfo.versionCode.toString()
}
if (version.isNotEmpty()) {
if (MainApplication.forceDebugLogging) Timber.d("igV found: %s", version)
if (MainApplication.forceDebugLogging) Timber.d(
"igV found: %s",
version
)
val remoteVersionCodeInt = remoteVersionCode.toInt()
val wantsVersion =
version.split(":".toRegex()).dropLastWhile { it.isEmpty() }
.toTypedArray()[1].replace("[^0-9]".toRegex(), "").toInt()
// now find out if user wants up to and including this version, or this version and newer
// if it starts with ^, it's this version and newer, if it ends with $, it's this version and older
version = version.split(":".toRegex()).dropLastWhile { it.isEmpty() }
version =
version.split(":".toRegex()).dropLastWhile { it.isEmpty() }
.toTypedArray()[1]
if (version.startsWith("^")) {
if (MainApplication.forceDebugLogging) Timber.d("igV: newer")
@ -272,7 +280,10 @@ class BackgroundUpdateChecker(context: Context, workerParams: WorkerParameters)
}
}
if (moduleUpdateCount != 0) {
if (MainApplication.forceDebugLogging) Timber.d("Found %d updates", moduleUpdateCount)
if (MainApplication.forceDebugLogging) Timber.d(
"Found %d updates",
moduleUpdateCount
)
postNotification(context, updateableModules, moduleUpdateCount, false)
}
}
@ -280,11 +291,11 @@ class BackgroundUpdateChecker(context: Context, workerParams: WorkerParameters)
if (MainApplication.getPreferences("mmm")!!
.getBoolean("pref_background_update_check_app", false)
) {
// don't check if app is from play store or fdroid
if (BuildConfig.FLAVOR != "play" || BuildConfig.FLAVOR != "fdroid") {
try {
val shouldUpdate = AppUpdateManager.appUpdateManager.checkUpdate(true)
val shouldUpdate =
AppUpdateManager.appUpdateManager.checkUpdate(true)
if (shouldUpdate) {
if (MainApplication.forceDebugLogging) Timber.d("Found app update")
postNotificationForAppUpdate(context)
@ -313,6 +324,48 @@ class BackgroundUpdateChecker(context: Context, workerParams: WorkerParameters)
MainApplication.getPreferences("mmm")!!
.getInt("pref_background_update_counter", 0) + 1
).apply()
} catch (e: Exception) {
Timber.e(e, "Failed to check for updates")
// post notification
if (ContextCompat.checkSelfPermission(
MainApplication.INSTANCE!!.applicationContext,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
) {
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.createNotificationChannel(
NotificationChannelCompat.Builder(
NOTIFICATION_CHANNEL_ID_ONGOING,
NotificationManagerCompat.IMPORTANCE_MIN
).setName(
context.getString(
R.string.notification_channel_category_background_update
)
).setDescription(
context.getString(
R.string.notification_channel_category_background_update_description
)
).setGroup(
NOTFIICATION_GROUP
).build()
)
val builder =
NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID_ONGOING)
builder.setSmallIcon(R.drawable.ic_baseline_update_24)
builder.priority = NotificationCompat.PRIORITY_HIGH
builder.setCategory(NotificationCompat.CATEGORY_SERVICE)
builder.setShowWhen(false)
builder.setOnlyAlertOnce(true)
builder.setOngoing(true)
builder.setAutoCancel(false)
builder.setGroup("update_bg")
builder.setContentTitle(context.getString(R.string.update_failed))
builder.setContentText(context.getString(R.string.update_failed_description))
notificationManager.notify(NOTIFICATION_ID, builder.build())
} else {
if (MainApplication.forceDebugLogging) Timber.d("Not posting notification because of missing permission")
}
}
}
fun postNotification(

@ -424,4 +424,6 @@
<string name="setup_crash_reporting_generic">Report crashes</string>
<string name="upgrade_dialog_message">Purchasing a premium subscription helps support this app and our other services, and unlocks cool features like ad removal, fast unlimited downloads, and so much more! Plans start at $3.49 USD a month (subject to change). Cancel anytime.\n\n<i>Note: The core features of this app remain free but may be limited.</i></string>
<string name="reinstall_warning_v2">This module is available from an online source and a local one. For security, you\'ll need to head over to the online tab to update. If there\'s no update available, you may need to remove this module before attemtping to reinstall.</string>
<string name="update_failed">Could not check for updates</string>
<string name="update_failed_description">Failed to check for updates. If this keeps happening, please ensure the app is not restricted from background usage.</string>
</resources>

Loading…
Cancel
Save