mirror of
https://github.com/viq/NewsBlur.git
synced 2025-09-18 21:43:31 +00:00
Schedule and cancel subscription sync services based on user auth state.
This commit is contained in:
parent
02a15da018
commit
2f18de918d
5 changed files with 34 additions and 18 deletions
|
@ -1,20 +1,15 @@
|
|||
package com.newsblur
|
||||
|
||||
import android.app.Application
|
||||
import android.app.job.JobScheduler
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import com.newsblur.service.SubscriptionSyncService
|
||||
import com.newsblur.util.Log
|
||||
|
||||
class NbApplication : Application(), DefaultLifecycleObserver {
|
||||
|
||||
override fun onCreate() {
|
||||
super<Application>.onCreate()
|
||||
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
||||
scheduleSubscriptionSync()
|
||||
}
|
||||
|
||||
override fun onStart(owner: LifecycleOwner) {
|
||||
|
@ -27,15 +22,6 @@ class NbApplication : Application(), DefaultLifecycleObserver {
|
|||
isAppForeground = false
|
||||
}
|
||||
|
||||
private fun scheduleSubscriptionSync() {
|
||||
val jobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
||||
val scheduledSubscriptionJob = jobScheduler.allPendingJobs.find { it.id == SubscriptionSyncService.JOB_ID }
|
||||
if (scheduledSubscriptionJob == null) {
|
||||
val result: Int = jobScheduler.schedule(SubscriptionSyncService.createJobInfo(this))
|
||||
Log.d(this, "Scheduled subscription result: ${if (result == JobScheduler.RESULT_FAILURE) "failed" else "completed"}")
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@JvmStatic
|
||||
|
|
|
@ -5,6 +5,7 @@ import android.os.Bundle
|
|||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.newsblur.service.SubscriptionSyncService
|
||||
import com.newsblur.util.*
|
||||
|
||||
/**
|
||||
|
@ -42,11 +43,12 @@ class InitActivity : AppCompatActivity() {
|
|||
upgradeCheck()
|
||||
|
||||
// see if a user is already logged in; if so, jump to the Main activity
|
||||
preferenceCheck()
|
||||
userAuthCheck()
|
||||
}
|
||||
|
||||
private fun preferenceCheck() {
|
||||
private fun userAuthCheck() {
|
||||
if (PrefsUtils.hasCookie(this)) {
|
||||
SubscriptionSyncService.schedule(this)
|
||||
val mainIntent = Intent(this, Main::class.java)
|
||||
startActivity(mainIntent)
|
||||
} else {
|
||||
|
|
|
@ -11,6 +11,7 @@ import androidx.lifecycle.lifecycleScope
|
|||
import com.newsblur.R
|
||||
import com.newsblur.databinding.ActivityLoginProgressBinding
|
||||
import com.newsblur.network.APIManager
|
||||
import com.newsblur.service.SubscriptionSyncService
|
||||
import com.newsblur.util.PrefsUtils
|
||||
import com.newsblur.util.UIUtils
|
||||
import com.newsblur.util.executeAsyncTask
|
||||
|
@ -60,6 +61,9 @@ class LoginProgress : FragmentActivity() {
|
|||
val b = AnimationUtils.loadAnimation(this, R.anim.text_up)
|
||||
binding.loginRetrievingFeeds.setText(R.string.login_retrieving_feeds)
|
||||
binding.loginFeedProgress.startAnimation(b)
|
||||
|
||||
SubscriptionSyncService.schedule(this)
|
||||
|
||||
val startMain = Intent(this, Main::class.java)
|
||||
startActivity(startMain)
|
||||
} else {
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.newsblur.service
|
|||
|
||||
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
|
||||
|
@ -46,9 +47,10 @@ class SubscriptionSyncService : JobService() {
|
|||
override fun onStopJob(params: JobParameters?): Boolean = false
|
||||
|
||||
companion object {
|
||||
const val JOB_ID = 2021
|
||||
|
||||
fun createJobInfo(context: Context): JobInfo = JobInfo.Builder(JOB_ID,
|
||||
private const val JOB_ID = 2021
|
||||
|
||||
private fun createJobInfo(context: Context): JobInfo = JobInfo.Builder(JOB_ID,
|
||||
ComponentName(context, SubscriptionSyncService::class.java))
|
||||
.apply {
|
||||
// sync every 24 hours
|
||||
|
@ -57,5 +59,23 @@ class SubscriptionSyncService : JobService() {
|
|||
setBackoffCriteria(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS, JobInfo.BACKOFF_POLICY_EXPONENTIAL)
|
||||
setPersisted(true)
|
||||
}.build()
|
||||
|
||||
fun schedule(context: Context) {
|
||||
val jobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
||||
val job = jobScheduler.allPendingJobs.find { it.id == JOB_ID }
|
||||
if (job == null) {
|
||||
val result: Int = jobScheduler.schedule(createJobInfo(context))
|
||||
Log.d(this, "Scheduled subscription result: ${if (result == JobScheduler.RESULT_FAILURE) "failed" else "completed"}")
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun cancel(context: Context) {
|
||||
val jobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
||||
jobScheduler.allPendingJobs.find { it.id == JOB_ID }?.let {
|
||||
jobScheduler.cancel(JOB_ID)
|
||||
Log.d(this, "Cancel sync job.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -30,6 +30,7 @@ import com.newsblur.R;
|
|||
import com.newsblur.activity.Login;
|
||||
import com.newsblur.domain.UserDetails;
|
||||
import com.newsblur.network.APIConstants;
|
||||
import com.newsblur.service.SubscriptionSyncService;
|
||||
import com.newsblur.util.PrefConstants.ThemeValue;
|
||||
import com.newsblur.service.NBSyncService;
|
||||
import com.newsblur.widget.WidgetUtils;
|
||||
|
@ -169,6 +170,9 @@ public class PrefsUtils {
|
|||
NBSyncService.softInterrupt();
|
||||
NBSyncService.clearState();
|
||||
|
||||
// cancel scheduled subscription sync service
|
||||
SubscriptionSyncService.cancel(context);
|
||||
|
||||
NotificationUtils.clear(context);
|
||||
|
||||
// wipe the prefs store
|
||||
|
|
Loading…
Add table
Reference in a new issue