add Voice Card Calendar - Android android.provider

Android examples for android.provider:CalendarContract

Description

add Voice Card Calendar

Demo Code


//package com.java2s;

import java.util.TimeZone;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.Color;
import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;

public class Main {
    private static final String CALENDAR_NAME = "VoiceCard";
    private static final int CALENDAR_ID_INDEX = 0,
            CALENDAR_ACCOUNT_NAME_INDEX = 1,
            CALENDAR_DISPLAY_NAME_INDEX = 2,
            CALENDAR_OWNER_ACCOUNT_INDEX = 3;
    public static final String[] CALENDAR_PROJECTION = new String[] {
            Calendars._ID, Calendars.ACCOUNT_NAME,
            Calendars.CALENDAR_DISPLAY_NAME, Calendars.OWNER_ACCOUNT };

    protected static void addVoiceCardCalendar(Context context) {
        if (getVoiceCardCalendar(context) == 0) {
            initCalendars(context);/*from   w  ww.  j a va  2 s. c o m*/
        }
    }

    protected static long getVoiceCardCalendar(Context context) {
        long calID = 0;
        Cursor cur = null;
        String where = null, selection[] = null;
        try {
            where = CALENDAR_PROJECTION[CALENDAR_DISPLAY_NAME_INDEX]
                    + " = ?";
            selection = new String[] { CALENDAR_NAME };
            cur = context.getContentResolver().query(Calendars.CONTENT_URI,
                    CALENDAR_PROJECTION, where, selection, null);
            if (cur != null && cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    calID = cur.getLong(CALENDAR_ID_INDEX);
                }
            }
        } catch (SQLException e) {
            System.out
                    .println("[CalendarIntentHelper][getVoiceCardCalendar]SQLException:"
                            + e);
        } catch (Exception ex) {
            System.out
                    .println("[CalendarIntentHelper][getVoiceCardCalendar]Exception:"
                            + ex);
        }
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
        return calID;
    }

    private static void initCalendars(Context activity) {
        TimeZone timeZone = TimeZone.getDefault();
        ContentValues value = new ContentValues();
        value.put(Calendars.OWNER_ACCOUNT, CALENDAR_NAME);
        value.put(Calendars.NAME, CALENDAR_NAME);
        value.put(Calendars.ACCOUNT_NAME, CALENDAR_NAME);
        value.put(Calendars.ACCOUNT_TYPE,
                CalendarContract.ACCOUNT_TYPE_LOCAL);
        value.put(Calendars.CALENDAR_DISPLAY_NAME, CALENDAR_NAME);
        value.put(Calendars.VISIBLE, 1);
        value.put(Calendars.CALENDAR_COLOR, Color.RED);
        value.put(Calendars.CALENDAR_ACCESS_LEVEL,
                Calendars.CAL_ACCESS_OWNER);
        value.put(Calendars.SYNC_EVENTS, 1);
        value.put(Calendars.CALENDAR_TIME_ZONE, timeZone.getID());
        value.put(Calendars.OWNER_ACCOUNT, CALENDAR_NAME);
        value.put(Calendars.CAN_ORGANIZER_RESPOND, 0);
        Uri calendarUri = Calendars.CONTENT_URI;
        calendarUri = calendarUri
                .buildUpon()
                .appendQueryParameter(
                        CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(Calendars.ACCOUNT_NAME, CALENDAR_NAME)
                .appendQueryParameter(Calendars.ACCOUNT_TYPE,
                        CalendarContract.ACCOUNT_TYPE_LOCAL).build();
        activity.getContentResolver().insert(calendarUri, value);
    }
}

Related Tutorials