|
|
@@ -10,7 +10,9 @@ import android.graphics.Color;
|
|
|
import android.net.Uri;
|
|
|
import android.provider.CalendarContract;
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
+import android.util.Log;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.uas.appworks.model.Schedule;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
@@ -102,7 +104,9 @@ public class ScheduleUtils {
|
|
|
int calId = checkCalendarAccount(context);
|
|
|
if (calId < 0) {
|
|
|
// 获取账户id失败直接返回,添加日历事件失败
|
|
|
- return -1;
|
|
|
+ if (addCalendarAccount(context)==-1 ) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
}
|
|
|
//开始添加日程事件
|
|
|
ContentValues event = new ContentValues();
|
|
|
@@ -111,9 +115,10 @@ public class ScheduleUtils {
|
|
|
event.put(CalendarContract.Events.TITLE, mSchedule.getTitle());//添加标题
|
|
|
event.put(CalendarContract.Events.DESCRIPTION, mSchedule.getRemarks());//添加描述|备注
|
|
|
event.put(CalendarContract.Events.EVENT_LOCATION, mSchedule.getAddress());//事件地点
|
|
|
- event.put(CalendarContract.Events.EVENT_COLOR, Color.GREEN);//颜色
|
|
|
+ event.put(CalendarContract.Events.RDATE, mSchedule.getRepeat());//标签
|
|
|
+ event.put(CalendarContract.Events.EXDATE, mSchedule.getTag());//标签
|
|
|
// event.put(CalendarContract.Events.DISPLAY_COLOR, Color.GREEN);//显示颜色
|
|
|
-// event.put(CalendarContract.Events.RDATE,"TODO");//事件重复日期
|
|
|
+ event.put(CalendarContract.Events.EVENT_COLOR, Color.GREEN);//颜色
|
|
|
event.put(CalendarContract.Events.ORGANIZER, DEF_EMAIL);//事件所有者的名称
|
|
|
event.put(CalendarContract.Events.LAST_DATE, System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);//事件重复日期
|
|
|
event.put(CalendarContract.Events.DTSTART, mSchedule.getStartTime());//日程开始时间
|
|
|
@@ -156,29 +161,57 @@ public class ScheduleUtils {
|
|
|
* @return
|
|
|
*/
|
|
|
public static List<Schedule> getSystemCalendar(Context context) {
|
|
|
- List<Schedule> mSchedules = null;
|
|
|
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
|
|
|
- return mSchedules;
|
|
|
+ //TODO 没有权限
|
|
|
+ return null;
|
|
|
}
|
|
|
String selection = CalendarContract.Events.ORGANIZER + " =? ";
|
|
|
String[] selectionArgs = new String[]{DEF_EMAIL};
|
|
|
+ Cursor userCursor = context.getContentResolver().query(CalendarContract.Events.CONTENT_URI,
|
|
|
+ null, selection, selectionArgs, null);
|
|
|
+ try {
|
|
|
+ if (userCursor == null)//查询返回空值
|
|
|
+ return null;
|
|
|
+ List<Schedule> mSchedules = new ArrayList<>();
|
|
|
+ while (userCursor.moveToNext()) {
|
|
|
+ mSchedules.add(getSchedule(userCursor));
|
|
|
+ }
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ if (userCursor != null) {
|
|
|
+ userCursor.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Schedule> getSystemCalendar(Context context, long startTime, long endTime) {
|
|
|
+ List<Schedule> mSchedules = null;
|
|
|
+ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ return mSchedules;
|
|
|
+ }
|
|
|
+ String selection = CalendarContract.Events.CAN_PARTIALLY_UPDATE + " =? "
|
|
|
+ +"and ( ("
|
|
|
+ + CalendarContract.Events.DTSTART + " >=? and " + CalendarContract.Events.DTSTART + " <=? ) or ("
|
|
|
+ + CalendarContract.Events.DTEND + " >=? and " + CalendarContract.Events.DTEND + " <=? ) or ( "
|
|
|
+ + CalendarContract.Events.DTSTART + " <? and " + CalendarContract.Events.DTEND + " >?"
|
|
|
+ + " ))";
|
|
|
+ String[] selectionArgs = new String[]{String.valueOf(0)
|
|
|
+ , String.valueOf(startTime), String.valueOf(endTime)
|
|
|
+ , String.valueOf(startTime), String.valueOf(endTime)
|
|
|
+ , String.valueOf(startTime), String.valueOf(endTime)
|
|
|
+ };
|
|
|
+// Log.i("gong", "selection=" + selection);
|
|
|
+// Log.i("gong", "startTime=" + startTime);
|
|
|
+// Log.i("gong", "endTime=" + endTime);
|
|
|
Cursor userCursor = context.getContentResolver().query(CalendarContract.Events.CONTENT_URI,
|
|
|
null, selection, selectionArgs, null);
|
|
|
try {
|
|
|
if (userCursor == null)//查询返回空值
|
|
|
return null;
|
|
|
mSchedules = new ArrayList<>();
|
|
|
- Schedule mSchedule = null;
|
|
|
while (userCursor.moveToNext()) {
|
|
|
- mSchedule = new Schedule();
|
|
|
- mSchedule.setId(userCursor.getInt(userCursor.getColumnIndex(CalendarContract.Events.SELF_ATTENDEE_STATUS)));
|
|
|
- mSchedule.setTitle(userCursor.getString(userCursor.getColumnIndex(CalendarContract.Events.TITLE)));
|
|
|
- mSchedule.setRemarks(userCursor.getString(userCursor.getColumnIndex(CalendarContract.Events.DESCRIPTION)));
|
|
|
- mSchedule.setAddress(userCursor.getString(userCursor.getColumnIndex(CalendarContract.Events.EVENT_LOCATION)));
|
|
|
- mSchedule.setStartTime(userCursor.getLong(userCursor.getColumnIndex(CalendarContract.Events.DTSTART)));
|
|
|
- mSchedule.setEndTime(userCursor.getLong(userCursor.getColumnIndex(CalendarContract.Events.DTEND)));
|
|
|
- mSchedule.setAllDay(userCursor.getInt(userCursor.getColumnIndex(CalendarContract.Events.ORIGINAL_ALL_DAY)));
|
|
|
- mSchedules.add(mSchedule);
|
|
|
+ mSchedules.add(getSchedule(userCursor));
|
|
|
}
|
|
|
|
|
|
} finally {
|
|
|
@@ -189,7 +222,6 @@ public class ScheduleUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 删除对应的日程
|
|
|
*
|
|
|
@@ -222,4 +254,36 @@ public class ScheduleUtils {
|
|
|
deleteSystemCalendar(context, mSchedule.getId());
|
|
|
return addCalendarEvent(context, mSchedule);
|
|
|
}
|
|
|
+ private static Schedule getSchedule(Cursor cursor) {
|
|
|
+ int calendar_id = cursor.getInt(cursor.getColumnIndex(CalendarContract.Events.CALENDAR_ID));
|
|
|
+ int _id = cursor.getInt(cursor.getColumnIndex(CalendarContract.Events.SELF_ATTENDEE_STATUS));
|
|
|
+ int event_color = cursor.getInt(cursor.getColumnIndex(CalendarContract.Events.EVENT_COLOR));
|
|
|
+ int has_alarm = cursor.getInt(cursor.getColumnIndex(CalendarContract.Events.HAS_ALARM));
|
|
|
+ int allDay = cursor.getInt(cursor.getColumnIndex(CalendarContract.Events.ORIGINAL_ALL_DAY));
|
|
|
+
|
|
|
+ long last_date = cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.LAST_DATE));
|
|
|
+ long startTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTSTART));
|
|
|
+ long endTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTEND));
|
|
|
+
|
|
|
+ String title = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.TITLE));
|
|
|
+ String remarks = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.DESCRIPTION));
|
|
|
+ String address = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.EVENT_LOCATION));
|
|
|
+ String rdate = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.RDATE));
|
|
|
+ String organizer = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.ORGANIZER));
|
|
|
+ String tag = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.EXDATE));
|
|
|
+ Schedule mSchedule = new Schedule();
|
|
|
+ mSchedule.setId(_id);
|
|
|
+ mSchedule.setAllDay(allDay);
|
|
|
+ mSchedule.setStartTime(startTime);
|
|
|
+ mSchedule.setEndTime(endTime);
|
|
|
+ mSchedule.setTitle(title);
|
|
|
+ mSchedule.setRemarks(remarks);
|
|
|
+ mSchedule.setRemarks(address);
|
|
|
+ mSchedule.setRepeat(rdate);
|
|
|
+ mSchedule.setStatus(organizer);
|
|
|
+ mSchedule.setTag(tag);
|
|
|
+ Log.i("gong", JSON.toJSONString(mSchedule));
|
|
|
+ Log.i("gong", "_________________________________");
|
|
|
+ return mSchedule;
|
|
|
+ }
|
|
|
}
|