Ejemplo n.º 1
0
 protected static Uri calendarsURI(Account account) {
   return Calendars.CONTENT_URI
       .buildUpon()
       .appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
       .appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type)
       .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
       .build();
 }
  private static long getCalendar(Account account) {
    // Find the Last.fm calendar if we've got one
    Uri calenderUri =
        Calendars.CONTENT_URI
            .buildUpon()
            .appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
            .appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type)
            .build();
    Cursor c1 =
        mContentResolver.query(calenderUri, new String[] {BaseColumns._ID}, null, null, null);
    if (c1.moveToNext()) {
      return c1.getLong(0);
    } else {
      ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

      ContentProviderOperation.Builder builder =
          ContentProviderOperation.newInsert(
              Calendars.CONTENT_URI
                  .buildUpon()
                  .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                  .appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
                  .appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type)
                  .build());
      builder.withValue(Calendars.ACCOUNT_NAME, account.name);
      builder.withValue(Calendars.ACCOUNT_TYPE, account.type);
      builder.withValue(Calendars.NAME, "Last.fm Events");
      builder.withValue(Calendars.CALENDAR_DISPLAY_NAME, "Last.fm Events");
      builder.withValue(Calendars.CALENDAR_COLOR, 0xD51007);
      builder.withValue(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_RESPOND);
      builder.withValue(Calendars.OWNER_ACCOUNT, account.name);
      builder.withValue(Calendars.SYNC_EVENTS, 1);
      operationList.add(builder.build());
      try {
        mContentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return -1;
      }
      return getCalendar(account);
    }
  }
  private static void moveCalendarData(
      final ContentResolver resolver,
      final String name,
      final String oldType,
      final String newType) {
    final Uri oldCalendars =
        Calendars.CONTENT_URI
            .buildUpon()
            .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
            .appendQueryParameter(Calendars.ACCOUNT_NAME, name)
            .appendQueryParameter(Calendars.ACCOUNT_TYPE, oldType)
            .build();

    // Update this calendar to have the new account type.
    final ContentValues values = new ContentValues();
    values.put(CalendarContract.Calendars.ACCOUNT_TYPE, newType);
    resolver.update(
        oldCalendars,
        values,
        Calendars.ACCOUNT_NAME + "=? AND " + Calendars.ACCOUNT_TYPE + "=?",
        new String[] {name, oldType});
  }