1
0
mirror of https://github.com/etesync/android synced 2025-07-13 18:18:21 +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()) if (!super.prepare())
return false return false
journal = JournalEntryManager(httpClient, remote, localCalendar().name!!) journal = JournalEntryManager(httpClient.okHttpClient, remote, localCalendar().name!!)
return true 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)) { 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) Logger.log.info("Synchronizing calendar #" + calendar.id + ", URL: " + calendar.name)
val syncManager = CalendarSyncManager(context, account, settings, extras, authority, syncResult, calendar, principal) CalendarSyncManager(context, account, settings, extras, authority, syncResult, calendar, principal).use {
syncManager.performSync() it.performSync()
}
} }
} catch (e: Exceptions.ServiceUnavailableException) { } catch (e: Exceptions.ServiceUnavailableException) {
syncResult.stats.numIoExceptions++ syncResult.stats.numIoExceptions++

View File

@ -56,8 +56,9 @@ class ContactsSyncAdapterService : SyncAdapterService() {
Logger.log.info("Taking settings from: " + addressBook.mainAccount) Logger.log.info("Taking settings from: " + addressBook.mainAccount)
val principal = HttpUrl.get(settings.uri!!)!! val principal = HttpUrl.get(settings.uri!!)!!
val syncManager = ContactsSyncManager(context, account, settings, extras, authority, provider, syncResult, addressBook, principal) ContactsSyncManager(context, account, settings, extras, authority, provider, syncResult, addressBook, principal).use {
syncManager.performSync() it.performSync()
}
} catch (e: Exception) { } catch (e: Exception) {
val syncPhase = R.string.sync_phase_journals val syncPhase = R.string.sync_phase_journals
val title = context.getString(R.string.sync_error_contacts, account.name) 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) values.put(ContactsContract.Settings.UNGROUPED_VISIBLE, 1)
localAddressBook.settings.putAll(values) localAddressBook.settings.putAll(values)
journal = JournalEntryManager(httpClient, remote, localAddressBook.url!!) journal = JournalEntryManager(httpClient.okHttpClient, remote, localAddressBook.url!!)
localAddressBook.includeGroups = true localAddressBook.includeGroups = true

View File

@ -111,9 +111,9 @@ abstract class SyncAdapterService : Service() {
Logger.log.info("Refreshing " + serviceType + " collections of service #" + serviceType.toString()) Logger.log.info("Refreshing " + serviceType + " collections of service #" + serviceType.toString())
val settings = AccountSettings(context, account) 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>>() val journals = LinkedList<Pair<JournalManager.Journal, CollectionInfo>>()
@ -150,6 +150,7 @@ abstract class SyncAdapterService : Service() {
} }
saveCollections(journals) saveCollections(journals)
httpClient.close()
} }
private fun saveCollections(journals: Iterable<Pair<JournalManager.Journal, CollectionInfo>>) { 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 io.requery.sql.EntityDataStore
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.defaultSharedPreferences
import java.io.Closeable
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.IOException import java.io.IOException
import java.util.* import java.util.*
import java.util.logging.Level import java.util.logging.Level
abstract class SyncManager<T: LocalResource<*>> @Throws(Exceptions.IntegrityException::class, Exceptions.GenericCryptoException::class) 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 notificationManager: SyncNotification
protected val info: CollectionInfo protected val info: CollectionInfo
protected var localCollection: LocalCollection<T>? = null protected var localCollection: LocalCollection<T>? = null
protected var httpClient: OkHttpClient protected var httpClient: HttpClient
protected var journal: JournalEntryManager? = null protected var journal: JournalEntryManager? = null
private var _journalEntity: JournalEntity? = 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) get() = JournalModel.Journal.fetch(data, info.getServiceEntity(data), info.uid)
init { init {
// create HttpClient with given logger // 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 data = (context.applicationContext as App).data
val serviceEntity = JournalModel.Service.fetch(data, accountName, serviceType) 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 protected abstract fun notificationId(): Int
override fun close() {
httpClient.close()
}
@TargetApi(21) @TargetApi(21)
fun performSync() { fun performSync() {
var syncPhase = R.string.sync_phase_prepare 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)) { 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}]") Logger.log.info("Synchronizing task list #${taskList.id} [${taskList.syncId}]")
val tasksSyncManager = TasksSyncManager(context, account, accountSettings, extras, authority, syncResult, taskList, principal); TasksSyncManager(context, account, accountSettings, extras, authority, syncResult, taskList, principal).use {
tasksSyncManager.performSync() it.performSync()
}
} }
} catch (e: Exceptions.ServiceUnavailableException) { } catch (e: Exceptions.ServiceUnavailableException) {
syncResult.stats.numIoExceptions++ syncResult.stats.numIoExceptions++

View File

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