michael@0: /** michael@0: * Copyright (c) 2012-2013, Gerald Garcia, Timo Berger michael@0: * michael@0: * This file is part of Andoid Caldav Sync Adapter Free. michael@0: * michael@0: * Andoid Caldav Sync Adapter Free is free software: you can redistribute michael@0: * it and/or modify it under the terms of the GNU General Public License michael@0: * as published by the Free Software Foundation, either version 3 of the michael@0: * License, or at your option any later version. michael@0: * michael@0: * Andoid Caldav Sync Adapter Free is distributed in the hope that michael@0: * it will be useful, but WITHOUT ANY WARRANTY; without even the implied michael@0: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the michael@0: * GNU General Public License for more details. michael@0: * michael@0: * You should have received a copy of the GNU General Public License michael@0: * along with Andoid Caldav Sync Adapter Free. michael@0: * If not, see . michael@0: * michael@0: */ michael@0: package org.gege.caldavsyncadapter; michael@0: michael@0: import android.content.ContentValues; michael@0: import android.provider.CalendarContract.Events; michael@0: import android.util.Log; michael@0: michael@0: /** michael@0: * abstract class for Calendar and Android events michael@0: */ michael@0: abstract public class Event { michael@0: private static final String TAG = "Event"; michael@0: michael@0: /** michael@0: * stores the ETAG of an event michael@0: */ michael@0: public static String ETAG = Events.SYNC_DATA1; michael@0: michael@0: /** michael@0: * internal Tag used to identify deleted events michael@0: */ michael@0: public static String INTERNALTAG = Events.SYNC_DATA2; michael@0: michael@0: /** michael@0: * store the whole VEVENT in here michael@0: * missing TAGs they might be missing for google update michael@0: * michael@0: * CREATED:20130906T102857Z michael@0: * DTSTAMP:20130906T102857Z michael@0: * LAST-MODIFIED:20130906T102857Z michael@0: * SEQUENCE:0 michael@0: */ michael@0: public static String RAWDATA = Events.SYNC_DATA3; michael@0: michael@0: /** michael@0: * stores the UID of an Event michael@0: * example: UID:e6be67c6-eff0-44f8-a1a0-6c2cb1029944-caldavsyncadapter michael@0: */ michael@0: public static String UID = Events.SYNC_DATA4; michael@0: michael@0: /** michael@0: * the event transformed into ContentValues michael@0: */ michael@0: public ContentValues ContentValues = new ContentValues(); michael@0: michael@0: abstract public String getETag(); michael@0: abstract public void setETag(String ETag); michael@0: michael@0: /** michael@0: * returns a list of all items that are comparable with this sync adapter michael@0: * @return a list of all items that are comparable with this sync adapter michael@0: */ michael@0: public static java.util.ArrayList getComparableItems() { michael@0: java.util.ArrayList Result = new java.util.ArrayList(); michael@0: Result.add(Events.DTSTART); michael@0: Result.add(Events.DTEND); michael@0: Result.add(Events.EVENT_TIMEZONE); michael@0: Result.add(Events.EVENT_END_TIMEZONE); michael@0: Result.add(Events.ALL_DAY); michael@0: Result.add(Events.DURATION); michael@0: Result.add(Events.TITLE); michael@0: Result.add(Events.CALENDAR_ID); michael@0: Result.add(Events._SYNC_ID); michael@0: //Result.add(Events.SYNC_DATA1); michael@0: Result.add(ETAG); michael@0: Result.add(Events.DESCRIPTION); michael@0: Result.add(Events.EVENT_LOCATION); michael@0: Result.add(Events.ACCESS_LEVEL); michael@0: Result.add(Events.STATUS); michael@0: Result.add(Events.RDATE); michael@0: Result.add(Events.RRULE); michael@0: Result.add(Events.EXRULE); michael@0: Result.add(Events.EXDATE); michael@0: Result.add(UID); michael@0: michael@0: return Result; michael@0: } michael@0: michael@0: /** michael@0: * sets the AndroidCalendarId for this event michael@0: * @param ID the AndroidCalendarId for this event michael@0: */ michael@0: public void setAndroidCalendarId(long ID) { michael@0: if (this.ContentValues.containsKey(Events.CALENDAR_ID)) michael@0: this.ContentValues.remove(Events.CALENDAR_ID); michael@0: michael@0: this.ContentValues.put(Events.CALENDAR_ID, ID); michael@0: } michael@0: michael@0: /** michael@0: * returns the AndroidCalendarId for this event. michael@0: * @return the AndroidCalendarId for this event michael@0: */ michael@0: public long getAndroidCalendarId() { michael@0: long Result = -1; michael@0: if (this.ContentValues.containsKey(Events.CALENDAR_ID)) michael@0: Result = this.ContentValues.getAsLong(Events.CALENDAR_ID); michael@0: return Result; michael@0: } michael@0: michael@0: /** michael@0: * returns the UID for this event. you can also check, whether the UID was stored from server. the V1.7 release and before didn't save them. michael@0: * example: UID:e6be67c6-eff0-44f8-a1a0-6c2cb1029944-caldavsyncadapter michael@0: * @return the UID for this event michael@0: */ michael@0: public String getUID() { michael@0: String Result = ""; michael@0: if (this.ContentValues.containsKey(UID)) michael@0: Result = this.ContentValues.getAsString(UID); michael@0: michael@0: return Result; michael@0: } michael@0: michael@0: /** michael@0: * compares the given ContentValues with the current ones for differences michael@0: * @param calendarEventValues the contentValues of the calendar event michael@0: * @return if the events are different michael@0: */ michael@0: public boolean checkEventValuesChanged(ContentValues calendarEventValues) { michael@0: boolean Result = false; michael@0: Object ValueAndroid = null; michael@0: Object ValueCalendar = null; michael@0: java.util.ArrayList CompareItems = Event.getComparableItems(); michael@0: michael@0: for (String Key: CompareItems) { michael@0: michael@0: if (this.ContentValues.containsKey(Key)) michael@0: ValueAndroid = this.ContentValues.get(Key); michael@0: else michael@0: ValueAndroid = null; michael@0: michael@0: if (calendarEventValues.containsKey(Key)) michael@0: ValueCalendar = calendarEventValues.get(Key); michael@0: else michael@0: ValueCalendar = null; michael@0: michael@0: /* michael@0: * TODO: Sync is designed to "Server always wins", should be a general option for this adapter michael@0: */ michael@0: if (ValueAndroid != null) { michael@0: if (ValueCalendar != null) { michael@0: if (!ValueAndroid.toString().equals(ValueCalendar.toString())) { michael@0: Log.d(TAG, "difference in " + Key.toString() + ":" + ValueAndroid.toString() + " <> " + ValueCalendar.toString()); michael@0: this.ContentValues.put(Key,ValueCalendar.toString()); michael@0: Result = true; michael@0: } michael@0: } else { michael@0: Log.d(TAG, "difference in " + Key.toString() + ":" + ValueAndroid.toString() + " <> null"); michael@0: this.ContentValues.putNull(Key); michael@0: Result = true; michael@0: } michael@0: } else { michael@0: if (ValueCalendar != null) { michael@0: Log.d(TAG, "difference in " + Key.toString() + ":null <> " + ValueCalendar.toString()); michael@0: this.ContentValues.put(Key, ValueCalendar.toString()); michael@0: Result = true; michael@0: } else { michael@0: // both null -> this is ok michael@0: } michael@0: } michael@0: } michael@0: michael@0: return Result; michael@0: } michael@0: }