修复:无摄像头设备闪光崩溃 #749

This commit is contained in:
pppscn
2026-07-17 22:59:00 +08:00
parent fccd1be698
commit f9ed93f4f9
2 changed files with 33 additions and 9 deletions

View File

@@ -112,7 +112,7 @@ class ForegroundService : Service() {
vibrationUtils.stopVibration()
}
//停止闪光灯
if (flashUtils.isFlashing) {
if (::flashUtils.isInitialized && flashUtils.isFlashing) {
flashUtils.stopFlashing()
}
//停止播放音乐
@@ -192,7 +192,7 @@ class ForegroundService : Service() {
vibrationUtils.startVibration(alarm.vibrate, alarm.repeatTimes)
}
//闪光灯提醒
if (alarm.flashTimes >= 0) {
if (alarm.flashTimes >= 0 && ::flashUtils.isInitialized && flashUtils.isFlashSupported) {
isFlash = true
flashUtils.startFlashing(alarm.flash, alarm.flashTimes)
}
@@ -252,7 +252,9 @@ class ForegroundService : Service() {
override fun onDestroy() {
//非纯客户端模式
if (!SettingUtils.enablePureClientMode) stopForegroundService()
flashUtils.release()
if (::flashUtils.isInitialized) {
flashUtils.release()
}
super.onDestroy()
}
@@ -337,7 +339,7 @@ class ForegroundService : Service() {
vibrationUtils.stopVibration()
}
//停止闪光灯
if (flashUtils.isFlashing) {
if (::flashUtils.isInitialized && flashUtils.isFlashing) {
flashUtils.stopFlashing()
}
} catch (e: Exception) {

View File

@@ -4,6 +4,7 @@ package cn.ppps.forwarder.utils
import android.content.Context
import android.hardware.Camera
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraAccessException
import android.hardware.camera2.CameraManager
import android.os.Build
@@ -17,6 +18,8 @@ class FlashUtils(context: Context) {
private var legacyParams: Camera.Parameters? = null
private var handler: Handler? = null
private val duration = 100L // 闪烁持续时间
var isFlashSupported = false
private set
var isFlashing = false
private set
@@ -24,16 +27,28 @@ class FlashUtils(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
try {
cameraId = cameraManager?.cameraIdList?.get(0) // 获取后置摄像头 ID
val manager = cameraManager
cameraId = manager?.cameraIdList?.firstOrNull { id ->
manager.getCameraCharacteristics(id).get(CameraCharacteristics.FLASH_INFO_AVAILABLE) == true
}
isFlashSupported = cameraId != null
} catch (e: CameraAccessException) {
e.printStackTrace()
isFlashSupported = false
}
} else {
try {
legacyCamera = Camera.open()
legacyParams = legacyCamera?.parameters
isFlashSupported = legacyParams?.supportedFlashModes?.contains(Camera.Parameters.FLASH_MODE_TORCH) == true
if (!isFlashSupported) {
legacyCamera?.release()
legacyCamera = null
legacyParams = null
}
} catch (e: Exception) {
e.printStackTrace()
isFlashSupported = false
}
}
}
@@ -44,11 +59,13 @@ class FlashUtils(context: Context) {
* @param repeatTimes 闪烁的重复次数0 表示无限循环
*/
fun startFlashing(pattern: String, repeatTimes: Int) {
if (isFlashing) return
if (!isFlashSupported || isFlashing || repeatTimes < 0 || pattern.isEmpty()) return
val sequence = pattern.filter { it == 'X' || it == 'O' || it == '1' || it == '0' }.toCharArray()
if (sequence.isEmpty()) return
isFlashing = true
handler = Handler(Looper.getMainLooper())
val sequence = pattern.toCharArray()
var index = 0
var repeatCount = 0
@@ -89,10 +106,14 @@ class FlashUtils(context: Context) {
* 设置闪光灯状态,兼容 Android 4.4+
*/
private fun setFlashlight(enable: Boolean) {
if (!isFlashSupported) return
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager?.setTorchMode(cameraId!!, enable)
val currentCameraId = cameraId ?: return
cameraManager?.setTorchMode(currentCameraId, enable)
} else {
if (legacyCamera == null || legacyParams == null) return
legacyParams?.flashMode = if (enable) Camera.Parameters.FLASH_MODE_TORCH else Camera.Parameters.FLASH_MODE_OFF
legacyCamera?.parameters = legacyParams
if (enable) legacyCamera?.startPreview() else legacyCamera?.stopPreview()
@@ -106,6 +127,7 @@ class FlashUtils(context: Context) {
* 释放旧 API 资源
*/
fun release() {
stopFlashing()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
legacyCamera?.stopPreview()
legacyCamera?.release()