|
@@ -110,6 +110,11 @@ export default class EntryAbility extends UIAbility {
|
|
|
private awareness: smartMobilityCommon.SmartMobilityAwareness | undefined =
|
|
private awareness: smartMobilityCommon.SmartMobilityAwareness | undefined =
|
|
|
canIUse("SystemCapability.SmartOptimizer.SmartMobility") ? smartMobilityCommon.getSmartMobilityAwareness() :
|
|
canIUse("SystemCapability.SmartOptimizer.SmartMobility") ? smartMobilityCommon.getSmartMobilityAwareness() :
|
|
|
undefined;
|
|
undefined;
|
|
|
|
|
+
|
|
|
|
|
+ // 服务就绪状态缓存,避免在 widget 控制事件中重复检查
|
|
|
|
|
+ private isServiceFullyReady: boolean = false;
|
|
|
|
|
+ private lastServiceReadyCheckTime: number = 0;
|
|
|
|
|
+ private readonly SERVICE_READY_CACHE_DURATION = 60000; // 缓存有效期60秒
|
|
|
/**
|
|
/**
|
|
|
* 窗口尺寸变化回调函数
|
|
* 窗口尺寸变化回调函数
|
|
|
* @param windowSize 新的窗口尺寸对象
|
|
* @param windowSize 新的窗口尺寸对象
|
|
@@ -538,129 +543,147 @@ export default class EntryAbility extends UIAbility {
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 发送卡片控制事件到主应用(增强版本,确保服务已初始化)
|
|
|
|
|
|
|
+ * 发送卡片控制事件到主应用(优化版本,使用预缓存的服务状态)
|
|
|
*/
|
|
*/
|
|
|
private async sendWidgetControlEvent(command: string, params: Record<string, Object>): Promise<void> {
|
|
private async sendWidgetControlEvent(command: string, params: Record<string, Object>): Promise<void> {
|
|
|
try {
|
|
try {
|
|
|
-
|
|
|
|
|
- // 获取UnifiedPlayerService实例
|
|
|
|
|
|
|
+ // 获取UnifiedPlayerService实例 - 基础检查
|
|
|
const unifiedService = UnifiedPlayerService.getInstance();
|
|
const unifiedService = UnifiedPlayerService.getInstance();
|
|
|
-
|
|
|
|
|
- // 确保服务已经初始化
|
|
|
|
|
if (!unifiedService) {
|
|
if (!unifiedService) {
|
|
|
hilog.error(0x0000, 'Heanup2', '❌ UnifiedPlayerService 未初始化');
|
|
hilog.error(0x0000, 'Heanup2', '❌ UnifiedPlayerService 未初始化');
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 获取详细的服务就绪状态信息
|
|
|
|
|
- const readinessInfo = unifiedService.getServiceReadinessInfo();
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', `🔍 Service readiness: ${JSON.stringify(readinessInfo.details)}`);
|
|
|
|
|
-
|
|
|
|
|
- // 智能等待服务完全就绪(多级检查)
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '🔄 检查服务就绪状态...');
|
|
|
|
|
- let ready = await this.waitForServiceReady(unifiedService);
|
|
|
|
|
-
|
|
|
|
|
- if (ready) {
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ UnifiedPlayerService 已就绪');
|
|
|
|
|
- } else {
|
|
|
|
|
- hilog.warn(0x0000, 'Heanup2', '⚠️ UnifiedPlayerService 未完全就绪,尝试执行基础操作');
|
|
|
|
|
-
|
|
|
|
|
- // 检查是否至少可以进行基础操作
|
|
|
|
|
- if (!unifiedService.isAllServicesReady()) {
|
|
|
|
|
- const finalReadinessInfo = unifiedService.getServiceReadinessInfo();
|
|
|
|
|
- hilog.error(0x0000, 'Heanup2',
|
|
|
|
|
- `❌ 基础服务未就绪,无法执行操作: ${JSON.stringify(finalReadinessInfo.details)}`);
|
|
|
|
|
|
|
+ // 使用预缓存的服务就绪状态 - 大幅减少检查时间
|
|
|
|
|
+ if (!this.isServiceFullyReady) {
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', '⚠️ 缓存显示服务未就绪,执行快速验证...');
|
|
|
|
|
+ const quickValidation = await this.validateAndCacheServiceReadiness();
|
|
|
|
|
+ if (!quickValidation) {
|
|
|
|
|
+ hilog.warn(0x0000, 'Heanup2', '❌ 服务验证失败,无法执行widget控制操作');
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 检查初始化状态
|
|
|
|
|
- const currentState = unifiedService.getCurrentState();
|
|
|
|
|
- const playlist = unifiedService.getPlaylist();
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2',
|
|
|
|
|
- `🎵 Widget command - Service state: isPlaying=${currentState.isPlaying}, currentIndex=${currentState.currentIndex}, playlistSize=${playlist.length}`);
|
|
|
|
|
-
|
|
|
|
|
- // 对于播放控制操作,检查是否有可播放内容
|
|
|
|
|
- if ((command === 'PLAY_PAUSE' || command === 'NEXT_SONG' || command === 'PREV_SONG') && playlist.length === 0) {
|
|
|
|
|
- hilog.warn(0x0000, 'Heanup2', '⚠️ 播放列表为空,尝试强制数据恢复');
|
|
|
|
|
-
|
|
|
|
|
- // 强制触发数据恢复
|
|
|
|
|
- await this.forceDataRestoration(unifiedService);
|
|
|
|
|
-
|
|
|
|
|
- // 重新检查播放列表
|
|
|
|
|
- const updatedPlaylist = unifiedService.getPlaylist();
|
|
|
|
|
- if (updatedPlaylist.length === 0) {
|
|
|
|
|
- hilog.warn(0x0000, 'Heanup2', '⚠️ 强制数据恢复后播放列表仍为空,无法执行播放控制操作');
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ // 播放列表检查 - 简化版本
|
|
|
|
|
+ if (command === 'PLAY_PAUSE' || command === 'NEXT_SONG' || command === 'PREV_SONG') {
|
|
|
|
|
+ const playlist = unifiedService.getPlaylist();
|
|
|
|
|
+ if (playlist.length === 0) {
|
|
|
|
|
+ hilog.warn(0x0000, 'Heanup2', '⚠️ 播放列表为空,尝试快速数据恢复');
|
|
|
|
|
+ await this.forceDataRestoration(unifiedService);
|
|
|
|
|
+
|
|
|
|
|
+ // 简化重新检查
|
|
|
|
|
+ const updatedPlaylist = unifiedService.getPlaylist();
|
|
|
|
|
+ if (updatedPlaylist.length === 0) {
|
|
|
|
|
+ hilog.warn(0x0000, 'Heanup2', '❌ 数据恢复后播放列表仍为空,跳过操作');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', `✅ 播放列表恢复成功: ${updatedPlaylist.length} 首歌曲`);
|
|
|
}
|
|
}
|
|
|
- hilog.info(0x0000, 'Heanup2', `✅ 强制数据恢复完成,播放列表大小: ${updatedPlaylist.length}`);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 根据命令执行相应的播放控制
|
|
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', `⚡ 执行widget控制命令: ${command} (服务已就绪)`);
|
|
|
|
|
+
|
|
|
|
|
+ // 直接执行命令 - 移除复杂的状态检查
|
|
|
switch (command) {
|
|
switch (command) {
|
|
|
case 'PLAY_PAUSE':
|
|
case 'PLAY_PAUSE':
|
|
|
- // 获取卡片传递的当前显示状态
|
|
|
|
|
- const widgetIsPlaying = params['widgetIsPlaying'] as boolean;
|
|
|
|
|
- const hasWidgetState = widgetIsPlaying !== undefined;
|
|
|
|
|
-
|
|
|
|
|
- // 获取服务内部状态
|
|
|
|
|
- const latestState = unifiedService.getCurrentState();
|
|
|
|
|
-
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', `🎵 卡片显示状态: isPlaying=${widgetIsPlaying} (有效: ${hasWidgetState})`);
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', `🎵 服务内部状态: isPlaying=${latestState.isPlaying}`);
|
|
|
|
|
-
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '⚠️ 使用向后兼容模式(基于服务状态)');
|
|
|
|
|
- if (latestState.isPlaying) {
|
|
|
|
|
|
|
+ // 简化版本:基于服务状态直接切换
|
|
|
|
|
+ const currentState = unifiedService.getCurrentState();
|
|
|
|
|
+ if (currentState.isPlaying) {
|
|
|
await unifiedService.pause();
|
|
await unifiedService.pause();
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ 兼容模式:暂停播放');
|
|
|
|
|
} else {
|
|
} else {
|
|
|
- hilog.info(0x0000, 'Heanup2', '🔄 兼容模式:开始播放');
|
|
|
|
|
await unifiedService.startPlayOrResumePlay();
|
|
await unifiedService.startPlayOrResumePlay();
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ 兼容模式:开始播放');
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case 'NEXT_SONG':
|
|
case 'NEXT_SONG':
|
|
|
await unifiedService.playNext();
|
|
await unifiedService.playNext();
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ Widget command: Next song');
|
|
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case 'PREV_SONG':
|
|
case 'PREV_SONG':
|
|
|
await unifiedService.playPrevious();
|
|
await unifiedService.playPrevious();
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ Widget command: Previous song');
|
|
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case 'OPEN_APP':
|
|
case 'OPEN_APP':
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ Widget command: Open app (handled by UI)');
|
|
|
|
|
- break;
|
|
|
|
|
- case 'OPEN_APP':
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ Widget command: Open app (handled by UI)');
|
|
|
|
|
|
|
+ // 打开应用由UI处理
|
|
|
break;
|
|
break;
|
|
|
|
|
+
|
|
|
case "toggleFavorite":
|
|
case "toggleFavorite":
|
|
|
await unifiedService.toggleFavorite();
|
|
await unifiedService.toggleFavorite();
|
|
|
break;
|
|
break;
|
|
|
|
|
+
|
|
|
default:
|
|
default:
|
|
|
- hilog.warn(0x0000, 'Heanup2', `❓ Unknown widget command: ${command}`);
|
|
|
|
|
|
|
+ hilog.warn(0x0000, 'Heanup2', `❓ 未知widget命令: ${command}`);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 操作完成后,广播最新状态给主应用UI和桌面卡片
|
|
|
|
|
- setTimeout(() => {
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '📡 桌面卡片操作后广播状态更新');
|
|
|
|
|
|
|
+ // 立即广播状态更新
|
|
|
|
|
+ try {
|
|
|
|
|
+ unifiedService.broadcastCurrentState();
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', `✅ Widget命令执行完成: ${command}`);
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ hilog.error(0x0000, 'Heanup2', `❌ 状态广播失败: ${error}`);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 同时触发UnifiedPlayerService的状态广播,确保所有监听器都能收到更新
|
|
|
|
|
- try {
|
|
|
|
|
- unifiedService.broadcastCurrentState();
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- hilog.error(0x0000, 'Heanup2', `❌ 触发服务状态广播失败: ${error}`);
|
|
|
|
|
- }
|
|
|
|
|
- }, 100); // 短延迟,确保操作完全完成
|
|
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ hilog.error(0x0000, 'Heanup2', `❌ Failed to process widget control command: ${error}`);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 检查并缓存服务就绪状态
|
|
|
|
|
+ */
|
|
|
|
|
+ private async validateAndCacheServiceReadiness(): Promise<boolean> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const currentTime = Date.now();
|
|
|
|
|
+
|
|
|
|
|
+ // 如果缓存仍然有效,直接返回缓存结果
|
|
|
|
|
+ if (this.isServiceFullyReady &&
|
|
|
|
|
+ (currentTime - this.lastServiceReadyCheckTime) < this.SERVICE_READY_CACHE_DURATION) {
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', '✅ 使用服务就绪状态缓存');
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取UnifiedPlayerService实例
|
|
|
|
|
+ const unifiedService = UnifiedPlayerService.getInstance();
|
|
|
|
|
+ if (!unifiedService) {
|
|
|
|
|
+ hilog.error(0x0000, 'Heanup2', '❌ UnifiedPlayerService 未初始化');
|
|
|
|
|
+ this.isServiceFullyReady = false;
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', `✅ Widget control command processed: ${command}`);
|
|
|
|
|
|
|
+ // 执行完整的服务就绪检查
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', '🔄 执行服务就绪状态检查...');
|
|
|
|
|
+
|
|
|
|
|
+ // 检查基础服务就绪状态
|
|
|
|
|
+ if (!unifiedService.isAllServicesReady()) {
|
|
|
|
|
+ this.isServiceFullyReady = false;
|
|
|
|
|
+ hilog.warn(0x0000, 'Heanup2', '⚠️ 基础服务未就绪');
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ // 检查播放列表是否有数据
|
|
|
|
|
+ const playlist = unifiedService.getPlaylist();
|
|
|
|
|
+ if (playlist.length === 0) {
|
|
|
|
|
+ // 对于空播放列表,可能需要等待数据恢复
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', '🔄 播放列表为空,等待数据恢复...');
|
|
|
|
|
+ const isReady = await unifiedService.waitForAllServicesReady(3000, true);
|
|
|
|
|
+ if (!isReady) {
|
|
|
|
|
+ this.isServiceFullyReady = false;
|
|
|
|
|
+ hilog.warn(0x0000, 'Heanup2', '⚠️ 数据恢复超时');
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 更新缓存状态
|
|
|
|
|
+ this.isServiceFullyReady = true;
|
|
|
|
|
+ this.lastServiceReadyCheckTime = currentTime;
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', '✅ 服务就绪状态检查完成并已缓存');
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
- hilog.error(0x0000, 'Heanup2', `❌ Failed to process widget control command: ${error}`);
|
|
|
|
|
|
|
+ hilog.error(0x0000, 'Heanup2', `❌ 服务就绪状态检查失败: ${error}`);
|
|
|
|
|
+ this.isServiceFullyReady = false;
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -696,72 +719,6 @@ export default class EntryAbility extends UIAbility {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 智能等待服务就绪(增强版本)
|
|
|
|
|
- */
|
|
|
|
|
- private async waitForServiceReady(unifiedService: UnifiedPlayerService): Promise<boolean> {
|
|
|
|
|
- try {
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '🔄 开始等待UnifiedPlayerService完全就绪');
|
|
|
|
|
-
|
|
|
|
|
- // 检查是否是刚启动的应用(数据还没恢复)
|
|
|
|
|
- const currentPlaylist = unifiedService.getPlaylist();
|
|
|
|
|
- const isJustStarted = currentPlaylist.length === 0;
|
|
|
|
|
- const isDataRestored = unifiedService.isDataRestorationCompleted();
|
|
|
|
|
-
|
|
|
|
|
- if (isJustStarted || !isDataRestored) {
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2',
|
|
|
|
|
- `🔄 检测到冷启动状态 - 播放列表为空: ${isJustStarted}, 数据未恢复: ${!isDataRestored}`);
|
|
|
|
|
-
|
|
|
|
|
- // 对于冷启动,给予更长的等待时间,并强制检查数据恢复
|
|
|
|
|
- const isReady = await unifiedService.waitForAllServicesReady(10000, true);
|
|
|
|
|
-
|
|
|
|
|
- if (isReady) {
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ UnifiedPlayerService完全就绪(冷启动)');
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 如果仍未就绪,尝试强制触发数据恢复
|
|
|
|
|
- hilog.warn(0x0000, 'Heanup2', '⚠️ 冷启动等待超时,尝试强制数据恢复');
|
|
|
|
|
- await this.forceDataRestoration(unifiedService);
|
|
|
|
|
-
|
|
|
|
|
- // 再次检查服务状态
|
|
|
|
|
- const retryReady = await unifiedService.waitForAllServicesReady(3000, false);
|
|
|
|
|
- if (retryReady) {
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ 强制恢复后服务就绪');
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- // 对于已经有数据的情况,使用正常的等待时间
|
|
|
|
|
- const isReady = await unifiedService.waitForAllServicesReady(5000, true);
|
|
|
|
|
-
|
|
|
|
|
- if (isReady) {
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '✅ UnifiedPlayerService完全就绪');
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 如果主要服务就绪但数据未恢复,再做一次宽松检查
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', '⚠️ 主要检查超时,进行备用检查');
|
|
|
|
|
- const basicReady = await unifiedService.waitForAllServicesReady(2000, false);
|
|
|
|
|
-
|
|
|
|
|
- if (basicReady) {
|
|
|
|
|
- // 检查是否至少有播放数据
|
|
|
|
|
- const currentState = unifiedService.getCurrentState();
|
|
|
|
|
- const playlist = unifiedService.getPlaylist();
|
|
|
|
|
-
|
|
|
|
|
- if (playlist.length > 0 || currentState.currentIndex >= 0) {
|
|
|
|
|
- hilog.info(0x0000, 'Heanup2', `✅ 基础服务就绪,播放列表: ${playlist.length} 首歌曲`);
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- hilog.warn(0x0000, 'Heanup2', '⚠️ Service readiness check timeout, proceeding with limited functionality');
|
|
|
|
|
- return false;
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- hilog.error(0x0000, 'Heanup2', `❌ Error waiting for service ready: ${error}`);
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 异步初始化播放器服务,避免阻塞生命周期
|
|
* 异步初始化播放器服务,避免阻塞生命周期
|
|
@@ -780,8 +737,20 @@ export default class EntryAbility extends UIAbility {
|
|
|
hilog.info(0x0000, 'Heanup2', '🔄 冷启动检测到数据未恢复,开始预恢复');
|
|
hilog.info(0x0000, 'Heanup2', '🔄 冷启动检测到数据未恢复,开始预恢复');
|
|
|
await unifiedService.forceDataRestoration();
|
|
await unifiedService.forceDataRestoration();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 初始化完成后,立即执行一次完整的服务就绪状态检查并缓存结果
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', '🔄 执行初始化后的服务就绪状态检查...');
|
|
|
|
|
+ const serviceReady = await this.validateAndCacheServiceReadiness();
|
|
|
|
|
+ if (serviceReady) {
|
|
|
|
|
+ hilog.info(0x0000, 'Heanup2', '✅ 服务完全就绪,widget控制事件已优化');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ hilog.warn(0x0000, 'Heanup2', '⚠️ 服务未完全就绪,widget控制事件将使用降级模式');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ UnifiedPlayerService 异步初始化失败: ${error}`);
|
|
hilog.error(0x0000, 'Heanup2', `❌ UnifiedPlayerService 异步初始化失败: ${error}`);
|
|
|
|
|
+ // 初始化失败时重置缓存状态
|
|
|
|
|
+ this.isServiceFullyReady = false;
|
|
|
}
|
|
}
|
|
|
}, 50); // 缩短延迟,让初始化更快开始
|
|
}, 50); // 缩短延迟,让初始化更快开始
|
|
|
}
|
|
}
|