Move syncronization to main process

This commit is contained in:
Koitharu
2023-06-17 17:36:12 +03:00
parent 4f32664b33
commit d548993e14
5 changed files with 48 additions and 23 deletions

View File

@@ -188,8 +188,7 @@
<service
android:name="org.koitharu.kotatsu.sync.ui.favourites.FavouritesSyncService"
android:exported="false"
android:label="@string/favourites"
android:process=":sync">
android:label="@string/favourites">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
@@ -200,8 +199,7 @@
<service
android:name="org.koitharu.kotatsu.sync.ui.history.HistorySyncService"
android:exported="false"
android:label="@string/history"
android:process=":sync">
android:label="@string/history">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>

View File

@@ -11,6 +11,10 @@ import android.database.Cursor
import android.net.Uri
import androidx.annotation.WorkerThread
import androidx.core.content.contentValuesOf
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.qualifiers.ApplicationContext
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
@@ -23,9 +27,9 @@ import org.koitharu.kotatsu.core.db.TABLE_HISTORY
import org.koitharu.kotatsu.core.db.TABLE_MANGA
import org.koitharu.kotatsu.core.db.TABLE_MANGA_TAGS
import org.koitharu.kotatsu.core.db.TABLE_TAGS
import org.koitharu.kotatsu.core.logs.LoggersModule
import org.koitharu.kotatsu.core.network.GZipInterceptor
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.core.logs.FileLogger
import org.koitharu.kotatsu.core.logs.SyncLogger
import org.koitharu.kotatsu.core.network.BaseHttpClient
import org.koitharu.kotatsu.core.util.ext.parseJsonOrNull
import org.koitharu.kotatsu.core.util.ext.toContentValues
import org.koitharu.kotatsu.core.util.ext.toJson
@@ -39,23 +43,20 @@ import java.util.concurrent.TimeUnit
private const val FIELD_TIMESTAMP = "timestamp"
/**
* Warning! This class may be used in another process
*/
@WorkerThread
class SyncHelper(
context: Context,
private val account: Account,
private val provider: ContentProviderClient,
class SyncHelper @AssistedInject constructor(
@ApplicationContext context: Context,
@BaseHttpClient baseHttpClient: OkHttpClient,
@Assisted private val account: Account,
@Assisted private val provider: ContentProviderClient,
private val settings: SyncSettings,
@SyncLogger private val logger: FileLogger,
) {
private val authorityHistory = context.getString(R.string.sync_authority_history)
private val authorityFavourites = context.getString(R.string.sync_authority_favourites)
private val settings = SyncSettings(context, account)
private val httpClient = OkHttpClient.Builder()
private val httpClient = baseHttpClient.newBuilder()
.authenticator(SyncAuthenticator(context, account, settings, SyncAuthApi(OkHttpClient())))
.addInterceptor(SyncInterceptor(context, account))
.addInterceptor(GZipInterceptor())
.build()
private val baseUrl: String by lazy {
val host = settings.host
@@ -64,8 +65,8 @@ class SyncHelper(
}
private val defaultGcPeriod: Long // gc period if sync enabled
get() = TimeUnit.DAYS.toMillis(4)
private val logger = LoggersModule.provideSyncLogger(context, AppSettings(context))
@WorkerThread
fun syncFavourites(syncResult: SyncResult) {
val data = JSONObject()
data.put(TABLE_FAVOURITE_CATEGORIES, getFavouriteCategories())
@@ -89,6 +90,7 @@ class SyncHelper(
gcFavourites()
}
@WorkerThread
fun syncHistory(syncResult: SyncResult) {
val data = JSONObject()
data.put(TABLE_HISTORY, getHistory())
@@ -321,4 +323,13 @@ class SyncHelper(
logger.log("$code ${request.url}")
}
}
@AssistedFactory
interface Factory {
fun create(
account: Account,
contentProviderClient: ContentProviderClient,
): SyncHelper
}
}

View File

@@ -0,0 +1,12 @@
package org.koitharu.kotatsu.sync.ui
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.koitharu.kotatsu.sync.domain.SyncHelper
@EntryPoint
@InstallIn(SingletonComponent::class)
interface SyncAdapterEntryPoint {
val syncHelperFactory: SyncHelper.Factory
}

View File

@@ -6,11 +6,12 @@ import android.content.ContentProviderClient
import android.content.Context
import android.content.SyncResult
import android.os.Bundle
import dagger.hilt.android.EntryPointAccessors
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.util.ext.onError
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
import org.koitharu.kotatsu.sync.domain.SyncController
import org.koitharu.kotatsu.sync.domain.SyncHelper
import org.koitharu.kotatsu.sync.ui.SyncAdapterEntryPoint
class FavouritesSyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context, true) {
@@ -24,7 +25,8 @@ class FavouritesSyncAdapter(context: Context) : AbstractThreadedSyncAdapter(cont
if (!context.resources.getBoolean(R.bool.is_sync_enabled)) {
return
}
val syncHelper = SyncHelper(context, account, provider)
val entryPoint = EntryPointAccessors.fromApplication(context, SyncAdapterEntryPoint::class.java)
val syncHelper = entryPoint.syncHelperFactory.create(account, provider)
runCatchingCancellable {
syncHelper.syncFavourites(syncResult)
SyncController.setLastSync(context, account, authority, System.currentTimeMillis())

View File

@@ -6,11 +6,12 @@ import android.content.ContentProviderClient
import android.content.Context
import android.content.SyncResult
import android.os.Bundle
import dagger.hilt.android.EntryPointAccessors
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.util.ext.onError
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
import org.koitharu.kotatsu.sync.domain.SyncController
import org.koitharu.kotatsu.sync.domain.SyncHelper
import org.koitharu.kotatsu.sync.ui.SyncAdapterEntryPoint
class HistorySyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context, true) {
@@ -24,7 +25,8 @@ class HistorySyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context
if (!context.resources.getBoolean(R.bool.is_sync_enabled)) {
return
}
val syncHelper = SyncHelper(context, account, provider)
val entryPoint = EntryPointAccessors.fromApplication(context, SyncAdapterEntryPoint::class.java)
val syncHelper = entryPoint.syncHelperFactory.create(account, provider)
runCatchingCancellable {
syncHelper.syncHistory(syncResult)
SyncController.setLastSync(context, account, authority, System.currentTimeMillis())