src/org/gege/caldavsyncadapter/Event.java

changeset 0
fb9019fb1bf7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/gege/caldavsyncadapter/Event.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012-2013, Gerald Garcia, Timo Berger
     1.6 + * 
     1.7 + * This file is part of Andoid Caldav Sync Adapter Free.
     1.8 + *
     1.9 + * Andoid Caldav Sync Adapter Free is free software: you can redistribute 
    1.10 + * it and/or modify it under the terms of the GNU General Public License 
    1.11 + * as published by the Free Software Foundation, either version 3 of the 
    1.12 + * License, or at your option any later version.
    1.13 + *
    1.14 + * Andoid Caldav Sync Adapter Free is distributed in the hope that 
    1.15 + * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
    1.16 + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 + * GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License
    1.20 + * along with Andoid Caldav Sync Adapter Free.  
    1.21 + * If not, see <http://www.gnu.org/licenses/>.
    1.22 + * 
    1.23 + */
    1.24 +package org.gege.caldavsyncadapter;
    1.25 +
    1.26 +import android.content.ContentValues;
    1.27 +import android.provider.CalendarContract.Events;
    1.28 +import android.util.Log;
    1.29 +
    1.30 +/**
    1.31 + * abstract class for Calendar and Android events 
    1.32 + */
    1.33 +abstract public class Event {
    1.34 +	private static final String TAG = "Event";
    1.35 +	
    1.36 +	/**
    1.37 +	 * stores the ETAG of an event
    1.38 +	 */
    1.39 +	public static String ETAG = Events.SYNC_DATA1;
    1.40 +
    1.41 +	/**
    1.42 +	 * internal Tag used to identify deleted events 
    1.43 +	 */
    1.44 +	public static String INTERNALTAG = Events.SYNC_DATA2;
    1.45 +
    1.46 +	/**
    1.47 +	 * store the whole VEVENT in here
    1.48 +	 * missing TAGs they might be missing for google update
    1.49 +	 * 
    1.50 +	 * CREATED:20130906T102857Z
    1.51 +	 * DTSTAMP:20130906T102857Z
    1.52 +	 * LAST-MODIFIED:20130906T102857Z
    1.53 +	 * SEQUENCE:0
    1.54 +	 */
    1.55 +	public static String RAWDATA = Events.SYNC_DATA3;
    1.56 +
    1.57 +	/**
    1.58 +	 * stores the UID of an Event
    1.59 +	 * example: UID:e6be67c6-eff0-44f8-a1a0-6c2cb1029944-caldavsyncadapter
    1.60 +	 */
    1.61 +	public static String UID = Events.SYNC_DATA4;
    1.62 +	
    1.63 +	/**
    1.64 +	 * the event transformed into ContentValues
    1.65 +	 */
    1.66 +	public ContentValues ContentValues = new ContentValues();
    1.67 +	
    1.68 +	abstract public String getETag();
    1.69 +	abstract public void setETag(String ETag);
    1.70 +
    1.71 +	/**
    1.72 +	 * returns a list of all items that are comparable with this sync adapter
    1.73 +	 * @return a list of all items that are comparable with this sync adapter
    1.74 +	 */
    1.75 +	public static java.util.ArrayList<String> getComparableItems() {
    1.76 +		java.util.ArrayList<String> Result = new java.util.ArrayList<String>();
    1.77 +		Result.add(Events.DTSTART);
    1.78 +		Result.add(Events.DTEND);
    1.79 +		Result.add(Events.EVENT_TIMEZONE);
    1.80 +		Result.add(Events.EVENT_END_TIMEZONE);
    1.81 +		Result.add(Events.ALL_DAY);
    1.82 +		Result.add(Events.DURATION);
    1.83 +		Result.add(Events.TITLE);
    1.84 +		Result.add(Events.CALENDAR_ID);
    1.85 +		Result.add(Events._SYNC_ID);
    1.86 +		//Result.add(Events.SYNC_DATA1);
    1.87 +		Result.add(ETAG);
    1.88 +		Result.add(Events.DESCRIPTION);
    1.89 +		Result.add(Events.EVENT_LOCATION);
    1.90 +		Result.add(Events.ACCESS_LEVEL);
    1.91 +		Result.add(Events.STATUS);
    1.92 +		Result.add(Events.RDATE);
    1.93 +		Result.add(Events.RRULE);
    1.94 +		Result.add(Events.EXRULE);
    1.95 +		Result.add(Events.EXDATE);
    1.96 +		Result.add(UID);
    1.97 +		
    1.98 +		return Result;
    1.99 +	}
   1.100 +	
   1.101 +	/**
   1.102 +	 * sets the AndroidCalendarId for this event
   1.103 +	 * @param ID the AndroidCalendarId for this event
   1.104 +	 */
   1.105 +	public void setAndroidCalendarId(long ID) {
   1.106 +		if (this.ContentValues.containsKey(Events.CALENDAR_ID))
   1.107 +			this.ContentValues.remove(Events.CALENDAR_ID);
   1.108 +		
   1.109 +		this.ContentValues.put(Events.CALENDAR_ID, ID);
   1.110 +	}
   1.111 +
   1.112 +	/**
   1.113 +	 * returns the AndroidCalendarId for this event.
   1.114 +	 * @return the AndroidCalendarId for this event
   1.115 +	 */
   1.116 +	public long getAndroidCalendarId() {
   1.117 +		long Result = -1;
   1.118 +		if (this.ContentValues.containsKey(Events.CALENDAR_ID))
   1.119 +			Result = this.ContentValues.getAsLong(Events.CALENDAR_ID);
   1.120 +		return Result;
   1.121 +	}
   1.122 +
   1.123 +	/**
   1.124 +	 * 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.
   1.125 +	 * example: UID:e6be67c6-eff0-44f8-a1a0-6c2cb1029944-caldavsyncadapter
   1.126 +	 * @return the UID for this event
   1.127 +	 */
   1.128 +	public String getUID() {
   1.129 +		String Result = "";
   1.130 +		if (this.ContentValues.containsKey(UID))
   1.131 +			Result = this.ContentValues.getAsString(UID);
   1.132 +		
   1.133 +		return Result;
   1.134 +	}
   1.135 +	
   1.136 +	/**
   1.137 +	 * compares the given ContentValues with the current ones for differences
   1.138 +	 * @param calendarEventValues the contentValues of the calendar event
   1.139 +	 * @return if the events are different
   1.140 +	 */
   1.141 +	public boolean checkEventValuesChanged(ContentValues calendarEventValues) {
   1.142 +		boolean Result = false;
   1.143 +		Object ValueAndroid = null;
   1.144 +		Object ValueCalendar = null;
   1.145 +		java.util.ArrayList<String> CompareItems = Event.getComparableItems();
   1.146 +		
   1.147 +		for (String Key: CompareItems) {
   1.148 +
   1.149 +			if (this.ContentValues.containsKey(Key)) 
   1.150 +				ValueAndroid = this.ContentValues.get(Key);
   1.151 +			else
   1.152 +				ValueAndroid = null;
   1.153 +
   1.154 +			if (calendarEventValues.containsKey(Key))
   1.155 +				ValueCalendar = calendarEventValues.get(Key);
   1.156 +			else
   1.157 +				ValueCalendar = null;
   1.158 +
   1.159 +			/*
   1.160 +			 * TODO: Sync is designed to "Server always wins", should be a general option for this adapter
   1.161 +			 */
   1.162 +			if (ValueAndroid != null) {
   1.163 +				if (ValueCalendar != null) {
   1.164 +					if (!ValueAndroid.toString().equals(ValueCalendar.toString())) {
   1.165 +						Log.d(TAG, "difference in " + Key.toString() + ":" + ValueAndroid.toString() + " <> " + ValueCalendar.toString());
   1.166 +						this.ContentValues.put(Key,ValueCalendar.toString());
   1.167 +						Result = true;
   1.168 +					}
   1.169 +				} else {
   1.170 +					Log.d(TAG, "difference in " + Key.toString() + ":" + ValueAndroid.toString() + " <> null");
   1.171 +					this.ContentValues.putNull(Key);
   1.172 +					Result = true;
   1.173 +				}
   1.174 +			} else {
   1.175 +				if (ValueCalendar != null) {
   1.176 +					Log.d(TAG, "difference in " + Key.toString() + ":null <> " + ValueCalendar.toString());
   1.177 +					this.ContentValues.put(Key, ValueCalendar.toString());
   1.178 +					Result = true;
   1.179 +				} else {
   1.180 +					// both null -> this is ok
   1.181 +				}
   1.182 +			}
   1.183 +		}
   1.184 +		
   1.185 +		return Result;
   1.186 +	}
   1.187 +}

mercurial