-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Base for fetching Deck Meta Data
- Loading branch information
1 parent
502ee7a
commit 257bb94
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
AnkiDroid/src/main/java/com/ichi2/anki/DeckMetaDataPreference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.ichi2.anki | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import com.google.gson.Gson | ||
import com.ichi2.anki.services.DeckMetaDataService | ||
import timber.log.Timber | ||
|
||
class DeckMetaDataPreference(context: Context) { | ||
|
||
private val sharedPreferences: SharedPreferences = | ||
context.getSharedPreferences("DeckMetaData", Context.MODE_PRIVATE) | ||
|
||
fun setString(key: String, value: String) { | ||
sharedPreferences.edit() | ||
.putString(key, value) | ||
.apply() | ||
} | ||
|
||
fun getString(key: String, default: String): String { | ||
return sharedPreferences.getString(key, default)!! | ||
} | ||
|
||
fun setMetaData(key: String, value: DeckMetaDataService.Meta) { | ||
val json: String = Gson().toJson(value) | ||
sharedPreferences.edit() | ||
.putString(key, json) | ||
.apply() | ||
Timber.tag("META").e(json) | ||
} | ||
|
||
fun getMetaData(key: String): DeckMetaDataService.Meta? { | ||
val jsonData = sharedPreferences | ||
.getString(key, null) | ||
|
||
return if (jsonData == null) { | ||
null | ||
} else { | ||
Gson().fromJson(jsonData, DeckMetaDataService.Meta::class.java) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
AnkiDroid/src/main/java/com/ichi2/anki/services/DeckMetaDataService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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" | ||
} | ||
} |