From 7d34e0a940b4638892e828ccb0061cf5d56295b9 Mon Sep 17 00:00:00 2001 From: Thomas Wirth Date: Thu, 1 Jul 2021 14:37:12 +0200 Subject: [PATCH] Set torch button background programmatically to fix issues on api 21 devices * switch button background to vector drawable --- .../config/detekt/baseline-bundledDebug.xml | 1 + .../config/detekt/baseline-unbundledDebug.xml | 1 + .../io/github/g00fy2/quickie/QROverlayView.kt | 34 ++++++++++++++++--- .../color/quickie_bg_torch_color_selector.xml | 7 ---- .../main/res/drawable/quickie_bg_torch.xml | 20 +++++------ .../main/res/layout/quickie_overlay_view.xml | 4 +-- quickie/src/main/res/values/colors.xml | 5 ++- 7 files changed, 45 insertions(+), 27 deletions(-) delete mode 100644 quickie/src/main/res/color/quickie_bg_torch_color_selector.xml diff --git a/quickie/config/detekt/baseline-bundledDebug.xml b/quickie/config/detekt/baseline-bundledDebug.xml index d098e021..c4d0769b 100644 --- a/quickie/config/detekt/baseline-bundledDebug.xml +++ b/quickie/config/detekt/baseline-bundledDebug.xml @@ -7,5 +7,6 @@ UnsafeCallOnNullableType:QRCodeAnalyzer.kt$QRCodeAnalyzer$image!! UnsafeCallOnNullableType:QROverlayView.kt$QROverlayView$maskBitmap!! UnsafeCallOnNullableType:QROverlayView.kt$QROverlayView$maskCanvas!! + TooManyFunctions:QROverlayView.kt$QROverlayView : FrameLayout diff --git a/quickie/config/detekt/baseline-unbundledDebug.xml b/quickie/config/detekt/baseline-unbundledDebug.xml index 64e61f83..74a14beb 100644 --- a/quickie/config/detekt/baseline-unbundledDebug.xml +++ b/quickie/config/detekt/baseline-unbundledDebug.xml @@ -6,5 +6,6 @@ UnsafeCallOnNullableType:QRCodeAnalyzer.kt$QRCodeAnalyzer$image!! UnsafeCallOnNullableType:QROverlayView.kt$QROverlayView$maskBitmap!! UnsafeCallOnNullableType:QROverlayView.kt$QROverlayView$maskCanvas!! + TooManyFunctions:QROverlayView.kt$QROverlayView : FrameLayout diff --git a/quickie/src/main/kotlin/io/github/g00fy2/quickie/QROverlayView.kt b/quickie/src/main/kotlin/io/github/g00fy2/quickie/QROverlayView.kt index 26780f37..116aa0c9 100644 --- a/quickie/src/main/kotlin/io/github/g00fy2/quickie/QROverlayView.kt +++ b/quickie/src/main/kotlin/io/github/g00fy2/quickie/QROverlayView.kt @@ -1,6 +1,7 @@ package io.github.g00fy2.quickie import android.content.Context +import android.content.res.ColorStateList import android.content.res.Resources.NotFoundException import android.graphics.Bitmap import android.graphics.Canvas @@ -18,6 +19,7 @@ import android.widget.FrameLayout import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.ColorUtils +import androidx.core.graphics.drawable.DrawableCompat import io.github.g00fy2.quickie.databinding.QuickieOverlayViewBinding import kotlin.math.min import kotlin.math.roundToInt @@ -29,8 +31,8 @@ internal class QROverlayView @JvmOverloads constructor( ) : FrameLayout(context, attrs, defStyleAttr) { private val binding = QuickieOverlayViewBinding.inflate(LayoutInflater.from(context), this) - private val strokeColor = ContextCompat.getColor(context, R.color.quickie_stroke) - private val highlightedStrokeColor = getAccentColor() + private val grayColor = ContextCompat.getColor(context, R.color.quickie_gray) + private val accentColor = getAccentColor() private val backgroundColor = ColorUtils.setAlphaComponent(Color.BLACK, BACKGROUND_ALPHA.roundToInt()) private val alphaPaint = Paint().apply { alpha = BACKGROUND_ALPHA.roundToInt() } private val strokePaint = Paint(Paint.ANTI_ALIAS_FLAG) @@ -74,7 +76,7 @@ internal class QROverlayView @JvmOverloads constructor( } override fun onDraw(canvas: Canvas) { - strokePaint.color = if (isHighlighted) highlightedStrokeColor else strokeColor + strokePaint.color = if (isHighlighted) accentColor else grayColor maskCanvas!!.drawColor(backgroundColor) maskCanvas!!.drawRoundRect(outerFrame, outerRadius, outerRadius, strokePaint) maskCanvas!!.drawRoundRect(innerFrame, innerRadius, innerRadius, transparentPaint) @@ -105,6 +107,7 @@ internal class QROverlayView @JvmOverloads constructor( fun setTorchVisibilityAndOnClick(visible: Boolean, action: (Boolean) -> Unit = {}) { binding.torchImageView.visibility = if (visible) View.VISIBLE else View.GONE binding.torchImageView.setOnClickListener { action(!it.isSelected) } + if (visible) binding.torchImageView.setTintAndStateAwareBackground() } fun setTorchState(on: Boolean) { @@ -140,7 +143,11 @@ internal class QROverlayView @JvmOverloads constructor( private fun getAccentColor(): Int { return TypedValue().let { - if (context.theme.resolveAttribute(android.R.attr.colorAccent, it, true)) it.data else Color.WHITE + if (context.theme.resolveAttribute(android.R.attr.colorAccent, it, true)) { + it.data + } else { + ContextCompat.getColor(context, R.color.quickie_accent_fallback) + } } } @@ -161,10 +168,29 @@ internal class QROverlayView @JvmOverloads constructor( return this } + private fun View.setTintAndStateAwareBackground() { + background?.let { drawable -> + val wrappedDrawable = DrawableCompat.wrap(drawable) + + val states = arrayOf( + intArrayOf(android.R.attr.state_pressed, android.R.attr.state_selected), + intArrayOf(android.R.attr.state_pressed, -android.R.attr.state_selected), + intArrayOf(-android.R.attr.state_pressed, android.R.attr.state_selected), + intArrayOf() + ) + val stateColors = intArrayOf(grayColor, accentColor, accentColor, grayColor) + val colotStateList = ColorStateList(states, stateColors).withAlpha(BUTTON_BACKGROUND_ALPHA.roundToInt()) + + DrawableCompat.setTintList(wrappedDrawable, colotStateList) + background = wrappedDrawable + } + } + private fun Float.toPx() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, resources.displayMetrics) companion object { private const val BACKGROUND_ALPHA = 0.77 * 255 + private const val BUTTON_BACKGROUND_ALPHA = 0.6 * 255 private const val STROKE_WIDTH = 4f private const val OUT_RADIUS = 16f private const val FRAME_MARGIN_RATIO = 1f / 4 diff --git a/quickie/src/main/res/color/quickie_bg_torch_color_selector.xml b/quickie/src/main/res/color/quickie_bg_torch_color_selector.xml deleted file mode 100644 index e8a84e73..00000000 --- a/quickie/src/main/res/color/quickie_bg_torch_color_selector.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/quickie/src/main/res/drawable/quickie_bg_torch.xml b/quickie/src/main/res/drawable/quickie_bg_torch.xml index 412c6ded..bdb88341 100644 --- a/quickie/src/main/res/drawable/quickie_bg_torch.xml +++ b/quickie/src/main/res/drawable/quickie_bg_torch.xml @@ -1,12 +1,10 @@ - - - - - - - \ No newline at end of file + + + \ No newline at end of file diff --git a/quickie/src/main/res/layout/quickie_overlay_view.xml b/quickie/src/main/res/layout/quickie_overlay_view.xml index 15b48eed..344f574f 100644 --- a/quickie/src/main/res/layout/quickie_overlay_view.xml +++ b/quickie/src/main/res/layout/quickie_overlay_view.xml @@ -54,7 +54,7 @@ android:layout_height="48dp" android:layout_gravity="center" android:indeterminate="true" - android:indeterminateTint="@color/quickie_stroke" + android:indeterminateTint="@color/quickie_gray" /> diff --git a/quickie/src/main/res/values/colors.xml b/quickie/src/main/res/values/colors.xml index 6f060248..68d924b6 100644 --- a/quickie/src/main/res/values/colors.xml +++ b/quickie/src/main/res/values/colors.xml @@ -1,8 +1,7 @@ - #dadce0 + #dadce0 #ffffff #00000000 - ?attr/colorAccent - @color/quickie_stroke + #03dac5 \ No newline at end of file