Better draw factory implementation

This commit is contained in:
J-Jamet committed 2021-03-06 10:22:14 +01:00
1 parent 966df11beb
commit be7cd3275a
9 files changed
+118 -139

No files matched your search

@@ -47,7 +47,6 @@ import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.Entry
import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.education.EntryActivityEducation
import com.kunzisoft.keepass.icons.assignDatabaseIcon
import com.kunzisoft.keepass.magikeyboard.MagikIME
import com.kunzisoft.keepass.model.EntryAttachmentState
import com.kunzisoft.keepass.model.StreamDirection
@@ -242,7 +241,9 @@ class EntryActivity : LockingActivity() {
val entryInfo = entry.getEntryInfo(mDatabase)
// Assign title icon
titleIconView?.assignDatabaseIcon(mDatabase!!.iconDrawableFactory, entryInfo.icon, iconColor)
titleIconView?.let { iconView ->
mDatabase?.iconDrawableFactory?.assignDatabaseIcon(iconView, entryInfo.icon, iconColor)
}
// Assign title text
val entryTitle = entryInfo.title
@@ -60,7 +60,6 @@ import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.database.element.node.Type
import com.kunzisoft.keepass.database.search.SearchHelper
import com.kunzisoft.keepass.education.GroupActivityEducation
import com.kunzisoft.keepass.icons.assignDatabaseIcon
import com.kunzisoft.keepass.model.GroupInfo
import com.kunzisoft.keepass.model.RegisterInfo
import com.kunzisoft.keepass.model.SearchInfo
@@ -533,9 +532,10 @@ class GroupActivity : LockingActivity(),
} else {
// Assign the group icon depending of IconPack or custom icon
iconView?.visibility = View.VISIBLE
mCurrentGroup?.let {
if (mDatabase?.iconDrawableFactory != null)
iconView?.assignDatabaseIcon(mDatabase?.iconDrawableFactory!!, it.icon, mIconColor)
mCurrentGroup?.let { currentGroup ->
iconView?.let { imageView ->
mDatabase?.iconDrawableFactory?.assignDatabaseIcon(imageView, currentGroup.icon, mIconColor)
}
if (toolbar != null) {
if (mCurrentGroup?.containsParent() == true)
@@ -37,7 +37,6 @@ import com.kunzisoft.keepass.activities.dialogs.GroupEditDialogFragment.EditGrou
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.DateInstant
import com.kunzisoft.keepass.database.element.icon.IconImage
import com.kunzisoft.keepass.icons.assignDatabaseIcon
import com.kunzisoft.keepass.model.GroupInfo
import com.kunzisoft.keepass.view.ExpirationView
import org.joda.time.DateTime
@@ -206,9 +205,7 @@ class GroupEditDialogFragment : DialogFragment() {
}
private fun assignIconView() {
if (mDatabase?.iconDrawableFactory != null) {
iconButtonView.assignDatabaseIcon(mDatabase?.iconDrawableFactory!!, mGroupInfo.icon, iconColor)
}
mDatabase?.iconDrawableFactory?.assignDatabaseIcon(iconButtonView, mGroupInfo.icon, iconColor)
}
fun setIcon(icon: IconImage) {
@@ -45,7 +45,6 @@ import com.kunzisoft.keepass.database.element.DateInstant
import com.kunzisoft.keepass.database.element.icon.IconImage
import com.kunzisoft.keepass.education.EntryEditActivityEducation
import com.kunzisoft.keepass.icons.IconDrawableFactory
import com.kunzisoft.keepass.icons.assignDatabaseIcon
import com.kunzisoft.keepass.model.*
import com.kunzisoft.keepass.otp.OtpEntryFields
import com.kunzisoft.keepass.settings.PreferencesUtil
@@ -240,9 +239,7 @@ class EntryEditFragment: StylishFragment() {
}
set(value) {
mEntryInfo.icon = value
drawFactory?.let { drawFactory ->
entryIconView.assignDatabaseIcon(drawFactory, value, iconColor)
}
drawFactory?.assignDatabaseIcon(entryIconView, value, iconColor)
}
var username: String
@@ -9,7 +9,6 @@ import androidx.recyclerview.widget.RecyclerView
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.icon.IconImageDraw
import com.kunzisoft.keepass.icons.IconDrawableFactory
import com.kunzisoft.keepass.icons.assignDatabaseIcon
class IconAdapter<I: IconImageDraw>(val context: Context, val tintIcon: Int)
: RecyclerView.Adapter<IconAdapter<I>.CustomIconViewHolder>() {
@@ -46,9 +45,7 @@ class IconAdapter<I: IconImageDraw>(val context: Context, val tintIcon: Int)
override fun onBindViewHolder(holder: CustomIconViewHolder, position: Int) {
val icon = iconList[position]
iconDrawableFactory?.let {
holder.iconImageView.assignDatabaseIcon(it, icon, tintIcon)
}
iconDrawableFactory?.assignDatabaseIcon(holder.iconImageView, icon, tintIcon)
holder.itemView.setOnClickListener { iconPickerListener?.iconPicked(icon) }
}
@@ -40,7 +40,6 @@ import com.kunzisoft.keepass.database.element.SortNodeEnum
import com.kunzisoft.keepass.database.element.node.Node
import com.kunzisoft.keepass.database.element.node.NodeVersionedInterface
import com.kunzisoft.keepass.database.element.node.Type
import com.kunzisoft.keepass.icons.assignDatabaseIcon
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.view.setTextSize
import com.kunzisoft.keepass.view.strikeOut
@@ -304,7 +303,7 @@ class NodeAdapter (private val context: Context)
}
holder.imageIdentifier?.setColorFilter(iconColor)
holder.icon.apply {
assignDatabaseIcon(mDatabase.iconDrawableFactory, subNode.icon, iconColor)
mDatabase.iconDrawableFactory.assignDatabaseIcon(this, subNode.icon, iconColor)
// Relative size of the icon
layoutParams?.apply {
height = (mIconDefaultDimension * mPrefSizeMultiplier).toInt()
@@ -36,7 +36,6 @@ import com.kunzisoft.keepass.database.element.Group
import com.kunzisoft.keepass.database.element.database.DatabaseKDB
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
import com.kunzisoft.keepass.database.search.SearchHelper
import com.kunzisoft.keepass.icons.assignDatabaseIcon
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.view.strikeOut
@@ -81,10 +80,9 @@ class SearchEntryCursorAdapter(private val context: Context,
val viewHolder = view.tag as ViewHolder
// Assign image
viewHolder.imageViewIcon?.assignDatabaseIcon(
database.iconDrawableFactory,
currentEntry.icon,
iconColor)
viewHolder.imageViewIcon?.let { iconView ->
database.iconDrawableFactory.assignDatabaseIcon(iconView, currentEntry.icon, iconColor)
}
// Assign title
viewHolder.textViewTitle?.apply {
@@ -46,8 +46,6 @@ import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper
import com.kunzisoft.keepass.activities.helpers.SpecialMode
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.icon.IconImage
import com.kunzisoft.keepass.icons.assignDatabaseIcon
import com.kunzisoft.keepass.icons.createIconFromDatabaseIcon
import com.kunzisoft.keepass.model.EntryInfo
import com.kunzisoft.keepass.model.SearchInfo
import com.kunzisoft.keepass.settings.AutofillSettingsActivity
@@ -274,9 +272,9 @@ object AutofillHelper {
val presentation = RemoteViews(context.packageName, R.layout.item_autofill_entry)
presentation.setTextViewText(R.id.autofill_entry_text, remoteViewsText)
if (remoteViewsIcon != null) {
presentation.assignDatabaseIcon(context,
Database.getInstance().iconDrawableFactory.assignDatabaseIcon(context,
presentation,
R.id.autofill_entry_icon,
Database.getInstance().iconDrawableFactory,
remoteViewsIcon,
ContextCompat.getColor(context, R.color.green))
}
@@ -284,8 +282,7 @@ object AutofillHelper {
}
private fun buildIconFromEntry(context: Context, entryInfo: EntryInfo): Icon? {
return createIconFromDatabaseIcon(context,
Database.getInstance().iconDrawableFactory,
return Database.getInstance().iconDrawableFactory.createIconFromDatabaseIcon(context,
entryInfo.icon,
ContextCompat.getColor(context, R.color.green))
}
@@ -67,7 +67,7 @@ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedK
/**
* Utility method to assign a drawable to an ImageView and tint it
*/
fun assignDrawableToImageView(superDrawable: SuperDrawable, imageView: ImageView?, tint: Boolean, tintColor: Int) {
private fun assignDrawableToImageView(superDrawable: SuperDrawable, imageView: ImageView?, tint: Boolean, tintColor: Int) {
if (imageView != null) {
imageView.setImageDrawable(superDrawable.drawable)
if (superDrawable.tintable && tint) {
@@ -81,7 +81,7 @@ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedK
/**
* Utility method to assign a drawable to a RemoteView and tint it
*/
fun assignDrawableToRemoteViews(superDrawable: SuperDrawable,
private fun assignDrawableToRemoteViews(superDrawable: SuperDrawable,
remoteViews: RemoteViews,
imageId: Int,
tintColor: Int = Color.BLACK) {
@@ -99,7 +99,7 @@ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedK
* Utility method to assign a drawable to a icon and tint it
*/
@RequiresApi(Build.VERSION_CODES.M)
fun assignDrawableToIcon(superDrawable: SuperDrawable,
private fun assignDrawableToIcon(superDrawable: SuperDrawable,
tintColor: Int = Color.BLACK): Icon {
val bitmap = superDrawable.drawable.toBitmap()
// Tint bitmap if it's not a custom icon
@@ -114,7 +114,7 @@ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedK
/**
* Get the [SuperDrawable] [iconDraw] (from cache, or build it and add it to the cache if not exists yet), then [tint] it with [tintColor] if needed
*/
fun getIconSuperDrawable(context: Context, iconDraw: IconImageDraw, width: Int, tint: Boolean = false, tintColor: Int = Color.WHITE): SuperDrawable {
private fun getIconSuperDrawable(context: Context, iconDraw: IconImageDraw, width: Int, tint: Boolean = false, tintColor: Int = Color.WHITE): SuperDrawable {
val icon = iconDraw.getIconImageToDraw()
return if (!icon.custom.isUnknown) {
SuperDrawable(getIconDrawable(context.resources, icon.custom))
@@ -128,39 +128,10 @@ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedK
}
}
/**
* Key class to retrieve a Drawable in the cache if it's tinted or not
*/
private inner class CacheKey(var resId: Int, var density: Int, var isTint: Boolean, var color: Int) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || javaClass != other.javaClass) return false
val cacheKey = other as CacheKey
return if (isTint)
resId == cacheKey.resId
&& density == cacheKey.density
&& cacheKey.isTint
&& color == cacheKey.color
else
resId == cacheKey.resId
&& density == cacheKey.density
&& !cacheKey.isTint
}
override fun hashCode(): Int {
var result = resId
result = 31 * result + density
result = 31 * result + isTint.hashCode()
result = 31 * result + color
return result
}
}
/**
* Simple method to init the cache with the custom icon and be much faster next time
*/
fun addToCache(resources: Resources, iconDraw: IconImageDraw) {
private fun addToCache(resources: Resources, iconDraw: IconImageDraw) {
val icon = iconDraw.getIconImageToDraw()
if (icon.custom.binaryFile.length > 0
&& !customIconMap.containsKey(icon.custom.uuid))
@@ -242,6 +213,73 @@ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedK
customIconMap.clear()
}
/**
* Assign a database [icon] to an ImageView and tint it with [tintColor] if needed
*/
fun assignDatabaseIcon(imageView: ImageView,
icon: IconImageDraw,
tintColor: Int = Color.WHITE) {
try {
val context = imageView.context
CoroutineScope(Dispatchers.IO).launch {
addToCache(context.resources, icon)
withContext(Dispatchers.Main) {
IconPackChooser.getSelectedIconPack(context)?.let { selectedIconPack ->
assignDrawableToImageView(
getIconSuperDrawable(context,
icon,
imageView.width,
true,
tintColor),
imageView,
selectedIconPack.tintable(),
tintColor)
}
}
}
} catch (e: Exception) {
Log.e(ImageView::class.java.name, "Unable to assign icon in image view", e)
}
}
fun assignDatabaseIcon(context: Context,
remoteViews: RemoteViews,
imageId: Int,
icon: IconImageDraw,
tintColor: Int = Color.BLACK) {
try {
assignDrawableToRemoteViews(
getIconSuperDrawable(context,
icon,
24,
true,
tintColor),
remoteViews,
imageId,
tintColor)
} catch (e: Exception) {
Log.e(RemoteViews::class.java.name, "Unable to assign icon in remote view", e)
}
}
@RequiresApi(Build.VERSION_CODES.M)
fun createIconFromDatabaseIcon(context: Context,
icon: IconImageDraw,
tintColor: Int = Color.BLACK): Icon? {
try {
return assignDrawableToIcon(
getIconSuperDrawable(context,
icon,
24,
true,
tintColor),
tintColor)
} catch (e: Exception) {
Log.e(RemoteViews::class.java.name, "Unable to assign icon in remote view", e)
}
return null
}
/**
* Build a blankDrawable drawable
* @param res Resource to build the drawable
@@ -264,83 +302,38 @@ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedK
*/
class SuperDrawable(var drawable: Drawable, var tintable: Boolean = false)
/**
* Key class to retrieve a Drawable in the cache if it's tinted or not
*/
private inner class CacheKey(var resId: Int, var density: Int, var isTint: Boolean, var color: Int) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || javaClass != other.javaClass) return false
val cacheKey = other as CacheKey
return if (isTint)
resId == cacheKey.resId
&& density == cacheKey.density
&& cacheKey.isTint
&& color == cacheKey.color
else
resId == cacheKey.resId
&& density == cacheKey.density
&& !cacheKey.isTint
}
override fun hashCode(): Int {
var result = resId
result = 31 * result + density
result = 31 * result + isTint.hashCode()
result = 31 * result + color
return result
}
}
companion object {
private val TAG = IconDrawableFactory::class.java.name
}
}
/**
* Assign a database [icon] to an ImageView and tint it with [tintColor] if needed
*/
fun ImageView.assignDatabaseIcon(iconFactory: IconDrawableFactory,
icon: IconImageDraw,
tintColor: Int = Color.WHITE) {
try {
val thisView = this
CoroutineScope(Dispatchers.IO).launch {
iconFactory.addToCache(resources, icon)
withContext(Dispatchers.Main) {
IconPackChooser.getSelectedIconPack(context)?.let { selectedIconPack ->
iconFactory.assignDrawableToImageView(
iconFactory.getIconSuperDrawable(context,
icon,
width,
true,
tintColor),
thisView,
selectedIconPack.tintable(),
tintColor)
}
}
}
} catch (e: Exception) {
Log.e(ImageView::class.java.name, "Unable to assign icon in image view", e)
}
}
fun RemoteViews.assignDatabaseIcon(context: Context,
imageId: Int,
iconFactory: IconDrawableFactory,
icon: IconImageDraw,
tintColor: Int = Color.BLACK) {
try {
val thisView = this
CoroutineScope(Dispatchers.IO).launch {
iconFactory.addToCache(context.resources, icon)
withContext(Dispatchers.Main) {
iconFactory.assignDrawableToRemoteViews(
iconFactory.getIconSuperDrawable(context,
icon,
24,
true,
tintColor),
thisView,
imageId,
tintColor)
}
}
} catch (e: Exception) {
Log.e(RemoteViews::class.java.name, "Unable to assign icon in remote view", e)
}
}
@RequiresApi(Build.VERSION_CODES.M)
fun createIconFromDatabaseIcon(context: Context,
iconFactory: IconDrawableFactory,
icon: IconImageDraw,
tintColor: Int = Color.BLACK): Icon? {
try {
return iconFactory.assignDrawableToIcon(
iconFactory.getIconSuperDrawable(context,
icon,
24,
true,
tintColor),
tintColor)
} catch (e: Exception) {
Log.e(RemoteViews::class.java.name, "Unable to assign icon in remote view", e)
}
return null
}