forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeckMetaDataService.kt
127 lines (108 loc) · 3.92 KB
/
DeckMetaDataService.kt
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.ichi2.anki.services
import android.annotation.SuppressLint
import android.app.AlarmManager
import android.app.Notification
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.IBinder
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.*
import com.ichi2.anki.CollectionHelper
import com.ichi2.anki.DeckMetaDataPreference
import com.ichi2.anki.NotificationChannels
import com.ichi2.anki.R
import com.ichi2.libanki.sched.Counts
import com.ichi2.libanki.utils.Time
import timber.log.Timber
import java.util.*
class DeckMetaDataService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Timber.tag("META").e("On Service start command...")
/**
* It sets the notification till the service is completed.
* */
val notification = NotificationCompat.Builder(this, NotificationChannels.getId(NotificationChannels.Channel.GENERAL))
.setOngoing(true)
.setContentTitle("Anki")
.setContentText("Ankidroid is syncing deck data.")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(PRIORITY_LOW)
.setSilent(true)
.setCategory(Notification.CATEGORY_SERVICE)
.build()
startForeground(FOREGROUND_SERVICE_ID, notification)
// Update the data of Deck Metadata from HERE
val col = CollectionHelper.getInstance().getCol(this)
val deckList = col.sched.deckDueList()
deckList.forEach {
val new = it?.newCount ?: -1
val lrn = it?.lrnCount ?: -1
val rev = it?.revCount ?: -1
val data = Meta(
it?.did ?: -1,
it?.fullDeckName ?: "Deck Name",
new,
lrn,
rev,
col.sched.eta(Counts(new, lrn, rev), false)
)
storeInPreference(data)
}
updateLastFetch()
stopSelf() // Stops the current running service.
return START_NOT_STICKY
}
private fun updateLastFetch() {
val metaPreference = DeckMetaDataPreference(this)
metaPreference.setString(LAST_FETCH_TIME, System.currentTimeMillis().toString())
}
/**
* It is used to store the data in the DeckMetaData Shared Preference.
* */
private fun storeInPreference(list: Meta) {
val metaPreference = DeckMetaDataPreference(this)
metaPreference.setMetaData(list.did.toString(), list)
}
/**
* Does Nothing
* */
override fun onBind(intent: Intent?): IBinder? {
// Kept Intentionally
return null
}
override fun onDestroy() {
Timber.tag("META").e("Deck meta data Service on Destroy...")
scheduleNextAlarm(Long.MAX_VALUE) // Temp
}
/**
* It is used to schedule the next alarm which helps in starting this service again.
**/
@SuppressLint("UnspecifiedImmutableFlag")
private fun scheduleNextAlarm(interval: Long) {
val intent = Intent(this, DeckMetaDataService::class.java)
val pendingIntent = PendingIntent.getService(
this.applicationContext, SERVICE_PENDING_INTENT_ID, intent, 0
)
val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
alarmManager.set(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + interval,
pendingIntent
)
Timber.tag("META").e("Service will start again at %s", (Time.calendar(System.currentTimeMillis() + interval).time))
}
data class Meta(
val did: Long,
val deckName: String,
val new: Int,
val lrn: Int,
val rev: Int,
val eta: Int
)
companion object {
const val FOREGROUND_SERVICE_ID = 10001
const val SERVICE_PENDING_INTENT_ID = 10002
const val LAST_FETCH_TIME = "LAST_FETCH"
}
}