mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2024-12-25 07:50:56 +01:00
fix #573, listen to CalDAV events in the background, update asap
This commit is contained in:
parent
1ca6f6379a
commit
87b14ea7c4
@ -177,6 +177,11 @@
|
||||
|
||||
<service android:name=".services.SnoozeService"/>
|
||||
|
||||
<service
|
||||
android:name=".jobs.CalDAVUpdateListener"
|
||||
android:exported="true"
|
||||
android:permission="android.permission.BIND_JOB_SERVICE"/>
|
||||
|
||||
<receiver android:name=".receivers.NotificationReceiver"/>
|
||||
|
||||
<receiver android:name=".receivers.CalDAVSyncReceiver"/>
|
||||
|
@ -31,6 +31,7 @@ import com.simplemobiletools.calendar.pro.extensions.*
|
||||
import com.simplemobiletools.calendar.pro.fragments.*
|
||||
import com.simplemobiletools.calendar.pro.helpers.*
|
||||
import com.simplemobiletools.calendar.pro.helpers.Formatter
|
||||
import com.simplemobiletools.calendar.pro.jobs.CalDAVUpdateListener
|
||||
import com.simplemobiletools.calendar.pro.models.Event
|
||||
import com.simplemobiletools.calendar.pro.models.EventType
|
||||
import com.simplemobiletools.calendar.pro.models.ListEvent
|
||||
@ -102,6 +103,10 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
||||
}
|
||||
|
||||
checkAppOnSDCard()
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
checkCalDAVUpdateListener()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
@ -265,6 +270,19 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
||||
mSearchMenuItem?.collapseActionView()
|
||||
}
|
||||
|
||||
private fun checkCalDAVUpdateListener() {
|
||||
if (isNougatPlus()) {
|
||||
val updateListener = CalDAVUpdateListener()
|
||||
if (config.caldavSync) {
|
||||
if (!updateListener.isScheduled(applicationContext)) {
|
||||
updateListener.scheduleJob(applicationContext)
|
||||
}
|
||||
} else {
|
||||
updateListener.cancelJob(applicationContext)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
private fun checkShortcuts() {
|
||||
val appIconColor = config.appIconColor
|
||||
|
@ -0,0 +1,65 @@
|
||||
package com.simplemobiletools.calendar.pro.jobs
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.app.job.JobInfo
|
||||
import android.app.job.JobParameters
|
||||
import android.app.job.JobScheduler
|
||||
import android.app.job.JobService
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.provider.CalendarContract
|
||||
import com.simplemobiletools.calendar.pro.extensions.recheckCalDAVCalendars
|
||||
|
||||
// based on https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#addTriggerContentUri(android.app.job.JobInfo.TriggerContentUri)
|
||||
@TargetApi(Build.VERSION_CODES.N)
|
||||
class CalDAVUpdateListener : JobService() {
|
||||
companion object {
|
||||
const val CALDAV_EVENT_CONTENT_JOB = 1
|
||||
}
|
||||
|
||||
private val mHandler = Handler()
|
||||
private val mWorker = Runnable {
|
||||
scheduleJob(this@CalDAVUpdateListener)
|
||||
jobFinished(mRunningParams, false)
|
||||
}
|
||||
|
||||
private var mRunningParams: JobParameters? = null
|
||||
|
||||
fun scheduleJob(context: Context) {
|
||||
val componentName = ComponentName(context, CalDAVUpdateListener::class.java)
|
||||
val uri = CalendarContract.Calendars.CONTENT_URI
|
||||
JobInfo.Builder(CALDAV_EVENT_CONTENT_JOB, componentName).apply {
|
||||
addTriggerContentUri(JobInfo.TriggerContentUri(uri, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS))
|
||||
context.getSystemService(JobScheduler::class.java).schedule(build())
|
||||
}
|
||||
}
|
||||
|
||||
fun isScheduled(context: Context): Boolean {
|
||||
val jobScheduler = context.getSystemService(JobScheduler::class.java)
|
||||
val jobs = jobScheduler.allPendingJobs ?: return false
|
||||
return jobs.any { it.id == CALDAV_EVENT_CONTENT_JOB }
|
||||
}
|
||||
|
||||
fun cancelJob(context: Context) {
|
||||
val js = context.getSystemService(JobScheduler::class.java)
|
||||
js.cancel(CALDAV_EVENT_CONTENT_JOB)
|
||||
}
|
||||
|
||||
override fun onStartJob(params: JobParameters): Boolean {
|
||||
mRunningParams = params
|
||||
|
||||
if (params.triggeredContentAuthorities != null && params.triggeredContentUris != null) {
|
||||
recheckCalDAVCalendars {}
|
||||
}
|
||||
|
||||
mHandler.post(mWorker)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onStopJob(params: JobParameters): Boolean {
|
||||
mHandler.removeCallbacks(mWorker)
|
||||
return false
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user