Skip to content

Commit d4838fb

Browse files
committed
better captcha.
1 parent 8995859 commit d4838fb

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

tv/src/main/java/com/windscribe/tv/welcome/fragment/CaptchaFragment.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,38 @@ class CaptchaFragment : Fragment(), WelcomeActivityCallback {
5454
private fun createAsciiArtBitmap(text: String): Bitmap {
5555
val paint = Paint().apply {
5656
typeface = Typeface.MONOSPACE
57-
textSize = 24f
58-
color = Color.GREEN
57+
textSize = 16f
58+
color = Color.WHITE
5959
isAntiAlias = false
60+
letterSpacing = 0.05f
6061
}
62+
val lines = text.split("\n").filter { it.isNotEmpty() }
63+
val testRect = android.graphics.Rect()
64+
paint.getTextBounds("", 0, 1, testRect)
65+
val charWidth = testRect.width().toFloat()
66+
val charHeight = testRect.height().toFloat()
6167

62-
val lines = text.split("\n")
63-
val maxWidth = lines.maxOfOrNull { paint.measureText(it) }?.toInt() ?: 0
64-
val lineHeight = paint.fontMetrics.let { it.bottom - it.top }
65-
val totalHeight = (lineHeight * lines.size).toInt()
68+
val maxLineLength = lines.maxOfOrNull { it.length } ?: 0
69+
val calculatedWidth = (charWidth * maxLineLength * 1.1f).toInt() // Add 10% buffer
6670

71+
// Use actual character height for line spacing
72+
val lineSpacing = charHeight * 1.2f // 20% extra for line spacing
73+
val totalHeight = (lineSpacing * lines.size).toInt()
74+
75+
// Generous padding for TV display
76+
val padding = 80
6777
val bitmap = Bitmap.createBitmap(
68-
maxWidth + 20,
69-
totalHeight + 20,
78+
calculatedWidth + padding,
79+
totalHeight + padding,
7080
Bitmap.Config.ARGB_8888
7181
)
72-
7382
val canvas = Canvas(bitmap)
7483
canvas.drawColor(Color.BLACK)
75-
76-
var y = -paint.fontMetrics.top + 10
84+
var y = charHeight + (padding / 2)
7785
for (line in lines) {
78-
canvas.drawText(line, 10f, y, paint)
79-
y += lineHeight
86+
canvas.drawText(line, (padding / 2).toFloat(), y, paint)
87+
y += lineSpacing
8088
}
81-
8289
return bitmap
8390
}
8491

tv/src/main/res/layout/fragment_captcha.xml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,14 @@
2929
app:layout_constraintStart_toStartOf="parent"
3030
app:layout_constraintTop_toTopOf="parent" />
3131

32-
<HorizontalScrollView
33-
android:layout_width="match_parent"
34-
android:layout_height="wrap_content"
35-
android:background="#000000"
36-
android:padding="16dp"
37-
android:id="@+id/asciiScroll"
32+
<ImageView
33+
app:layout_constraintStart_toStartOf="parent"
34+
app:layout_constraintEnd_toEndOf="parent"
3835
app:layout_constraintTop_toBottomOf="@id/error"
39-
android:scrollbars="horizontal">
40-
41-
<ImageView
42-
android:id="@+id/asciiView"
43-
android:layout_width="wrap_content"
44-
android:layout_height="wrap_content"
45-
android:scaleType="matrix"
46-
android:contentDescription="ASCII Captcha" />
47-
</HorizontalScrollView>
36+
android:id="@+id/asciiView"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:contentDescription="ASCII Captcha" />
4840

4941
<androidx.appcompat.widget.AppCompatEditText
5042
android:id="@+id/captcha_solution"
@@ -64,8 +56,8 @@
6456
android:textColor="@color/colorWhite"
6557
android:textColorHint="@color/colorWhite50"
6658
android:textCursorDrawable="@drawable/color_cursor"
67-
app:layout_constraintStart_toStartOf="@id/asciiScroll"
68-
app:layout_constraintTop_toBottomOf="@id/asciiScroll" />
59+
app:layout_constraintStart_toStartOf="@id/asciiView"
60+
app:layout_constraintTop_toBottomOf="@id/asciiView" />
6961

7062
<Button
7163
android:id="@+id/login_sign_up"

0 commit comments

Comments
 (0)