Skip to content

Commit 8687787

Browse files
authored
fix(accesskey): don't crash when access-key bitmap render fails (#1027)
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 0a9ef19 commit 8687787

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

apps/flipcash/shared/accesskey/src/main/kotlin/com/flipcash/app/accesskey/BaseAccessKeyViewModel.kt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import com.getcode.theme.GradientSpec
2525
import com.getcode.theme.White
2626
import com.getcode.ui.utils.toAGColor
2727
import com.getcode.util.resources.ResourceHelper
28+
import com.getcode.utils.TraceType
2829
import com.getcode.utils.decodeBase64
30+
import com.getcode.utils.trace
2931
import androidx.lifecycle.ViewModel
3032
import com.getcode.view.LoadingSuccessState
3133
import kotlinx.coroutines.flow.MutableStateFlow
@@ -91,15 +93,28 @@ abstract class BaseAccessKeyViewModel(
9193
}
9294

9395
viewModelScope.launch(dispatchers.IO) {
94-
val accessKeyBitmap = createBitmapForExport(drawBackground = true, words = words, entropyB64 = entropyB64)
95-
val accessKeyBitmapDisplay = createBitmapForExport(drawBackground = false, words, entropyB64)
96-
val accessKeyCroppedBitmap =
97-
Bitmap.createBitmap(accessKeyBitmapDisplay, 0, 500, 1200, 1450)
98-
99-
uiFlow.update {
100-
it.copy(
101-
accessKeyBitmap = accessKeyBitmap,
102-
accessKeyCroppedBitmap = accessKeyCroppedBitmap
96+
runCatching {
97+
val accessKeyBitmap = createBitmapForExport(drawBackground = true, words = words, entropyB64 = entropyB64)
98+
val accessKeyBitmapDisplay = createBitmapForExport(drawBackground = false, words, entropyB64)
99+
val accessKeyCroppedBitmap =
100+
Bitmap.createBitmap(accessKeyBitmapDisplay, 0, 500, 1200, 1450)
101+
102+
uiFlow.update {
103+
it.copy(
104+
accessKeyBitmap = accessKeyBitmap,
105+
accessKeyCroppedBitmap = accessKeyCroppedBitmap
106+
)
107+
}
108+
}.onFailure { error ->
109+
if (error is kotlin.coroutines.cancellation.CancellationException) throw error
110+
// Bitmap rendering can fail on some OEM devices with
111+
// "width and height must be > 0". Report as a non-fatal instead
112+
// of crashing; the UI handles null bitmaps. Refs Bugsnag 6a4528ca.
113+
trace(
114+
tag = "access-key",
115+
message = "Failed to render access key bitmap",
116+
error = error,
117+
type = TraceType.Error,
103118
)
104119
}
105120
}

libs/quickresponse/src/main/kotlin/com/getcode/libs/qr/QRCodeGenerator.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ internal fun generateQr(
4444
null
4545
}
4646

47-
val matrixWidth = bitmapMatrix?.width ?: size
48-
val matrixHeight = bitmapMatrix?.height ?: size
47+
// Guard against a zero/negative dimension reaching Bitmap.createBitmap,
48+
// which throws IllegalArgumentException("width and height must be > 0").
49+
val matrixWidth = (bitmapMatrix?.width ?: size).coerceAtLeast(1)
50+
val matrixHeight = (bitmapMatrix?.height ?: size).coerceAtLeast(1)
4951

50-
val newBitmap = createBitmap(bitmapMatrix?.width ?: size, bitmapMatrix?.height ?: size)
52+
val newBitmap = createBitmap(matrixWidth, matrixHeight)
5153

5254
val pixels = IntArray(matrixWidth * matrixHeight)
5355

0 commit comments

Comments
 (0)