package com.pingan.calendar;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.provider.CalendarContract;
import android.text.TextUtils;
import com.pingan.carowner.library_log.LogUtil;
import com.pingan.lifecircle.utils.DateUtils;
import java.util.Date;
import java.util.TimeZone;
public class CalendarUtils {
public static final String TAG = "CalendarUtils";
private static String CALENDER_URL = "content://com.android.calendar/calendars";
private static String CALENDER_EVENT_URL = "content://com.android.calendar/events";
private static String CALENDER_REMINDER_URL = "content://com.android.calendar/reminders";
private static String CALENDARS_NAME = "boohee";
private static String CALENDARS_ACCOUNT_NAME = "BOOHEE@boohee.com";
private static String CALENDARS_ACCOUNT_TYPE = "com.android.boohee";
private static String CALENDARS_DISPLAY_NAME = "BOOHEE账户";
private static int ERROR_CODE = -1;
/**
* 检查是否已经添加了日历账户,如果没有添加先添加一个日历账户再查询
* 获取账户成功返回账户id,否则返回-1
*/
public static int checkAndAddCalendarAccount(Context context) {
int oldId = checkCalendarAccount(context);
if (oldId >= 0) {
LogUtil.e(TAG, "checkCalendarAccount oldId >= 0 11");
return oldId;
} else {
long addId = addCalendarAccount(context);
if (addId >= 0) {
LogUtil.e(TAG, "checkCalendarAccount addId >= 0 22");
return checkCalendarAccount(context);
} else {
LogUtil.e(TAG, "checkCalendarAccount addId = -1");
return ERROR_CODE;
}
}
}
/**
* 检查是否存在现有账户,存在则返回账户id,否则返回-1
*/
private static int checkCalendarAccount(Context context) {
Cursor userCursor = context.getContentResolver().query(Uri.parse(CALENDER_URL), null, null, null, null);
try {
if (userCursor == null) { //查询返回空值
return ERROR_CODE;
}
int count = userCursor.getCount();
if (count > 0) { //存在现有账户,取第一个账户的id返回
userCursor.moveToFirst();
return userCursor.getInt(userCursor.getColumnIndex(CalendarContract.Calendars._ID));
} else {
return ERROR_CODE;
}
} finally {
if (userCursor != null) {
userCursor.close();
}
}
}
/**
* 添加日历账户,账户创建成功则返回账户id,否则返回-1
*/
private static long addCalendarAccount(Context context) {
TimeZone timeZone = TimeZone.getDefault();
ContentValues value = new ContentValues();
value.put(CalendarContract.Calendars.NAME, CALENDARS_NAME);
value.put(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_ACCOUNT_NAME);
value.put(CalendarContract.Calendars.ACCOUNT_TYPE, CALENDARS_ACCOUNT_TYPE);
value.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CALENDARS_DISPLAY_NAME);
value.put(CalendarContract.Calendars.VISIBLE, 1);
value.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.BLUE);
value.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
value.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
value.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, timeZone.getID());
value.put(CalendarContract.Calendars.OWNER_ACCOUNT, CALENDARS_ACCOUNT_NAME);
value.put(CalendarContract.Calendars.CAN_ORGANIZER_RESPOND, 0);
Uri calendarUri = Uri.parse(CALENDER_URL);
calendarUri = calendarUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_ACCOUNT_NAME)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CALENDARS_ACCOUNT_TYPE)
.build();
Uri result = context.getContentResolver().insert(calendarUri, value);
long id = result == null ? ERROR_CODE : ContentUris.parseId(result);
return id;
}
/**
* 添加日历事件
*/
public static int addCalendarEvent(Context context, String title, String description,
String startDate, String endDate, int recurrenceRule, String reminderTime) {
try {
LogUtil.e(TAG, "addCalendarEvent ");
if (context == null) {
return ERROR_CODE;
}
int calId = checkAndAddCalendarAccount(context); //获取日历账户的id
if (calId < 0) { //获取账户id失败直接返回,添加日历事件失败
return ERROR_CODE;
}
//添加日历事件
ContentValues event = new ContentValues();
event.put("title", title);//日程标题
event.put("description", reminderTime);//日程内容
event.put("calendar_id", calId); //插入账户的id 日历id
event.put(CalendarContract.Events.DTSTART, DateUtils.parseDateTime(reminderTime).getTime());
event.put(CalendarContract.Events.DTEND, DateUtils.parseDateTime(reminderTime).getTime());
event.put(CalendarContract.Events.HAS_ALARM, 1);//设置有闹钟提醒
// event.put(CalendarContract.Events.EVENT_TIMEZONE, "Asia/Shanghai");//这个是时区,必须有
event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
// event.put(CalendarContract.Events.ALL_DAY,1);
if (recurrenceRule < 0) {
recurrenceRule = 0;
}
// recurrenceRule = 1;
//UNTIL=20000131T090000Z
// if (!TextUtils.isEmpty(reminderTime) && reminderTime.length() == 19) {
// String yearStr = reminderTime.substring(0, 10).replaceAll("-", "");
// String dateStr = reminderTime.substring(11, 19).replaceAll(":", "");
// event.put(CalendarContract.Events.RRULE, "FREQ=DAILY;INTERVAL=" + recurrenceRule + ";UNTIL=" + yearStr + "T" + dateStr + "Z" + ";WKST=SU");
// }
// String duration = "P" + countSecondBetweenTwoDates(DateUtils.parseDateTime(reminderTime), DateUtils.parseDateTime(reminderTime)) + "S";
// event.put(CalendarContract.Events.DURATION, duration);
// event.put(CalendarContract.Events.DTEND, (byte[]) null);
Uri newEvent = context.getContentResolver().insert(Uri.parse(CALENDER_EVENT_URL), event); //添加事件
if (newEvent == null) { //添加日历事件失败直接返回
LogUtil.e(TAG, "addCalendarEvent 添加日历事件失败");
return ERROR_CODE;
}
//事件提醒的设定
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));
values.put(CalendarContract.Reminders.MINUTES, 0);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = context.getContentResolver().insert(Uri.parse(CALENDER_REMINDER_URL), values);
if (uri == null) { //添加事件提醒失败直接返回
LogUtil.e(TAG, "addCalendarEvent 添加事件提醒失败");
return ERROR_CODE;
}
} catch (Exception e) {
return ERROR_CODE;
}
return 0;
}
/**
* 删除日历事件
*/
public static int deleteCalendarEvent(Context context, String title, String mDescription) {
Cursor eventCursor = null;
try {
LogUtil.e(TAG, "deleteCalendarEvent title=" + title);
if (context == null) {
return ERROR_CODE;
}
if (TextUtils.isEmpty(title)) {
return ERROR_CODE;
}
eventCursor = context.getContentResolver().query(Uri.parse(CALENDER_EVENT_URL), null, null, null, null);
if (eventCursor == null) { //查询返回空值
return ERROR_CODE;
}
if (eventCursor.getCount() > 0) {
//遍历所有事件,找到title跟需要查询的title一样的项
for (eventCursor.moveToFirst(); !eventCursor.isAfterLast(); eventCursor.moveToNext()) {
String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"));
if (TextUtils.isEmpty(eventTitle)) {
continue;
}
String description = eventCursor.getString(eventCursor.getColumnIndex("description"));
if (TextUtils.isEmpty(mDescription)) {
if (title.equals(eventTitle)) {
LogUtil.e(TAG, "deleteCalendarEvent eventTitle 删除单个提醒" + eventTitle);
int id = eventCursor.getInt(eventCursor.getColumnIndex(CalendarContract.Calendars._ID));//取得id
Uri deleteUri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), id);
int rows = context.getContentResolver().delete(deleteUri, null, null);
if (rows == -1) { //事件删除失败
return ERROR_CODE;
}
}
} else if (title.equals(eventTitle) && mDescription.equals(description)) {
LogUtil.e(TAG, "deleteCalendarEvent description 删除单个提醒" + description);
int id = eventCursor.getInt(eventCursor.getColumnIndex(CalendarContract.Calendars._ID));//取得id
Uri deleteUri = ContentUris.withAppendedId(Uri.parse(CALENDER_EVENT_URL), id);
int rows = context.getContentResolver().delete(deleteUri, null, null);
if (rows == -1) { //事件删除失败
return ERROR_CODE;
}
}
}
}
} finally {
if (eventCursor != null) {
eventCursor.close();
}
}
return 0;
}
/**
* 计算两个日期之间的秒数
* @param startDate
* @return 返回秒数
*/
public static int countSecondBetweenTwoDates(Date startDate, Date endTime) {
long a = startDate.getTime();
long b = endTime.getTime();
int c = (int) ((b - a) / 1000);
return c;
}
}
添加日历提醒code
最新推荐文章于 2024-08-12 04:03:21 发布