Skip to content

Commit

Permalink
Set torch button background programmatically to fix issues on api 21 …
Browse files Browse the repository at this point in the history
…devices

* switch button background to vector drawable
  • Loading branch information
G00fY2 committed Jul 1, 2021
1 parent 186eabd commit 7d34e0a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 27 deletions.
1 change: 1 addition & 0 deletions quickie/config/detekt/baseline-bundledDebug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<ID>UnsafeCallOnNullableType:QRCodeAnalyzer.kt$QRCodeAnalyzer$image!!</ID>
<ID>UnsafeCallOnNullableType:QROverlayView.kt$QROverlayView$maskBitmap!!</ID>
<ID>UnsafeCallOnNullableType:QROverlayView.kt$QROverlayView$maskCanvas!!</ID>
<ID>TooManyFunctions:QROverlayView.kt$QROverlayView : FrameLayout</ID>
</CurrentIssues>
</SmellBaseline>
1 change: 1 addition & 0 deletions quickie/config/detekt/baseline-unbundledDebug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<ID>UnsafeCallOnNullableType:QRCodeAnalyzer.kt$QRCodeAnalyzer$image!!</ID>
<ID>UnsafeCallOnNullableType:QROverlayView.kt$QROverlayView$maskBitmap!!</ID>
<ID>UnsafeCallOnNullableType:QROverlayView.kt$QROverlayView$maskCanvas!!</ID>
<ID>TooManyFunctions:QROverlayView.kt$QROverlayView : FrameLayout</ID>
</CurrentIssues>
</SmellBaseline>
34 changes: 30 additions & 4 deletions quickie/src/main/kotlin/io/github/g00fy2/quickie/QROverlayView.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}

Expand All @@ -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
Expand Down

This file was deleted.

20 changes: 9 additions & 11 deletions quickie/src/main/res/drawable/quickie_bg_torch.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp">
<shape android:shape="oval">
<solid android:color="@color/quickie_bg_torch_color_selector"/>
</shape>
</item>
</layer-list>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="48dp"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="48dp">
<path
android:fillColor="@color/quickie_gray"
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0"/>
</vector>
4 changes: 2 additions & 2 deletions quickie/src/main/res/layout/quickie_overlay_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>

<androidx.appcompat.widget.AppCompatTextView
Expand All @@ -68,7 +68,7 @@
android:paddingStart="24dp"
android:paddingTop="4dp"
android:text="@string/quickie_please_wait"
android:textColor="@color/quickie_stroke"
android:textColor="@color/quickie_gray"
style="@style/TextAppearance.AppCompat.Subhead"
/>
</LinearLayout>
Expand Down
5 changes: 2 additions & 3 deletions quickie/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="quickie_stroke">#dadce0</color>
<color name="quickie_gray">#dadce0</color>
<color name="quickie_white">#ffffff</color>
<color name="quickie_transparent">#00000000</color>
<color name="quickie_torch_selected">?attr/colorAccent</color>
<color name="quickie_torch_unselected">@color/quickie_stroke</color>
<color name="quickie_accent_fallback">#03dac5</color>
</resources>

0 comments on commit 7d34e0a

Please sign in to comment.