static PendingIntent getCalendarPendingIntent(Context context, int requestCode) { Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); builder.appendPath("time"); ContentUris.appendId(builder, System.currentTimeMillis()); return PendingIntent.getActivity( context, requestCode, new Intent(Intent.ACTION_VIEW).setData(builder.build()), PendingIntent.FLAG_UPDATE_CURRENT); } }
/** * Performs a query to return all visible instances in the given range. * This is a blocking function and should not be done on the UI thread. * This will cause an expansion of recurring events to fill this time * range if they are not already expanded and will slow down for larger * time ranges with many recurring events. * * @param cr The ContentResolver to use for the query * @param projection The columns to return * @param begin The start of the time range to query in UTC millis since * epoch * @param end The end of the time range to query in UTC millis since * epoch * @return A Cursor containing all instances in the given range */ public static final Cursor query(ContentResolver cr, String[] projection, long begin, long end) { Uri.Builder builder = CONTENT_URI.buildUpon(); ContentUris.appendId(builder, begin); ContentUris.appendId(builder, end); return cr.query(builder.build(), projection, WHERE_CALENDARS_SELECTED, WHERE_CALENDARS_ARGS, DEFAULT_SORT_ORDER); }
/** * Retrieves the days with events for the Julian days starting at * "startDay" for "numDays". It returns a cursor containing startday and * endday representing the max range of days for all events beginning on * each startday.This is a blocking function and should not be done on * the UI thread. * * @param cr the ContentResolver * @param startDay the first Julian day in the range * @param numDays the number of days to load (must be at least 1) * @param projection the columns to return in the cursor * @return a database cursor containing a list of start and end days for * events */ public static final Cursor query(ContentResolver cr, int startDay, int numDays, String[] projection) { if (numDays < 1) { return null; } int endDay = startDay + numDays - 1; Uri.Builder builder = CONTENT_URI.buildUpon(); ContentUris.appendId(builder, startDay); ContentUris.appendId(builder, endDay); return cr.query(builder.build(), projection, SELECTION, null /* selection args */, STARTDAY); } }
static Intent createOpenCalendarAtDayIntent(DateTime goToTime) { Intent intent = createCalendarIntent(); Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); builder.appendPath(TIME); if (goToTime.getMillis() != 0) { intent.putExtra(KEY_DETAIL_VIEW, true); ContentUris.appendId(builder, goToTime.getMillis()); } intent.setData(builder.build()); return intent; }
/** * Starts the Google calendar Activity to display the exported calendar. */ private void displayCalendarOnGoogleCalendar() { // displaying Calendar long startMillis = DateTime.now() .getMillis(); Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); builder.appendPath("time"); ContentUris.appendId(builder, startMillis); Intent intent = new Intent(Intent.ACTION_VIEW).setData(builder.build()); startActivity(intent); }
/** * Performs a query to return all visible instances in the given range * that match the given query. This is a blocking function and should * not be done on the UI thread. This will cause an expansion of * recurring events to fill this time range if they are not already * expanded and will slow down for larger time ranges with many * recurring events. * * @param cr The ContentResolver to use for the query * @param projection The columns to return * @param begin The start of the time range to query in UTC millis since * epoch * @param end The end of the time range to query in UTC millis since * epoch * @param searchQuery A string of space separated search terms. Segments * enclosed by double quotes will be treated as a single * term. * @return A Cursor of instances matching the search terms in the given * time range */ public static final Cursor query(ContentResolver cr, String[] projection, long begin, long end, String searchQuery) { Uri.Builder builder = CONTENT_SEARCH_URI.buildUpon(); ContentUris.appendId(builder, begin); ContentUris.appendId(builder, end); builder = builder.appendPath(searchQuery); return cr.query(builder.build(), projection, WHERE_CALENDARS_SELECTED, WHERE_CALENDARS_ARGS, DEFAULT_SORT_ORDER); }
private List<CalendarEvent> getPastEventsWithColorList() { Uri.Builder builder = Instances.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, 0); ContentUris.appendId(builder, DateUtil.now(zone).getMillis()); List<CalendarEvent> eventList = queryList(builder.build(), getPastEventsWithColorSelection()); for (CalendarEvent event : eventList) { event.setDefaultCalendarColor(); } return eventList; }
private List<CalendarEvent> getTimeFilteredEventList() { Uri.Builder builder = Instances.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, mStartOfTimeRange.getMillis()); ContentUris.appendId(builder, mEndOfTimeRange.getMillis()); List<CalendarEvent> eventList = queryList(builder.build(), getCalendarSelection()); // Above filters are not exactly correct for AllDay events: for them that filter // time should be moved by a time zone... (i.e. by several hours) // This is why we need to do additional filtering after querying a Content Provider: for (Iterator<CalendarEvent> it = eventList.iterator(); it.hasNext(); ) { CalendarEvent event = it.next(); if (!event.getEndDate().isAfter(mStartOfTimeRange) || !mEndOfTimeRange.isAfter(event.getStartDate())) { // We remove using Iterator to avoid ConcurrentModificationException it.remove(); } } return eventList; }
@Override protected Cursor getChildrenCursor(Cursor groupCursor) { // Given the group, we return a cursor for all the children within that group // Return a cursor that points to this contact's phone numbers Uri.Builder builder = Contacts.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, groupCursor.getLong(GROUP_ID_COLUMN_INDEX)); builder.appendEncodedPath(Contacts.Data.CONTENT_DIRECTORY); Uri phoneNumbersUri = builder.build(); mQueryHandler.startQuery(TOKEN_CHILD, groupCursor.getPosition(), phoneNumbersUri, PHONE_NUMBER_PROJECTION, Phone.MIMETYPE + "=?", new String[] { Phone.CONTENT_ITEM_TYPE }, null); return null; } }
@Override protected Cursor getChildrenCursor(Cursor groupCursor) { // Given the group, we return a cursor for all the children within that group // Return a cursor that points to this contact's phone numbers Uri.Builder builder = Contacts.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, groupCursor.getLong(GROUP_ID_COLUMN_INDEX)); builder.appendEncodedPath(Contacts.Data.CONTENT_DIRECTORY); Uri phoneNumbersUri = builder.build(); mQueryHandler.startQuery(TOKEN_CHILD, groupCursor.getPosition(), phoneNumbersUri, PHONE_NUMBER_PROJECTION, Phone.MIMETYPE + "=?", new String[] { Phone.CONTENT_ITEM_TYPE }, null); return null; } }
private void addEvent() { try { /* * NOTE: * Setting CalendarContractWrapper.Events.CALENDAR_ID only works with * AOSP calendar app > 4.3, not Google's calendar app! */ Intent intent = new Intent(Intent.ACTION_INSERT); intent.setPackage("com.android.calendar"); intent.setData(CalendarContract.Events.CONTENT_URI); intent.putExtra(CalendarContract.Events.CALENDAR_ID, mCalendarId); startActivity(intent); } catch (ActivityNotFoundException e) { try { // open calendar at today Calendar cal = GregorianCalendar.getInstance(); Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); builder.appendPath("time"); ContentUris.appendId(builder, cal.getTimeInMillis()); Intent intent = new Intent(Intent.ACTION_VIEW) .setData(builder.build()); startActivity(intent); } catch (ActivityNotFoundException ex) { Toast.makeText(this, "Not supported. Please open calendar manually!", Toast.LENGTH_LONG).show(); } } }