@@ -10,6 +10,7 @@ package com.nextcloud.ui.fileactions
1010import android.content.Context
1111import android.content.Intent
1212import android.graphics.Canvas
13+ import android.graphics.drawable.Drawable
1314import android.graphics.drawable.PictureDrawable
1415import android.os.Bundle
1516import android.view.LayoutInflater
@@ -81,48 +82,59 @@ class ClientIntegration(
8182 }
8283 text.text = endpoint.name
8384
84- if (endpoint.icon != null ) {
85- sheet.lifecycleScope.launch(Dispatchers .IO ) {
86- val client = OwnCloudClientManagerFactory .getDefaultSingleton()
87- .getNextcloudClientFor(user.toOwnCloudAccount(), context)
85+ sheet.lifecycleScope.launch(Dispatchers .IO ) {
86+ val client = OwnCloudClientManagerFactory .getDefaultSingleton()
87+ .getNextcloudClientFor(user.toOwnCloudAccount(), context)
8888
89- val drawable =
90- GlideHelper .getDrawable(context, client, client.baseUri.toString() + endpoint.icon)
91- ?.mutate()
92-
93- val px = DisplayUtils .convertDpToPixel(
94- context.resources.getDimension(R .dimen.iconized_single_line_item_icon_size),
95- context
96- )
97- val returnedBitmap =
98- createBitmap(drawable?.intrinsicWidth ? : px, drawable?.intrinsicHeight ? : px)
99-
100- val canvas = Canvas (returnedBitmap)
101- canvas.drawPicture((drawable as PictureDrawable ).picture)
102-
103- val d = returnedBitmap.toDrawable(context.resources)
89+ val rawDrawable = if (endpoint.icon != null ) {
90+ GlideHelper .getDrawable(context, client, client.baseUri.toString() + endpoint.icon)?.mutate()
91+ } else {
92+ null
93+ }
10494
105- val tintedDrawable = viewThemeUtils.platform.tintDrawable(
106- context,
107- d
108- )
95+ val tintableDrawable = prepareDrawableForTinting(rawDrawable) ? : getDefaultIconDrawable()
10996
110- withContext(Dispatchers .Main ) {
111- icon.setImageDrawable(tintedDrawable)
97+ withContext(Dispatchers .Main ) {
98+ tintableDrawable?.let {
99+ val tinted = viewThemeUtils.platform.tintDrawable(context, it)
100+ icon.setImageDrawable(tinted)
112101 }
113102 }
114- } else {
115- val tintedDrawable = viewThemeUtils.platform.tintDrawable(
116- context,
117- AppCompatResources .getDrawable(context, R .drawable.ic_activity)!!
118- )
119-
120- icon.setImageDrawable(tintedDrawable)
121103 }
122104 }
105+
123106 return itemBinding.root
124107 }
125108
109+ private fun prepareDrawableForTinting (drawable : Drawable ? ): Drawable ? {
110+ if (drawable == null ) {
111+ return null
112+ }
113+
114+ if (drawable is PictureDrawable ) {
115+ val defaultSize = DisplayUtils .convertDpToPixel(
116+ context.resources.getDimension(R .dimen.iconized_single_line_item_icon_size),
117+ context
118+ ).toInt().coerceAtLeast(1 )
119+
120+ val width = if (drawable.intrinsicWidth > 0 ) drawable.intrinsicWidth else defaultSize
121+ val height = if (drawable.intrinsicHeight > 0 ) drawable.intrinsicHeight else defaultSize
122+
123+ val safeWidth = width.coerceAtLeast(1 )
124+ val safeHeight = height.coerceAtLeast(1 )
125+
126+ val bitmap = createBitmap(safeWidth, safeHeight)
127+ val canvas = Canvas (bitmap)
128+ canvas.drawPicture(drawable.picture)
129+
130+ return bitmap.toDrawable(context.resources)
131+ }
132+
133+ return drawable
134+ }
135+
136+ private fun getDefaultIconDrawable (): Drawable ? = AppCompatResources .getDrawable(context, R .drawable.ic_activity)
137+
126138 private fun requestClientIntegration (endpoint : Endpoint , fileId : String , filePath : String ) {
127139 sheet.lifecycleScope.launch(Dispatchers .IO ) {
128140 val client = OwnCloudClientManagerFactory .getDefaultSingleton()
0 commit comments