Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ internal suspend fun GlobalDownloadManager.stageAndPromotePendingDownloadQueue(
songs: List<SongItem>,
userInitiated: Boolean,
batchIdentity: DownloadExecutionRoomStore.DownloadBatchIdentity? = null,
requiresWifiNetworkOverride: Boolean? = null,
onPageReady: suspend (pageIndex: Int, page: StagedPendingDownloadQueue) -> Unit =
{ _, _ -> }
): StagedPendingDownloadQueue {
Expand All @@ -397,8 +398,9 @@ internal suspend fun GlobalDownloadManager.stageAndPromotePendingDownloadQueue(
.filter(cancellationForceNewSongKeys::contains)
.toSet()
val currentNetworkType = appContext.currentDownloadNetworkTypeOrNull()
val requiresWifiNetwork = !userInitiated ||
currentNetworkType == null || currentNetworkType == TrafficNetworkType.WIFI
val requiresWifiNetwork = requiresWifiNetworkOverride ?: (
!userInitiated || currentNetworkType == null || currentNetworkType == TrafficNetworkType.WIFI
)
val downloadAudioQuality = resolveDownloadAudioQualitySelection(appContext)
// 首批交接前保存整批载荷,进程退出后尾部歌曲仍可沿原恢复入口继续
val waitingBatch = database.withTransaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import moe.ouom.neriplayer.core.player.download.network.currentDownloadParalleli
import moe.ouom.neriplayer.core.player.download.network.resolveDownloadDispatchWindow
import moe.ouom.neriplayer.data.model.SongItem
import moe.ouom.neriplayer.data.model.stableKey
import moe.ouom.neriplayer.data.model.traffic.TrafficNetworkType
import moe.ouom.neriplayer.data.local.media.LocalSongSupport
import moe.ouom.neriplayer.data.traffic.currentDownloadNetworkTypeOrNull
import moe.ouom.neriplayer.common.time.elapsedMillisSince
Expand Down Expand Up @@ -111,12 +112,30 @@ internal fun GlobalDownloadManager.startBatchDownload(
true
}
if (!deletionSettled) {
val networkType = appContext.currentDownloadNetworkTypeOrNull()
if (capturedAdmissionTicket != null &&
isDownloadAdmissionTicketCurrentForStableKeys(
context = appContext,
admissionTicket = capturedAdmissionTicket,
stableKeys = requestedSongKeys
) && maybeRequestTrafficRiskDownloadConfirmation(
context = appContext,
songs = requestedSongs,
isBatch = true,
skipTrafficRiskPrompt = skipTrafficRiskPrompt,
networkType = networkType
)
) {
return@admission null
}
val deferred = deferDownloadForDeleteCleanup(
context = appContext,
songs = requestedSongs,
userInitiated = userInitiated,
admissionTicket = capturedAdmissionTicket,
batchPresentationId = batchPresentationId
batchPresentationId = batchPresentationId,
requiresWifiNetworkOverride = !userInitiated ||
networkType == null || networkType == TrafficNetworkType.WIFI
)
if (!deferred) {
if (batchPresentationId != 0L) {
Expand Down Expand Up @@ -186,6 +205,8 @@ internal fun GlobalDownloadManager.startBatchDownload(
)
var preparedSnapshot: DownloadBatchSnapshotSelection? = null
var durableBatchIdentity: DownloadExecutionRoomStore.DownloadBatchIdentity? = null
var trafficRiskConfirmationRequested = false
var requiresWifiNetwork = true
val batchCreated = admitDownloadMutationForStableKeys(
context = appContext,
admissionTicket = admissionTicket,
Expand All @@ -195,6 +216,21 @@ internal fun GlobalDownloadManager.startBatchDownload(
song.stableKey() in admittedSongKeys &&
song.stableKey() !in initiallyCompletedSongKeys
}
// 确认前不能落盘或恢复旧任务,否则取消后重启仍会恢复这次批次
val networkType = appContext.currentDownloadNetworkTypeOrNull()
trafficRiskConfirmationRequested = maybeRequestTrafficRiskDownloadConfirmation(
context = appContext,
songs = admittedSongs,
isBatch = true,
skipTrafficRiskPrompt = skipTrafficRiskPrompt,
networkType = networkType
)
if (trafficRiskConfirmationRequested) {
return@snapshotAdmission
}
// 准备期间切到移动网络时,仍保留本次准入的 WIFI 限制
requiresWifiNetwork = !userInitiated ||
networkType == null || networkType == TrafficNetworkType.WIFI
// 显式恢复可能释放旧 owner,必须在新批次归属查询前完成
if (userInitiated && !clearSongCancellationForFreshStart(
context = appContext,
Expand Down Expand Up @@ -231,6 +267,9 @@ internal fun GlobalDownloadManager.startBatchDownload(
)
}
}
if (trafficRiskConfirmationRequested) {
return@startup
}
if (!batchCreated || preparedSnapshot == null) {
NPLogger.w(
TAG,
Expand Down Expand Up @@ -533,17 +572,6 @@ internal fun GlobalDownloadManager.startBatchDownload(
scheduleCompletedBatchDownloadPresentationRemoval(batchPresentationId)
return@admission
}
if (
maybeRequestTrafficRiskDownloadConfirmation(
context = appContext,
songs = songsToStage,
isBatch = true,
skipTrafficRiskPrompt = skipTrafficRiskPrompt
)
) {
clearBatchDownloadPresentation(batchPresentationId)
return@admission
}
val canStartFirstDurablePageImmediately = cleanupBeforeStart &&
!shouldDeferQueuedDownloadStartForNetwork(
networkType = appContext.currentDownloadNetworkTypeOrNull(),
Expand All @@ -556,6 +584,7 @@ internal fun GlobalDownloadManager.startBatchDownload(
songs = songsToStage,
userInitiated = userInitiated,
batchIdentity = durableBatchIdentity,
requiresWifiNetworkOverride = requiresWifiNetwork,
// 首页 operation 已和持久批次绑定,立即唤醒共享泵
// 后续页按原队列顺序提升,不再阻塞第一首开始传输
onPageReady = { _, page ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,8 @@ internal suspend fun GlobalDownloadManager.maybeRequestTrafficRiskDownloadConfir
context: Context,
songs: List<SongItem>,
isBatch: Boolean,
skipTrafficRiskPrompt: Boolean
skipTrafficRiskPrompt: Boolean,
networkType: TrafficNetworkType? = context.currentDownloadNetworkTypeOrNull()
): Boolean {
if (skipTrafficRiskPrompt) {
return false
Expand All @@ -1056,7 +1057,6 @@ internal suspend fun GlobalDownloadManager.maybeRequestTrafficRiskDownloadConfir
if (distinctSongs.isEmpty()) {
return false
}
val networkType = context.currentDownloadNetworkTypeOrNull()
if (networkType == TrafficNetworkType.WIFI) {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,16 @@ internal suspend fun GlobalDownloadManager.deferDownloadForDeleteCleanup(
songs: Collection<SongItem>,
userInitiated: Boolean,
admissionTicket: Long?,
batchPresentationId: Long? = null
batchPresentationId: Long? = null,
requiresWifiNetworkOverride: Boolean? = null
): Boolean {
val ticket = admissionTicket ?: return false
var deferred = false
val admitted = admitDownloadMutationForStableKeys(
context, ticket, songs.map(SongItem::stableKey)
) {
deferred = persistDownloadWaitingForDeleteCleanup(
context, songs, userInitiated, ticket, batchPresentationId
context, songs, userInitiated, ticket, batchPresentationId, requiresWifiNetworkOverride
)
}
return admitted && deferred
Expand All @@ -341,7 +342,8 @@ private suspend fun GlobalDownloadManager.persistDownloadWaitingForDeleteCleanup
songs: Collection<SongItem>,
userInitiated: Boolean,
admissionTicket: Long,
batchPresentationId: Long?
batchPresentationId: Long?,
requiresWifiNetworkOverride: Boolean?
): Boolean {
val appContext = context.applicationContext
val distinctSongs = songs.distinctBy(SongItem::stableKey)
Expand Down Expand Up @@ -378,8 +380,9 @@ private suspend fun GlobalDownloadManager.persistDownloadWaitingForDeleteCleanup
return false
}
val currentNetworkType = appContext.currentDownloadNetworkTypeOrNull()
val requiresWifiNetwork = !userInitiated ||
currentNetworkType == null || currentNetworkType == TrafficNetworkType.WIFI
val requiresWifiNetwork = requiresWifiNetworkOverride ?: (
!userInitiated || currentNetworkType == null || currentNetworkType == TrafficNetworkType.WIFI
)
val operationCreatedAtMs = nextDownloadOperationCreatedAtMs(
requestedAtMs = System.currentTimeMillis(),
cancellationCutoffs = songKeys.mapNotNull { songKey ->
Expand Down
Loading
Loading