- 新增 SystemCalendarMirror / MirrorSnapshot / MirrorInbound:把选定的 CalDAV 日历本镜像写入系统日历(供小艺、桌面日历卡片、手表可见),并做入站对账,区分"自己写入的 / 用户改的 / 用户删的 / 用户新建的",镜像标识一律用稳定的 bookId(accId-href哈希),不再用会随勾选顺序漂移的 calKey - 后台同步接入镜像:同步前先做入站对账、同步后自动出站镜像,无需用户手动触发 - 系统日历备份目标改用 bookId:当用户取消同步某个日历本导致目标失效时,自动清空目标并把模式回退为"仅显示";开启备份必须先选定目标本,不再自动挑第一个本 - EventDb:覆盖行保留 RECURRENCE-ID,排除主事件对应发生改用 overrideKey(calKey, uid, recurrenceId, startTime),修复重复日程出现"幽灵条目"的问题 - 新增 WRITE_CALENDAR 权限声明及其用途说明;添加日历不再出现"本机",日历本对齐显示;版本升至 0.0.3(300)
91 lines
4.2 KiB
Plaintext
91 lines
4.2 KiB
Plaintext
// entry/src/main/ets/syncwork/SyncWorkAbility.ets
|
|
// 延迟任务兜底同步:App 进程被杀/设备重启后,系统在满足条件(有网络)时拉起本扩展执行同步
|
|
// 注意:必须幂等——同步本身基于 ETag 全量比对,天然幂等;执行完必须 stopWork,否则系统按超时处理
|
|
import { WorkSchedulerExtensionAbility, workScheduler } from '@kit.BackgroundTasksKit';
|
|
import { BusinessError } from '@kit.BasicServicesKit';
|
|
import { DavAccount, AccountStore, TYPE_CALDAV } from '../common/AccountStore';
|
|
import { SyncEngine } from '../common/SyncEngine';
|
|
import { CardDataService } from '../common/CardDataService';
|
|
import { ReminderService } from '../common/ReminderService';
|
|
import { SystemCalendarImport } from '../common/SystemCalendarImport';
|
|
import { SystemCalendarMirror } from '../common/SystemCalendarMirror';
|
|
import { MirrorInbound } from '../common/MirrorInbound';
|
|
import { AppSettings } from '../common/AppSettings';
|
|
import { LogUtil } from '../common/LogUtil';
|
|
|
|
export default class SyncWorkAbility extends WorkSchedulerExtensionAbility {
|
|
onWorkStart(workInfo: workScheduler.WorkInfo): void {
|
|
LogUtil.init(this.context);
|
|
LogUtil.write(`延迟任务触发:workId=${workInfo.workId}`);
|
|
this.doSync(workInfo);
|
|
}
|
|
|
|
onWorkStop(workInfo: workScheduler.WorkInfo): void {
|
|
LogUtil.write(`延迟任务结束:workId=${workInfo.workId}`);
|
|
}
|
|
|
|
private async doSync(workInfo: workScheduler.WorkInfo): Promise<void> {
|
|
try {
|
|
const context = this.context;
|
|
const accounts: DavAccount[] = await AccountStore.loadAll(context);
|
|
// 备份模式:先导入系统本地日历(幂等),再随同步推送
|
|
try {
|
|
await SystemCalendarImport.importIfNeeded(context);
|
|
} catch (err) {
|
|
// 导入失败不影响正常同步
|
|
}
|
|
// 入站对账:把用户在系统日历里的改动读回来置 dirty(必须在同步之前)
|
|
try {
|
|
await MirrorInbound.reconcile(context);
|
|
} catch (err) {
|
|
// 对账失败不影响正常同步
|
|
}
|
|
let ok: number = 0;
|
|
let attempted: number = 0;
|
|
for (const acc of accounts) {
|
|
if (acc.type !== TYPE_CALDAV) {
|
|
continue;
|
|
}
|
|
attempted++;
|
|
try {
|
|
// 超时保护由 syncAccount 内部处理(全量重拉 10 分钟 / 常规 5 分钟),不再套外层超时
|
|
await SyncEngine.syncAccount(context, acc);
|
|
ok++;
|
|
} catch (err) {
|
|
const e = err as BusinessError;
|
|
LogUtil.write(`延迟任务同步 id=${acc.id} 失败: ${e.message}`);
|
|
}
|
|
}
|
|
// 全部账号同步成功才结束"一次性全量重拉"状态
|
|
if (attempted > 0 && ok === attempted) {
|
|
await AppSettings.markFullRefetchDone(context);
|
|
}
|
|
await SyncEngine.settleLocalEvents(context);
|
|
await SyncEngine.pruneOrphanRows(context, accounts);
|
|
// ⚠️ 必须回写账号:syncAccount 会就地更新 acc(服务器日历本颜色、写权限等)。
|
|
// 缺了这一步,后台同步刷到的颜色只留在内存里、进程退出即丢失,
|
|
// 界面下次冷启动仍用旧颜色(表现为"同步之后颜色还是不对")。
|
|
await AccountStore.saveAll(context, accounts);
|
|
// 出站镜像:把服务器最新状态写进系统日历(自动化,无需手点)
|
|
try {
|
|
if (await AppSettings.getMirrorEnabled(context) && await SystemCalendarMirror.hasPermission()) {
|
|
const mr = await SystemCalendarMirror.syncNow(context);
|
|
LogUtil.write(`延迟任务自动镜像:本=${mr.books} 新增=${mr.added} 更新=${mr.updated} 删除=${mr.deleted}`);
|
|
}
|
|
} catch (err) {
|
|
// 镜像失败不影响同步
|
|
}
|
|
// 同步后刷新卡片 + 重建提醒,保证后台同步的成果直接可见
|
|
await CardDataService.pushToAllForms(context);
|
|
await ReminderService.refreshReminders(context);
|
|
LogUtil.write(`延迟任务同步完成:${ok} 个账号`);
|
|
} catch (err) {
|
|
const e = err as BusinessError;
|
|
LogUtil.write(`延迟任务同步失败: ${e.message}`);
|
|
} finally {
|
|
// 必须主动结束,否则系统按 120 秒超时处理
|
|
workScheduler.stopWork(workInfo);
|
|
}
|
|
}
|
|
}
|