|
|
@@ -110,11 +110,11 @@ export default class EntryAbility extends UIAbility {
|
|
|
private awareness: smartMobilityCommon.SmartMobilityAwareness | undefined =
|
|
|
canIUse("SystemCapability.SmartOptimizer.SmartMobility") ? smartMobilityCommon.getSmartMobilityAwareness() :
|
|
|
undefined;
|
|
|
-
|
|
|
// 服务就绪状态缓存,避免在 widget 控制事件中重复检查
|
|
|
private isServiceFullyReady: boolean = false;
|
|
|
private lastServiceReadyCheckTime: number = 0;
|
|
|
- private readonly SERVICE_READY_CACHE_DURATION = 60000; // 缓存有效期60秒
|
|
|
+ private readonly SERVICE_READY_CACHE_DURATION = 60000;
|
|
|
+ // 缓存有效期60秒
|
|
|
/**
|
|
|
* 窗口尺寸变化回调函数
|
|
|
* @param windowSize 新的窗口尺寸对象
|
|
|
@@ -155,7 +155,7 @@ export default class EntryAbility extends UIAbility {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- private unifiedService: UnifiedPlayerService=UnifiedPlayerService.getInstance();
|
|
|
+ private unifiedService: UnifiedPlayerService = UnifiedPlayerService.getInstance();
|
|
|
|
|
|
async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
|
|
|
AppUtil.init(this.context);
|
|
|
@@ -251,32 +251,22 @@ export default class EntryAbility extends UIAbility {
|
|
|
hilog.error(0x0000, 'Heanup2', `Failed to release UnifiedPlayerService: ${error}`);
|
|
|
});
|
|
|
}
|
|
|
- } catch (error) {
|
|
|
- hilog.error(0x0000, 'Heanup2', `Failed to release UnifiedPlayerService: ${error}`);
|
|
|
- }
|
|
|
-
|
|
|
- // 清理所有Form ID(应用卸载时)
|
|
|
- try {
|
|
|
+ // 清理所有Form ID(应用卸载时)
|
|
|
this.clearAllFormIds();
|
|
|
+ // 注销卡片call事件监听器
|
|
|
+ this.unregisterWidgetCallListeners();
|
|
|
+ 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
|
|
|
+ canIUse("SystemCapability.SmartOptimizer.SmartMobility") ?this.awareness.off('smartMobilityStatus', types, callBack): hilog.info(0x0000, 'Received smart mobility info: ', 'can not use smart mobility');
|
|
|
+ }
|
|
|
} catch (error) {
|
|
|
- hilog.error(0x0000, 'Heanup2', `Failed to clear form IDs: ${error}`);
|
|
|
- }
|
|
|
-
|
|
|
- // 注销卡片call事件监听器
|
|
|
- this.unregisterWidgetCallListeners();
|
|
|
-
|
|
|
- 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);
|
|
|
- }
|
|
|
- }catch (error) {
|
|
|
- console.error(`Failed to getHiCarStatus: ${error.code}, message: ${error.message}`);
|
|
|
+ console.error(`Failed to getHiCarStatus: ${error.code}, message: ${error.message}`);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
@@ -389,53 +379,55 @@ export default class EntryAbility extends UIAbility {
|
|
|
hilog.info(0x0000, 'Heanup2', '%{public}s', 'Ability onBackground end');
|
|
|
}
|
|
|
|
|
|
- getHiCarStatus(){
|
|
|
- try {
|
|
|
- if (!this.awareness){
|
|
|
- this.awareness=canIUse("SystemCapability.CarService.DistributedEngine")?smartMobilityCommon.getSmartMobilityAwareness():undefined;
|
|
|
- }
|
|
|
- console.log('enter getHiCarStatus,awareness:'+JSON.stringify(this.awareness));
|
|
|
- if(this.awareness){
|
|
|
-
|
|
|
-
|
|
|
- console.log('enter awareness');
|
|
|
- // 业务类型
|
|
|
- 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));
|
|
|
- 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);
|
|
|
- }
|
|
|
- }catch (error) {
|
|
|
- console.error(`Failed to getHiCarStatus: ${error.code}, message: ${error.message}`);
|
|
|
+ getHiCarStatus() {
|
|
|
+ try {
|
|
|
+ if (!this.awareness) {
|
|
|
+ this.awareness =
|
|
|
+ canIUse("SystemCapability.CarService.DistributedEngine") ? smartMobilityCommon.getSmartMobilityAwareness() :
|
|
|
+ undefined;
|
|
|
+ }
|
|
|
+ console.log('enter getHiCarStatus,awareness:' + JSON.stringify(this.awareness));
|
|
|
+ if (this.awareness) {
|
|
|
+
|
|
|
|
|
|
+ console.log('enter awareness');
|
|
|
+ // 业务类型
|
|
|
+ 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));
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error(`Failed to getHiCarStatus: ${error.code}, message: ${error.message}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
//发送广播通知更新UI
|
|
|
sendChangeEvent() {
|
|
|
const eventData: emitter.EventData = {};
|
|
|
@@ -651,12 +643,14 @@ export default class EntryAbility extends UIAbility {
|
|
|
const widgetIsPlaying = params['widgetIsPlaying'] as boolean;
|
|
|
if (widgetIsPlaying !== undefined) {
|
|
|
hilog.info(0x0000, 'Heanup2', `🎵 基于widget状态执行播放控制: ${widgetIsPlaying ? '暂停' : '播放'}`);
|
|
|
- commandPromise = widgetIsPlaying ? this.unifiedService.pause() : this.unifiedService.startPlayOrResumePlay();
|
|
|
+ commandPromise =
|
|
|
+ widgetIsPlaying ? this.unifiedService.pause() : this.unifiedService.startPlayOrResumePlay();
|
|
|
} else {
|
|
|
// 回退到状态检查
|
|
|
const currentState = this.unifiedService.getCurrentState();
|
|
|
hilog.info(0x0000, 'Heanup2', `🎵 基于播放器状态执行播放控制: ${currentState.isPlaying ? '暂停' : '播放'}`);
|
|
|
- commandPromise = currentState.isPlaying ? this.unifiedService.pause() : this.unifiedService.startPlayOrResumePlay();
|
|
|
+ commandPromise =
|
|
|
+ currentState.isPlaying ? this.unifiedService.pause() : this.unifiedService.startPlayOrResumePlay();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -714,7 +708,7 @@ export default class EntryAbility extends UIAbility {
|
|
|
|
|
|
// 如果缓存仍然有效,直接返回缓存结果
|
|
|
if (this.isServiceFullyReady &&
|
|
|
- (currentTime - this.lastServiceReadyCheckTime) < this.SERVICE_READY_CACHE_DURATION) {
|
|
|
+ (currentTime - this.lastServiceReadyCheckTime) < this.SERVICE_READY_CACHE_DURATION) {
|
|
|
hilog.info(0x0000, 'Heanup2', '✅ 使用服务就绪状态缓存');
|
|
|
return true;
|
|
|
}
|
|
|
@@ -763,38 +757,6 @@ 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 {
|
|
|
- hilog.warn(0x0000, 'Heanup2', '⚠️ 强制数据恢复失败,尝试等待');
|
|
|
- // 如果强制恢复失败,再等待一段时间
|
|
|
- await unifiedService.waitForDataRestoration(2000);
|
|
|
- }
|
|
|
-
|
|
|
- // 额外等待一小段时间,确保播放列表数据完全加载
|
|
|
- await new Promise<void>(resolve => setTimeout(resolve, 300));
|
|
|
-
|
|
|
- } catch (error) {
|
|
|
- hilog.error(0x0000, 'Heanup2', `❌ 强制数据恢复失败: ${error}`);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
* 异步初始化播放器服务,避免阻塞生命周期
|
|
|
@@ -841,7 +803,8 @@ export default class EntryAbility extends UIAbility {
|
|
|
// 但要避免在应用销毁时重复保存,因为onDestroy中已经保存过了
|
|
|
try {
|
|
|
const unifiedPlayerService = UnifiedPlayerService.getInstance();
|
|
|
- if (unifiedPlayerService && unifiedPlayerService.isAllServicesReady() && unifiedPlayerService.isServiceInitialized()) {
|
|
|
+ if (unifiedPlayerService && unifiedPlayerService.isAllServicesReady() &&
|
|
|
+ unifiedPlayerService.isServiceInitialized()) {
|
|
|
// 检查服务是否已经被释放,如果已释放说明是应用销毁流程,不需要再保存
|
|
|
// 保存当前状态,确保即使应用被强制杀死也能保存正确状态
|
|
|
await unifiedPlayerService.saveCurrentStateExternal();
|