import { hilog } from '@kit.PerformanceAnalysisKit'; import { WidgetData } from './WidgetTypes'; import { AvSessionWidgetListener } from './AvSessionWidgetListener'; const TAG = 'GlobalWidgetService'; /** * 全局卡片服务 * 用于主应用直接更新卡片数据,绕过跨进程通信限制 */ export class GlobalWidgetService { private static instance: GlobalWidgetService | null = null; private avSessionListener: AvSessionWidgetListener; private constructor() { this.avSessionListener = AvSessionWidgetListener.getInstance(); } public static getInstance(): GlobalWidgetService { if (!GlobalWidgetService.instance) { GlobalWidgetService.instance = new GlobalWidgetService(); } return GlobalWidgetService.instance; } /** * 更新卡片数据(主应用调用) */ public updateWidgetData(data: WidgetData): void { try { // 在全局服务层面也进行按钮状态验证和修复 const correctedData = this.validateAndFixButtonStates(data); // 更新AvSession监听器的数据 this.avSessionListener.updateWidgetData(correctedData); hilog.info(0x0000, TAG, `Widget data updated globally: isPlaying=${correctedData.playState.isPlaying}, title=${correctedData.currentSong.title}, hasNext=${correctedData.playlist.hasNext}, hasPrevious=${correctedData.playlist.hasPrevious}, currentIndex=${correctedData.playlist.currentIndex}, totalCount=${correctedData.playlist.totalCount}`); } catch (error) { hilog.error(0x0000, TAG, `Failed to update widget data globally: ${error}`); } } /** * 验证和修复按钮状态 */ private validateAndFixButtonStates(data: WidgetData): WidgetData { hilog.info(0x0000, TAG, `Validating button states: original hasNext=${data.playlist.hasNext}, hasPrevious=${data.playlist.hasPrevious}, currentIndex=${data.playlist.currentIndex}, totalCount=${data.playlist.totalCount}`); // 创建新的 WidgetData 对象 const correctedData: WidgetData = { playState: { isPlaying: data.playState.isPlaying, isPaused: data.playState.isPaused, isLoading: data.playState.isLoading }, currentSong: { id: data.currentSong.id, title: data.currentSong.title, artist: data.currentSong.artist, album: data.currentSong.album, coverImagePath: data.currentSong.coverImagePath, duration: data.currentSong.duration }, progress: { currentPosition: data.progress.currentPosition, duration: data.progress.duration, percentage: data.progress.percentage, currentTimeText: data.progress.currentTimeText, totalTimeText: data.progress.totalTimeText }, playlist: { hasNext: data.playlist.hasNext, hasPrevious: data.playlist.hasPrevious, currentIndex: data.playlist.currentIndex, totalCount: data.playlist.totalCount }, config: { size: data.config.size, theme: data.config.theme, showProgress: data.config.showProgress, showCover: data.config.showCover } }; if (data.playlist.totalCount > 1) { const correctHasNext = data.playlist.currentIndex < data.playlist.totalCount - 1; const correctHasPrevious = data.playlist.currentIndex > 0; // 更新按钮状态 correctedData.playlist.hasNext = correctHasNext; correctedData.playlist.hasPrevious = correctHasPrevious; if (correctHasNext !== data.playlist.hasNext || correctHasPrevious !== data.playlist.hasPrevious) { hilog.info(0x0000, TAG, `Fixed button states in GlobalWidgetService: hasNext=${correctHasNext}, hasPrevious=${correctHasPrevious}`); } } else { // 单首歌或无歌曲时,禁用所有按钮 correctedData.playlist.hasNext = false; correctedData.playlist.hasPrevious = false; } return correctedData; } }