Fix vector drawables on Android < 7.0

This commit is contained in:
kitsunyan
2020-07-07 23:56:08 +03:00
parent 2cd9dd761d
commit cd4fca7147
5 changed files with 37 additions and 10 deletions
+1
View File
@@ -119,6 +119,7 @@ dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:' + versions.kotlin
implementation 'androidx.fragment:fragment:1.2.5'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'com.squareup.okhttp3:okhttp:4.7.2'
implementation 'io.reactivex.rxjava3:rxjava:3.0.4'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
@@ -36,7 +36,6 @@ import android.widget.ProgressBar
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import androidx.core.text.util.LinkifyCompat
import androidx.recyclerview.widget.RecyclerView
@@ -445,7 +444,7 @@ class ProductAdapter(private val callbacks: Callbacks, private val columns: Int)
itemView.layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
RecyclerView.LayoutParams.WRAP_CONTENT)
val placeholder = ContextCompat.getDrawable(image.context, R.drawable.ic_photo_camera)!!.mutate()
val placeholder = image.context.getDrawableCompat(R.drawable.ic_photo_camera).mutate()
placeholder.setTint(ColorUtils.blendARGB(primaryColor, accentColor, 0.5f)
.let { ColorUtils.blendARGB(0x00ffffff and it, it, 0.2f) })
this.placeholder = PaddingDrawable(placeholder, 2f)
@@ -1068,7 +1067,7 @@ class ProductAdapter(private val callbacks: Callbacks, private val columns: Int)
layoutParams.topMargin = if (position > 0 && items[position - 1] !is Item.LinkItem)
-context.resources.sizeScaled(8) else 0
holder.itemView.isEnabled = item.uri != null
holder.icon.setImageResource(item.iconResId)
holder.icon.setImageDrawable(holder.icon.context.getDrawableCompat(item.iconResId))
holder.text.text = item.getTitle(context)
holder.link.visibility = if (item.uri != null) View.VISIBLE else View.GONE
holder.link.text = item.displayLink
@@ -1088,7 +1087,7 @@ class ProductAdapter(private val callbacks: Callbacks, private val columns: Int)
}
} else {
null
} ?: ContextCompat.getDrawable(context, R.drawable.ic_perm_device_information))
} ?: context.getDrawableCompat(R.drawable.ic_perm_device_information))
val localCache = PackageItemResolver.LocalCache()
val labels = item.permissions.map { permission ->
val labelFromPackage = PackageItemResolver.loadLabel(context, localCache, permission)
@@ -8,7 +8,6 @@ import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
@@ -167,7 +166,7 @@ class ScreenshotsFragment(): DialogFragment() {
itemView.layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
RecyclerView.LayoutParams.MATCH_PARENT)
val placeholder = ContextCompat.getDrawable(itemView.context, R.drawable.ic_photo_camera)!!.mutate()
val placeholder = itemView.context.getDrawableCompat(R.drawable.ic_photo_camera).mutate()
placeholder.setTint(itemView.context.getColorFromAttr(android.R.attr.textColorPrimary).defaultColor
.let { ColorUtils.blendARGB(0x00ffffff and it, it, 0.25f) })
this.placeholder = PaddingDrawable(placeholder, 4f)
@@ -7,7 +7,6 @@ import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.os.LocaleList
import android.provider.Settings
import androidx.core.content.ContextCompat
import nya.kitsunyan.foxydroid.BuildConfig
import nya.kitsunyan.foxydroid.R
import nya.kitsunyan.foxydroid.utility.extension.android.*
@@ -20,7 +19,7 @@ import java.util.Locale
object Utils {
private fun createDefaultApplicationIcon(context: Context, tintAttrResId: Int): Drawable {
return ContextCompat.getDrawable(context, R.drawable.ic_application_default)!!.mutate()
return context.getDrawableCompat(R.drawable.ic_application_default).mutate()
.apply { setTintList(context.getColorFromAttr(tintAttrResId)) }
}
@@ -31,7 +30,7 @@ object Utils {
}
fun getToolbarIcon(context: Context, resId: Int): Drawable {
val drawable = ContextCompat.getDrawable(context, resId)!!.mutate()
val drawable = context.getDrawableCompat(resId).mutate()
drawable.setTintList(context.getColorFromAttr(android.R.attr.textColorPrimary))
return drawable
}
@@ -8,14 +8,18 @@ import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.TypedValue
import android.util.Xml
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
import com.squareup.picasso.Picasso
import com.squareup.picasso.RequestCreator
import nya.kitsunyan.foxydroid.utility.extension.android.*
import org.xmlpull.v1.XmlPullParser
import kotlin.math.*
object TypefaceExtra {
@@ -23,6 +27,31 @@ object TypefaceExtra {
val light = Typeface.create("sans-serif-light", Typeface.NORMAL)!!
}
fun Context.getDrawableCompat(resId: Int): Drawable {
val drawable = if (!Android.sdk(24)) {
val fileName = TypedValue().apply { resources.getValue(resId, this, true) }.string
if (fileName.endsWith(".xml")) {
resources.getXml(resId).use {
val eventType = generateSequence { it.next() }
.find { it == XmlPullParser.START_TAG || it == XmlPullParser.END_DOCUMENT }
if (eventType == XmlPullParser.START_TAG) {
when (it.name) {
"vector" -> VectorDrawableCompat.createFromXmlInner(resources, it, Xml.asAttributeSet(it), theme)
else -> null
}
} else {
null
}
}
} else {
null
}
} else {
null
}
return drawable ?: ContextCompat.getDrawable(this, resId)!!
}
fun Context.getColorFromAttr(attrResId: Int): ColorStateList {
val typedArray = obtainStyledAttributes(intArrayOf(attrResId))
val (colorStateList, resId) = try {
@@ -40,7 +69,7 @@ fun Context.getDrawableFromAttr(attrResId: Int): Drawable {
} finally {
typedArray.recycle()
}
return ContextCompat.getDrawable(this, resId)!!
return getDrawableCompat(resId)
}
fun Resources.sizeScaled(size: Int): Int {