@@ -43,19 +43,18 @@ import java.io.FileInputStream
4343import java.io.FileOutputStream
4444import java.io.IOException
4545import java.io.OutputStream
46+ import java.io.PipedInputStream
47+ import java.io.PipedOutputStream
4648import java.nio.ByteBuffer
4749import java.nio.channels.Channels
4850import java.util.LinkedList
4951import java.util.Queue
5052import java.util.UUID
51- import java.util.function.Supplier
52- import timber.log.Timber
53- import java.io.PipedInputStream
54- import java.io.PipedOutputStream
5553import java.util.concurrent.ExecutorService
5654import java.util.concurrent.Executors
5755import java.util.concurrent.Future
58- import kotlin.math.ceil
56+ import java.util.function.Supplier
57+ import timber.log.Timber
5958
6059
6160abstract class CryptoImplDecorator (
@@ -81,10 +80,11 @@ abstract class CryptoImplDecorator(
8180 Executors .newFixedThreadPool(3 , threadFactory)
8281 }
8382
84- protected fun getLruCacheFor (type : CloudType ): DiskLruCache ? {
83+ protected fun getLruCacheFor (type : CloudType ): DiskLruCache ? {
8584 return getOrCreateLruCache(getCacheTypeFromCloudType(type), sharedPreferencesHandler.lruCacheSize())
8685 }
87- private fun getOrCreateLruCache (key : LruFileCacheUtil .Cache , cacheSize : Int ): DiskLruCache ? {
86+
87+ private fun getOrCreateLruCache (key : LruFileCacheUtil .Cache , cacheSize : Int ): DiskLruCache ? {
8888 return diskLruCache.computeIfAbsent(key) {
8989 val where = LruFileCacheUtil (context).resolve(it)
9090 try {
@@ -96,7 +96,7 @@ abstract class CryptoImplDecorator(
9696 }
9797 }
9898
99- private fun getCacheTypeFromCloudType (type : CloudType ) : LruFileCacheUtil .Cache {
99+ private fun getCacheTypeFromCloudType (type : CloudType ): LruFileCacheUtil .Cache {
100100 return when (type) {
101101 CloudType .DROPBOX -> LruFileCacheUtil .Cache .DROPBOX
102102 CloudType .GOOGLE_DRIVE -> LruFileCacheUtil .Cache .GOOGLE_DRIVE
@@ -401,12 +401,12 @@ abstract class CryptoImplDecorator(
401401 decrypted + = read.toLong()
402402
403403 progressAware
404- .onProgress(
405- Progress .progress(DownloadState .decryption(cryptoFile)) //
406- .between(0 ) //
407- .and (cleartextSize) //
408- .withValue(decrypted)
409- )
404+ .onProgress(
405+ Progress .progress(DownloadState .decryption(cryptoFile)) //
406+ .between(0 ) //
407+ .and (cleartextSize) //
408+ .withValue(decrypted)
409+ )
410410 }
411411 }
412412 thumbnailWriter.flush()
@@ -423,44 +423,27 @@ abstract class CryptoImplDecorator(
423423 }
424424 }
425425
426- private fun closeQuietly (closeable : Closeable ) {
426+ private fun closeQuietly (closeable : Closeable ) {
427427 try {
428428 closeable.close();
429- } catch (e : IOException ) {
429+ } catch (e: IOException ) {
430430 // ignore
431431 }
432432 }
433- private fun startThumbnailGeneratorThread (diskCache : DiskLruCache ? , cacheKey : String , thumbnailReader : PipedInputStream ) : Future <* > {
433+
434+ private fun startThumbnailGeneratorThread (diskCache : DiskLruCache ? , cacheKey : String , thumbnailReader : PipedInputStream ): Future <* > {
434435 return thumbnailExecutorService.submit {
435436 try {
436437 val options = BitmapFactory .Options ()
437- val thumbnailBitmap : Bitmap ?
438- // options.inJustDecodeBounds = true
439- // read properties of the image: outWidth, outHeight (no bitmap allocation!)
440- // BitmapFactory.decodeStream(thumbnailReaderTee, null, options)
441- // options.inJustDecodeBounds = false
442- // options.outWidth; options.outHeight
438+ val thumbnailBitmap: Bitmap ?
443439 options.inSampleSize = 4 // pixel number reduced by a factor of 1/16
444- // options.inSampleSize = 8 // pixel number reduced by a factor of 1/64
445440
446- // obtain a subsampled version of the image
447441 val bitmap = BitmapFactory .decodeStream(thumbnailReader, null , options)
448-
449442 val thumbnailWidth = 100
450443 val thumbnailHeight = 100
451- // var aspectRatio = 1f
452- // bitmap?.let {
453- // if (it.height != 0) {
454- // aspectRatio = it.width.toFloat() / it.height
455- // }
456- // }
457- // val thumbnailHeight = ceil(1 / aspectRatio * thumbnailWidth).toInt()
458-
459- // generate thumbnail preserving aspect ratio
460444 thumbnailBitmap = ThumbnailUtils .extractThumbnail(bitmap, thumbnailWidth, thumbnailHeight)
461445
462- // store it in cloud-related LRU cache
463- if (thumbnailBitmap != null ) {
446+ if (thumbnailBitmap != null ) {
464447 storeThumbnail(diskCache, cacheKey, thumbnailBitmap)
465448 }
466449
@@ -471,34 +454,30 @@ abstract class CryptoImplDecorator(
471454 }
472455 }
473456
474- protected fun generateCacheKey (cloudFile : CloudFile ) : String {
457+ protected fun generateCacheKey (cloudFile : CloudFile ): String {
475458 return buildString {
476- // distinguish between two files with same path but on different instances of the same cloud
477459 if (cloudFile.cloud?.id() != null )
478460 this .append(cloudFile.cloud!! .id())
479461 else
480- // this.append(null obj) will add the string "null"
481462 this .append(" c" ) // "common"
482463 this .append(" -" )
483464 this .append(cloudFile.path.hashCode())
484465 }
485466 }
486467
487- private fun isGenerateThumbnailsEnabled (cache : DiskLruCache ? , fileName : String ) : Boolean {
488- return sharedPreferencesHandler.useLruCache() &&
468+ private fun isGenerateThumbnailsEnabled (cache : DiskLruCache ? , fileName : String ): Boolean {
469+ return sharedPreferencesHandler.useLruCache() &&
489470 sharedPreferencesHandler.generateThumbnails() != ThumbnailsOption .NEVER &&
490471 cache != null &&
491472 isImageMediaType(fileName)
492473 }
493474
494- private fun storeThumbnail (cache : DiskLruCache ? , cacheKey : String , thumbnailBitmap : Bitmap ){
495- // write the thumbnail in a file (on disk)
496- val thumbnailFile : File = File .createTempFile(UUID .randomUUID().toString(), " .thumbnail" , internalCache)
475+ private fun storeThumbnail (cache : DiskLruCache ? , cacheKey : String , thumbnailBitmap : Bitmap ) {
476+ val thumbnailFile: File = File .createTempFile(UUID .randomUUID().toString(), " .thumbnail" , internalCache)
497477 thumbnailBitmap.compress(Bitmap .CompressFormat .JPEG , 100 , thumbnailFile.outputStream())
498478
499479 try {
500480 cache?.let {
501- // store File to LruCache (on disk)
502481 LruFileCacheUtil .storeToLruCache(it, cacheKey, thumbnailFile)
503482 } ? : Timber .tag(" CryptoImplDecorator" ).e(" Failed to store item in LRU cache" )
504483 } catch (e: IOException ) {
0 commit comments