88 lines
2.9 KiB
Kotlin
88 lines
2.9 KiB
Kotlin
package com.hikoncont.util.adaptation
|
|
|
|
import com.hikoncont.util.DeviceDetector.DeviceBrand
|
|
import com.hikoncont.util.DeviceDetector.KeepAlivePolicy
|
|
|
|
/**
|
|
* Strategy registry for keep-alive behavior across device families.
|
|
*
|
|
* Add new strategies here instead of injecting ROM if/else checks into
|
|
* service modules.
|
|
*/
|
|
object KeepAliveAdaptationRegistry {
|
|
|
|
private val strategies: List<DeviceAdaptationStrategy> = listOf(
|
|
OppoFamilyAndroid14ConservativeStrategy,
|
|
XiaomiLegacyAggressiveStrategy,
|
|
DefaultBalancedStrategy
|
|
).sortedByDescending { it.priority }
|
|
|
|
fun resolve(info: DeviceRuntimeInfo): StrategyResolution {
|
|
val strategy = strategies.firstOrNull { it.matches(info) } ?: DefaultBalancedStrategy
|
|
return StrategyResolution(
|
|
strategyId = strategy.id,
|
|
keepAlivePolicy = strategy.keepAlivePolicy(info)
|
|
)
|
|
}
|
|
}
|
|
|
|
private object OppoFamilyAndroid14ConservativeStrategy : DeviceAdaptationStrategy {
|
|
override val id: String = "oppo_family_android14_conservative"
|
|
override val priority: Int = 100
|
|
|
|
override fun matches(info: DeviceRuntimeInfo): Boolean {
|
|
if (info.sdkInt < 34) return false
|
|
return info.brand == DeviceBrand.OPPO ||
|
|
info.brand == DeviceBrand.VIVO ||
|
|
info.brand == DeviceBrand.REALME ||
|
|
info.brand == DeviceBrand.ONEPLUS
|
|
}
|
|
|
|
override fun keepAlivePolicy(info: DeviceRuntimeInfo): KeepAlivePolicy {
|
|
return KeepAlivePolicy(
|
|
useActivityKeepAlive = false,
|
|
enableAggressiveWorkers = false,
|
|
keepAliveCheckIntervalMs = 30_000L,
|
|
minForegroundKickIntervalMs = 20_000L
|
|
)
|
|
}
|
|
}
|
|
|
|
private object XiaomiLegacyAggressiveStrategy : DeviceAdaptationStrategy {
|
|
override val id: String = "xiaomi_legacy_aggressive"
|
|
override val priority: Int = 90
|
|
|
|
override fun matches(info: DeviceRuntimeInfo): Boolean {
|
|
return info.brand == DeviceBrand.XIAOMI && info.sdkInt <= 33
|
|
}
|
|
|
|
override fun keepAlivePolicy(info: DeviceRuntimeInfo): KeepAlivePolicy {
|
|
return KeepAlivePolicy(
|
|
useActivityKeepAlive = true,
|
|
enableAggressiveWorkers = true,
|
|
keepAliveCheckIntervalMs = 5_000L,
|
|
minForegroundKickIntervalMs = 2_500L
|
|
)
|
|
}
|
|
}
|
|
|
|
private object DefaultBalancedStrategy : DeviceAdaptationStrategy {
|
|
override val id: String = "default_balanced"
|
|
override val priority: Int = 0
|
|
|
|
override fun matches(info: DeviceRuntimeInfo): Boolean {
|
|
return true
|
|
}
|
|
|
|
override fun keepAlivePolicy(info: DeviceRuntimeInfo): KeepAlivePolicy {
|
|
val disableActivityKeepAlive = info.brand == DeviceBrand.OPPO || info.brand == DeviceBrand.VIVO
|
|
return KeepAlivePolicy(
|
|
useActivityKeepAlive = !disableActivityKeepAlive,
|
|
enableAggressiveWorkers = true,
|
|
keepAliveCheckIntervalMs = 10_000L,
|
|
minForegroundKickIntervalMs = 5_000L
|
|
)
|
|
}
|
|
}
|
|
|