|
|
@ -729,37 +729,85 @@ private class SetReminderHandler: FunctionHandler { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private func setReminder(title: String, content: String, time: String) async -> Result { |
|
|
private func setReminder(title: String, content: String, time: String) async -> Result { |
|
|
guard let eventStore = eventStore else { |
|
|
Log.d(TAG, "设置提醒: $title, 时间: $timeStr") |
|
|
return Result(success: false, message: "事件存储未初始化") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 请求日历权限 |
|
|
|
|
|
let status = await requestCalendarAccess() |
|
|
|
|
|
guard status == .authorized else { |
|
|
|
|
|
return Result(success: false, message: "日历权限被拒绝") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 解析时间 |
|
|
|
|
|
let formatter = DateFormatter() |
|
|
|
|
|
formatter.dateFormat = "yyyy-MM-dd HH:mm" |
|
|
|
|
|
guard let date = formatter.date(from: time) else { |
|
|
|
|
|
return Result(success: false, message: "时间格式错误") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 创建提醒 |
|
|
|
|
|
let reminder = EKReminder(eventStore: eventStore) |
|
|
|
|
|
reminder.title = title |
|
|
|
|
|
reminder.notes = content |
|
|
|
|
|
reminder.calendar = eventStore.defaultCalendarForNewReminders() |
|
|
|
|
|
|
|
|
|
|
|
let alarm = EKAlarm(absoluteDate: date) |
|
|
|
|
|
reminder.addAlarm(alarm) |
|
|
|
|
|
|
|
|
|
|
|
do { |
|
|
try { |
|
|
try eventStore.save(reminder, commit: true) |
|
|
// 解析时间字符串 |
|
|
return Result(success: true, message: "提醒设置成功") |
|
|
val time = try { |
|
|
} catch { |
|
|
val dateFormat = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm", java.util.Locale.getDefault()) |
|
|
return Result(success: false, message: "设置提醒失败:\(error.localizedDescription)") |
|
|
dateFormat.parse(timeStr)?.time ?: System.currentTimeMillis() |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(TAG, "解析时间失败: $timeStr, ${e.message}") |
|
|
|
|
|
return Result( |
|
|
|
|
|
success = false, |
|
|
|
|
|
message = "提醒时间格式无效,请使用'yyyy-MM-dd HH:mm'格式" |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 检查日历权限 |
|
|
|
|
|
if (!checkPermissionAndGuide(Manifest.permission.WRITE_CALENDAR, "日历写入", "设置提醒")) { |
|
|
|
|
|
return Result( |
|
|
|
|
|
success = false, |
|
|
|
|
|
message = "需要日历权限才能创建提醒,请在权限管理中开启日历权限" |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 直接使用ContentProvider写入日历 |
|
|
|
|
|
val endTime = time + 3600000 // 默认1小时后结束 |
|
|
|
|
|
|
|
|
|
|
|
// 获取默认日历ID |
|
|
|
|
|
val calendarId = getDefaultCalendarId() |
|
|
|
|
|
if (calendarId == -1L) { |
|
|
|
|
|
Log.w(TAG, "未找到默认日历") |
|
|
|
|
|
return tryCreateAlarm(title, content, timeStr) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 创建事件 |
|
|
|
|
|
val eventValues = ContentValues().apply { |
|
|
|
|
|
put(CalendarContract.Events.CALENDAR_ID, calendarId) |
|
|
|
|
|
put(CalendarContract.Events.TITLE, title) |
|
|
|
|
|
put(CalendarContract.Events.DESCRIPTION, content) |
|
|
|
|
|
put(CalendarContract.Events.DTSTART, time) |
|
|
|
|
|
put(CalendarContract.Events.DTEND, endTime) |
|
|
|
|
|
put(CalendarContract.Events.HAS_ALARM, 1) |
|
|
|
|
|
put(CalendarContract.Events.EVENT_TIMEZONE, java.util.TimeZone.getDefault().id) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val eventUri = context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, eventValues) |
|
|
|
|
|
if (eventUri == null) { |
|
|
|
|
|
Log.e(TAG, "插入日历事件失败") |
|
|
|
|
|
return tryCreateAlarm(title, content, timeStr) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取事件ID |
|
|
|
|
|
val eventId = ContentUris.parseId(eventUri) |
|
|
|
|
|
|
|
|
|
|
|
// 添加提醒(15分钟前提醒) |
|
|
|
|
|
val reminderValues = ContentValues().apply { |
|
|
|
|
|
put(CalendarContract.Reminders.EVENT_ID, eventId) |
|
|
|
|
|
put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT) |
|
|
|
|
|
put(CalendarContract.Reminders.MINUTES, 15) // 提前15分钟提醒 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val reminderUri = context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues) |
|
|
|
|
|
if (reminderUri != null) { |
|
|
|
|
|
return Result( |
|
|
|
|
|
success = true, |
|
|
|
|
|
message = "已成功创建提醒: $title, 时间: $timeStr" |
|
|
|
|
|
) |
|
|
|
|
|
} else { |
|
|
|
|
|
// 仍然创建了事件,但没有提醒 |
|
|
|
|
|
return Result( |
|
|
|
|
|
success = true, |
|
|
|
|
|
message = "已创建日历事件,但未设置提醒: $title" |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(TAG, "设置提醒失败: ${e.message}", e) |
|
|
|
|
|
return Result( |
|
|
|
|
|
success = false, |
|
|
|
|
|
message = "设置提醒失败: ${e.message}" |
|
|
|
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -773,6 +821,97 @@ private class SetReminderHandler: FunctionHandler { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /** |
|
|
|
|
|
// * 设置提醒 |
|
|
|
|
|
// * @param title 提醒标题 |
|
|
|
|
|
// * @param content 提醒内容(可选) |
|
|
|
|
|
// * @param timeStr 提醒时间,格式为"yyyy-MM-dd HH:mm" |
|
|
|
|
|
// * @return 操作结果 |
|
|
|
|
|
// */ |
|
|
|
|
|
// fun setReminder(title: String, content: String, timeStr: String): Result { |
|
|
|
|
|
// Log.d(TAG, "设置提醒: $title, 时间: $timeStr") |
|
|
|
|
|
|
|
|
|
|
|
// try { |
|
|
|
|
|
// // 解析时间字符串 |
|
|
|
|
|
// val time = try { |
|
|
|
|
|
// val dateFormat = java.text.SimpleDateFormat("yyyy-MM-dd HH:mm", java.util.Locale.getDefault()) |
|
|
|
|
|
// dateFormat.parse(timeStr)?.time ?: System.currentTimeMillis() |
|
|
|
|
|
// } catch (e: Exception) { |
|
|
|
|
|
// Log.e(TAG, "解析时间失败: $timeStr, ${e.message}") |
|
|
|
|
|
// return Result( |
|
|
|
|
|
// success = false, |
|
|
|
|
|
// message = "提醒时间格式无效,请使用'yyyy-MM-dd HH:mm'格式" |
|
|
|
|
|
// ) |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
// // 检查日历权限 |
|
|
|
|
|
// if (!checkPermissionAndGuide(Manifest.permission.WRITE_CALENDAR, "日历写入", "设置提醒")) { |
|
|
|
|
|
// return Result( |
|
|
|
|
|
// success = false, |
|
|
|
|
|
// message = "需要日历权限才能创建提醒,请在权限管理中开启日历权限" |
|
|
|
|
|
// ) |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
// // 直接使用ContentProvider写入日历 |
|
|
|
|
|
// val endTime = time + 3600000 // 默认1小时后结束 |
|
|
|
|
|
|
|
|
|
|
|
// // 获取默认日历ID |
|
|
|
|
|
// val calendarId = getDefaultCalendarId() |
|
|
|
|
|
// if (calendarId == -1L) { |
|
|
|
|
|
// Log.w(TAG, "未找到默认日历") |
|
|
|
|
|
// return tryCreateAlarm(title, content, timeStr) |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
// // 创建事件 |
|
|
|
|
|
// val eventValues = ContentValues().apply { |
|
|
|
|
|
// put(CalendarContract.Events.CALENDAR_ID, calendarId) |
|
|
|
|
|
// put(CalendarContract.Events.TITLE, title) |
|
|
|
|
|
// put(CalendarContract.Events.DESCRIPTION, content) |
|
|
|
|
|
// put(CalendarContract.Events.DTSTART, time) |
|
|
|
|
|
// put(CalendarContract.Events.DTEND, endTime) |
|
|
|
|
|
// put(CalendarContract.Events.HAS_ALARM, 1) |
|
|
|
|
|
// put(CalendarContract.Events.EVENT_TIMEZONE, java.util.TimeZone.getDefault().id) |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
// val eventUri = context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, eventValues) |
|
|
|
|
|
// if (eventUri == null) { |
|
|
|
|
|
// Log.e(TAG, "插入日历事件失败") |
|
|
|
|
|
// return tryCreateAlarm(title, content, timeStr) |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
// // 获取事件ID |
|
|
|
|
|
// val eventId = ContentUris.parseId(eventUri) |
|
|
|
|
|
|
|
|
|
|
|
// // 添加提醒(15分钟前提醒) |
|
|
|
|
|
// val reminderValues = ContentValues().apply { |
|
|
|
|
|
// put(CalendarContract.Reminders.EVENT_ID, eventId) |
|
|
|
|
|
// put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT) |
|
|
|
|
|
// put(CalendarContract.Reminders.MINUTES, 15) // 提前15分钟提醒 |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
// val reminderUri = context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues) |
|
|
|
|
|
// if (reminderUri != null) { |
|
|
|
|
|
// return Result( |
|
|
|
|
|
// success = true, |
|
|
|
|
|
// message = "已成功创建提醒: $title, 时间: $timeStr" |
|
|
|
|
|
// ) |
|
|
|
|
|
// } else { |
|
|
|
|
|
// // 仍然创建了事件,但没有提醒 |
|
|
|
|
|
// return Result( |
|
|
|
|
|
// success = true, |
|
|
|
|
|
// message = "已创建日历事件,但未设置提醒: $title" |
|
|
|
|
|
// ) |
|
|
|
|
|
// } |
|
|
|
|
|
// } catch (e: Exception) { |
|
|
|
|
|
// Log.e(TAG, "设置提醒失败: ${e.message}", e) |
|
|
|
|
|
// return Result( |
|
|
|
|
|
// success = false, |
|
|
|
|
|
// message = "设置提醒失败: ${e.message}" |
|
|
|
|
|
// ) |
|
|
|
|
|
// } |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
/// 获取当前时间处理器 |
|
|
/// 获取当前时间处理器 |
|
|
private class GetCurrentTimeHandler: FunctionHandler { |
|
|
private class GetCurrentTimeHandler: FunctionHandler { |
|
|
func handle(arguments: [String: Any]) async throws -> String { |
|
|
func handle(arguments: [String: Any]) async throws -> String { |
|
|
|