Replaced glide by Coil

This commit is contained in:
Sylvain Berfini 2022-04-20 12:56:31 +02:00
parent 7f63de9e1b
commit 0c085ed0b5
5 changed files with 58 additions and 93 deletions

View file

@ -229,9 +229,11 @@ dependencies {
implementation 'androidx.emoji:emoji:1.1.0'
implementation 'androidx.emoji:emoji-bundled:1.1.0'
// https://github.com/bumptech/glide/blob/master/LICENSE, BSD / Apache v2
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
// https://github.com/coil-kt/coil/blob/main/LICENSE.txt Apache v2.0
def coil_version = "2.0.0-rc03"
implementation("io.coil-kt:coil:$coil_version")
implementation("io.coil-kt:coil-gif:$coil_version")
implementation("io.coil-kt:coil-svg:$coil_version")
// https://github.com/Baseflow/PhotoView/blob/master/LICENSE Apache v2.0
implementation 'com.github.chrisbanes:PhotoView:2.3.0'

View file

@ -22,10 +22,18 @@ package org.linphone
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import coil.ImageLoader
import coil.ImageLoaderFactory
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
import coil.disk.DiskCache
import coil.memory.MemoryCache
import org.linphone.core.*
import org.linphone.core.tools.Log
import org.linphone.mediastream.Version
class LinphoneApplication : Application() {
class LinphoneApplication : Application(), ImageLoaderFactory {
companion object {
@SuppressLint("StaticFieldLeak")
lateinit var corePreferences: CorePreferences
@ -74,4 +82,28 @@ class LinphoneApplication : Application() {
ensureCoreExists(applicationContext)
Log.i("[Application] Created")
}
override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.components {
add(SvgDecoder.Factory())
if (Version.sdkAboveOrEqual(Version.API28_PIE_90)) {
add(ImageDecoderDecoder.Factory())
} else {
add(GifDecoder.Factory())
}
}
.memoryCache {
MemoryCache.Builder(this)
.maxSizePercent(0.25)
.build()
}
.diskCache {
DiskCache.Builder()
.directory(cacheDir.resolve("image_cache"))
.maxSizePercent(0.02)
.build()
}
.build()
}
}

View file

@ -42,6 +42,7 @@ import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.window.layout.FoldingFeature
import coil.imageLoader
import com.google.android.material.snackbar.Snackbar
import java.io.UnsupportedEncodingException
import java.net.URLDecoder
@ -63,7 +64,6 @@ import org.linphone.databinding.MainActivityBinding
import org.linphone.utils.AppUtils
import org.linphone.utils.Event
import org.linphone.utils.FileUtils
import org.linphone.utils.GlideApp
class MainActivity : GenericActivity(), SnackBarActivity, NavController.OnDestinationChangedListener {
private lateinit var binding: MainActivityBinding
@ -99,7 +99,7 @@ class MainActivity : GenericActivity(), SnackBarActivity, NavController.OnDestin
override fun onTrimMemory(level: Int) {
Log.w("[Main Activity] onTrimMemory called with level $level !")
GlideApp.get(this@MainActivity).clearMemory()
applicationContext.imageLoader.memoryCache?.clear()
}
}

View file

@ -23,7 +23,6 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.text.Editable
import android.text.TextWatcher
@ -39,16 +38,11 @@ import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.Guideline
import androidx.databinding.*
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.signature.ObjectKey
import coil.load
import coil.request.CachePolicy
import coil.transform.CircleCropTransformation
import com.google.android.material.switchmaterial.SwitchMaterial
import org.linphone.BR
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R
import org.linphone.activities.GenericActivity
@ -313,18 +307,8 @@ fun setImageViewScaleType(imageView: ImageView, scaleType: ImageView.ScaleType)
@BindingAdapter("glideRoundPath")
fun loadRoundImageWithGlide(imageView: ImageView, path: String?) {
if (path != null && path.isNotEmpty() && FileUtils.isExtensionImage(path)) {
if (corePreferences.vfsEnabled && path.endsWith(FileUtils.VFS_PLAIN_FILE_EXTENSION)) {
GlideApp.with(imageView)
.load(path)
.signature(ObjectKey(coreContext.contactsManager.latestContactFetch))
.apply(RequestOptions.circleCropTransform())
.into(imageView)
} else {
GlideApp
.with(imageView)
.load(path)
.apply(RequestOptions.circleCropTransform())
.into(imageView)
imageView.load(path) {
transformations(CircleCropTransformation())
}
} else {
Log.w("[Data Binding] [Glide] Can't load $path")
@ -335,13 +319,11 @@ fun loadRoundImageWithGlide(imageView: ImageView, path: String?) {
fun loadImageWithGlide(imageView: ImageView, path: String?) {
if (path != null && path.isNotEmpty() && FileUtils.isExtensionImage(path)) {
if (corePreferences.vfsEnabled && path.endsWith(FileUtils.VFS_PLAIN_FILE_EXTENSION)) {
GlideApp.with(imageView)
.load(path)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imageView)
imageView.load(path) {
diskCachePolicy(CachePolicy.DISABLED)
}
} else {
GlideApp.with(imageView).load(path).into(imageView)
imageView.load(path)
}
} else {
Log.w("[Data Binding] [Glide] Can't load $path")
@ -357,35 +339,18 @@ fun loadAvatarWithGlide(imageView: ImageView, path: Uri?) {
fun loadAvatarWithGlide(imageView: ImageView, path: String?) {
if (path != null) {
imageView.visibility = View.VISIBLE
GlideApp
.with(imageView)
.load(path)
.signature(ObjectKey(coreContext.contactsManager.latestContactFetch))
.apply(RequestOptions.circleCropTransform())
.listener(object : RequestListener<Drawable?> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable?>?,
isFirstResource: Boolean
): Boolean {
Log.w("[Data Binding] [Glide] Can't load $path")
imageView.load(path) {
transformations(CircleCropTransformation())
listener(
onError = { _, _ ->
Log.w("[Data Binding] [Coil] Can't load $path")
imageView.visibility = View.GONE
return false
}
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable?>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
},
onSuccess = { _, _ ->
imageView.visibility = View.VISIBLE
return false
}
})
.into(imageView)
)
}
} else {
imageView.visibility = View.GONE
}

View file

@ -1,34 +0,0 @@
/*
* Copyright (c) 2010-2020 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.utils
import android.content.Context
import android.util.Log
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule
@GlideModule
class LinphoneGlideModule : AppGlideModule() {
override fun applyOptions(context: Context, builder: GlideBuilder) {
// Tells Glide to only log errors, prevents warnings because contact doesn't have a picture
builder.setLogLevel(Log.ERROR)
}
}