Fix account not having its credentials when syncing with the worker and catch all exceptions to avoid zombie notification

This commit is contained in:
Shinokuni 2020-07-25 11:52:06 +02:00
parent 1e8e387d1b
commit 7314440534
1 changed files with 43 additions and 33 deletions

View File

@ -9,6 +9,7 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.work.Worker import androidx.work.Worker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.readrops.api.services.SyncResult
import com.readrops.app.R import com.readrops.app.R
import com.readrops.app.ReadropsApp import com.readrops.app.ReadropsApp
import com.readrops.app.activities.MainActivity import com.readrops.app.activities.MainActivity
@ -16,20 +17,22 @@ import com.readrops.app.repositories.ARepository
import com.readrops.db.Database import com.readrops.db.Database
import com.readrops.db.entities.Item import com.readrops.db.entities.Item
import com.readrops.db.entities.account.Account import com.readrops.db.entities.account.Account
import com.readrops.api.services.SyncResult
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters) { class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters) {
private lateinit var disposable: Disposable private var disposable: Disposable? = null
private val notificationManager = NotificationManagerCompat.from(applicationContext) private val notificationManager = NotificationManagerCompat.from(applicationContext)
private val database = Database.getInstance(applicationContext) private val database = Database.getInstance(applicationContext)
override fun doWork(): Result { override fun doWork(): Result {
val accounts = database.accountDao().selectAll()
var result = Result.success() var result = Result.success()
val syncResults = mutableMapOf<Account, SyncResult>()
try {
val accounts = database.accountDao().selectAll()
val notificationBuilder = NotificationCompat.Builder(applicationContext, ReadropsApp.SYNC_CHANNEL_ID) val notificationBuilder = NotificationCompat.Builder(applicationContext, ReadropsApp.SYNC_CHANNEL_ID)
.setContentTitle(applicationContext.getString(R.string.auto_synchro)) .setContentTitle(applicationContext.getString(R.string.auto_synchro))
@ -37,11 +40,13 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
.setSmallIcon(R.drawable.ic_notif) .setSmallIcon(R.drawable.ic_notif)
.setOnlyAlertOnce(true) .setOnlyAlertOnce(true)
val syncResults = mutableMapOf<Account, SyncResult>()
accounts.forEach { accounts.forEach {
notificationBuilder.setContentText(it.accountName) notificationBuilder.setContentText(it.accountName)
notificationManager.notify(SYNC_NOTIFICATION_ID, notificationBuilder.build()) notificationManager.notify(SYNC_NOTIFICATION_ID, notificationBuilder.build())
it.login = SharedPreferencesManager.readString(applicationContext, it.loginKey)
it.password = SharedPreferencesManager.readString(applicationContext, it.passwordKey)
val repository = ARepository.repositoryFactory(it, applicationContext) val repository = ARepository.repositoryFactory(it, applicationContext)
disposable = repository.sync(null) disposable = repository.sync(null)
@ -53,22 +58,27 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
if (repository.syncResult != null) syncResults[it] = repository.syncResult if (repository.syncResult != null) syncResults[it] = repository.syncResult
} }
} catch (e: Exception) {
Log.e(TAG, e.message!!)
result = Result.failure()
} finally {
notificationManager.cancel(SYNC_NOTIFICATION_ID) notificationManager.cancel(SYNC_NOTIFICATION_ID)
displaySyncResultNotif(syncResults) displaySyncResultNotif(syncResults)
return result return result
} }
}
override fun onStopped() { override fun onStopped() {
super.onStopped() super.onStopped()
disposable.dispose() disposable?.dispose()
notificationManager.cancel(SYNC_NOTIFICATION_ID) notificationManager.cancel(SYNC_NOTIFICATION_ID)
} }
private fun displaySyncResultNotif(syncResults: Map<Account, SyncResult>) { private fun displaySyncResultNotif(syncResults: Map<Account, SyncResult>) {
val notifContent = SyncResultAnalyser(applicationContext, syncResults, database).getSyncNotifContent() val notifContent = SyncResultAnalyser(applicationContext, syncResults, database)
.getSyncNotifContent()
if (notifContent.title != null) { if (notifContent.title != null) {
val intent = Intent(applicationContext, MainActivity::class.java).apply { val intent = Intent(applicationContext, MainActivity::class.java).apply {
@ -85,7 +95,8 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
.setContentText(notifContent.content) .setContentText(notifContent.content)
.setStyle(NotificationCompat.BigTextStyle().bigText(notifContent.content)) .setStyle(NotificationCompat.BigTextStyle().bigText(notifContent.content))
.setSmallIcon(R.drawable.ic_notif) .setSmallIcon(R.drawable.ic_notif)
.setContentIntent(PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)) .setContentIntent(PendingIntent.getActivity(applicationContext, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(true) .setAutoCancel(true)
notifContent.item?.let { notifContent.item?.let {
@ -100,8 +111,7 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
notificationBuilder.setLargeIcon(it) notificationBuilder.setLargeIcon(it)
} }
notificationManager.notify(SYNC_RESULT_NOTIFICATION_ID, notificationManager.notify(SYNC_RESULT_NOTIFICATION_ID, notificationBuilder.build())
notificationBuilder.build())
} }
} }