| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- 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;
- }
- }
|