ensure Local Calendar With Name - Android Android OS

Android examples for Android OS:Calendar Name

Description

ensure Local Calendar With Name

Demo Code


//package com.java2s;

import java.util.TimeZone;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;

import android.provider.CalendarContract.Calendars;

public class Main {
    public static Uri ensureLocalCalendarWithName(Context ctx, String name) {
        Uri query = CalendarContract.Calendars.CONTENT_URI;

        Cursor c = ctx.getContentResolver().query(query, null,
                Calendars.NAME + "=?", new String[] { name }, null);
        long id = -1;
        if (c.moveToNext()) {
            id = c.getLong(c.getColumnIndex(Calendars._ID));
        }//  w ww.  j ava2 s .  c  o  m
        c.close();
        if (id > 0) {
            return query.buildUpon().appendPath(Long.toString(id)).build();
        }

        Uri target = CalendarContract.Calendars.CONTENT_URI
                .buildUpon()
                .appendQueryParameter(
                        CalendarContract.CALLER_IS_SYNCADAPTER,
                        Boolean.TRUE.toString())
                .appendQueryParameter(
                        CalendarContract.Calendars.ACCOUNT_NAME,
                        "dummyAccount")
                .appendQueryParameter(
                        CalendarContract.Calendars.ACCOUNT_TYPE,
                        CalendarContract.ACCOUNT_TYPE_LOCAL).build();

        ContentValues values = new ContentValues();

        values.put(Calendars.ACCOUNT_NAME, "dummyAccount");
        values.put(Calendars.ACCOUNT_TYPE,
                CalendarContract.ACCOUNT_TYPE_LOCAL);

        //inseriamo lo stesso valore nome e display name per semplicita
        values.put(Calendars.NAME, name);
        values.put(Calendars.CALENDAR_DISPLAY_NAME, name);

        values.put(Calendars.CALENDAR_COLOR, 0xFF0000);

        values.put(Calendars.VISIBLE, 1);
        values.put(Calendars.SYNC_EVENTS, 1);
        values.put(Calendars.CALENDAR_TIME_ZONE, TimeZone.getDefault()
                .toString());

        Uri newCalendar = ctx.getContentResolver().insert(target, values);
        return newCalendar;
    }
}

Related Tutorials