| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { PlaylistState, PlayProgress, PlayState, SongInfo, WidgetConfig, WidgetData } from './WidgetTypes';
- import { AvSessionWidgetListener } from './AvSessionWidgetListener';
- import commonEventManager from '@ohos.commonEventManager';
- import {
- PLAYER_STATE_CHANGED_EVENT,
- PLAYER_SONG_CHANGED_EVENT,
- PLAYER_PROGRESS_CHANGED_EVENT
- } from './WidgetEventConstants';
- const TAG = 'GlobalWidgetService';
- /**
- * 全局卡片服务
- * 用于主应用直接更新卡片数据,绕过跨进程通信限制
- */
- interface GeneratedObjectLiteralInterface_1 {
- playState: PlayState;
- currentSong: SongInfo;
- progress: PlayProgress;
- playlist: PlaylistState;
- config: WidgetConfig;
- timestamp: number;
- source: string;
- }
- 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);
-
- // 通过 CommonEvent 发送数据到 Form 进程
- this.broadcastToFormProcess(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}`);
- }
- }
- /**
- * 通过 CommonEvent 广播数据到 Form 进程
- */
- private async broadcastToFormProcess(data: WidgetData): Promise<void> {
- try {
- hilog.info(0x0000, TAG, `🚀 Starting broadcast to form process for: ${data.currentSong.title}`);
-
- // 构造广播数据
- const broadcastData: GeneratedObjectLiteralInterface_1 = {
- playState: data.playState,
- currentSong: data.currentSong,
- progress: data.progress,
- playlist: data.playlist,
- config: data.config,
- timestamp: Date.now(),
- source: 'main_app_global_service'
- };
- const publishInfo: commonEventManager.CommonEventPublishData = {
- data: JSON.stringify(broadcastData)
- };
- hilog.info(0x0000, TAG, `🚀 Publishing CommonEvent: ${PLAYER_STATE_CHANGED_EVENT}, data size: ${publishInfo.data?.length || 0} characters`);
-
- // 发送状态变化事件到 Form 进程
- await commonEventManager.publish(PLAYER_STATE_CHANGED_EVENT, publishInfo, (err) => {
- if (err) {
- hilog.error(0x0000, TAG, `❌ Failed to broadcast to form process: ${JSON.stringify(err)}`);
- } else {
- hilog.info(0x0000, TAG, `✅ Successfully broadcasted widget data to form process: ${data.currentSong.title}`);
- }
- });
- } catch (error) {
- hilog.error(0x0000, TAG, `❌ Error broadcasting to form process: ${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
- }
- };
-
- // 注意:这里不再简单地基于索引修复按钮状态
- // 因为按钮状态应该由 UnifiedPlayerService 中的播放模式逻辑正确计算
- // 在随机播放模式下,按钮状态取决于播放历史记录,而不是简单的索引位置
- if (data.playlist.totalCount > 1) {
- // 保持从 UnifiedPlayerService 传来的状态,这些状态已经考虑了播放模式
- hilog.info(0x0000, TAG, `Keeping original button states from UnifiedPlayerService: hasNext=${data.playlist.hasNext}, hasPrevious=${data.playlist.hasPrevious}`);
- } else {
- // 单首歌或无歌曲时,禁用所有按钮
- correctedData.playlist.hasNext = false;
- correctedData.playlist.hasPrevious = false;
- hilog.info(0x0000, TAG, `Single song or empty playlist, disabled all navigation buttons`);
- }
-
- return correctedData;
- }
- }
|