1
0
mirror of https://github.com/etesync/android synced 2024-12-23 15:18:14 +00:00

Fix closing of the HttpClient when syncing.

This commit is contained in:
Tom Hacohen 2019-03-21 15:35:17 +00:00
parent bc829d0d1e
commit d04d0f902f
8 changed files with 23 additions and 15 deletions

View File

@ -59,7 +59,7 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra
if (!super.prepare())
return false
journal = JournalEntryManager(httpClient, remote, localCalendar().name!!)
journal = JournalEntryManager(httpClient.okHttpClient, remote, localCalendar().name!!)
return true
}

View File

@ -55,8 +55,9 @@ class CalendarsSyncAdapterService : SyncAdapterService() {
for (calendar in AndroidCalendar.find(account, provider, LocalCalendar.Factory, CalendarContract.Calendars.SYNC_EVENTS + "!=0", null)) {
Logger.log.info("Synchronizing calendar #" + calendar.id + ", URL: " + calendar.name)
val syncManager = CalendarSyncManager(context, account, settings, extras, authority, syncResult, calendar, principal)
syncManager.performSync()
CalendarSyncManager(context, account, settings, extras, authority, syncResult, calendar, principal).use {
it.performSync()
}
}
} catch (e: Exceptions.ServiceUnavailableException) {
syncResult.stats.numIoExceptions++

View File

@ -56,8 +56,9 @@ class ContactsSyncAdapterService : SyncAdapterService() {
Logger.log.info("Taking settings from: " + addressBook.mainAccount)
val principal = HttpUrl.get(settings.uri!!)!!
val syncManager = ContactsSyncManager(context, account, settings, extras, authority, provider, syncResult, addressBook, principal)
syncManager.performSync()
ContactsSyncManager(context, account, settings, extras, authority, provider, syncResult, addressBook, principal).use {
it.performSync()
}
} catch (e: Exception) {
val syncPhase = R.string.sync_phase_journals
val title = context.getString(R.string.sync_error_contacts, account.name)

View File

@ -81,7 +81,7 @@ constructor(context: Context, account: Account, settings: AccountSettings, extra
values.put(ContactsContract.Settings.UNGROUPED_VISIBLE, 1)
localAddressBook.settings.putAll(values)
journal = JournalEntryManager(httpClient, remote, localAddressBook.url!!)
journal = JournalEntryManager(httpClient.okHttpClient, remote, localAddressBook.url!!)
localAddressBook.includeGroups = true

View File

@ -111,9 +111,9 @@ abstract class SyncAdapterService : Service() {
Logger.log.info("Refreshing " + serviceType + " collections of service #" + serviceType.toString())
val settings = AccountSettings(context, account)
val httpClient = HttpClient.Builder(context, settings).setForeground(false).build().okHttpClient
val httpClient = HttpClient.Builder(context, settings).setForeground(false).build()
val journalsManager = JournalManager(httpClient, HttpUrl.get(settings.uri!!)!!)
val journalsManager = JournalManager(httpClient.okHttpClient, HttpUrl.get(settings.uri!!)!!)
val journals = LinkedList<Pair<JournalManager.Journal, CollectionInfo>>()
@ -150,6 +150,7 @@ abstract class SyncAdapterService : Service() {
}
saveCollections(journals)
httpClient.close()
}
private fun saveCollections(journals: Iterable<Pair<JournalManager.Journal, CollectionInfo>>) {

View File

@ -33,19 +33,20 @@ import io.requery.Persistable
import io.requery.sql.EntityDataStore
import okhttp3.OkHttpClient
import org.jetbrains.anko.defaultSharedPreferences
import java.io.Closeable
import java.io.FileNotFoundException
import java.io.IOException
import java.util.*
import java.util.logging.Level
abstract class SyncManager<T: LocalResource<*>> @Throws(Exceptions.IntegrityException::class, Exceptions.GenericCryptoException::class)
constructor(protected val context: Context, protected val account: Account, protected val settings: AccountSettings, protected val extras: Bundle, protected val authority: String, protected val syncResult: SyncResult, journalUid: String, protected val serviceType: CollectionInfo.Type, accountName: String) {
constructor(protected val context: Context, protected val account: Account, protected val settings: AccountSettings, protected val extras: Bundle, protected val authority: String, protected val syncResult: SyncResult, journalUid: String, protected val serviceType: CollectionInfo.Type, accountName: String): Closeable {
protected val notificationManager: SyncNotification
protected val info: CollectionInfo
protected var localCollection: LocalCollection<T>? = null
protected var httpClient: OkHttpClient
protected var httpClient: HttpClient
protected var journal: JournalEntryManager? = null
private var _journalEntity: JournalEntity? = null
@ -85,9 +86,8 @@ constructor(protected val context: Context, protected val account: Account, prot
get() = JournalModel.Journal.fetch(data, info.getServiceEntity(data), info.uid)
init {
// create HttpClient with given logger
httpClient = HttpClient.Builder(context, settings).setForeground(false).build().okHttpClient
httpClient = HttpClient.Builder(context, settings).setForeground(false).build()
data = (context.applicationContext as App).data
val serviceEntity = JournalModel.Service.fetch(data, accountName, serviceType)
@ -108,6 +108,10 @@ constructor(protected val context: Context, protected val account: Account, prot
protected abstract fun notificationId(): Int
override fun close() {
httpClient.close()
}
@TargetApi(21)
fun performSync() {
var syncPhase = R.string.sync_phase_prepare

View File

@ -73,8 +73,9 @@ class TasksSyncAdapterService: SyncAdapterService() {
for (taskList in AndroidTaskList.find(account, taskProvider, LocalTaskList.Factory, "${TaskContract.TaskLists.SYNC_ENABLED}!=0", null)) {
Logger.log.info("Synchronizing task list #${taskList.id} [${taskList.syncId}]")
val tasksSyncManager = TasksSyncManager(context, account, accountSettings, extras, authority, syncResult, taskList, principal);
tasksSyncManager.performSync()
TasksSyncManager(context, account, accountSettings, extras, authority, syncResult, taskList, principal).use {
it.performSync()
}
}
} catch (e: Exceptions.ServiceUnavailableException) {
syncResult.stats.numIoExceptions++

View File

@ -58,7 +58,7 @@ class TasksSyncManager(
if (!super.prepare())
return false
journal = JournalEntryManager(httpClient, remote, localTaskList().url!!)
journal = JournalEntryManager(httpClient.okHttpClient, remote, localTaskList().url!!)
return true
}