never create more than the allowed number of shortcuts

This commit is contained in:
Conny Duck 2024-04-22 19:40:31 +02:00
parent 7960db6c78
commit 1099246cdc
No known key found for this signature in database
2 changed files with 58 additions and 50 deletions

View File

@ -1066,7 +1066,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
} }
updateProfiles() updateProfiles()
shareShortcutHelper.updateShortcut(accountManager.activeAccount!!) shareShortcutHelper.updateShortcuts()
} }
@SuppressLint("CheckResult") @SuppressLint("CheckResult")

View File

@ -30,70 +30,78 @@ import com.bumptech.glide.Glide
import com.keylesspalace.tusky.MainActivity import com.keylesspalace.tusky.MainActivity
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.di.ApplicationScope import com.keylesspalace.tusky.di.ApplicationScope
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class ShareShortcutHelper @Inject constructor( class ShareShortcutHelper @Inject constructor(
private val context: Context, private val context: Context,
private val accountManager: AccountManager,
@ApplicationScope private val externalScope: CoroutineScope @ApplicationScope private val externalScope: CoroutineScope
) { ) {
fun updateShortcut(account: AccountEntity) { fun updateShortcuts() {
externalScope.launch { externalScope.launch(Dispatchers.IO) {
val innerSize = context.resources.getDimensionPixelSize(R.dimen.adaptive_bitmap_inner_size) val innerSize = context.resources.getDimensionPixelSize(R.dimen.adaptive_bitmap_inner_size)
val outerSize = context.resources.getDimensionPixelSize(R.dimen.adaptive_bitmap_outer_size) val outerSize = context.resources.getDimensionPixelSize(R.dimen.adaptive_bitmap_outer_size)
val bmp = if (TextUtils.isEmpty(account.profilePictureUrl)) { val maxNumberOfShortcuts = ShortcutManagerCompat.getMaxShortcutCountPerActivity(context)
Glide.with(context)
.asBitmap() val shortcuts = accountManager.accounts.take(maxNumberOfShortcuts).map { account ->
.load(R.drawable.avatar_default)
.submitAsync(innerSize, innerSize) val bmp = if (TextUtils.isEmpty(account.profilePictureUrl)) {
} else { Glide.with(context)
Glide.with(context) .asBitmap()
.asBitmap() .load(R.drawable.avatar_default)
.load(account.profilePictureUrl) .submitAsync(innerSize, innerSize)
.error(R.drawable.avatar_default) } else {
.submitAsync(innerSize, innerSize) Glide.with(context)
.asBitmap()
.load(account.profilePictureUrl)
.error(R.drawable.avatar_default)
.submitAsync(innerSize, innerSize)
}
// inset the loaded bitmap inside a 108dp transparent canvas so it looks good as adaptive icon
val outBmp = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARGB_8888)
val canvas = Canvas(outBmp)
canvas.drawBitmap(
bmp,
(outerSize - innerSize).toFloat() / 2f,
(outerSize - innerSize).toFloat() / 2f,
null
)
val icon = IconCompat.createWithAdaptiveBitmap(outBmp)
val person = Person.Builder()
.setIcon(icon)
.setName(account.displayName)
.setKey(account.identifier)
.build()
// This intent will be sent when the user clicks on one of the launcher shortcuts. Intent from share sheet will be different
val intent = Intent(context, MainActivity::class.java).apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(ShortcutManagerCompat.EXTRA_SHORTCUT_ID, account.id.toString())
}
ShortcutInfoCompat.Builder(context, account.id.toString())
.setIntent(intent)
.setCategories(setOf("com.keylesspalace.tusky.Share"))
.setShortLabel(account.displayName)
.setPerson(person)
.setLongLived(true)
.setIcon(icon)
.build()
} }
// inset the loaded bitmap inside a 108dp transparent canvas so it looks good as adaptive icon ShortcutManagerCompat.addDynamicShortcuts(context, shortcuts)
val outBmp = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARGB_8888)
val canvas = Canvas(outBmp)
canvas.drawBitmap(
bmp,
(outerSize - innerSize).toFloat() / 2f,
(outerSize - innerSize).toFloat() / 2f,
null
)
val icon = IconCompat.createWithAdaptiveBitmap(outBmp)
val person = Person.Builder()
.setIcon(icon)
.setName(account.displayName)
.setKey(account.identifier)
.build()
// This intent will be sent when the user clicks on one of the launcher shortcuts. Intent from share sheet will be different
val intent = Intent(context, MainActivity::class.java).apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(ShortcutManagerCompat.EXTRA_SHORTCUT_ID, account.id.toString())
}
val shortcutInfo = ShortcutInfoCompat.Builder(context, account.id.toString())
.setIntent(intent)
.setCategories(setOf("com.keylesspalace.tusky.Share"))
.setShortLabel(account.displayName)
.setPerson(person)
.setLongLived(true)
.setIcon(icon)
.build()
ShortcutManagerCompat.addDynamicShortcuts(context, listOf(shortcutInfo))
} }
} }