Commit 231e54db authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Lint

parent 6e502ae8
......@@ -19,7 +19,7 @@ android {
defaultConfig {
applicationId "at.bitfire.davdroid"
versionCode 400000000
versionCode 400000001
buildConfigField "long", "buildTime", System.currentTimeMillis() + "L"
minSdkVersion 21 // Android 5
......
......@@ -106,10 +106,10 @@ class WebdavMountsActivity: AppCompatActivity() {
if (info.rootDocument?.quotaUsed != null || info.rootDocument?.quotaAvailable != null) {
binding.quota.visibility = View.VISIBLE
binding.quota.text = context.getString(R.string.webdav_mounts_quota_used_available,
info.rootDocument.quotaAvailable?.let {
info.rootDocument.quotaUsed?.let {
FileUtils.byteCountToDisplaySize(it)
} ?: "?",
info.rootDocument.quotaUsed?.let {
info.rootDocument.quotaAvailable?.let {
FileUtils.byteCountToDisplaySize(it)
} ?: "?"
)
......
......@@ -22,7 +22,6 @@ import android.webkit.MimeTypeMap
import androidx.annotation.WorkerThread
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import at.bitfire.cert4android.CertUtils
import at.bitfire.dav4jvm.DavCollection
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.Response
......@@ -41,8 +40,6 @@ import okhttp3.logging.HttpLoggingInterceptor
import java.io.ByteArrayOutputStream
import java.io.FileNotFoundException
import java.net.HttpURLConnection
import java.security.MessageDigest
import java.security.spec.MGF1ParameterSpec.SHA1
import java.util.*
import java.util.concurrent.*
import java.util.logging.Level
......@@ -312,32 +309,33 @@ class DavDocumentsProvider: DocumentsProvider() {
override fun copyDocument(sourceDocumentId: String, targetParentDocumentId: String): String {
Logger.log.fine("WebDAV copyDocument $sourceDocumentId $targetParentDocumentId")
val srcDoc = documentDao.get(sourceDocumentId.toLong()) ?: throw FileNotFoundException()
val dstDoc = documentDao.get(targetParentDocumentId.toLong()) ?: throw FileNotFoundException()
val dstFolder = documentDao.get(targetParentDocumentId.toLong()) ?: throw FileNotFoundException()
val name = srcDoc.name
if (srcDoc.mountId != dstDoc.mountId)
if (srcDoc.mountId != dstFolder.mountId)
throw UnsupportedOperationException("Can't COPY between WebDAV servers")
var dstDocId = sourceDocumentId
val dstDocId: String
httpClient(srcDoc.mountId).use { client ->
val dav = DavResource(client.okHttpClient, srcDoc.toHttpUrl(db))
try {
val dstUrl = dstDoc.toHttpUrl(db).newBuilder()
val dstUrl = dstFolder.toHttpUrl(db).newBuilder()
.addPathSegment(name)
.build()
dav.copy(dstUrl, false) {
// successfully copied
dstDocId = documentDao.insertOrReplace(WebDavDocument(
mountId = dstDoc.mountId,
parentId = dstDoc.id,
name = name,
isDirectory = srcDoc.isDirectory,
displayName = srcDoc.displayName,
mimeType = srcDoc.mimeType,
size = srcDoc.size
)).toString()
}
dstDocId = documentDao.insertOrReplace(WebDavDocument(
mountId = dstFolder.mountId,
parentId = dstFolder.id,
name = name,
isDirectory = srcDoc.isDirectory,
displayName = srcDoc.displayName,
mimeType = srcDoc.mimeType,
size = srcDoc.size
)).toString()
notifyFolderChanged(targetParentDocumentId)
} catch (e: HttpException) {
if (e.code == HttpURLConnection.HTTP_NOT_FOUND)
......@@ -428,10 +426,11 @@ class DavDocumentsProvider: DocumentsProvider() {
try {
dav.move(newLocation, false) {
// successfully moved
doc.parentId = dstParent.id
documentDao.update(doc)
}
doc.parentId = dstParent.id
documentDao.update(doc)
notifyFolderChanged(sourceParentDocumentId)
notifyFolderChanged(targetParentDocumentId)
} catch (e: HttpException) {
......@@ -521,7 +520,11 @@ class DavDocumentsProvider: DocumentsProvider() {
val accessor = RandomAccessCallback(context!!, client, url, doc.mimeType, fileInfo, signal)
storageManager.openProxyFileDescriptor(modeFlags, accessor, accessor.workerHandler)
} else {
val fd = StreamingFileDescriptor(context!!, client, url, doc.mimeType, signal)
val fd = StreamingFileDescriptor(context!!, client, url, doc.mimeType, signal) {
// called when transfer is finished
notifyFolderChanged(doc.parentId)
}
if (readOnly)
fd.download()
else
......@@ -555,7 +558,7 @@ class DavDocumentsProvider: DocumentsProvider() {
BitmapFactory.decodeStream(data)?.let { bitmap ->
val thumb = ThumbnailUtils.extractThumbnail(bitmap, sizeHint.x, sizeHint.y)
val baos = ByteArrayOutputStream()
thumb.compress(Bitmap.CompressFormat.JPEG, 92, baos)
thumb.compress(Bitmap.CompressFormat.JPEG, 95, baos)
result = baos.toByteArray()
}
}
......@@ -616,26 +619,25 @@ class DavDocumentsProvider: DocumentsProvider() {
context!!.contentResolver.notifyChange(buildChildDocumentsUri(authority, parentDocumentId), null)
}
private fun sha1(s: String): String {
val md = MessageDigest.getInstance(SHA1.digestAlgorithm)
return CertUtils.hexString(md.digest(s.toByteArray()))
}
private fun HttpException.throwForDocumentProvider(ignorePreconditionFailed: Boolean = false) {
when (code) {
HttpURLConnection.HTTP_UNAUTHORIZED -> {
// TODO edit mount
val intent = Intent(context!!, WebdavMountsActivity::class.java)
throw AuthenticationRequiredException(this, PendingIntent.getActivity(context!!, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE))
if (Build.VERSION.SDK_INT >= 26) {
// TODO edit mount
val intent = Intent(context!!, WebdavMountsActivity::class.java)
throw AuthenticationRequiredException(this, PendingIntent.getActivity(context!!, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE))
}
}
HttpURLConnection.HTTP_NOT_FOUND -> throw FileNotFoundException()
HttpURLConnection.HTTP_NOT_FOUND ->
throw FileNotFoundException()
HttpURLConnection.HTTP_PRECON_FAILED ->
if (!ignorePreconditionFailed)
throw this
else ->
throw this
if (ignorePreconditionFailed)
return
}
// re-throw
throw this
}
}
\ No newline at end of file
package at.bitfire.davdroid.webdav
import android.content.Context
import android.os.Build
import android.os.storage.StorageManager
import androidx.annotation.WorkerThread
import androidx.core.content.ContextCompat
import at.bitfire.davdroid.AndroidSingleton
import at.bitfire.davdroid.log.Logger
import okhttp3.HttpUrl
import org.apache.commons.io.FileUtils
import java.io.File
@WorkerThread
class PageCache(
context: Context
) {
......@@ -30,8 +34,11 @@ class PageCache(
init {
val storageManager = ContextCompat.getSystemService(context, StorageManager::class.java)!!
val cacheDir = File(context.cacheDir, "webdav/page")
val maxBytes = storageManager.getCacheQuotaBytes(storageManager.getUuidForPath(cacheDir)) / 2
Logger.log.info("Initializing WebDAV page cache in $cacheDir")
val maxBytes = if (Build.VERSION.SDK_INT >= 26)
storageManager.getCacheQuotaBytes(storageManager.getUuidForPath(cacheDir)) / 2
else
50*FileUtils.ONE_MB
Logger.log.info("Initializing WebDAV page cache in $cacheDir with ${FileUtils.byteCountToDisplaySize(maxBytes)}")
cache = DiskCache(cacheDir, maxBytes)
}
......
package at.bitfire.davdroid.webdav
import android.annotation.TargetApi
import android.content.Context
import android.os.CancellationSignal
import android.os.Handler
......@@ -7,6 +8,7 @@ import android.os.HandlerThread
import android.os.ProxyFileDescriptorCallback
import android.system.ErrnoException
import android.system.OsConstants
import androidx.annotation.RequiresApi
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.HttpUtils
import at.bitfire.dav4jvm.exception.HttpException
......@@ -20,6 +22,7 @@ import java.io.InterruptedIOException
import java.net.HttpURLConnection
import java.util.logging.Level
@TargetApi(26)
class RandomAccessCallback(
val context: Context,
val httpClient: HttpClient,
......
......@@ -24,7 +24,8 @@ class StreamingFileDescriptor(
val client: HttpClient,
val url: HttpUrl,
val mimeType: String?,
val cancellationSignal: CancellationSignal?
val cancellationSignal: CancellationSignal?,
val finishedCallback: () -> Unit
) {
companion object {
......@@ -57,6 +58,8 @@ class StreamingFileDescriptor(
readFd.close()
writeFd.close()
} catch (ignored: IOException) {}
finishedCallback()
}
cancellationSignal?.setOnCancelListener {
......
......@@ -2,13 +2,17 @@ package at.bitfire.davdroid.webdav
import android.content.Context
import android.graphics.Point
import android.os.Build
import android.os.storage.StorageManager
import androidx.annotation.WorkerThread
import androidx.core.content.ContextCompat
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.model.WebDavDocument
import org.apache.commons.io.FileUtils
import java.io.File
import java.util.*
@WorkerThread
class ThumbnailCache(context: Context) {
val cache: DiskCache
......@@ -16,8 +20,11 @@ class ThumbnailCache(context: Context) {
init {
val storageManager = ContextCompat.getSystemService(context, StorageManager::class.java)!!
val cacheDir = File(context.cacheDir, "webdav/thumbnail")
val maxBytes = storageManager.getCacheQuotaBytes(storageManager.getUuidForPath(cacheDir)) / 2
Logger.log.info("Initializing WebDAV thumbnail cache with ${maxBytes/1024/1024} MB")
val maxBytes = if (Build.VERSION.SDK_INT >= 26)
storageManager.getCacheQuotaBytes(storageManager.getUuidForPath(cacheDir)) / 2
else
50*FileUtils.ONE_MB
Logger.log.info("Initializing WebDAV thumbnail cache with ${FileUtils.byteCountToDisplaySize(maxBytes)}")
cache = DiskCache(cacheDir, maxBytes)
}
......
......@@ -428,7 +428,7 @@
<!-- WebDAV accounts -->
<string name="webdav_authority">at.bitfire.davdroid.webdav</string>
<string name="webdav_mounts_title">WebDAV mounts</string>
<string name="webdav_mounts_quota_used_available">Quota used: %s / available: %s</string>
<string name="webdav_mounts_quota_used_available">Quota used: %1$s / available: %2$s</string>
<string name="webdav_mounts_share_content">Share content</string>
<string name="webdav_mounts_unmount">Unmount</string>
<string name="webdav_add_mount_title">Add WebDAV mount</string>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment