@ -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 (