Android Open Source - Multi-Mania-app Google Cal Util






From Project

Back to project page Multi-Mania-app.

License

The source code is released under:

MIT License

If you think the Android project Multi-Mania-app 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 be.ana.nmct.multimania.utils;
//  w w  w  . ja va2 s.  c o m
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.provider.CalendarContract;

import java.util.List;

import be.ana.nmct.multimania.R;
import be.ana.nmct.multimania.data.MultimaniaContract;
import be.ana.nmct.multimania.model.Talk;
import be.ana.nmct.multimania.vm.ScheduleTalkVm;

/**
 * Created by Axel on 29/10/2014.
 */
public class GoogleCalUtil {

    private final static String TAG = GoogleCalUtil.class.getCanonicalName();

    private final static Uri CAL_URI = CalendarContract.Calendars.CONTENT_URI;
    private final static Uri EVENT_URI = CalendarContract.Events.CONTENT_URI;

    public final static String PREFERENCE_NAME = "calendar_preferences";
    public final static String PREFERENCE_CALENDER_ID = "calendar_id";
    public final static String PREFERENCE_ACCOUNTNAME = "calendar_accountname";

    private Context mContext;
    private static String mCalendarName;
    private SettingsUtil mUtil;

    public GoogleCalUtil(Context context, String calendarName) {
        this.mContext = context;
        this.mCalendarName = calendarName;
        this.mUtil = new SettingsUtil(mContext, PREFERENCE_NAME);
    }

    /**
     * Creates instance of ContentValues with all calendar properties set
     * @return ContentValues from CalendarContract
     */
    private ContentValues buildNewCalContentValues() {
        final ContentValues cv = new ContentValues();
        String accountName = mUtil.getStringPreference(PREFERENCE_ACCOUNTNAME);
        cv.put(CalendarContract.Calendars.ACCOUNT_NAME, accountName);
        cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        cv.put(CalendarContract.Calendars.NAME, mCalendarName);
        cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, mCalendarName);
        cv.put(CalendarContract.Calendars.CALENDAR_COLOR, mContext.getResources().getColor(R.color.primaryColor));
        cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
        cv.put(CalendarContract.Calendars.OWNER_ACCOUNT, accountName);
        cv.put(CalendarContract.Calendars.VISIBLE, 1);
        cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
        return cv;
    }

    /**
     * Creates a Calendar entry in the users calendar (Google Cal or other cal app)
     */
    public void createCalendar() {
        ContentResolver cr = mContext.getContentResolver();
        final ContentValues cv = buildNewCalContentValues();
        Uri newUri = cr.insert(buildCalUri(), cv);
        mUtil.setPreference(PREFERENCE_CALENDER_ID, Long.parseLong(newUri.getLastPathSegment()));
    }


    /**
     * Deletes a Calendar entry in the users calendar (Google Cal or other cal app)
     */
    public void deleteCalendar() {
        ContentResolver cr = mContext.getContentResolver();
        long calId = mUtil.getLongPreference(PREFERENCE_CALENDER_ID);
        Uri calUri = ContentUris.withAppendedId(buildCalUri(), calId);
        cr.delete(calUri, null, null);
    }

    /**
     * Adds a list of talks to the calendar
     * @param talks The list of talks to add
     */
    public void addTalkList(List<ScheduleTalkVm> talks){
        for(int i = 0; i < talks.size(); i++){
            addTalk(talks.get(i));
        }
    }

    /** Adds a talk to the calendar
     * @param talk The context (e.g. activity)
     * @return returns the calEventId
     */
    public long addTalk(ScheduleTalkVm talk) {
        ContentResolver cr = mContext.getContentResolver();
        ContentValues cv = new ContentValues();

        long calId = mUtil.getLongPreference(PREFERENCE_CALENDER_ID);
        cv.put(CalendarContract.Events.CALENDAR_ID, calId);
        cv.put(CalendarContract.Events.TITLE, talk.title);
        cv.put(CalendarContract.Events.DTSTART, Utility.getDateInMillis(talk.from));
        cv.put(CalendarContract.Events.DTEND, Utility.getDateInMillis(talk.to));
        cv.put(CalendarContract.Events.EVENT_LOCATION, talk.room);
        cv.put(CalendarContract.Events.DESCRIPTION, talk.description);
        cv.put(CalendarContract.Events.EVENT_TIMEZONE, Utility.getTimeZoneId());

        long calEventId = Long.parseLong(cr.insert(buildEventUri(), cv).getLastPathSegment());
        saveCalEventId(talk, calEventId);

        return calEventId;
    }

    /**
     *  Saves the calendar event Id to the SQLite db for future reference
     * @param talk The talk to save the caleventId to
     * @param eventId The eventId to save
     * @return an integer indicating how many rows were updated (should be 1 row)
     */
    private int saveCalEventId(ScheduleTalkVm talk, long eventId) {
        ContentValues cv = new ContentValues();
        cv.put(MultimaniaContract.TalkEntry.CALEVENT_ID, eventId);
        return mContext.getContentResolver().update(
                MultimaniaContract.TalkEntry.CONTENT_URI,
                cv,
                MultimaniaContract.TalkEntry._ID + "=?",
                new String[] {String.valueOf(talk.id)}
        );
    }

    /**
     * Deletes a talk from the users calendar (in Google Cal or other calendar app)
     * @param talk The talk to delete
     * @return The amount of rows that were affected
     */
    public int deleteTalk(Talk talk){
        return mContext.getContentResolver().delete(
                buildEventUri(),
                "("+ CalendarContract.Events._ID+" = ?)",
                new String[] {String.valueOf(talk.calEventId)}
        );
    }

    /**
     * Gets the Uri needed to CRUD the calendar
     * @return Uri that enables CRUD actions on the calendar
     */
    public Uri buildCalUri() {
        return CAL_URI
                .buildUpon()
                .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, mUtil.getStringPreference(PREFERENCE_ACCOUNTNAME))
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE,
                        CalendarContract.ACCOUNT_TYPE_LOCAL)
                .build();
    }

    /**
     * Gets the Uri needed to CRUD an event
     * @return Uri that enables CRUD actions on an event
     */
    private Uri buildEventUri() {
        return EVENT_URI
                .buildUpon()
                .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, mUtil.getStringPreference(PREFERENCE_ACCOUNTNAME))
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE,
                        CalendarContract.ACCOUNT_TYPE_LOCAL)
                .build();
    }
}




Java Source Code List

be.ana.nmct.multimania.ApiTest.java
be.ana.nmct.multimania.ApplicationTest.java
be.ana.nmct.multimania.CalendarTest.java
be.ana.nmct.multimania.DbTest.java
be.ana.nmct.multimania.Import.java
be.ana.nmct.multimania.NotificationImport.java
be.ana.nmct.multimania.ProviderTest.java
be.ana.nmct.multimania.SyncTest.java
be.ana.nmct.multimania.data.ApiActions.java
be.ana.nmct.multimania.data.DbHelper.java
be.ana.nmct.multimania.data.GsonLoader.java
be.ana.nmct.multimania.data.MultimaniaContract.java
be.ana.nmct.multimania.data.MultimaniaProvider.java
be.ana.nmct.multimania.data.NewsItemLoader.java
be.ana.nmct.multimania.data.RoomLoader.java
be.ana.nmct.multimania.data.TagLoader.java
be.ana.nmct.multimania.data.TalkLoader.java
be.ana.nmct.multimania.model.IData.java
be.ana.nmct.multimania.model.NewsItem.java
be.ana.nmct.multimania.model.Room.java
be.ana.nmct.multimania.model.Speaker.java
be.ana.nmct.multimania.model.Tag.java
be.ana.nmct.multimania.model.TalkSpeaker.java
be.ana.nmct.multimania.model.TalkTag.java
be.ana.nmct.multimania.model.Talk.java
be.ana.nmct.multimania.model.User.java
be.ana.nmct.multimania.service.AuthenticatorService.java
be.ana.nmct.multimania.service.Authenticator.java
be.ana.nmct.multimania.service.BootListener.java
be.ana.nmct.multimania.service.NotificationReceiver.java
be.ana.nmct.multimania.service.NotificationSender.java
be.ana.nmct.multimania.service.SyncAdapter.java
be.ana.nmct.multimania.service.SyncService.java
be.ana.nmct.multimania.ui.AboutFragment.java
be.ana.nmct.multimania.ui.LoadActivity.java
be.ana.nmct.multimania.ui.MainActivity.java
be.ana.nmct.multimania.ui.MapFragment.java
be.ana.nmct.multimania.ui.MyScheduleFragment.java
be.ana.nmct.multimania.ui.MySchedulesFragment.java
be.ana.nmct.multimania.ui.NavigationDrawerFragment.java
be.ana.nmct.multimania.ui.NewsFragment.java
be.ana.nmct.multimania.ui.NewsItemActivity.java
be.ana.nmct.multimania.ui.NewsItemFragment.java
be.ana.nmct.multimania.ui.ScheduleFragment.java
be.ana.nmct.multimania.ui.SchedulesFragment.java
be.ana.nmct.multimania.ui.SettingsFragment.java
be.ana.nmct.multimania.ui.SuggestionFragment.java
be.ana.nmct.multimania.ui.TalkActivity.java
be.ana.nmct.multimania.ui.TalkFragment.java
be.ana.nmct.multimania.utils.GoogleCalUtil.java
be.ana.nmct.multimania.utils.SettingsHelper.java
be.ana.nmct.multimania.utils.SettingsUtil.java
be.ana.nmct.multimania.utils.SyncUtils.java
be.ana.nmct.multimania.utils.Utility.java
be.ana.nmct.multimania.vm.NavigationItem.java
be.ana.nmct.multimania.vm.ScheduleTalkVm.java
be.ana.nmct.multimania.widget.StaggeredGridView.java
com.bulletnoid.android.widget.StaggeredGridView.BulletStaggeredGridView.java
com.bulletnoid.android.widget.StaggeredGridView.HeaderFooterListAdapter.java
com.bulletnoid.android.widget.StaggeredGridView.ScrollerCompatIcs.java
com.bulletnoid.android.widget.StaggeredGridView.ScrollerCompat.java