I can not create a calendar that syncs with Google

1

I'm trying to create a calendar in the Google account, I've managed to create calendars but none of them are synchronized with the Google account and I do not know what I'm doing wrong.

I think I know where the problem is but I can not solve it.

the code is as follows:

public static long createCalendar (Activity activity, String name, String account, boolean local){

        String color = "blue";

        ContentValues calendarvalues = new ContentValues();

        //The account that was used to sync the entry to the device. If the account_type is not {@link #ACCOUNT_TYPE_LOCAL} then the name and
        // type must match an account on the device or the calendar will be deleted.
        if(local) {
            calendarvalues.put(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMYLOCAL");
            calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        }else{
            calendarvalues.put(CalendarContract.Calendars.ACCOUNT_NAME, account);
            calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, account);
        }
        //Local  CalendarContract.ACCOUNT_TYPE_LOCAL

        calendarvalues.put(CalendarContract.Calendars.NAME, name);
        calendarvalues.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, name);
        calendarvalues.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.parseColor(color));
        calendarvalues.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
//        //None          CalendarContract.Calendars.CAL_ACCESS_NONE          Cannot access the calendar
//        //freeBusy      CalendarContract.Calendars.CAL_ACCESS_FREEBUSY      Can only see free/busy information about the calendar
//        //Read          CalendarContract.Calendars.CAL_ACCESS_READ          Can read all event details
//        //Respond       CalendarContract.Calendars.CAL_ACCESS_RESPOND       Can reply yes/no/maybe to an event
//        //Override      CalendarContract.Calendars.CAL_ACCESS_OVERRIDE      not used
//        //Contributor   CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR   Full access to modify the calendar, but not the access control settings
//        //Editor        CalendarContract.Calendars.CAL_ACCESS_EDITOR        Full access to modify the calendar, but not the access control settings
//        //Owner         CalendarContract.Calendars.CAL_ACCESS_OWNER         Full access to the calendar
//        //Root          CalendarContract.Calendars.CAL_ACCESS_ROOT          Domain admin

        calendarvalues.put(CalendarContract.Calendars.OWNER_ACCOUNT, account);
        calendarvalues.put(CalendarContract.Calendars.VISIBLE, 1);
        calendarvalues.put(CalendarContract.Calendars.SYNC_EVENTS, 1);

//        calendarvalues.put(CalendarContract.Calendars.CALENDAR_LOCATION, "Spain");


        Uri calUri = null;
        Uri result = null;

        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

            PermissionUtil.requestCalendarPermission(activity);

            return -1;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            calUri = CalendarContract.Calendars.CONTENT_URI;
        }else{
            calUri = Uri.parse("content://com.android.calendar/calendars");
        }

        if(calUri != null) {
            if(local) {
                calUri = calUri.buildUpon()
                        .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMYLOCAL")
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
                        .build();
            }else {
                calUri = calUri.buildUpon()
                        .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account)
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, account)
                        .build();
            }

            result = activity.getContentResolver().insert(calUri, calendarvalues);
        }

        if (result != null) {
            try {
                return Long.parseLong(result.getLastPathSegment());
            } catch (Exception e) {
                return -1;
            }
        }
        return -1;
    }

I think the error is in the following line:

calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, account);

Because I've been looking at the values returned by the following query:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
     calUri = CalendarContract.Calendars.CONTENT_URI;
}else{
     calUri = Uri.parse(calendarUriString);
}

String[] projection = new String[]{
            CalendarContract.Calendars._ID,
            CalendarContract.Calendars.NAME,
            CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
            CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
            CalendarContract.Calendars.ACCOUNT_NAME,
            CalendarContract.Calendars.ACCOUNT_TYPE,
    //      CalendarContract.Calendars.CALENDAR_COLOR,
            CalendarContract.Calendars.OWNER_ACCOUNT,
            CalendarContract.Calendars.VISIBLE,
            CalendarContract.Calendars.SYNC_EVENTS,
 };

 Cursor cursor = activity.getContentResolver().query(calUri, projection, null, null, null);

and the result is as follows:

id: 1
Name: My Calendar @ Local
Display name: My Calendar
access level: 700
AccountName: My Calendar @ Local
AccountType: com.local
ownerAccount: Owner Account visible: 1
sync: 1

id: 2
Name: [email protected]
Display name: [email protected]
access level: 700
AccountName: [email protected]
AccountType: com.google
ownerAccount: [email protected]
visible: 1
sync: 1

id: 3
Name: Test Cal
Display name: Test Cal
access level: 700
AccountName: [email protected]
AccountType: [email protected]
ownerAccount: 1
visible: 1
sync: 1

I tried the following:

calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");

But the calendar is not even created.

If someone knows how to do it or I can pass some documentation or examples that help me, I would appreciate it.

    
asked by Joacer 16.09.2016 в 17:19
source

0 answers