|
|
@@ -22,7 +22,8 @@ import { systemShare } from '@kit.ShareKit';
|
|
|
import { SpiderMan } from '@simplepeng/spider-man';
|
|
|
import { CustomCrashHandler } from '../common/utils/CustomCrashHandler';
|
|
|
import { smartMobilityCommon } from '@kit.CarKit';
|
|
|
-
|
|
|
+import { url } from '@kit.ArkTS';
|
|
|
+import { display } from '@kit.ArkUI';
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -104,7 +105,8 @@ class MyParcelable implements rpc.Parcelable {
|
|
|
export default class EntryAbility extends UIAbility {
|
|
|
// UI上下文对象,用于获取窗口信息
|
|
|
private uiContext?: UIContext;
|
|
|
- private awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
|
|
|
+ // private awareness: smartMobilityCommon.SmartMobilityAwareness = smartMobilityCommon.getSmartMobilityAwareness();
|
|
|
+ private awareness: smartMobilityCommon.SmartMobilityAwareness|undefined = canIUse("SystemCapability.SmartOptimizer.SmartMobility")?smartMobilityCommon.getSmartMobilityAwareness():undefined;
|
|
|
|
|
|
/**
|
|
|
* 窗口尺寸变化回调函数
|
|
|
@@ -154,10 +156,10 @@ export default class EntryAbility extends UIAbility {
|
|
|
AppUtil.init(this.context);
|
|
|
AppStorage.setOrCreate('context', this.context);
|
|
|
hilog.info(0x0000, 'Heanup2', '%{public}s', 'Ability onCreate');
|
|
|
-
|
|
|
+
|
|
|
// 注册卡片call事件监听器
|
|
|
this.registerWidgetCallListeners();
|
|
|
-
|
|
|
+
|
|
|
// 异步初始化统一播放器服务,避免阻塞生命周期
|
|
|
this.initializePlayerServiceAsync();
|
|
|
|
|
|
@@ -175,7 +177,7 @@ export default class EntryAbility extends UIAbility {
|
|
|
async onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): Promise<void> {
|
|
|
hilog.info(0x0000, 'Heanup2', `onNewWant, want=${JSON.stringify(want)}`);
|
|
|
super.onNewWant(want, launchParam);
|
|
|
-
|
|
|
+
|
|
|
// 异步处理Want参数,避免阻塞生命周期
|
|
|
this.handleWantAsync(want);
|
|
|
this.handleWeChatCallIfNeed(want)
|
|
|
@@ -237,7 +239,7 @@ export default class EntryAbility extends UIAbility {
|
|
|
|
|
|
onDestroy() {
|
|
|
hilog.info(0x0000, 'Heanup2', '%{public}s', 'Ability onDestroy');
|
|
|
-
|
|
|
+
|
|
|
// 关键修复:在APP销毁时保存播放器状态
|
|
|
try {
|
|
|
const unifiedPlayerService = UnifiedPlayerService.getInstance();
|
|
|
@@ -249,27 +251,31 @@ export default class EntryAbility extends UIAbility {
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `Failed to release UnifiedPlayerService: ${error}`);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 注销卡片call事件监听器
|
|
|
this.unregisterWidgetCallListeners();
|
|
|
-
|
|
|
- let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.CAR_HOP];
|
|
|
- // 出行连接状态回调函数
|
|
|
- const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
|
|
|
- hilog.info(0x0000, 'Received smart mobility info: ', JSON.stringify(info));
|
|
|
- };
|
|
|
- // 解注册智慧出行连接状态的监听 示例2
|
|
|
- this.awareness.off('smartMobilityStatus', types, callBack);
|
|
|
- }
|
|
|
+
|
|
|
+ hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
|
|
|
+ if(this.awareness){
|
|
|
+ let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.CAR_HOP];
|
|
|
+ // 出行连接状态回调函数
|
|
|
+ const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
|
|
|
+ hilog.info(0x0000, 'Received smart mobility info: ', JSON.stringify(info));
|
|
|
+ };
|
|
|
+ // 解注册智慧出行连接状态的监听 示例2
|
|
|
+ this.awareness.off('smartMobilityStatus', types, callBack);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
onWindowStageCreate(windowStage: window.WindowStage) {
|
|
|
// Main window is created, set main page for this ability
|
|
|
hilog.info(0x0000, 'Heanup2', '%{public}s', 'Ability onWindowStageCreate');
|
|
|
-
|
|
|
+
|
|
|
// 使用自定义崩溃处理器替代 SpiderMan.init()
|
|
|
// SpiderMan.init();
|
|
|
CustomCrashHandler.init();
|
|
|
-
|
|
|
+
|
|
|
AppUtil.init(this.context);
|
|
|
|
|
|
//1.获取应用主窗口。
|
|
|
@@ -280,6 +286,14 @@ export default class EntryAbility extends UIAbility {
|
|
|
// LogUtil.info( 'getMainWindow = ');
|
|
|
GlobalContext.getContext().setObject('windowClass', data); // 使用GlobalContext替代globalThis
|
|
|
|
|
|
+ let curDisplay = display.getDisplayByIdSync(windowClass.getWindowProperties().displayId);
|
|
|
+ console.info('twocold curDisplay = '+curDisplay.name);
|
|
|
+ if(curDisplay.name =='HiCar'||curDisplay.name =='SuperLauncher'){
|
|
|
+ AppStorage.setOrCreate('curDisplayIsHiCar', true);
|
|
|
+ }else{
|
|
|
+ AppStorage.setOrCreate('curDisplayIsHiCar', false);
|
|
|
+ }
|
|
|
+
|
|
|
windowClass.setWindowLayoutFullScreen(true).then(() => {
|
|
|
console.info('Succeeded in setting the window layout to full-screen mode.');
|
|
|
}).catch((e: BusinessError) => {
|
|
|
@@ -349,55 +363,60 @@ export default class EntryAbility extends UIAbility {
|
|
|
onForeground() {
|
|
|
// Ability has brought to foreground
|
|
|
hilog.info(0x0000, 'Heanup2', '%{public}s', 'Ability onForeground start');
|
|
|
-
|
|
|
+
|
|
|
// 异步处理前台逻辑,避免阻塞生命周期
|
|
|
setTimeout(() => {
|
|
|
this.handleForegroundAsync();
|
|
|
}, 10);
|
|
|
-
|
|
|
+
|
|
|
hilog.info(0x0000, 'Heanup2', '%{public}s', 'Ability onForeground end');
|
|
|
}
|
|
|
|
|
|
onBackground() {
|
|
|
// Ability has back to background
|
|
|
hilog.info(0x0000, 'Heanup2', '%{public}s', 'Ability onBackground start');
|
|
|
-
|
|
|
+
|
|
|
// 异步处理后台逻辑,避免阻塞生命周期
|
|
|
setTimeout(() => {
|
|
|
this.handleBackgroundAsync();
|
|
|
}, 10);
|
|
|
-
|
|
|
+
|
|
|
hilog.info(0x0000, 'Heanup2', '%{public}s', 'Ability onBackground end');
|
|
|
}
|
|
|
|
|
|
|
|
|
getHiCarStatus(){
|
|
|
- this.awareness = smartMobilityCommon.getSmartMobilityAwareness();
|
|
|
+ if(this.awareness){
|
|
|
+ // this.awareness = smartMobilityCommon.getSmartMobilityAwareness();
|
|
|
|
|
|
- // 业务类型
|
|
|
- let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.HICAR];
|
|
|
- // 获取出行业务连接状态
|
|
|
- let info = this.awareness.getSmartMobilityStatus(types[0]);
|
|
|
+ // 业务类型
|
|
|
+ let types: smartMobilityCommon.SmartMobilityType[] = [smartMobilityCommon.SmartMobilityType.HICAR];
|
|
|
+ // 获取出行业务连接状态
|
|
|
+ let info = this.awareness.getSmartMobilityStatus(types[0]);
|
|
|
|
|
|
- hilog.info(0x0000, 'getHiCarStatus info: ', JSON.stringify(info));
|
|
|
- if(info&&info.status==1){
|
|
|
- AppStorage.setOrCreate('isHiCarStatus', true);
|
|
|
- }else{
|
|
|
- AppStorage.setOrCreate('isHiCarStatus', false);
|
|
|
- }
|
|
|
-
|
|
|
- // 出行连接状态回调函数
|
|
|
- const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
|
|
|
- hilog.info(0x0000, 'getHiCarStatus Received smart mobility info: ', JSON.stringify(info));
|
|
|
+ hilog.info(0x0000, 'getHiCarStatus info: ', JSON.stringify(info));
|
|
|
if(info&&info.status==1){
|
|
|
AppStorage.setOrCreate('isHiCarStatus', true);
|
|
|
}else{
|
|
|
AppStorage.setOrCreate('isHiCarStatus', false);
|
|
|
}
|
|
|
- this.sendChangeEvent()
|
|
|
- };
|
|
|
- // 注册智慧出行连接状态的监听
|
|
|
- this.awareness.on('smartMobilityStatus', types, callBack);
|
|
|
+
|
|
|
+ // 出行连接状态回调函数
|
|
|
+ const callBack = (info: smartMobilityCommon.SmartMobilityInfo) => {
|
|
|
+ hilog.info(0x0000, 'getHiCarStatus Received smart mobility info: ', JSON.stringify(info));
|
|
|
+ if(info&&info.status==1){
|
|
|
+ AppStorage.setOrCreate('isHiCarStatus', true);
|
|
|
+ }else{
|
|
|
+ AppStorage.setOrCreate('isHiCarStatus', false);
|
|
|
+ }
|
|
|
+ this.sendChangeEvent()
|
|
|
+ };
|
|
|
+ // 注册智慧出行连接状态的监听
|
|
|
+ this.awareness.on('smartMobilityStatus', types, callBack);
|
|
|
+ }else{
|
|
|
+ AppStorage.setOrCreate('isHiCarStatus', false);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
}
|
|
|
@@ -419,12 +438,12 @@ export default class EntryAbility extends UIAbility {
|
|
|
try {
|
|
|
const params: Record<string, Object> = JSON.parse(data.readString()) as Record<string, Object>;
|
|
|
hilog.info(0x0000, 'Heanup2', `🎵 Widget call: playPause received`);
|
|
|
-
|
|
|
+
|
|
|
// 异步发送播放/暂停事件到主应用(包含服务就绪检查)
|
|
|
this.sendWidgetControlEvent('PLAY_PAUSE', params).catch((error: Error) => {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ playPause async error: ${error}`);
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
return new MyParcelable(1, 'playPause_success');
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ playPause handler error: ${error}`);
|
|
|
@@ -438,12 +457,12 @@ export default class EntryAbility extends UIAbility {
|
|
|
hilog.info(0x0000, 'Heanup2', `🎵 Widget call: nextSong received`);
|
|
|
const params: Record<string, Object> = JSON.parse(data.readString()) as Record<string, Object>;
|
|
|
hilog.info(0x0000, 'Heanup2', `Widget nextSong params: ${JSON.stringify(params)}`);
|
|
|
-
|
|
|
+
|
|
|
// 异步发送下一首事件到主应用(包含服务就绪检查)
|
|
|
this.sendWidgetControlEvent('NEXT_SONG', params).catch((error: Error) => {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ nextSong async error: ${error}`);
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
return new MyParcelable(2, 'nextSong_success');
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ nextSong handler error: ${error}`);
|
|
|
@@ -457,12 +476,12 @@ export default class EntryAbility extends UIAbility {
|
|
|
hilog.info(0x0000, 'Heanup2', `🎵 Widget call: prevSong received`);
|
|
|
const params: Record<string, Object> = JSON.parse(data.readString()) as Record<string, Object>;
|
|
|
hilog.info(0x0000, 'Heanup2', `Widget prevSong params: ${JSON.stringify(params)}`);
|
|
|
-
|
|
|
+
|
|
|
// 异步发送上一首事件到主应用(包含服务就绪检查)
|
|
|
this.sendWidgetControlEvent('PREV_SONG', params).catch((error: Error) => {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ prevSong async error: ${error}`);
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
return new MyParcelable(3, 'prevSong_success');
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ prevSong handler error: ${error}`);
|
|
|
@@ -476,12 +495,12 @@ export default class EntryAbility extends UIAbility {
|
|
|
hilog.info(0x0000, 'Heanup2', `🎵 Widget call: openApp received`);
|
|
|
const params: Record<string, Object> = JSON.parse(data.readString()) as Record<string, Object>;
|
|
|
hilog.info(0x0000, 'Heanup2', `Widget openApp params: ${JSON.stringify(params)}`);
|
|
|
-
|
|
|
+
|
|
|
// 异步发送打开应用事件到主应用
|
|
|
this.sendWidgetControlEvent('OPEN_APP', params).catch((error: Error) => {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ openApp async error: ${error}`);
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
return new MyParcelable(4, 'openApp_success');
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ openApp handler error: ${error}`);
|
|
|
@@ -520,29 +539,29 @@ export default class EntryAbility extends UIAbility {
|
|
|
private async sendWidgetControlEvent(command: string, params: Record<string, Object>): Promise<void> {
|
|
|
try {
|
|
|
hilog.info(0x0000, 'Heanup2', `🎵 收到桌面卡片指令: ${command}`);
|
|
|
-
|
|
|
+
|
|
|
// 获取UnifiedPlayerService实例
|
|
|
const unifiedService = UnifiedPlayerService.getInstance();
|
|
|
-
|
|
|
+
|
|
|
// 确保服务已经初始化
|
|
|
if (!unifiedService) {
|
|
|
hilog.error(0x0000, 'Heanup2', '❌ UnifiedPlayerService 未初始化');
|
|
|
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();
|
|
|
@@ -550,19 +569,19 @@ export default class EntryAbility extends UIAbility {
|
|
|
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) {
|
|
|
@@ -571,20 +590,20 @@ export default class EntryAbility extends UIAbility {
|
|
|
}
|
|
|
hilog.info(0x0000, 'Heanup2', `✅ 强制数据恢复完成,播放列表大小: ${updatedPlaylist.length}`);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 根据命令执行相应的播放控制
|
|
|
switch (command) {
|
|
|
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}`);
|
|
|
-
|
|
|
+
|
|
|
if (hasWidgetState) {
|
|
|
// 新逻辑:根据卡片显示的状态来决定操作
|
|
|
// - 如果卡片显示播放按钮(widgetIsPlaying=false),则执行播放操作
|
|
|
@@ -612,31 +631,31 @@ export default class EntryAbility extends UIAbility {
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
-
|
|
|
+
|
|
|
case 'NEXT_SONG':
|
|
|
await unifiedService.playNext();
|
|
|
hilog.info(0x0000, 'Heanup2', '✅ Widget command: Next song');
|
|
|
break;
|
|
|
-
|
|
|
+
|
|
|
case 'PREV_SONG':
|
|
|
await unifiedService.playPrevious();
|
|
|
hilog.info(0x0000, 'Heanup2', '✅ Widget command: Previous song');
|
|
|
break;
|
|
|
-
|
|
|
+
|
|
|
case 'OPEN_APP':
|
|
|
hilog.info(0x0000, 'Heanup2', '✅ Widget command: Open app (handled by UI)');
|
|
|
break;
|
|
|
-
|
|
|
+
|
|
|
default:
|
|
|
hilog.warn(0x0000, 'Heanup2', `❓ Unknown widget command: ${command}`);
|
|
|
break;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 操作完成后,广播最新状态给主应用UI和桌面卡片
|
|
|
setTimeout(() => {
|
|
|
this.broadcastCurrentPlayerState();
|
|
|
hilog.info(0x0000, 'Heanup2', '📡 桌面卡片操作后广播状态更新');
|
|
|
-
|
|
|
+
|
|
|
// 同时触发UnifiedPlayerService的状态广播,确保所有监听器都能收到更新
|
|
|
try {
|
|
|
unifiedService.broadcastCurrentState();
|
|
|
@@ -644,9 +663,9 @@ export default class EntryAbility extends UIAbility {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ 触发服务状态广播失败: ${error}`);
|
|
|
}
|
|
|
}, 100); // 短延迟,确保操作完全完成
|
|
|
-
|
|
|
+
|
|
|
hilog.info(0x0000, 'Heanup2', `✅ Widget control command processed: ${command}`);
|
|
|
-
|
|
|
+
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ Failed to process widget control command: ${error}`);
|
|
|
}
|
|
|
@@ -658,16 +677,16 @@ export default class EntryAbility extends UIAbility {
|
|
|
private async forceDataRestoration(unifiedService: UnifiedPlayerService): Promise<void> {
|
|
|
try {
|
|
|
hilog.info(0x0000, 'Heanup2', '🔄 开始强制数据恢复...');
|
|
|
-
|
|
|
+
|
|
|
// 检查是否已经有数据恢复完成
|
|
|
if (unifiedService.isDataRestorationCompleted()) {
|
|
|
hilog.info(0x0000, 'Heanup2', '✅ 数据已恢复,无需强制恢复');
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 调用UnifiedPlayerService的强制数据恢复方法
|
|
|
const restored = await unifiedService.forceDataRestoration();
|
|
|
-
|
|
|
+
|
|
|
if (restored) {
|
|
|
hilog.info(0x0000, 'Heanup2', '✅ 强制数据恢复成功');
|
|
|
} else {
|
|
|
@@ -675,10 +694,10 @@ export default class EntryAbility extends UIAbility {
|
|
|
// 如果强制恢复失败,再等待一段时间
|
|
|
await unifiedService.waitForDataRestoration(2000);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 额外等待一小段时间,确保播放列表数据完全加载
|
|
|
await new Promise<void>(resolve => setTimeout(resolve, 300));
|
|
|
-
|
|
|
+
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ 强制数据恢复失败: ${error}`);
|
|
|
}
|
|
|
@@ -690,27 +709,27 @@ 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) {
|
|
|
@@ -720,28 +739,28 @@ export default class EntryAbility extends UIAbility {
|
|
|
} 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) {
|
|
|
@@ -760,14 +779,14 @@ export default class EntryAbility extends UIAbility {
|
|
|
hilog.info(0x0000, 'Heanup2', '🔄 开始异步初始化 UnifiedPlayerService');
|
|
|
await UnifiedPlayerService.getInstance().initialize(this.context);
|
|
|
hilog.info(0x0000, 'Heanup2', '✅ UnifiedPlayerService 异步初始化成功');
|
|
|
-
|
|
|
+
|
|
|
// 对于冷启动场景,立即尝试数据恢复
|
|
|
const unifiedService = UnifiedPlayerService.getInstance();
|
|
|
if (!unifiedService.isDataRestorationCompleted()) {
|
|
|
hilog.info(0x0000, 'Heanup2', '🔄 冷启动检测到数据未恢复,开始预恢复');
|
|
|
await unifiedService.forceDataRestoration();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 延迟广播当前状态,确保卡片能接收到初始状态
|
|
|
setTimeout(() => {
|
|
|
this.broadcastCurrentPlayerState();
|
|
|
@@ -784,14 +803,14 @@ export default class EntryAbility extends UIAbility {
|
|
|
private handleForegroundAsync(): void {
|
|
|
try {
|
|
|
hilog.info(0x0000, 'Heanup2', '🔄 处理前台逻辑');
|
|
|
-
|
|
|
+
|
|
|
// 确保播放器服务可用
|
|
|
const playerService = UnifiedPlayerService.getInstance();
|
|
|
if (playerService) {
|
|
|
// 广播当前状态给卡片
|
|
|
this.broadcastCurrentPlayerState();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
hilog.info(0x0000, 'Heanup2', '✅ 前台逻辑处理完成');
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ 前台逻辑处理失败: ${error}`);
|
|
|
@@ -804,10 +823,10 @@ export default class EntryAbility extends UIAbility {
|
|
|
private handleBackgroundAsync(): void {
|
|
|
try {
|
|
|
hilog.info(0x0000, 'Heanup2', '🔄 处理后台逻辑');
|
|
|
-
|
|
|
+
|
|
|
// 后台时可以执行一些清理或保存操作
|
|
|
// 但要确保不会阻塞生命周期
|
|
|
-
|
|
|
+
|
|
|
hilog.info(0x0000, 'Heanup2', '✅ 后台逻辑处理完成');
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, 'Heanup2', `❌ 后台逻辑处理失败: ${error}`);
|
|
|
@@ -853,7 +872,7 @@ export default class EntryAbility extends UIAbility {
|
|
|
const currentSong = unifiedService.getCurrentSong();
|
|
|
const playlist = unifiedService.getPlaylist();
|
|
|
const currentIndex = unifiedService.getCurrentIndex();
|
|
|
-
|
|
|
+
|
|
|
if (currentSong) {
|
|
|
// 构建播放器状态广播数据
|
|
|
const broadcastData: BroadcastData = {
|
|
|
@@ -884,18 +903,18 @@ export default class EntryAbility extends UIAbility {
|
|
|
totalCount: playlist.length
|
|
|
} as PlaylistBroadcast
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// 发送状态变化事件
|
|
|
const publishInfo: PublishInfo = {
|
|
|
data: JSON.stringify(broadcastData)
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// 使用emitter发送事件
|
|
|
const eventData: EventDataWrapper = {
|
|
|
data: broadcastData
|
|
|
};
|
|
|
emitter.emit({ eventId: 1001 }, eventData); // 使用特定的事件ID
|
|
|
-
|
|
|
+
|
|
|
hilog.info(0x0000, 'Heanup2', `📡 Broadcasted current player state: ${currentSong.name}, isPlaying=${currentState.isPlaying}`);
|
|
|
} else {
|
|
|
hilog.info(0x0000, 'Heanup2', '📡 No current song to broadcast');
|