Skip to content

Commit 88ed7e2

Browse files
author
ideafun
committed
fix(compat): Fix WEBP_LOSSY crash on Android 10 and below
- Add API level check for Bitmap.CompressFormat.WEBP_LOSSY (API 30+) - Fall back to WEBP format on older Android versions - Fix getColor() compatibility in MainActivity - Bump version to 0.0.6
1 parent d86b731 commit 88ed7e2

5 files changed

Lines changed: 43 additions & 10 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
applicationId = "com.kevinluo.autoglm"
1313
minSdk = 24
1414
targetSdk = 34
15-
versionCode = 5
16-
versionName = "0.0.5"
15+
versionCode = 6
16+
versionName = "0.0.6"
1717

1818
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1919
}

app/src/main/java/com/kevinluo/autoglm/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ class MainActivity : AppCompatActivity(), PhoneAgentListener {
10161016

10171017
runOnUiThread {
10181018
statusText.text = statusMessage
1019-
shizukuStatusIndicator.background.setTint(getColor(statusColor))
1019+
shizukuStatusIndicator.background.setTint(ContextCompat.getColor(this, statusColor))
10201020

10211021
// Show buttons based on Shizuku state
10221022
if (serviceConnected) {

app/src/main/java/com/kevinluo/autoglm/history/HistoryDetailActivity.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,13 @@ class HistoryDetailActivity : AppCompatActivity() {
499499

500500
val file = File(cacheDir, "autoglm_task_${System.currentTimeMillis()}.webp")
501501
FileOutputStream(file).use { out ->
502-
bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSY, 90, out)
502+
@Suppress("DEPRECATION")
503+
val format = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
504+
Bitmap.CompressFormat.WEBP_LOSSY
505+
} else {
506+
Bitmap.CompressFormat.WEBP
507+
}
508+
bitmap.compress(format, 90, out)
503509
}
504510
return file
505511
}
@@ -550,7 +556,13 @@ class HistoryDetailActivity : AppCompatActivity() {
550556

551557
uri?.let {
552558
resolver.openOutputStream(it)?.use { out ->
553-
bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSY, 90, out)
559+
@Suppress("DEPRECATION")
560+
val format = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
561+
Bitmap.CompressFormat.WEBP_LOSSY
562+
} else {
563+
Bitmap.CompressFormat.WEBP
564+
}
565+
bitmap.compress(format, 90, out)
554566
}
555567

556568
contentValues.clear()
@@ -567,7 +579,8 @@ class HistoryDetailActivity : AppCompatActivity() {
567579

568580
val file = File(autoglmDir, filename)
569581
FileOutputStream(file).use { out ->
570-
bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSY, 90, out)
582+
@Suppress("DEPRECATION")
583+
bitmap.compress(Bitmap.CompressFormat.WEBP, 90, out)
571584
}
572585

573586
// Notify gallery

app/src/main/java/com/kevinluo/autoglm/history/HistoryManager.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.kevinluo.autoglm.history
33
import android.content.Context
44
import android.graphics.Bitmap
55
import android.graphics.BitmapFactory
6+
import android.os.Build
67
import android.util.Base64
78
import com.kevinluo.autoglm.action.AgentAction
89
import com.kevinluo.autoglm.util.Logger
@@ -346,7 +347,13 @@ class HistoryManager private constructor(private val context: Context) {
346347
val file = File(taskDir, "step_${stepNumber}${suffix}.webp")
347348

348349
FileOutputStream(file).use { out ->
349-
bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSY, 85, out)
350+
@Suppress("DEPRECATION")
351+
val format = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
352+
Bitmap.CompressFormat.WEBP_LOSSY
353+
} else {
354+
Bitmap.CompressFormat.WEBP
355+
}
356+
bitmap.compress(format, 85, out)
350357
}
351358

352359
return file.absolutePath

app/src/main/java/com/kevinluo/autoglm/screenshot/ScreenshotService.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.kevinluo.autoglm.screenshot
22

33
import android.graphics.Bitmap
44
import android.graphics.BitmapFactory
5+
import android.os.Build
56
import android.util.Base64
67
import com.kevinluo.autoglm.IUserService
78
import com.kevinluo.autoglm.util.ErrorHandler
@@ -92,6 +93,18 @@ class ScreenshotService(
9293
// Screenshot compression settings - optimized for API upload
9394
private const val WEBP_QUALITY = 65 // Reduced from 70 for better compression
9495

96+
/**
97+
* Returns the appropriate WebP compress format based on API level.
98+
* WEBP_LOSSY is only available on API 30+, use deprecated WEBP for older versions.
99+
*/
100+
@Suppress("DEPRECATION")
101+
val WEBP_FORMAT: Bitmap.CompressFormat
102+
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
103+
Bitmap.CompressFormat.WEBP_LOSSY
104+
} else {
105+
Bitmap.CompressFormat.WEBP
106+
}
107+
95108
// Screenshot scaling settings - use max dimensions instead of fixed scale factor
96109
// This ensures consistent output size regardless of device resolution
97110
private const val MAX_WIDTH = 720 // Max width after scaling
@@ -191,7 +204,7 @@ class ScreenshotService(
191204

192205
// Convert to WebP for better compression
193206
val webpStream = ByteArrayOutputStream()
194-
bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSY, WEBP_QUALITY, webpStream)
207+
bitmap.compress(WEBP_FORMAT, WEBP_QUALITY, webpStream)
195208
bitmap.recycle()
196209

197210
val webpData = webpStream.toByteArray()
@@ -370,7 +383,7 @@ class ScreenshotService(
370383
val bitmap = Bitmap.createBitmap(FALLBACK_WIDTH, FALLBACK_HEIGHT, Bitmap.Config.ARGB_8888)
371384

372385
val outputStream = ByteArrayOutputStream()
373-
bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSY, WEBP_QUALITY, outputStream)
386+
bitmap.compress(WEBP_FORMAT, WEBP_QUALITY, outputStream)
374387
bitmap.recycle()
375388

376389
val base64Data = Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP)
@@ -432,7 +445,7 @@ class ScreenshotService(
432445
*/
433446
fun encodeBitmapToBase64(
434447
bitmap: Bitmap,
435-
format: Bitmap.CompressFormat = Bitmap.CompressFormat.WEBP_LOSSY,
448+
format: Bitmap.CompressFormat = WEBP_FORMAT,
436449
quality: Int = WEBP_QUALITY
437450
): String {
438451
val outputStream = ByteArrayOutputStream()

0 commit comments

Comments
 (0)