GlobalWidgetService.ets 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { WidgetData } from './WidgetTypes';
  3. import { AvSessionWidgetListener } from './AvSessionWidgetListener';
  4. const TAG = 'GlobalWidgetService';
  5. /**
  6. * 全局卡片服务
  7. * 用于主应用直接更新卡片数据,绕过跨进程通信限制
  8. */
  9. export class GlobalWidgetService {
  10. private static instance: GlobalWidgetService | null = null;
  11. private avSessionListener: AvSessionWidgetListener;
  12. private constructor() {
  13. this.avSessionListener = AvSessionWidgetListener.getInstance();
  14. }
  15. public static getInstance(): GlobalWidgetService {
  16. if (!GlobalWidgetService.instance) {
  17. GlobalWidgetService.instance = new GlobalWidgetService();
  18. }
  19. return GlobalWidgetService.instance;
  20. }
  21. /**
  22. * 更新卡片数据(主应用调用)
  23. */
  24. public updateWidgetData(data: WidgetData): void {
  25. try {
  26. // 在全局服务层面也进行按钮状态验证和修复
  27. const correctedData = this.validateAndFixButtonStates(data);
  28. // 更新AvSession监听器的数据
  29. this.avSessionListener.updateWidgetData(correctedData);
  30. 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}`);
  31. } catch (error) {
  32. hilog.error(0x0000, TAG, `Failed to update widget data globally: ${error}`);
  33. }
  34. }
  35. /**
  36. * 验证和修复按钮状态
  37. */
  38. private validateAndFixButtonStates(data: WidgetData): WidgetData {
  39. hilog.info(0x0000, TAG, `Validating button states: original hasNext=${data.playlist.hasNext}, hasPrevious=${data.playlist.hasPrevious}, currentIndex=${data.playlist.currentIndex}, totalCount=${data.playlist.totalCount}`);
  40. // 创建新的 WidgetData 对象
  41. const correctedData: WidgetData = {
  42. playState: {
  43. isPlaying: data.playState.isPlaying,
  44. isPaused: data.playState.isPaused,
  45. isLoading: data.playState.isLoading
  46. },
  47. currentSong: {
  48. id: data.currentSong.id,
  49. title: data.currentSong.title,
  50. artist: data.currentSong.artist,
  51. album: data.currentSong.album,
  52. coverImagePath: data.currentSong.coverImagePath,
  53. duration: data.currentSong.duration
  54. },
  55. progress: {
  56. currentPosition: data.progress.currentPosition,
  57. duration: data.progress.duration,
  58. percentage: data.progress.percentage,
  59. currentTimeText: data.progress.currentTimeText,
  60. totalTimeText: data.progress.totalTimeText
  61. },
  62. playlist: {
  63. hasNext: data.playlist.hasNext,
  64. hasPrevious: data.playlist.hasPrevious,
  65. currentIndex: data.playlist.currentIndex,
  66. totalCount: data.playlist.totalCount
  67. },
  68. config: {
  69. size: data.config.size,
  70. theme: data.config.theme,
  71. showProgress: data.config.showProgress,
  72. showCover: data.config.showCover
  73. }
  74. };
  75. if (data.playlist.totalCount > 1) {
  76. const correctHasNext = data.playlist.currentIndex < data.playlist.totalCount - 1;
  77. const correctHasPrevious = data.playlist.currentIndex > 0;
  78. // 更新按钮状态
  79. correctedData.playlist.hasNext = correctHasNext;
  80. correctedData.playlist.hasPrevious = correctHasPrevious;
  81. if (correctHasNext !== data.playlist.hasNext || correctHasPrevious !== data.playlist.hasPrevious) {
  82. hilog.info(0x0000, TAG, `Fixed button states in GlobalWidgetService: hasNext=${correctHasNext}, hasPrevious=${correctHasPrevious}`);
  83. }
  84. } else {
  85. // 单首歌或无歌曲时,禁用所有按钮
  86. correctedData.playlist.hasNext = false;
  87. correctedData.playlist.hasPrevious = false;
  88. }
  89. return correctedData;
  90. }
  91. }