diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt index b02f4048..1e3ee89d 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarSyncManager.kt @@ -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 } diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarsSyncAdapterService.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarsSyncAdapterService.kt index bf27a604..4a66901f 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarsSyncAdapterService.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/CalendarsSyncAdapterService.kt @@ -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++ diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncAdapterService.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncAdapterService.kt index 068de217..72df316c 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncAdapterService.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncAdapterService.kt @@ -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) diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncManager.kt index 0dabd3c9..d33641d3 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/ContactsSyncManager.kt @@ -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 diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.kt index 11fc8f38..b1718ff9 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.kt @@ -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>() @@ -150,6 +150,7 @@ abstract class SyncAdapterService : Service() { } saveCollections(journals) + httpClient.close() } private fun saveCollections(journals: Iterable>) { diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.kt index b4ede920..bcc18e08 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncManager.kt @@ -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> @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? = 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 diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncAdapterService.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncAdapterService.kt index 454141c9..4900eafa 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncAdapterService.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncAdapterService.kt @@ -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++ diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt index 6db482ce..9539e8a6 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/TasksSyncManager.kt @@ -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 }