flattening the room and user icon loading into the same file

This commit is contained in:
Adam Brown 2022-02-21 17:14:36 +00:00
parent 9f44975b4a
commit 33c30e27fa
4 changed files with 96 additions and 138 deletions

View File

@ -1,65 +0,0 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.notifications
import android.content.Context
import android.graphics.Bitmap
import androidx.annotation.WorkerThread
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.signature.ObjectKey
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class BitmapLoader @Inject constructor(private val context: Context) {
private val iconWidth = context.resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_width)
private val iconHeight = context.resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height)
/**
* Get icon of a room.
* If already in cache, use it, else load it and call BitmapLoaderListener.onBitmapsLoaded() when ready
*/
@WorkerThread
fun getRoomBitmap(path: String?): Bitmap? {
if (path == null) {
return null
}
return loadRoomBitmap(path)
}
@WorkerThread
private fun loadRoomBitmap(path: String): Bitmap? {
return path.let {
try {
Glide.with(context)
.asBitmap()
.load(path)
.fitCenter()
.format(DecodeFormat.PREFER_ARGB_8888)
.signature(ObjectKey("room-icon-notification"))
.submit(iconWidth, iconHeight)
.get()
} catch (e: Exception) {
Timber.e(e, "decodeFile failed")
null
}
}
}
}

View File

@ -1,66 +0,0 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.notifications
import android.content.Context
import android.os.Build
import androidx.annotation.WorkerThread
import androidx.core.graphics.drawable.IconCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class IconLoader @Inject constructor(private val context: Context) {
/**
* Get icon of a user.
* If already in cache, use it, else load it and call IconLoaderListener.onIconsLoaded() when ready
* Before Android P, this does nothing because the icon won't be used
*/
@WorkerThread
fun getUserIcon(path: String?): IconCompat? {
if (path == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
return null
}
return loadUserIcon(path)
}
@WorkerThread
private fun loadUserIcon(path: String): IconCompat? {
return path.let {
try {
Glide.with(context)
.asBitmap()
.load(path)
.apply(RequestOptions.circleCropTransform()
.format(DecodeFormat.PREFER_ARGB_8888))
.submit()
.get()
} catch (e: Exception) {
Timber.e(e, "decodeFile failed")
null
}?.let { bitmap ->
IconCompat.createWithBitmap(bitmap)
}
}
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.notifications
import android.content.Context
import android.graphics.Bitmap
import android.os.Build
import androidx.annotation.WorkerThread
import androidx.core.graphics.drawable.IconCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.signature.ObjectKey
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class NotificationBitmapLoader @Inject constructor(private val context: Context) {
/**
* Get icon of a room
*/
@WorkerThread
fun getRoomBitmap(path: String?): Bitmap? {
if (path == null) {
return null
}
return loadRoomBitmap(path)
}
@WorkerThread
private fun loadRoomBitmap(path: String): Bitmap? {
return try {
Glide.with(context)
.asBitmap()
.load(path)
.format(DecodeFormat.PREFER_ARGB_8888)
.signature(ObjectKey("room-icon-notification"))
.submit()
.get()
} catch (e: Exception) {
Timber.e(e, "decodeFile failed")
null
}
}
/**
* Get icon of a user.
* Before Android P, this does nothing because the icon won't be used
*/
@WorkerThread
fun getUserIcon(path: String?): IconCompat? {
if (path == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
return null
}
return loadUserIcon(path)
}
@WorkerThread
private fun loadUserIcon(path: String): IconCompat? {
return try {
val bitmap = Glide.with(context)
.asBitmap()
.load(path)
.transform(CircleCrop())
.format(DecodeFormat.PREFER_ARGB_8888)
.signature(ObjectKey("user-icon-notification"))
.submit()
.get()
IconCompat.createWithBitmap(bitmap)
} catch (e: Exception) {
Timber.e(e, "decodeFile failed")
null
}
}
}

View File

@ -16,7 +16,6 @@
package im.vector.app.features.notifications package im.vector.app.features.notifications
import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.Person import androidx.core.app.Person
@ -28,11 +27,9 @@ import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
class RoomGroupMessageCreator @Inject constructor( class RoomGroupMessageCreator @Inject constructor(
private val iconLoader: IconLoader, private val bitmapLoader: NotificationBitmapLoader,
private val bitmapLoader: BitmapLoader,
private val stringProvider: StringProvider, private val stringProvider: StringProvider,
private val notificationUtils: NotificationUtils, private val notificationUtils: NotificationUtils
private val appContext: Context
) { ) {
fun createRoomMessage(events: List<NotifiableMessageEvent>, roomId: String, userDisplayName: String, userAvatarUrl: String?): RoomNotification.Message { fun createRoomMessage(events: List<NotifiableMessageEvent>, roomId: String, userDisplayName: String, userAvatarUrl: String?): RoomNotification.Message {
@ -41,7 +38,7 @@ class RoomGroupMessageCreator @Inject constructor(
val roomIsGroup = !firstKnownRoomEvent.roomIsDirect val roomIsGroup = !firstKnownRoomEvent.roomIsDirect
val style = NotificationCompat.MessagingStyle(Person.Builder() val style = NotificationCompat.MessagingStyle(Person.Builder()
.setName(userDisplayName) .setName(userDisplayName)
.setIcon(iconLoader.getUserIcon(userAvatarUrl)) .setIcon(bitmapLoader.getUserIcon(userAvatarUrl))
.setKey(firstKnownRoomEvent.matrixID) .setKey(firstKnownRoomEvent.matrixID)
.build() .build()
).also { ).also {
@ -92,7 +89,7 @@ class RoomGroupMessageCreator @Inject constructor(
} else { } else {
Person.Builder() Person.Builder()
.setName(event.senderName) .setName(event.senderName)
.setIcon(iconLoader.getUserIcon(event.senderAvatarPath)) .setIcon(bitmapLoader.getUserIcon(event.senderAvatarPath))
.setKey(event.senderId) .setKey(event.senderId)
.build() .build()
} }