Skip to content

Commit

Permalink
Revert "Migrate from Picasso to Glide for image loading"
Browse files Browse the repository at this point in the history
This reverts commit 5025392.

This is part 3/3 of reverts to get back to Picasso that handled images
in the chat view without any flickering.
  • Loading branch information
robinlinden committed Aug 14, 2023
1 parent 58a6c83 commit ff4e400
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ maven_install(
"com.google.android.material:material:1.4.0",
"com.google.code.gson:gson:2.8.6",
"com.google.guava:guava:19.0",
"com.squareup.picasso:picasso:2.8",
"com.typesafe.scala-logging:scala-logging_2.11:3.7.2",
"javax.inject:javax.inject:1",
"junit:junit:4.13.1",
Expand Down
4 changes: 2 additions & 2 deletions atox/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ dependencies {

implementation(libs.androidx.multidex)

implementation(libs.bumptech.glide)

implementation(libs.nayuki.qrcodegen)

implementation(libs.square.picasso)

debugImplementation(libs.square.leakcanary)

androidTestImplementation(kotlin("test"))
Expand Down
35 changes: 30 additions & 5 deletions atox/src/main/kotlin/ui/NotificationHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.Rect
import android.media.AudioAttributes
import android.media.RingtoneManager
import android.os.Build
Expand All @@ -18,10 +24,10 @@ import androidx.core.app.Person
import androidx.core.app.RemoteInput
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.IconCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.core.os.bundleOf
import androidx.navigation.NavDeepLinkBuilder
import com.bumptech.glide.Glide
import com.squareup.picasso.Picasso
import com.squareup.picasso.Transformation
import javax.inject.Inject
import javax.inject.Singleton
import ltd.evilcorp.atox.Action
Expand Down Expand Up @@ -86,6 +92,27 @@ class NotificationHelper @Inject constructor(

fun dismissNotifications(publicKey: PublicKey) = notifier.cancel(publicKey.string().hashCode())

private val circleTransform = object : Transformation {
override fun transform(bitmap: Bitmap): Bitmap {
val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)
val paint = Paint()
val rect = Rect(0, 0, bitmap.width, bitmap.height)

paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
canvas.drawCircle(bitmap.width / 2.0f, bitmap.height / 2.0f, bitmap.width / 2.0f, paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, rect, rect, paint)
if (bitmap != output) {
bitmap.recycle()
}
return output
}

override fun key() = "circleTransform"
}

fun showMessageNotification(
contact: Contact,
message: String,
Expand All @@ -103,9 +130,7 @@ class NotificationHelper @Inject constructor(

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val icon = if (contact.avatarUri.isNotEmpty()) {
IconCompat.createWithBitmap(
Glide.with(context).load(contact.avatarUri).circleCrop().submit().get().toBitmap()
)
IconCompat.createWithBitmap(Picasso.get().load(contact.avatarUri).transform(circleTransform).get())
} else {
null
}
Expand Down
15 changes: 5 additions & 10 deletions atox/src/main/kotlin/ui/chat/ChatAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import android.widget.ListView
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
import com.bumptech.glide.signature.ObjectKey
import com.squareup.picasso.Picasso
import java.net.URLConnection
import java.text.DateFormat
import java.util.Locale
Expand All @@ -37,7 +35,6 @@ import ltd.evilcorp.core.vo.isStarted

private const val TAG = "ChatAdapter"
private const val IMAGE_TO_SCREEN_RATIO = 0.75
private const val MAX_IMAGE_HEIGHT_PX = 1000

private fun FileTransfer.isImage() = try {
URLConnection.guessContentTypeFromName(fileName).startsWith("image/")
Expand Down Expand Up @@ -196,13 +193,11 @@ class ChatAdapter(

if (fileTransfer.isImage() && (fileTransfer.isComplete() || fileTransfer.outgoing)) {
vh.completedLayout.visibility = View.VISIBLE
Glide.with(vh.imagePreview)
val targetWidth = Resources.getSystem().displayMetrics.widthPixels * IMAGE_TO_SCREEN_RATIO
Picasso.get()
.load(fileTransfer.destination)
// Make sure fts with the same destination have unique caches.
.signature(ObjectKey(fileTransfer.id))
.downsample(DownsampleStrategy.AT_MOST)
.override((Resources.getSystem().displayMetrics.widthPixels * IMAGE_TO_SCREEN_RATIO).roundToInt(), MAX_IMAGE_HEIGHT_PX)
.into(vh.imagePreview)
.resize(targetWidth.roundToInt(), 0)
.onlyScaleDown().into(vh.imagePreview)
} else {
vh.completedLayout.visibility = View.GONE
}
Expand Down
4 changes: 4 additions & 0 deletions atox/src/main/kotlin/ui/chat/ChatViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import com.squareup.picasso.Picasso
import java.io.File
import java.io.FileInputStream
import javax.inject.Inject
Expand Down Expand Up @@ -128,6 +129,9 @@ class ChatViewModel @Inject constructor(
}

fun createFt(file: Uri) = scope.launch {
// Make sure there's no stale cached image in Picasso.
// This happens if the user sends 2 different files with the same path (e.g. by overwriting one with the other.)
Picasso.get().invalidate(file)
fileTransferManager.create(publicKey, file)
}

Expand Down
3 changes: 1 addition & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-t
nayuki-qrcodegen = "io.nayuki:qrcodegen:1.8.0"

square-leakcanary = "com.squareup.leakcanary:leakcanary-android:2.12"

bumptech-glide = "com.github.bumptech.glide:glide:4.13.1"
square-picasso = "com.squareup.picasso:picasso:2.8"

tox4j-android-aarch64 = { module = "org.toktok:tox4j-c_aarch64-linux-android", version.ref = "tox4j-android" }
tox4j-android-arm = { module = "org.toktok:tox4j-c_armv7a-linux-androideabi", version.ref = "tox4j-android" }
Expand Down

0 comments on commit ff4e400

Please sign in to comment.