Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.futo.inputmethod.updates
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.job.JobParameters
import android.app.job.JobService
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.futo.inputmethod.latin.R
const val CHANNEL_ID = "UPDATES"
const val NOTIFICATION_ID = 1
class UpdateCheckingService : JobService() {
private var job: Job? = null
override fun onStartJob(params: JobParameters?): Boolean {
job = CoroutineScope(Dispatchers.IO).launch {
if(checkForUpdateAndSaveToPreferences(applicationContext)) {
val updateResult = retrieveSavedLastUpdateCheckResult(applicationContext)
if(updateResult != null && updateResult.isNewer()) {
// Show a notification : "Update available"
val manager = applicationContext.getSystemService(
Context.NOTIFICATION_SERVICE
) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
"Update Notifications",
NotificationManager.IMPORTANCE_MIN
)
manager.createNotificationChannel(channel)
}
val contentIntent = PendingIntent.getActivity(
applicationContext,
0,
Intent(Intent.ACTION_VIEW, Uri.parse(updateResult.apkUrl)),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setContentTitle(getString(R.string.update_available))
.setContentText(getString(R.string.update_available_notification,"${UpdateResult.currentVersionString()} -> ${updateResult.nextVersionString}"))
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(contentIntent)
manager.notify(NOTIFICATION_ID, notification.build())
}
} else {
Log.i("UpdateCheckingService", "no update available, or failed to check")
}
jobFinished(params, false)
}
return true
}
override fun onStopJob(params: JobParameters?): Boolean {
job?.cancel()
return false
}
}