|
|
@@ -0,0 +1,79 @@
|
|
|
+import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
|
+import { Context } from '@kit.AbilityKit';
|
|
|
+import { PreferencesUtil } from '../utils/PreferencesUtil';
|
|
|
+import { GlobalWidgetManager } from './GlobalWidgetManager';
|
|
|
+
|
|
|
+const TAG = 'WidgetRegistrationFix';
|
|
|
+
|
|
|
+/**
|
|
|
+ * 卡片注册修复工具
|
|
|
+ * 用于修复卡片注册和持久化问题
|
|
|
+ */
|
|
|
+export class WidgetRegistrationFix {
|
|
|
+ private static instance: WidgetRegistrationFix | null = null;
|
|
|
+ private preferencesUtil: PreferencesUtil = PreferencesUtil.getInstance();
|
|
|
+ private globalWidgetManager: GlobalWidgetManager = GlobalWidgetManager.getInstance();
|
|
|
+
|
|
|
+ private constructor() {}
|
|
|
+
|
|
|
+ public static getInstance(): WidgetRegistrationFix {
|
|
|
+ if (!WidgetRegistrationFix.instance) {
|
|
|
+ WidgetRegistrationFix.instance = new WidgetRegistrationFix();
|
|
|
+ }
|
|
|
+ return WidgetRegistrationFix.instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行卡片注册修复
|
|
|
+ */
|
|
|
+ public async fixWidgetRegistration(context: Context): Promise<void> {
|
|
|
+ try {
|
|
|
+ hilog.info(0x0000, TAG, '🔧 开始卡片注册修复...');
|
|
|
+
|
|
|
+ // 同步活跃卡片到持久化存储
|
|
|
+ await this.syncActiveWidgetsToPersistence(context);
|
|
|
+
|
|
|
+ hilog.info(0x0000, TAG, '✅ 卡片注册修复完成');
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ hilog.error(0x0000, TAG, `❌ 卡片注册修复失败: ${error}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步活跃卡片到持久化存储
|
|
|
+ */
|
|
|
+ private async syncActiveWidgetsToPersistence(context: Context): Promise<void> {
|
|
|
+ try {
|
|
|
+ const prefs = await this.preferencesUtil.getPreferences(context);
|
|
|
+ const persistedIds = await this.preferencesUtil.getFormIds(prefs);
|
|
|
+ const activeWidgets = this.globalWidgetManager.getActiveWidgets();
|
|
|
+ const activeIds = Array.from(activeWidgets.keys());
|
|
|
+
|
|
|
+ hilog.info(0x0000, TAG, `📊 持久化: ${persistedIds.length}个, 活跃: ${activeIds.length}个`);
|
|
|
+
|
|
|
+ // 将活跃但未持久化的卡片添加到持久化存储
|
|
|
+ const missingInPersistence = activeIds.filter(id => !persistedIds.includes(id));
|
|
|
+ if (missingInPersistence.length > 0) {
|
|
|
+ hilog.info(0x0000, TAG, `🔧 同步 ${missingInPersistence.length} 个活跃卡片到持久化存储`);
|
|
|
+
|
|
|
+ // 串行处理,避免并发问题
|
|
|
+ for (const formId of missingInPersistence) {
|
|
|
+ try {
|
|
|
+ await this.preferencesUtil.addFormId(prefs, formId);
|
|
|
+ hilog.info(0x0000, TAG, `✅ 同步卡片成功: ${formId}`);
|
|
|
+ } catch (error) {
|
|
|
+ hilog.error(0x0000, TAG, `❌ 同步卡片失败 ${formId}: ${error}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ hilog.error(0x0000, TAG, `❌ 同步活跃卡片失败: ${error}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|