|
1 /** |
|
2 * Copyright (c) 2012-2013, Gerald Garcia, Timo Berger |
|
3 * |
|
4 * This file is part of Andoid Caldav Sync Adapter Free. |
|
5 * |
|
6 * Andoid Caldav Sync Adapter Free is free software: you can redistribute |
|
7 * it and/or modify it under the terms of the GNU General Public License |
|
8 * as published by the Free Software Foundation, either version 3 of the |
|
9 * License, or at your option any later version. |
|
10 * |
|
11 * Andoid Caldav Sync Adapter Free is distributed in the hope that |
|
12 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 * GNU General Public License for more details. |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License |
|
17 * along with Andoid Caldav Sync Adapter Free. |
|
18 * If not, see <http://www.gnu.org/licenses/>. |
|
19 * |
|
20 */ |
|
21 package org.gege.caldavsyncadapter; |
|
22 |
|
23 import android.content.ContentValues; |
|
24 import android.provider.CalendarContract.Events; |
|
25 import android.util.Log; |
|
26 |
|
27 /** |
|
28 * abstract class for Calendar and Android events |
|
29 */ |
|
30 abstract public class Event { |
|
31 private static final String TAG = "Event"; |
|
32 |
|
33 /** |
|
34 * stores the ETAG of an event |
|
35 */ |
|
36 public static String ETAG = Events.SYNC_DATA1; |
|
37 |
|
38 /** |
|
39 * internal Tag used to identify deleted events |
|
40 */ |
|
41 public static String INTERNALTAG = Events.SYNC_DATA2; |
|
42 |
|
43 /** |
|
44 * store the whole VEVENT in here |
|
45 * missing TAGs they might be missing for google update |
|
46 * |
|
47 * CREATED:20130906T102857Z |
|
48 * DTSTAMP:20130906T102857Z |
|
49 * LAST-MODIFIED:20130906T102857Z |
|
50 * SEQUENCE:0 |
|
51 */ |
|
52 public static String RAWDATA = Events.SYNC_DATA3; |
|
53 |
|
54 /** |
|
55 * stores the UID of an Event |
|
56 * example: UID:e6be67c6-eff0-44f8-a1a0-6c2cb1029944-caldavsyncadapter |
|
57 */ |
|
58 public static String UID = Events.SYNC_DATA4; |
|
59 |
|
60 /** |
|
61 * the event transformed into ContentValues |
|
62 */ |
|
63 public ContentValues ContentValues = new ContentValues(); |
|
64 |
|
65 abstract public String getETag(); |
|
66 abstract public void setETag(String ETag); |
|
67 |
|
68 /** |
|
69 * returns a list of all items that are comparable with this sync adapter |
|
70 * @return a list of all items that are comparable with this sync adapter |
|
71 */ |
|
72 public static java.util.ArrayList<String> getComparableItems() { |
|
73 java.util.ArrayList<String> Result = new java.util.ArrayList<String>(); |
|
74 Result.add(Events.DTSTART); |
|
75 Result.add(Events.DTEND); |
|
76 Result.add(Events.EVENT_TIMEZONE); |
|
77 Result.add(Events.EVENT_END_TIMEZONE); |
|
78 Result.add(Events.ALL_DAY); |
|
79 Result.add(Events.DURATION); |
|
80 Result.add(Events.TITLE); |
|
81 Result.add(Events.CALENDAR_ID); |
|
82 Result.add(Events._SYNC_ID); |
|
83 //Result.add(Events.SYNC_DATA1); |
|
84 Result.add(ETAG); |
|
85 Result.add(Events.DESCRIPTION); |
|
86 Result.add(Events.EVENT_LOCATION); |
|
87 Result.add(Events.ACCESS_LEVEL); |
|
88 Result.add(Events.STATUS); |
|
89 Result.add(Events.RDATE); |
|
90 Result.add(Events.RRULE); |
|
91 Result.add(Events.EXRULE); |
|
92 Result.add(Events.EXDATE); |
|
93 Result.add(UID); |
|
94 |
|
95 return Result; |
|
96 } |
|
97 |
|
98 /** |
|
99 * sets the AndroidCalendarId for this event |
|
100 * @param ID the AndroidCalendarId for this event |
|
101 */ |
|
102 public void setAndroidCalendarId(long ID) { |
|
103 if (this.ContentValues.containsKey(Events.CALENDAR_ID)) |
|
104 this.ContentValues.remove(Events.CALENDAR_ID); |
|
105 |
|
106 this.ContentValues.put(Events.CALENDAR_ID, ID); |
|
107 } |
|
108 |
|
109 /** |
|
110 * returns the AndroidCalendarId for this event. |
|
111 * @return the AndroidCalendarId for this event |
|
112 */ |
|
113 public long getAndroidCalendarId() { |
|
114 long Result = -1; |
|
115 if (this.ContentValues.containsKey(Events.CALENDAR_ID)) |
|
116 Result = this.ContentValues.getAsLong(Events.CALENDAR_ID); |
|
117 return Result; |
|
118 } |
|
119 |
|
120 /** |
|
121 * 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. |
|
122 * example: UID:e6be67c6-eff0-44f8-a1a0-6c2cb1029944-caldavsyncadapter |
|
123 * @return the UID for this event |
|
124 */ |
|
125 public String getUID() { |
|
126 String Result = ""; |
|
127 if (this.ContentValues.containsKey(UID)) |
|
128 Result = this.ContentValues.getAsString(UID); |
|
129 |
|
130 return Result; |
|
131 } |
|
132 |
|
133 /** |
|
134 * compares the given ContentValues with the current ones for differences |
|
135 * @param calendarEventValues the contentValues of the calendar event |
|
136 * @return if the events are different |
|
137 */ |
|
138 public boolean checkEventValuesChanged(ContentValues calendarEventValues) { |
|
139 boolean Result = false; |
|
140 Object ValueAndroid = null; |
|
141 Object ValueCalendar = null; |
|
142 java.util.ArrayList<String> CompareItems = Event.getComparableItems(); |
|
143 |
|
144 for (String Key: CompareItems) { |
|
145 |
|
146 if (this.ContentValues.containsKey(Key)) |
|
147 ValueAndroid = this.ContentValues.get(Key); |
|
148 else |
|
149 ValueAndroid = null; |
|
150 |
|
151 if (calendarEventValues.containsKey(Key)) |
|
152 ValueCalendar = calendarEventValues.get(Key); |
|
153 else |
|
154 ValueCalendar = null; |
|
155 |
|
156 /* |
|
157 * TODO: Sync is designed to "Server always wins", should be a general option for this adapter |
|
158 */ |
|
159 if (ValueAndroid != null) { |
|
160 if (ValueCalendar != null) { |
|
161 if (!ValueAndroid.toString().equals(ValueCalendar.toString())) { |
|
162 Log.d(TAG, "difference in " + Key.toString() + ":" + ValueAndroid.toString() + " <> " + ValueCalendar.toString()); |
|
163 this.ContentValues.put(Key,ValueCalendar.toString()); |
|
164 Result = true; |
|
165 } |
|
166 } else { |
|
167 Log.d(TAG, "difference in " + Key.toString() + ":" + ValueAndroid.toString() + " <> null"); |
|
168 this.ContentValues.putNull(Key); |
|
169 Result = true; |
|
170 } |
|
171 } else { |
|
172 if (ValueCalendar != null) { |
|
173 Log.d(TAG, "difference in " + Key.toString() + ":null <> " + ValueCalendar.toString()); |
|
174 this.ContentValues.put(Key, ValueCalendar.toString()); |
|
175 Result = true; |
|
176 } else { |
|
177 // both null -> this is ok |
|
178 } |
|
179 } |
|
180 } |
|
181 |
|
182 return Result; |
|
183 } |
|
184 } |