|
|
@@ -71,27 +71,31 @@ implements SizeChangeListener, ThemeChangeListener, PreferencesChangeListener {
|
|
|
|
|
|
// 使用实例变量而不是静态变量,确保每个进程都能正确设置监听器
|
|
|
private globalListenerSetup: boolean = false;
|
|
|
+
|
|
|
+ // 防抖机制:避免短时间内重复更新
|
|
|
+ private lastUpdateTime: number = 0;
|
|
|
+ private updateDebounceDelay: number = 100; // 100ms防抖延迟
|
|
|
|
|
|
/**
|
|
|
* 初始化服务
|
|
|
*/
|
|
|
private initializeServices(): void {
|
|
|
- hilog.info(0x0000, TAG, 'Heanup widget initializeServices called');
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility initializeServices called');
|
|
|
|
|
|
if (!this.widgetDataManager) {
|
|
|
this.widgetDataManager = new WidgetDataManager();
|
|
|
- hilog.info(0x0000, TAG, 'WidgetDataManager initialized');
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility WidgetDataManager initialized');
|
|
|
}
|
|
|
|
|
|
// 设置全局状态监听器(每个进程实例设置一次)
|
|
|
- hilog.info(0x0000, TAG, `Heanup widget checking global listener setup: ${this.globalListenerSetup}`);
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility checking global listener setup: ${this.globalListenerSetup}`);
|
|
|
if (!this.globalListenerSetup) {
|
|
|
- hilog.info(0x0000, TAG, 'Heanup widget setting up global listener...');
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility setting up global listener...');
|
|
|
this.setupGlobalStateListener();
|
|
|
this.globalListenerSetup = true;
|
|
|
- hilog.info(0x0000, TAG, 'Heanup widget global listener setup flag set to true');
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility global listener setup flag set to true');
|
|
|
} else {
|
|
|
- hilog.info(0x0000, TAG, 'Heanup widget global listener already setup, skipping');
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility global listener already setup, skipping');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -100,17 +104,19 @@ implements SizeChangeListener, ThemeChangeListener, PreferencesChangeListener {
|
|
|
*/
|
|
|
private setupGlobalStateListener(): void {
|
|
|
try {
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility getting AvSessionWidgetListener instance...');
|
|
|
const avSessionListener = AvSessionWidgetListener.getInstance();
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility adding state listener...');
|
|
|
avSessionListener.addStateListener((data: WidgetData) => {
|
|
|
- hilog.info(0x0000, TAG, `Heanup widget global listener received state update: isPlaying=${data.playState.isPlaying}, title=${data.currentSong.title}`);
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility global listener received state update: isPlaying=${data.playState.isPlaying}, title=${data.currentSong.title}`);
|
|
|
|
|
|
// 总是尝试更新,让updateAllWidgetsWithData自己检查
|
|
|
this.updateAllWidgetsWithData(data);
|
|
|
});
|
|
|
|
|
|
- hilog.info(0x0000, TAG, 'Heanup widget global state listener setup successfully');
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility global state listener setup successfully');
|
|
|
} catch (error) {
|
|
|
- hilog.error(0x0000, TAG, `Heanup widget failed to setup global listener: ${error}`);
|
|
|
+ hilog.error(0x0000, TAG, `Heanup EntryFormAbility failed to setup global listener: ${error}`);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -118,48 +124,88 @@ implements SizeChangeListener, ThemeChangeListener, PreferencesChangeListener {
|
|
|
* 使用指定数据更新所有卡片
|
|
|
*/
|
|
|
private updateAllWidgetsWithData(data: WidgetData): void {
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility updateAllWidgetsWithData called with isPlaying=${data.playState.isPlaying}, title=${data.currentSong.title}`);
|
|
|
+
|
|
|
+ // 防抖机制:避免短时间内重复更新
|
|
|
+ const now = Date.now();
|
|
|
+ if (now - this.lastUpdateTime < this.updateDebounceDelay) {
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility update debounced, skipping (${now - this.lastUpdateTime}ms since last update)`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.lastUpdateTime = now;
|
|
|
+
|
|
|
const activeWidgets = this.globalWidgetManager.getActiveWidgets();
|
|
|
- hilog.info(0x0000, TAG, `Heanup widget updateAllWidgetsWithData called, active widgets: ${activeWidgets.size}`);
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility active widgets count: ${activeWidgets.size}`);
|
|
|
+
|
|
|
+ // 打印所有活跃的widget ID
|
|
|
+ if (activeWidgets.size > 0) {
|
|
|
+ activeWidgets.forEach((size: WidgetSize, formId: string) => {
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility found active widget: ${formId}, size: ${size}`);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
if (activeWidgets.size === 0) {
|
|
|
- hilog.warn(0x0000, TAG, 'Heanup widget no widgets to update in this process, skipping');
|
|
|
+ hilog.warn(0x0000, TAG, 'Heanup EntryFormAbility no widgets to update in this process, skipping');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
activeWidgets.forEach((size: WidgetSize, formId: string) => {
|
|
|
- try {
|
|
|
- // 根据卡片尺寸适配数据
|
|
|
- const adaptedData = this.layoutManager.adaptDataForSize(data, size);
|
|
|
-
|
|
|
- hilog.info(0x0000, TAG, `Heanup widget ${formId} calling formProvider.updateForm with isPlaying=${adaptedData.isPlaying}`);
|
|
|
-
|
|
|
- // 直接使用系统API更新卡片
|
|
|
- const formData = formBindingData.createFormBindingData(adaptedData);
|
|
|
- formProvider.updateForm(formId, formData).then(() => {
|
|
|
- hilog.info(0x0000, TAG, `Heanup widget ${formId} updated successfully: isPlaying=${adaptedData.isPlaying}, title=${adaptedData.songTitle}`);
|
|
|
- }).catch(() => {//不能传error参数
|
|
|
- hilog.error(0x0000, TAG, `Heanup widget ${formId} update failed: `);
|
|
|
- });
|
|
|
- } catch (error) {
|
|
|
- hilog.error(0x0000, TAG, `Heanup widget ${formId} process error: ${error}`);
|
|
|
- }
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility updating widget: ${formId}`);
|
|
|
+ this.updateSingleWidget(formId, size, data);
|
|
|
});
|
|
|
|
|
|
- hilog.info(0x0000, TAG, `Heanup widget batch update completed for ${activeWidgets.size} widgets`);
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility batch update completed for ${activeWidgets.size} widgets`);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新单个卡片,带重试机制
|
|
|
+ */
|
|
|
+ private updateSingleWidget(formId: string, size: WidgetSize, data: WidgetData, retryCount: number = 0): void {
|
|
|
+ try {
|
|
|
+ // 根据卡片尺寸适配数据
|
|
|
+ const adaptedData = this.layoutManager.adaptDataForSize(data, size);
|
|
|
+
|
|
|
+ hilog.info(0x0000, TAG, `Heanup widget ${formId} calling formProvider.updateForm with isPlaying=${adaptedData.isPlaying}, hasNext=${adaptedData.hasNext}, hasPrevious=${adaptedData.hasPrevious}`);
|
|
|
+
|
|
|
+ // 直接使用系统API更新卡片
|
|
|
+ const formData = formBindingData.createFormBindingData(adaptedData);
|
|
|
+ formProvider.updateForm(formId, formData).then(() => {
|
|
|
+ hilog.info(0x0000, TAG, `Heanup widget ${formId} updated successfully: isPlaying=${adaptedData.isPlaying}, title=${adaptedData.songTitle}, hasNext=${adaptedData.hasNext}, hasPrevious=${adaptedData.hasPrevious}`);
|
|
|
+ }).catch(() => {
|
|
|
+ hilog.error(0x0000, TAG, `Heanup widget ${formId} update failed, retry count: ${retryCount}`);
|
|
|
+
|
|
|
+ // 重试机制:最多重试2次
|
|
|
+ if (retryCount < 2) {
|
|
|
+ setTimeout(() => {
|
|
|
+ this.updateSingleWidget(formId, size, data, retryCount + 1);
|
|
|
+ }, 1000 * (retryCount + 1)); // 递增延迟
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ hilog.error(0x0000, TAG, `Heanup widget ${formId} process error: ${error}`);
|
|
|
+
|
|
|
+ // 如果是处理错误,也尝试重试
|
|
|
+ if (retryCount < 2) {
|
|
|
+ setTimeout(() => {
|
|
|
+ this.updateSingleWidget(formId, size, data, retryCount + 1);
|
|
|
+ }, 1000 * (retryCount + 1));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 卡片创建时调用
|
|
|
*/
|
|
|
onAddForm(want: Want): formBindingData.FormBindingData {
|
|
|
- hilog.info(0x0000, TAG, 'onAddForm called');
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility onAddForm called');
|
|
|
|
|
|
// 初始化服务
|
|
|
try {
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility initializing services...');
|
|
|
this.initializeServices();
|
|
|
- hilog.info(0x0000, TAG, 'Services initialized successfully');
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility services initialized successfully');
|
|
|
} catch (error) {
|
|
|
- hilog.error(0x0000, TAG, `Failed to initialize services: ${error}`);
|
|
|
+ hilog.error(0x0000, TAG, `Heanup EntryFormAbility failed to initialize services: ${error}`);
|
|
|
}
|
|
|
|
|
|
const formId = want.parameters?.['ohos.extra.param.key.form_identity'] as string;
|
|
|
@@ -252,7 +298,19 @@ implements SizeChangeListener, ThemeChangeListener, PreferencesChangeListener {
|
|
|
* 卡片更新时调用
|
|
|
*/
|
|
|
onUpdateForm(formId: string): void {
|
|
|
- hilog.info(0x0000, TAG, `onUpdateForm called: ${formId}`);
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility onUpdateForm called: ${formId}`);
|
|
|
+
|
|
|
+ // 确保服务已初始化
|
|
|
+ if (!this.globalListenerSetup) {
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility services not initialized, initializing now...');
|
|
|
+ this.initializeServices();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保widget已注册到GlobalWidgetManager
|
|
|
+ if (!this.globalWidgetManager.hasWidget(formId)) {
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility widget ${formId} not registered in onUpdateForm, registering as LARGE size`);
|
|
|
+ this.globalWidgetManager.registerWidget(formId, 'large' as WidgetSize);
|
|
|
+ }
|
|
|
|
|
|
// 只更新指定的卡片,避免重复更新
|
|
|
this.updateWidgetData(formId);
|
|
|
@@ -300,13 +358,26 @@ implements SizeChangeListener, ThemeChangeListener, PreferencesChangeListener {
|
|
|
* 处理卡片事件(用户交互)
|
|
|
*/
|
|
|
onFormEvent(formId: string, message: string): void {
|
|
|
- hilog.info(0x0000, TAG, `onFormEvent called: ${formId}, message: ${message}`);
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility onFormEvent called: ${formId}, message: ${message}`);
|
|
|
+
|
|
|
+ // 确保服务已初始化
|
|
|
+ if (!this.globalListenerSetup) {
|
|
|
+ hilog.info(0x0000, TAG, 'Heanup EntryFormAbility services not initialized in onFormEvent, initializing now...');
|
|
|
+ this.initializeServices();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保widget已注册到GlobalWidgetManager
|
|
|
+ if (!this.globalWidgetManager.hasWidget(formId)) {
|
|
|
+ hilog.info(0x0000, TAG, `Heanup EntryFormAbility widget ${formId} not registered, registering as LARGE size`);
|
|
|
+ // 默认注册为LARGE尺寸,实际尺寸可以后续检测
|
|
|
+ this.globalWidgetManager.registerWidget(formId, 'large' as WidgetSize);
|
|
|
+ }
|
|
|
|
|
|
try {
|
|
|
const eventData = JSON.parse(message) as Object;
|
|
|
this.handleWidgetEvent(formId, eventData);
|
|
|
} catch (error) {
|
|
|
- hilog.error(0x0000, TAG, `Failed to parse form event: ${error}`);
|
|
|
+ hilog.error(0x0000, TAG, `Heanup EntryFormAbility failed to parse form event: ${error}`);
|
|
|
}
|
|
|
}
|
|
|
|