Skip to content

Commit

Permalink
Create a new WebdavScope and scope caches and credentials store to it
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed May 14, 2024
1 parent 190777c commit 3ac51ba
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.security.crypto.MasterKey
import at.bitfire.davdroid.db.Credentials
import javax.inject.Inject

@WebdavScoped
class CredentialsStore @Inject constructor(context: Application) {

@Retention(AnnotationRetention.SOURCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.network.MemoryCookieStore
import at.bitfire.davdroid.ui.webdav.WebdavMountsActivity
import at.bitfire.davdroid.webdav.DavDocumentsProvider.DavDocumentsActor
import at.bitfire.davdroid.webdav.cache.HeadResponseCacheBuilder
import at.bitfire.davdroid.webdav.cache.HeadResponseCache
import at.bitfire.davdroid.webdav.cache.ThumbnailCache
import dagger.hilt.EntryPoint
import dagger.hilt.EntryPoints
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent
Expand Down Expand Up @@ -84,7 +85,15 @@ class DavDocumentsProvider: DocumentsProvider() {
@InstallIn(SingletonComponent::class)
interface DavDocumentsProviderEntryPoint {
fun appDatabase(): AppDatabase
fun webdavComponentBuilder(): WebdavComponentBuilder
}

@EntryPoint
@InstallIn(WebdavComponent::class)
interface DavDocumentsProviderWebdavEntryPoint {
fun credentialsStore(): CredentialsStore
fun headResponseCache(): HeadResponseCache
fun thumbnailCache(): ThumbnailCache
}

companion object {
Expand All @@ -110,16 +119,22 @@ class DavDocumentsProvider: DocumentsProvider() {

private val ourContext by lazy { context!! } // requireContext() requires API level 30
private val authority by lazy { ourContext.getString(R.string.webdav_authority) }
private val entryPoint by lazy { EntryPointAccessors.fromApplication<DavDocumentsProviderEntryPoint>(ourContext) }
private val globalEntryPoint by lazy { EntryPointAccessors.fromApplication<DavDocumentsProviderEntryPoint>(ourContext) }
private val webdavEntryPoint by lazy {
EntryPoints.get(
globalEntryPoint.webdavComponentBuilder().build(),
DavDocumentsProviderWebdavEntryPoint::class.java
)
}

private val db by lazy { entryPoint.appDatabase() }
private val db by lazy { globalEntryPoint.appDatabase() }
private val mountDao by lazy { db.webDavMountDao() }
private val documentDao by lazy { db.webDavDocumentDao() }

private val credentialsStore by lazy { entryPoint.credentialsStore() }
private val credentialsStore by lazy { webdavEntryPoint.credentialsStore() }
private val cookieStore by lazy { mutableMapOf<Long, CookieJar>() }
private val headResponseCache by lazy { HeadResponseCacheBuilder.getInstance() }
private val thumbnailCache by lazy { ThumbnailCache.getInstance(ourContext) }
private val headResponseCache by lazy { webdavEntryPoint.headResponseCache() }
private val thumbnailCache by lazy { webdavEntryPoint.thumbnailCache() }

private val connectivityManager by lazy { ourContext.getSystemService<ConnectivityManager>()!! }
private val storageManager by lazy { ourContext.getSystemService<StorageManager>()!! }
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/kotlin/at/bitfire/davdroid/webdav/WebdavScope.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
*/

package at.bitfire.davdroid.webdav

import dagger.hilt.DefineComponent
import dagger.hilt.components.SingletonComponent
import javax.inject.Scope

@Scope
@Retention(AnnotationRetention.BINARY)
annotation class WebdavScoped

@WebdavScoped
@DefineComponent(parent = SingletonComponent::class)
interface WebdavComponent

@DefineComponent.Builder
interface WebdavComponentBuilder {
fun build(): WebdavComponent
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@ package at.bitfire.davdroid.webdav.cache

import at.bitfire.davdroid.db.WebDavDocument
import at.bitfire.davdroid.webdav.HeadResponse
import java.lang.ref.WeakReference
import at.bitfire.davdroid.webdav.WebdavComponent
import at.bitfire.davdroid.webdav.WebdavScoped
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn

/**
* Memory cache for HEAD responses. Using a [WebDavDocument.CacheKey] as key guarantees that
* the cached response won't be used anymore if the ETag changes.
*/
typealias HeadResponseCache = ExtendedLruCache<WebDavDocument.CacheKey, HeadResponse>

object HeadResponseCacheBuilder {

private const val MAX_ENTRIES = 50

private var _cache: WeakReference<HeadResponseCache>? = null

@Synchronized
fun getInstance(): HeadResponseCache {
_cache?.get()?.let { return it }
val newCache = HeadResponseCache(MAX_ENTRIES)
_cache = WeakReference(newCache)
return newCache
}

@Module
@InstallIn(WebdavComponent::class)
object HeadResponseCacheModule {
@Provides
@WebdavScoped
fun headResponseCache(): HeadResponseCache = HeadResponseCache(
maxSize = 50 // max entries
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,24 @@

package at.bitfire.davdroid.webdav.cache

import android.content.Context
import android.app.Application
import android.graphics.Point
import android.os.Build
import android.os.storage.StorageManager
import androidx.core.content.getSystemService
import at.bitfire.davdroid.db.WebDavDocument
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.webdav.WebdavScoped
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
import java.io.File
import javax.inject.Inject

/**
* Simple disk cache for image thumbnails.
*/
class ThumbnailCache private constructor(context: Context) {

companion object {

private var _instance: ThumbnailCache? = null

@Synchronized
fun getInstance(context: Context): ThumbnailCache {
_instance?.let { return it }

val newInstance = ThumbnailCache(context)
_instance = newInstance
return newInstance
}

}
@WebdavScoped
class ThumbnailCache @Inject constructor(context: Application) {

val storage: DiskCache

Expand Down

0 comments on commit 3ac51ba

Please sign in to comment.