mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-08-26 21:02:28 +02:00
fix(download): preserve SAF and defer background starts
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.zarz.spotiflac
|
||||
|
||||
/** Maps Android/OEM foreground-service launch denials to a stable Dart code. */
|
||||
object ForegroundServiceStartPolicy {
|
||||
const val START_NOT_ALLOWED_CODE = "foreground_service_start_not_allowed"
|
||||
|
||||
fun isStartNotAllowed(error: Throwable): Boolean {
|
||||
if (error.javaClass.name == "android.app.ForegroundServiceStartNotAllowedException") {
|
||||
return true
|
||||
}
|
||||
val message = error.message.orEmpty()
|
||||
return message.contains("startForegroundService() not allowed", ignoreCase = true) ||
|
||||
message.contains("mAllowStartForeground false", ignoreCase = true)
|
||||
}
|
||||
|
||||
fun errorCode(error: Throwable): String =
|
||||
if (isStartNotAllowed(error)) START_NOT_ALLOWED_CODE else "ERROR"
|
||||
}
|
||||
@@ -2326,7 +2326,11 @@ class MainActivity: FlutterFragmentActivity() {
|
||||
else -> result.notImplemented()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
result.error("ERROR", e.message, null)
|
||||
result.error(
|
||||
ForegroundServiceStartPolicy.errorCode(e),
|
||||
e.message,
|
||||
null,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.zarz.spotiflac
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class ForegroundServiceStartPolicyTest {
|
||||
@Test
|
||||
fun mapsAndroidForegroundStartDenialsToStableCode() {
|
||||
val error = IllegalStateException(
|
||||
"startForegroundService() not allowed due to mAllowStartForeground false",
|
||||
)
|
||||
|
||||
assertTrue(ForegroundServiceStartPolicy.isStartNotAllowed(error))
|
||||
assertEquals(
|
||||
ForegroundServiceStartPolicy.START_NOT_ALLOWED_CODE,
|
||||
ForegroundServiceStartPolicy.errorCode(error),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun leavesUnrelatedPlatformFailuresGeneric() {
|
||||
val error = IllegalStateException("network unavailable")
|
||||
|
||||
assertFalse(ForegroundServiceStartPolicy.isStartNotAllowed(error))
|
||||
assertEquals("ERROR", ForegroundServiceStartPolicy.errorCode(error))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user