Android Open Source - android-calendar-api-sample Calendar Util






From Project

Back to project page android-calendar-api-sample.

License

The source code is released under:

MIT License

If you think the Android project android-calendar-api-sample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.roottony.calendar;
//from   ww  w.  j  a v a  2s.  c  o  m
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;
import android.util.Log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class CalendarUtil {

    private static final String TAG = CalendarUtil.class.getSimpleName();


    public static void printCalendars(Context context) {
        runGenericQuery(context, "Calendars", CalendarContract.Calendars.CONTENT_URI);
    }

    public static void printEvents(Context context) {
        runGenericQuery(context, "Events", CalendarContract.Events.CONTENT_URI);
    }

    public static void printAttendees(Context context) {
        runGenericQuery(context, "Attendees", CalendarContract.Attendees.CONTENT_URI);
    }

    public static void printReminders(Context context) {
        runGenericQuery(context, "Reminders", CalendarContract.Reminders.CONTENT_URI);
    }

    public static void printEventInstances(Context context) {
        // Specify the date range you want to search for recurring
        // event instances
        Calendar beginTime = Calendar.getInstance();
        beginTime.set(2014, Calendar.APRIL, 1, 5, 0);
        long startMillis = beginTime.getTimeInMillis();
        Calendar endTime = Calendar.getInstance();
        endTime.set(2014, Calendar.APRIL, 2, 8, 0);
        long endMillis = endTime.getTimeInMillis();

        Cursor cursor;
        ContentResolver cr = context.getContentResolver();

        // Construct the query with the desired date range.
        Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
        ContentUris.appendId(builder, startMillis);
        ContentUris.appendId(builder, endMillis);

        cursor =  cr.query(builder.build(), null, null, null, null);

        String cursorContent = DatabaseUtils.dumpCursorToString(cursor);

        if (cursor != null) {
            cursor.close();
        }

        Log.i(TAG, "-------Instances cursorContent: \n" + cursorContent);
    }

    private static void runGenericQuery(Context context, String queryName, Uri queryUri) {
        // Run query
        Cursor cursor;
        ContentResolver cr = context.getContentResolver();

        // Submit the query and get a Cursor object back.
        cursor = cr.query(queryUri, null, null, null, null);

        String cursorContent = DatabaseUtils.dumpCursorToString(cursor);

        if (cursor != null) {
            cursor.close();
        }

        Log.i(TAG, "------- " + queryName + " cursorContent: \n" + cursorContent);
    }

}




Java Source Code List

com.roottony.calendar.CalendarContentProviderFragment.java
com.roottony.calendar.CalendarIntentFragment.java
com.roottony.calendar.CalendarUtil.java
com.roottony.calendar.MainActivity.java