Show sync indicator when davx5/etesync are syncing

pull/1148/head
Alex Baker 5 years ago
parent 95fe64fb65
commit f2a0eac367

@ -77,6 +77,11 @@
<uses-permission android:name="com.google.android.googleapps.permission.GOOGLE_AUTH"/> <uses-permission android:name="com.google.android.googleapps.permission.GOOGLE_AUTH"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" android:maxSdkVersion="25"/> <uses-permission android:name="android.permission.GET_ACCOUNTS" android:maxSdkVersion="25"/>
<!-- ****************************** -->
<!-- Check DAVx5/EteSync sync state -->
<!-- ****************************** -->
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<!-- ***************************** --> <!-- ***************************** -->
<!-- Exclude OpenTasks permissions --> <!-- Exclude OpenTasks permissions -->
<!-- ***************************** --> <!-- ***************************** -->

@ -1,19 +1,29 @@
package org.tasks.opentasks package org.tasks.opentasks
import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.content.SyncStatusObserver
import android.database.ContentObserver import android.database.ContentObserver
import android.net.Uri import android.net.Uri
import android.os.Handler import android.os.Handler
import android.os.HandlerThread import android.os.HandlerThread
import dagger.hilt.android.qualifiers.ApplicationContext
import org.dmfs.tasks.contract.TaskContract.* import org.dmfs.tasks.contract.TaskContract.*
import org.tasks.LocalBroadcastManager
import org.tasks.R import org.tasks.R
import org.tasks.preferences.Preferences
import org.tasks.sync.SyncAdapters import org.tasks.sync.SyncAdapters
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
class OpenTaskContentObserver @Inject constructor( class OpenTaskContentObserver @Inject constructor(
private val syncAdapters: SyncAdapters @ApplicationContext context: Context,
) : ContentObserver(getHandler()) { private val syncAdapters: SyncAdapters,
private val preferences: Preferences,
private val localBroadcastManager: LocalBroadcastManager
) : ContentObserver(getHandler()), SyncStatusObserver {
val authority = context.getString(R.string.opentasks_authority)
override fun onChange(selfChange: Boolean) = onChange(selfChange, null) override fun onChange(selfChange: Boolean) = onChange(selfChange, null)
@ -28,17 +38,28 @@ class OpenTaskContentObserver @Inject constructor(
syncAdapters.syncOpenTasks() syncAdapters.syncOpenTasks()
} }
override fun onStatusChanged(which: Int) {
val active = ContentResolver.getCurrentSyncs().any { it.authority == authority }
if (preferences.getBoolean(R.string.p_sync_ongoing_android, false) != active) {
preferences.setBoolean(R.string.p_sync_ongoing_android, active)
localBroadcastManager.broadcastRefresh()
}
}
companion object { companion object {
fun getHandler() = HandlerThread("OT-handler)").let { fun getHandler() = HandlerThread("OT-handler)").let {
it.start() it.start()
Handler(it.looper) Handler(it.looper)
} }
fun registerObserver(context: Context, observer: ContentObserver) { fun registerObserver(context: Context, observer: OpenTaskContentObserver) {
getUris(context.getString(R.string.opentasks_authority)) getUris(observer.authority).forEach {
.forEach { context.contentResolver.registerContentObserver(it, false, observer)
context.contentResolver.registerContentObserver(it, false, observer) }
} ContentResolver.addStatusChangeListener(
ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE,
observer
)
} }
private fun getUris(authority: String): List<Uri> = private fun getUris(authority: String): List<Uri> =

@ -502,6 +502,7 @@ class Preferences @JvmOverloads constructor(private val context: Context, name:
private val syncFlags = listOf( private val syncFlags = listOf(
R.string.p_sync_ongoing_google_tasks, R.string.p_sync_ongoing_google_tasks,
R.string.p_sync_ongoing_caldav, R.string.p_sync_ongoing_caldav,
R.string.p_sync_ongoing_etesync) R.string.p_sync_ongoing_etesync,
R.string.p_sync_ongoing_android)
} }
} }

@ -337,6 +337,7 @@
<string name="p_sync_ongoing_caldav">sync_ongoing_caldav</string> <string name="p_sync_ongoing_caldav">sync_ongoing_caldav</string>
<string name="p_sync_ongoing_etesync">sync_ongoing_etesync</string> <string name="p_sync_ongoing_etesync">sync_ongoing_etesync</string>
<string name="p_sync_ongoing_opentasks">sync_ongoing_opentasks</string> <string name="p_sync_ongoing_opentasks">sync_ongoing_opentasks</string>
<string name="p_sync_ongoing_android">sync_ongoing_android</string>
<string name="p_last_backup">last_backup</string> <string name="p_last_backup">last_backup</string>
<string name="p_show_description">show_description</string> <string name="p_show_description">show_description</string>
<string name="p_show_full_description">show_full_description</string> <string name="p_show_full_description">show_full_description</string>

Loading…
Cancel
Save