Skip to content

Support Android 11 Conversation features #1809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1809.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support Android 11 Conversation features
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ package org.matrix.android.sdk.api.session.room
enum class RoomSortOrder {
NAME,
ACTIVITY,
PRIORITY_AND_ACTIVITY,
NONE
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ import org.matrix.android.sdk.internal.database.model.RoomSummaryEntityFields

internal fun RealmQuery<RoomSummaryEntity>.process(sortOrder: RoomSortOrder): RealmQuery<RoomSummaryEntity> {
when (sortOrder) {
RoomSortOrder.NAME -> {
RoomSortOrder.NAME -> {
sort(RoomSummaryEntityFields.DISPLAY_NAME, Sort.ASCENDING)
}
RoomSortOrder.ACTIVITY -> {
RoomSortOrder.ACTIVITY -> {
sort(RoomSummaryEntityFields.LAST_ACTIVITY_TIME, Sort.DESCENDING)
}
RoomSortOrder.NONE -> {
RoomSortOrder.PRIORITY_AND_ACTIVITY -> {
sort(
arrayOf(
RoomSummaryEntityFields.IS_FAVOURITE,
RoomSummaryEntityFields.IS_LOW_PRIORITY,
RoomSummaryEntityFields.LAST_ACTIVITY_TIME),
arrayOf(Sort.DESCENDING, Sort.ASCENDING, Sort.DESCENDING))
}
RoomSortOrder.NONE -> {
}
}
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class AvatarRenderer @Inject constructor(private val activeSessionHolder: Active
.toBitmap(width = iconSize, height = iconSize))
}
}
.apply(RequestOptions.centerCropTransform())
.submit(iconSize, iconSize)
.get()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package im.vector.app.features.home

import android.content.Context
import android.content.pm.ShortcutInfo
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Build
import androidx.annotation.WorkerThread
import androidx.core.content.pm.ShortcutInfoCompat
Expand Down Expand Up @@ -45,7 +47,7 @@ class ShortcutCreator @Inject constructor(
private val adaptiveIconOuterSides = dimensionConverter.dpToPx(adaptiveIconOuterSidesDp)
private val iconSize by lazy {
if (useAdaptiveIcon) {
adaptiveIconSize - adaptiveIconOuterSides
adaptiveIconSize - (adaptiveIconOuterSides * 2)
} else {
dimensionConverter.dpToPx(72)
}
Expand All @@ -56,7 +58,7 @@ class ShortcutCreator @Inject constructor(
}

@WorkerThread
fun create(roomSummary: RoomSummary): ShortcutInfoCompat {
fun create(roomSummary: RoomSummary, rank: Int = 1): ShortcutInfoCompat {
val intent = RoomDetailActivity.shortcutIntent(context, roomSummary.roomId)
val bitmap = try {
avatarRenderer.shortcutDrawable(GlideApp.with(context), roomSummary.toMatrixItem(), iconSize)
Expand All @@ -67,16 +69,24 @@ class ShortcutCreator @Inject constructor(
.setShortLabel(roomSummary.displayName)
.setIcon(bitmap?.toProfileImageIcon())
.setIntent(intent)
.setLongLived(true)
.setRank(rank)

// Make it show up in the direct share menu
.setCategories(setOf(directShareCategory))
.setCategories(setOf(
directShareCategory,
ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION))

.build()
}

private fun Bitmap.toProfileImageIcon(): IconCompat {
return if (useAdaptiveIcon) {
IconCompat.createWithAdaptiveBitmap(this)
val insetBmp = Bitmap.createBitmap(adaptiveIconSize, adaptiveIconSize, Bitmap.Config.ARGB_8888)
val canvas = Canvas(insetBmp)
canvas.drawBitmap(this, adaptiveIconOuterSides.toFloat(), adaptiveIconOuterSides.toFloat(), null)

IconCompat.createWithAdaptiveBitmap(insetBmp)
} else {
IconCompat.createWithBitmap(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import androidx.core.content.pm.ShortcutManagerCompat
import im.vector.app.core.di.ActiveSessionHolder
import io.reactivex.disposables.Disposable
import io.reactivex.disposables.Disposables
import org.matrix.android.sdk.api.query.RoomTagQueryFilter
import org.matrix.android.sdk.api.session.room.RoomSortOrder
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
import org.matrix.android.sdk.rx.asObservable
Expand All @@ -46,17 +46,25 @@ class ShortcutsHandler @Inject constructor(
?.getPagedRoomSummariesLive(
roomSummaryQueryParams {
memberships = listOf(Membership.JOIN)
roomTagQueryFilter = RoomTagQueryFilter(isFavorite = true, null, null)
}
},
sortOrder = RoomSortOrder.PRIORITY_AND_ACTIVITY
)
?.asObservable()
?.subscribe { rooms ->
val shortcuts = rooms
.take(n = 4) // Android only allows us to create 4 shortcuts
.map { shortcutCreator.create(it) }
// Remove dead shortcuts (i.e. deleted rooms)
val roomIds = rooms.map { it.roomId }
val deadShortcutIds = ShortcutManagerCompat.getShortcuts(context, ShortcutManagerCompat.FLAG_MATCH_DYNAMIC)
.map { it.id }
.filter { !roomIds.contains(it) }
ShortcutManagerCompat.removeLongLivedShortcuts(context, deadShortcutIds)

val shortcuts = rooms.mapIndexed { index, room ->
shortcutCreator.create(room, index)
}

ShortcutManagerCompat.removeAllDynamicShortcuts(context)
ShortcutManagerCompat.addDynamicShortcuts(context, shortcuts)
shortcuts.forEach { shortcut ->
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
}
}
?: Disposables.empty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ class NotificationUtils @Inject constructor(private val context: Context,
// that can be displayed in not disturb mode if white listed (the later will need compat28.x)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)

// ID of the corresponding shortcut, for conversation features under API 30+
.setShortcutId(roomInfo.roomId)

// Title for API < 16 devices.
.setContentTitle(roomInfo.roomDisplayName)
// Content for API < 16 devices.
Expand Down