|
|
@@ -75,6 +75,10 @@ implements SizeChangeListener, ThemeChangeListener, PreferencesChangeListener {
|
|
|
// 防抖机制:避免短时间内重复更新
|
|
|
private lastUpdateTime: number = 0;
|
|
|
private updateDebounceDelay: number = 100; // 100ms防抖延迟
|
|
|
+
|
|
|
+ // 进程启动时间,用于检测进程重启
|
|
|
+ private processStartTime: number = Date.now();
|
|
|
+ private lastHealthCheck: number = 0;
|
|
|
|
|
|
/**
|
|
|
* 初始化服务
|
|
|
@@ -104,22 +108,56 @@ implements SizeChangeListener, ThemeChangeListener, PreferencesChangeListener {
|
|
|
*/
|
|
|
private setupGlobalStateListener(): void {
|
|
|
try {
|
|
|
- hilog.info(0x0000, TAG, 'Heanup EntryFormAbility getting AvSessionWidgetListener instance...');
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [Process:${this.processStartTime}] getting AvSessionWidgetListener instance...`);
|
|
|
const avSessionListener = AvSessionWidgetListener.getInstance();
|
|
|
- hilog.info(0x0000, TAG, 'Heanup EntryFormAbility adding state listener...');
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [Process:${this.processStartTime}] adding state listener...`);
|
|
|
avSessionListener.addStateListener((data: WidgetData) => {
|
|
|
- hilog.info(0x0000, TAG, `Heanup EntryFormAbility global listener received state update: isPlaying=${data.playState.isPlaying}, title=${data.currentSong.title}`);
|
|
|
+ const now = Date.now();
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [Process:${this.processStartTime}] global listener received state update: isPlaying=${data.playState.isPlaying}, title=${data.currentSong.title}, age=${now - this.processStartTime}ms`);
|
|
|
+
|
|
|
+ // 更新健康检查时间
|
|
|
+ this.lastHealthCheck = now;
|
|
|
|
|
|
// 总是尝试更新,让updateAllWidgetsWithData自己检查
|
|
|
this.updateAllWidgetsWithData(data);
|
|
|
});
|
|
|
|
|
|
- hilog.info(0x0000, TAG, 'Heanup EntryFormAbility global state listener setup successfully');
|
|
|
+ // 启动健康检查定时器
|
|
|
+ this.startHealthCheck();
|
|
|
+
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [Process:${this.processStartTime}] global state listener setup successfully`);
|
|
|
} catch (error) {
|
|
|
- hilog.error(0x0000, TAG, `Heanup EntryFormAbility failed to setup global listener: ${error}`);
|
|
|
+ hilog.error(0x0000, TAG, `Heanup EntryFormAbility [Process:${this.processStartTime}] failed to setup global listener: ${error}`);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 启动健康检查,定期检测监听器是否还在工作
|
|
|
+ */
|
|
|
+ private startHealthCheck(): void {
|
|
|
+ setInterval(() => {
|
|
|
+ const now = Date.now();
|
|
|
+ const timeSinceLastUpdate = now - this.lastHealthCheck;
|
|
|
+
|
|
|
+ // 如果超过30秒没有收到任何状态更新,可能监听器失效了
|
|
|
+ if (timeSinceLastUpdate > 30000) {
|
|
|
+ hilog.warn(0x0000, TAG, `Heanup EntryFormAbility [Process:${this.processStartTime}] health check failed: ${timeSinceLastUpdate}ms since last update`);
|
|
|
+
|
|
|
+ // 尝试强制重连和重新请求当前状态
|
|
|
+ this.playerControlService.forceReconnect().then(() => {
|
|
|
+ return this.playerControlService.getCurrentPlayState();
|
|
|
+ }).then((currentState) => {
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [Process:${this.processStartTime}] health check recovery: got current state`);
|
|
|
+ this.updateAllWidgetsWithData(currentState);
|
|
|
+ }).catch(() => {
|
|
|
+ hilog.error(0x0000, TAG, `Heanup EntryFormAbility [Process:${this.processStartTime}] health check recovery failed:`);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [Process:${this.processStartTime}] health check OK: ${timeSinceLastUpdate}ms since last update`);
|
|
|
+ }
|
|
|
+ }, 15000); // 每15秒检查一次
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 使用指定数据更新所有卡片
|
|
|
*/
|
|
|
@@ -358,17 +396,29 @@ implements SizeChangeListener, ThemeChangeListener, PreferencesChangeListener {
|
|
|
* 处理卡片事件(用户交互)
|
|
|
*/
|
|
|
onFormEvent(formId: string, message: string): void {
|
|
|
- hilog.info(0x0000, TAG, `Heanup EntryFormAbility onFormEvent called: ${formId}, message: ${message}`);
|
|
|
+ const processId = `${Date.now()}_${Math.random().toString(36).substr(2, 5)}`;
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [${processId}] onFormEvent called: ${formId}, message: ${message}`);
|
|
|
|
|
|
// 确保服务已初始化
|
|
|
if (!this.globalListenerSetup) {
|
|
|
- hilog.info(0x0000, TAG, 'Heanup EntryFormAbility services not initialized in onFormEvent, initializing now...');
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [${processId}] services not initialized in onFormEvent, initializing now...`);
|
|
|
this.initializeServices();
|
|
|
+
|
|
|
+ // 强制请求当前状态,确保新进程能获取到最新数据
|
|
|
+ setTimeout(() => {
|
|
|
+ this.playerControlService.getCurrentPlayState().then((currentState) => {
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [${processId}] got current state: isPlaying=${currentState.playState.isPlaying}, title=${currentState.currentSong.title}`);
|
|
|
+ // 手动触发一次状态更新,确保widget显示正确
|
|
|
+ this.updateAllWidgetsWithData(currentState);
|
|
|
+ }).catch(() => {
|
|
|
+ hilog.error(0x0000, TAG, `Heanup EntryFormAbility [${processId}] failed to get current state: `);
|
|
|
+ });
|
|
|
+ }, 1000);
|
|
|
}
|
|
|
|
|
|
// 确保widget已注册到GlobalWidgetManager
|
|
|
if (!this.globalWidgetManager.hasWidget(formId)) {
|
|
|
- hilog.info(0x0000, TAG, `Heanup EntryFormAbility widget ${formId} not registered, registering as LARGE size`);
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility [${processId}] widget ${formId} not registered, registering as LARGE size`);
|
|
|
// 默认注册为LARGE尺寸,实际尺寸可以后续检测
|
|
|
this.globalWidgetManager.registerWidget(formId, 'large' as WidgetSize);
|
|
|
}
|
|
|
@@ -377,7 +427,7 @@ implements SizeChangeListener, ThemeChangeListener, PreferencesChangeListener {
|
|
|
const eventData = JSON.parse(message) as Object;
|
|
|
this.handleWidgetEvent(formId, eventData);
|
|
|
} catch (error) {
|
|
|
- hilog.error(0x0000, TAG, `Heanup EntryFormAbility failed to parse form event: ${error}`);
|
|
|
+ hilog.error(0x0000, TAG, `Heanup EntryFormAbility [${processId}] failed to parse form event: ${error}`);
|
|
|
}
|
|
|
}
|
|
|
|