100 Events._SYNC_ID, |
102 Events._SYNC_ID, |
101 Events.SYNC_DATA1, |
103 Events.SYNC_DATA1, |
102 Events.CALENDAR_ID |
104 Events.CALENDAR_ID |
103 }; |
105 }; |
104 */ |
106 */ |
105 |
107 |
106 // ignore same CTag |
108 // ignore same CTag |
107 //private static final boolean FORCE_SYNCHRONIZE = false; |
109 //private static final boolean FORCE_SYNCHRONIZE = false; |
108 // drop all calendar before synchro |
110 // drop all calendar before synchro |
109 //private static final boolean DROP_CALENDAR_EVENTS = false; |
111 //private static final boolean DROP_CALENDAR_EVENTS = false; |
110 |
112 |
111 public SyncAdapter(Context context, boolean autoInitialize) { |
113 public SyncAdapter(Context context, boolean autoInitialize) { |
112 super(context, autoInitialize); |
114 super(context, autoInitialize); |
113 //android.os.Debug.waitForDebugger(); |
115 //android.os.Debug.waitForDebugger(); |
114 mAccountManager = AccountManager.get(context); |
116 mAccountManager = AccountManager.get(context); |
115 try { |
117 try { |
116 mVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; |
118 mVersion = context.getPackageManager() |
117 } catch (NameNotFoundException e) { |
119 .getPackageInfo(context.getPackageName(), 0).versionName; |
118 e.printStackTrace(); |
120 } catch (NameNotFoundException e) { |
119 } |
121 e.printStackTrace(); |
|
122 } |
120 // mContext = context; |
123 // mContext = context; |
121 } |
124 } |
122 |
125 |
123 @Override |
126 @Override |
124 public void onPerformSync(Account account, Bundle extras, String authority, |
127 public void onPerformSync(Account account, Bundle extras, String authority, |
125 ContentProviderClient provider, SyncResult syncResult) { |
128 ContentProviderClient provider, SyncResult syncResult) { |
126 boolean bolError = false; |
129 boolean bolError = false; |
127 |
130 |
128 String url = mAccountManager.getUserData(account, AuthenticatorActivity.USER_DATA_URL_KEY); |
131 String url = mAccountManager.getUserData(account, AuthenticatorActivity.USER_DATA_URL_KEY); |
129 this.mCountPerformSync += 1; |
132 String trust = mAccountManager.getUserData(account, Constants.USER_DATA_TRUST_ALL_KEY); |
130 Log.v(TAG, "onPerformSync() count:" + String.valueOf(this.mCountPerformSync) + " on " + account.name + " with URL " + url); |
133 this.mCountPerformSync += 1; |
131 |
134 Log.v(TAG, "onPerformSync() count:" + String.valueOf(this.mCountPerformSync) + " on " + account.name + " with URL " + url); |
132 CalendarList serverCalList; |
135 |
133 |
136 CalendarList serverCalList; |
134 CalendarList androidCalList = new CalendarList(account, provider, CalendarSource.Android, url); |
137 |
135 androidCalList.readCalendarFromClient(); |
138 CalendarList androidCalList = new CalendarList(account, provider, CalendarSource.Android, url); |
136 ArrayList<Uri> notifyList = new ArrayList<Uri>(); |
139 androidCalList.readCalendarFromClient(); |
137 |
140 ArrayList<Uri> notifyList = new ArrayList<Uri>(); |
138 try { |
141 |
139 String Username = ""; |
142 try { |
140 String UserDataVersion = mAccountManager.getUserData(account, AuthenticatorActivity.USER_DATA_VERSION); |
143 String Username = ""; |
141 if (UserDataVersion == null) { |
144 String UserDataVersion = mAccountManager.getUserData(account, AuthenticatorActivity.USER_DATA_VERSION); |
142 Username = account.name; |
145 if (UserDataVersion == null) { |
143 } else { |
146 Username = account.name; |
144 Username = mAccountManager.getUserData(account, AuthenticatorActivity.USER_DATA_USERNAME); |
147 } else { |
145 } |
148 Username = mAccountManager.getUserData(account, AuthenticatorActivity.USER_DATA_USERNAME); |
146 |
149 } |
147 CaldavFacade facade = new CaldavFacade(Username, mAccountManager.getPassword(account), url); |
150 |
148 facade.setAccount(account); |
151 CaldavFacade facade = new CaldavFacade(Username, mAccountManager.getPassword(account), url, trust); |
149 facade.setProvider(provider); |
152 facade.setAccount(account); |
150 facade.setVersion(mVersion); |
153 facade.setProvider(provider); |
151 serverCalList = facade.getCalendarList(this.getContext()); |
154 facade.setVersion(mVersion); |
152 //String davProperties = facade.getLastDav(); |
155 serverCalList = facade.getCalendarList(this.getContext()); |
153 Log.i(TAG, String.valueOf(androidCalList.getCalendarList().size()) + " calendars found at android"); |
156 //String davProperties = facade.getLastDav(); |
154 |
157 Log.i(TAG, String.valueOf(androidCalList.getCalendarList() |
155 for (DavCalendar serverCalendar : serverCalList.getCalendarList()) { |
158 .size()) + " calendars found at android"); |
156 Log.i(TAG, "Detected calendar name=" + serverCalendar.getCalendarDisplayName() + " URI=" + serverCalendar.getURI()); |
159 |
157 |
160 for (DavCalendar serverCalendar : serverCalList.getCalendarList()) { |
158 Uri androidCalendarUri = serverCalendar.checkAndroidCalendarList(androidCalList, this.getContext()); |
161 Log.i(TAG, "Detected calendar name=" + serverCalendar.getCalendarDisplayName() + " URI=" + serverCalendar |
159 |
162 .getURI()); |
160 // check if the adapter was able to get an existing calendar or create a new one |
163 |
161 if (androidCalendarUri != null) { |
164 Uri androidCalendarUri = serverCalendar.checkAndroidCalendarList(androidCalList, this |
162 // the provider seems to work correct, reset the counter |
165 .getContext()); |
163 mCountProviderFailed = 0; |
166 |
164 DavCalendar androidCalendar = androidCalList.getCalendarByAndroidUri(androidCalendarUri); |
167 // check if the adapter was able to get an existing calendar or create a new one |
165 |
168 if (androidCalendarUri != null) { |
166 //if ((FORCE_SYNCHRONIZE) || (androidCalendar.getcTag() == null) || (!androidCalendar.getcTag().equals(serverCalendar.getcTag()))) { |
169 // the provider seems to work correct, reset the counter |
167 if ((androidCalendar.getcTag() == null) || (!androidCalendar.getcTag().equals(serverCalendar.getcTag()))) { |
170 mCountProviderFailed = 0; |
168 Log.d(TAG, "CTag has changed, something to synchronise"); |
171 DavCalendar androidCalendar = androidCalList.getCalendarByAndroidUri(androidCalendarUri); |
169 if (serverCalendar.readCalendarEvents(facade)) { |
172 |
170 this.synchroniseEvents(androidCalendar, serverCalendar, syncResult.stats, notifyList); |
173 //if ((FORCE_SYNCHRONIZE) || (androidCalendar.getcTag() == null) || (!androidCalendar.getcTag().equals(serverCalendar.getcTag()))) { |
171 |
174 if ((androidCalendar.getcTag() == null) || (!androidCalendar.getcTag() |
172 Log.d(TAG, "Updating stored CTag"); |
175 .equals(serverCalendar.getcTag()))) { |
173 //serverCalendar.updateAndroidCalendar(androidCalendarUri, Calendar.CTAG, serverCalendar.getcTag()); |
176 Log.d(TAG, "CTag has changed, something to synchronise"); |
174 androidCalendar.setCTag(serverCalendar.getcTag(), true); |
177 if (serverCalendar.readCalendarEvents(facade)) { |
175 } else { |
178 this.synchroniseEvents(androidCalendar, serverCalendar, syncResult.stats, notifyList); |
176 Log.d(TAG, "unable to read events from server calendar"); |
179 |
177 } |
180 Log.d(TAG, "Updating stored CTag"); |
178 } else { |
181 //serverCalendar.updateAndroidCalendar(androidCalendarUri, Calendar.CTAG, serverCalendar.getcTag()); |
179 Log.d(TAG, "CTag has not changed, nothing to do"); |
182 androidCalendar.setCTag(serverCalendar.getcTag(), true); |
|
183 } else { |
|
184 Log.d(TAG, "unable to read events from server calendar"); |
|
185 } |
|
186 } else { |
|
187 Log.d(TAG, "CTag has not changed, nothing to do"); |
180 |
188 |
181 /* this is unnecessary. "SkippedEntries" are: |
189 /* this is unnecessary. "SkippedEntries" are: |
182 * Counter for tracking how many entries, either from the server or the local store, |
190 * Counter for tracking how many entries, either from the server or the local store, |
183 * were ignored during the sync operation. This could happen if the SyncAdapter detected |
191 * were ignored during the sync operation. This could happen if the SyncAdapter detected |
184 * some unparsable data but decided to skip it and move on rather than failing immediately. |
192 * some unparsable data but decided to skip it and move on rather than failing immediately. |
258 syncResult.stats.numAuthExceptions++; |
267 syncResult.stats.numAuthExceptions++; |
259 Log.e(TAG, "AuthenticationException", e);*/ |
268 Log.e(TAG, "AuthenticationException", e);*/ |
260 } catch (final ParseException e) { |
269 } catch (final ParseException e) { |
261 syncResult.stats.numParseExceptions++; |
270 syncResult.stats.numParseExceptions++; |
262 Log.e(TAG, "ParseException", e); |
271 Log.e(TAG, "ParseException", e); |
263 NotificationsHelper.signalSyncErrors(this.getContext(), "Caldav sync error (parsing)", e.getMessage()); |
272 NotificationsHelper.signalSyncErrors(this.getContext(), "Caldav sync error (parsing)", e |
|
273 .getMessage()); |
264 //NotificationsHelper.getCurrentSyncLog().addException(e); |
274 //NotificationsHelper.getCurrentSyncLog().addException(e); |
265 /*} catch (final JSONException e) { |
275 /*} catch (final JSONException e) { |
266 syncResult.stats.numParseExceptions++; |
276 syncResult.stats.numParseExceptions++; |
267 Log.e(TAG, "JSONException", e);*/ |
277 Log.e(TAG, "JSONException", e);*/ |
268 } catch (Exception e) { |
278 } catch (Exception e) { |
269 Log.e(TAG, "Updating calendar exception " + e.getClass().getName(), e); |
279 Log.e(TAG, "Updating calendar exception " + e.getClass().getName(), e); |
270 syncResult.stats.numParseExceptions++; |
280 syncResult.stats.numParseExceptions++; |
271 NotificationsHelper.signalSyncErrors(this.getContext(), "Caldav sync error (general)", e.getMessage()); |
281 NotificationsHelper.signalSyncErrors(this.getContext(), "Caldav sync error (general)", e |
|
282 .getMessage()); |
272 //NotificationsHelper.getCurrentSyncLog().addException(e); |
283 //NotificationsHelper.getCurrentSyncLog().addException(e); |
273 //throw new RuntimeException(e); |
284 //throw new RuntimeException(e); |
274 } |
285 } |
275 } |
286 } |
276 |
287 |
277 public void onSyncCanceled () { |
288 public void onSyncCanceled() { |
278 //TODO: implement SyncCanceled |
289 //TODO: implement SyncCanceled |
279 this.mCountSyncCanceled += 1; |
290 this.mCountSyncCanceled += 1; |
280 Log.v(TAG, "onSyncCanceled() count:" + String.valueOf(this.mCountSyncCanceled)); |
291 Log.v(TAG, "onSyncCanceled() count:" + String.valueOf(this.mCountSyncCanceled)); |
281 } |
292 } |
282 |
293 |
283 |
294 |
284 /** |
295 /** |
285 * both calender event and android event have been found. |
296 * both calender event and android event have been found. |
286 * server wins always at the moment. |
297 * server wins always at the moment. |
287 * @param androidCalendar |
298 * |
288 * @param serverCalendar |
299 * @param androidCalendar |
289 * @param stats |
300 * @param serverCalendar |
290 * @param notifyList |
301 * @param stats |
291 * @throws ClientProtocolException |
302 * @param notifyList |
292 * @throws URISyntaxException |
303 * @throws ClientProtocolException |
293 * @throws IOException |
304 * @throws URISyntaxException |
294 * @throws ParserConfigurationException |
305 * @throws IOException |
295 * @throws SAXException |
306 * @throws ParserConfigurationException |
296 * @throws RemoteException |
307 * @throws SAXException |
297 * @throws CaldavProtocolException |
308 * @throws RemoteException |
298 * @throws ParserException |
309 * @throws CaldavProtocolException |
299 * @see SyncAdapter#updateAndroidEvent(ContentProviderClient, Account, AndroidEvent, CalendarEvent) |
310 * @throws ParserException |
300 * @see SyncAdapter#tagAndroidEvent(ContentProviderClient, Account, AndroidEvent) |
311 * @see SyncAdapter#updateAndroidEvent(ContentProviderClient, Account, AndroidEvent, CalendarEvent) |
301 * @see SyncAdapter#untagAndroidEvents(ContentProviderClient, Account, Uri) |
312 * @see SyncAdapter#tagAndroidEvent(ContentProviderClient, Account, AndroidEvent) |
302 * @see SyncAdapter#deleteUntaggedEvents(ContentProviderClient, Account, Uri) |
313 * @see SyncAdapter#untagAndroidEvents(ContentProviderClient, Account, Uri) |
303 */ |
314 * @see SyncAdapter#deleteUntaggedEvents(ContentProviderClient, Account, Uri) |
304 private void synchroniseEvents( |
315 */ |
305 DavCalendar androidCalendar, |
316 private void synchroniseEvents( |
306 DavCalendar serverCalendar, |
317 DavCalendar androidCalendar, |
307 SyncStats stats, |
318 DavCalendar serverCalendar, |
308 ArrayList<Uri> notifyList |
319 SyncStats stats, |
309 ) throws ClientProtocolException, URISyntaxException, IOException, ParserConfigurationException, SAXException, RemoteException, CaldavProtocolException, ParserException { |
320 ArrayList<Uri> notifyList |
|
321 ) throws ClientProtocolException, URISyntaxException, IOException, ParserConfigurationException, SAXException, RemoteException, CaldavProtocolException, ParserException { |
310 |
322 |
311 /*if (DROP_CALENDAR_EVENTS) { |
323 /*if (DROP_CALENDAR_EVENTS) { |
312 dropAllEvents(account, provider, androidCalendar.getAndroidCalendarUri()); |
324 dropAllEvents(account, provider, androidCalendar.getAndroidCalendarUri()); |
313 }*/ |
325 }*/ |
314 |
326 |
315 int rowInsert = 0; |
327 int rowInsert = 0; |
316 int rowUpdate = 0; |
328 int rowUpdate = 0; |
317 int rowTag = 0; |
329 int rowTag = 0; |
318 int rowDelete = 0; |
330 int rowDelete = 0; |
319 int rowUntag = 0; |
331 int rowUntag = 0; |
320 int rowSkip = 0; |
332 int rowSkip = 0; |
321 |
333 |
322 for (CalendarEvent calendarEvent : serverCalendar.getCalendarEvents()) { |
334 for (CalendarEvent calendarEvent : serverCalendar.getCalendarEvents()) { |
323 try { |
335 try { |
324 AndroidEvent androidEvent = calendarEvent.getAndroidEvent(androidCalendar); |
336 AndroidEvent androidEvent = calendarEvent.getAndroidEvent(androidCalendar); |
325 |
337 |
326 Log.i(TAG, "Event " + calendarEvent.getUri().toString()+ " androidUri="+androidEvent); |
338 Log.i(TAG, "Event " + calendarEvent.getUri() |
327 |
339 .toString() + " androidUri=" + androidEvent); |
328 if (androidEvent == null) { |
340 |
|
341 if (androidEvent == null) { |
329 /* new android event */ |
342 /* new android event */ |
330 if (calendarEvent.createAndroidEvent(androidCalendar)) { |
343 if (calendarEvent.createAndroidEvent(androidCalendar)) { |
331 rowInsert += 1; |
344 rowInsert += 1; |
332 androidEvent = calendarEvent.getAndroidEvent(androidCalendar); |
345 androidEvent = calendarEvent.getAndroidEvent(androidCalendar); |
333 notifyList.add(androidEvent.getUri()); |
346 notifyList.add(androidEvent.getUri()); |
334 } else { |
347 } else { |
335 rowSkip += 1; |
348 rowSkip += 1; |
336 } |
349 } |
337 } else { |
350 } else { |
338 /* the android exists */ |
351 /* the android exists */ |
339 String androidETag = androidEvent.getETag(); |
352 String androidETag = androidEvent.getETag(); |
340 if (androidETag == null) |
353 if (androidETag == null) |
341 androidETag = ""; |
354 androidETag = ""; |
342 Log.d(TAG, "Event compare: " + androidETag + " <> " + calendarEvent.getETag().toString()); |
355 Log.d(TAG, "Event compare: " + androidETag + " <> " + calendarEvent.getETag() |
343 if ((androidEvent.getETag() == null) || (!androidETag.equals(calendarEvent.getETag()))) { |
356 .toString()); |
|
357 if ((androidEvent.getETag() == null) || (!androidETag.equals(calendarEvent.getETag()))) { |
344 /* the android event is getting updated */ |
358 /* the android event is getting updated */ |
345 if (calendarEvent.updateAndroidEvent(androidEvent)) { |
359 if (calendarEvent.updateAndroidEvent(androidEvent)) { |
346 rowUpdate += 1; |
360 rowUpdate += 1; |
347 notifyList.add(androidEvent.getUri()); |
361 notifyList.add(androidEvent.getUri()); |
348 } else { |
362 } else { |
349 rowSkip += 1; |
363 rowSkip += 1; |
350 } |
364 } |
351 } |
365 } |
352 } |
366 } |
353 if (androidEvent != null) |
367 if (androidEvent != null) |
354 //if (androidEvent.tagAndroidEvent()) |
368 //if (androidEvent.tagAndroidEvent()) |
355 if (androidCalendar.tagAndroidEvent(androidEvent)) |
369 if (androidCalendar.tagAndroidEvent(androidEvent)) |
356 rowTag += 1; |
370 rowTag += 1; |
357 |
371 |
358 |
372 |
359 } catch (ParserException ex) { |
373 } catch (ParserException ex) { |
360 Log.e(TAG, "Parser exception", ex); |
374 Log.e(TAG, "Parser exception", ex); |
361 stats.numParseExceptions++; |
375 stats.numParseExceptions++; |
362 |
376 |
363 NotificationsHelper.signalSyncErrors(getContext(), "Caldav sync error (parsing)", ex.getMessage()); |
377 NotificationsHelper.signalSyncErrors(getContext(), "Caldav sync error (parsing)", ex |
364 //NotificationsHelper.getCurrentSyncLog().addException(ex); |
378 .getMessage()); |
365 } catch (CaldavProtocolException ex) { |
379 //NotificationsHelper.getCurrentSyncLog().addException(ex); |
366 Log.e(TAG, "Caldav exception", ex); |
380 } catch (CaldavProtocolException ex) { |
367 stats.numParseExceptions++; |
381 Log.e(TAG, "Caldav exception", ex); |
368 |
382 stats.numParseExceptions++; |
369 NotificationsHelper.signalSyncErrors(getContext(), "Caldav sync error (caldav)", ex.getMessage()); |
383 |
370 //NotificationsHelper.getCurrentSyncLog().addException(ex); |
384 NotificationsHelper.signalSyncErrors(getContext(), "Caldav sync error (caldav)", ex.getMessage()); |
371 } |
385 //NotificationsHelper.getCurrentSyncLog().addException(ex); |
372 } |
386 } |
373 |
387 } |
374 rowDelete = androidCalendar.deleteUntaggedEvents(); |
388 |
375 rowUntag = androidCalendar.untagAndroidEvents(); |
389 rowDelete = androidCalendar.deleteUntaggedEvents(); |
|
390 rowUntag = androidCalendar.untagAndroidEvents(); |
376 |
391 |
377 /*Log.i(TAG,"Statistiks for Calendar: " + serverCalendar.getURI().toString()); |
392 /*Log.i(TAG,"Statistiks for Calendar: " + serverCalendar.getURI().toString()); |
378 Log.i(TAG,"Statistiks for AndroidCalendar: " + androidCalendar.getAndroidCalendarUri().toString()); |
393 Log.i(TAG,"Statistiks for AndroidCalendar: " + androidCalendar.getAndroidCalendarUri().toString()); |
379 Log.i(TAG,"Rows inserted: " + String.valueOf(rowInsert)); |
394 Log.i(TAG,"Rows inserted: " + String.valueOf(rowInsert)); |
380 Log.i(TAG,"Rows updated: " + String.valueOf(rowUpdate)); |
395 Log.i(TAG,"Rows updated: " + String.valueOf(rowUpdate)); |
381 Log.i(TAG,"Rows deleted: " + String.valueOf(rowDelete)); |
396 Log.i(TAG,"Rows deleted: " + String.valueOf(rowDelete)); |
382 Log.i(TAG,"Rows skipped: " + String.valueOf(rowSkip));*/ |
397 Log.i(TAG,"Rows skipped: " + String.valueOf(rowSkip));*/ |
383 Log.i(TAG,"Rows tagged: " + String.valueOf(rowTag)); |
398 Log.i(TAG, "Rows tagged: " + String.valueOf(rowTag)); |
384 Log.i(TAG,"Rows untagged: " + String.valueOf(rowUntag)); |
399 Log.i(TAG, "Rows untagged: " + String.valueOf(rowUntag)); |
385 |
400 |
386 stats.numInserts += rowInsert; |
401 stats.numInserts += rowInsert; |
387 stats.numUpdates += rowUpdate; |
402 stats.numUpdates += rowUpdate; |
388 stats.numDeletes += rowDelete; |
403 stats.numDeletes += rowDelete; |
389 stats.numSkippedEntries += rowSkip; |
404 stats.numSkippedEntries += rowSkip; |
390 stats.numEntries += rowInsert + rowUpdate + rowDelete; |
405 stats.numEntries += rowInsert + rowUpdate + rowDelete; |
391 |
406 |
392 } |
407 } |
393 |
408 |
394 /** |
409 /** |
395 * checks the android events for the dirty flag. |
410 * checks the android events for the dirty flag. |
396 * the flag is set by android when the event has been changed. |
411 * the flag is set by android when the event has been changed. |
397 * the dirty flag is removed when an android event has been updated from calendar event |
412 * the dirty flag is removed when an android event has been updated from calendar event |
398 * @param provider |
413 * |
399 * @param account |
414 * @param provider |
400 * @param calendarUri |
415 * @param account |
401 * @param facade |
416 * @param calendarUri |
402 * @param caldavCalendarUri |
417 * @param facade |
403 * @param stats |
418 * @param caldavCalendarUri |
404 * @param notifyList |
419 * @param stats |
405 * @return count of dirty events |
420 * @param notifyList |
406 */ |
421 * @return count of dirty events |
407 private int checkDirtyAndroidEvents( |
422 */ |
408 ContentProviderClient provider, |
423 private int checkDirtyAndroidEvents( |
409 Account account, |
424 ContentProviderClient provider, |
410 Uri calendarUri, |
425 Account account, |
411 CaldavFacade facade, |
426 Uri calendarUri, |
412 URI caldavCalendarUri, |
427 CaldavFacade facade, |
413 SyncStats stats, |
428 URI caldavCalendarUri, |
414 ArrayList<Uri> notifyList |
429 SyncStats stats, |
415 ) { |
430 ArrayList<Uri> notifyList |
416 Cursor curEvent = null; |
431 ) { |
417 Cursor curAttendee = null; |
432 Cursor curEvent = null; |
418 Cursor curReminder = null; |
433 Cursor curAttendee = null; |
419 Long EventID; |
434 Cursor curReminder = null; |
420 Long CalendarID; |
435 Long EventID; |
421 AndroidEvent androidEvent = null; |
436 Long CalendarID; |
422 int rowDirty = 0; |
437 AndroidEvent androidEvent = null; |
423 int rowInsert = 0; |
438 int rowDirty = 0; |
424 int rowUpdate = 0; |
439 int rowInsert = 0; |
425 int rowDelete = 0; |
440 int rowUpdate = 0; |
426 |
441 int rowDelete = 0; |
427 try { |
442 |
428 CalendarID = ContentUris.parseId(calendarUri); |
443 try { |
429 String selection = "(" + Events.DIRTY + " = ?) AND (" + Events.CALENDAR_ID + " = ?)"; |
444 CalendarID = ContentUris.parseId(calendarUri); |
430 String[] selectionArgs = new String[] {"1", CalendarID.toString()}; |
445 String selection = "(" + Events.DIRTY + " = ?) AND (" + Events.CALENDAR_ID + " = ?)"; |
431 curEvent = provider.query(Events.CONTENT_URI, null, selection, selectionArgs, null); |
446 String[] selectionArgs = new String[]{"1", CalendarID.toString()}; |
432 |
447 curEvent = provider.query(Events.CONTENT_URI, null, selection, selectionArgs, null); |
433 while (curEvent.moveToNext()) { |
448 |
434 EventID = curEvent.getLong(curEvent.getColumnIndex(Events._ID)); |
449 while (curEvent.moveToNext()) { |
435 Uri returnedUri = ContentUris.withAppendedId(Events.CONTENT_URI, EventID); |
450 EventID = curEvent.getLong(curEvent.getColumnIndex(Events._ID)); |
436 |
451 Uri returnedUri = ContentUris.withAppendedId(Events.CONTENT_URI, EventID); |
437 //androidEvent = new AndroidEvent(account, provider, returnedUri, calendarUri); |
452 |
438 androidEvent = new AndroidEvent(returnedUri, calendarUri); |
453 //androidEvent = new AndroidEvent(account, provider, returnedUri, calendarUri); |
439 androidEvent.readContentValues(curEvent); |
454 androidEvent = new AndroidEvent(returnedUri, calendarUri); |
440 |
455 androidEvent.readContentValues(curEvent); |
441 selection = "(" + Attendees.EVENT_ID + " = ?)"; |
456 |
442 selectionArgs = new String[] {String.valueOf(EventID)}; |
457 selection = "(" + Attendees.EVENT_ID + " = ?)"; |
443 curAttendee = provider.query(Attendees.CONTENT_URI, null, selection, selectionArgs, null); |
458 selectionArgs = new String[]{String.valueOf(EventID)}; |
444 selection = "(" + Reminders.EVENT_ID + " = ?)"; |
459 curAttendee = provider.query(Attendees.CONTENT_URI, null, selection, selectionArgs, null); |
445 selectionArgs = new String[] {String.valueOf(EventID)}; |
460 selection = "(" + Reminders.EVENT_ID + " = ?)"; |
446 curReminder = provider.query(Reminders.CONTENT_URI, null, selection, selectionArgs, null); |
461 selectionArgs = new String[]{String.valueOf(EventID)}; |
447 androidEvent.readAttendees(curAttendee); |
462 curReminder = provider.query(Reminders.CONTENT_URI, null, selection, selectionArgs, null); |
448 androidEvent.readReminder(curReminder); |
463 androidEvent.readAttendees(curAttendee); |
449 curAttendee.close(); |
464 androidEvent.readReminder(curReminder); |
450 curReminder.close(); |
465 curAttendee.close(); |
451 |
466 curReminder.close(); |
452 String SyncID = androidEvent.ContentValues.getAsString(Events._SYNC_ID); |
467 |
453 |
468 String SyncID = androidEvent.ContentValues.getAsString(Events._SYNC_ID); |
454 boolean Deleted = false; |
469 |
455 int intDeleted = 0; |
470 boolean Deleted = false; |
456 intDeleted = curEvent.getInt(curEvent.getColumnIndex(Events.DELETED)); |
471 int intDeleted = 0; |
457 Deleted = (intDeleted == 1); |
472 intDeleted = curEvent.getInt(curEvent.getColumnIndex(Events.DELETED)); |
458 |
473 Deleted = (intDeleted == 1); |
459 if (SyncID == null) { |
474 |
460 // new Android event |
475 if (SyncID == null) { |
461 String newGUID = java.util.UUID.randomUUID().toString() + "-caldavsyncadapter"; |
476 // new Android event |
462 String calendarPath = caldavCalendarUri.getPath(); |
477 String newGUID = java.util.UUID.randomUUID().toString() + "-caldavsyncadapter"; |
463 if (!calendarPath.endsWith("/")) |
478 String calendarPath = caldavCalendarUri.getPath(); |
464 calendarPath += "/"; |
479 if (!calendarPath.endsWith("/")) |
465 |
480 calendarPath += "/"; |
466 SyncID = calendarPath + newGUID + ".ics"; |
481 |
467 |
482 SyncID = calendarPath + newGUID + ".ics"; |
468 androidEvent.createIcs(newGUID); |
483 |
469 |
484 androidEvent.createIcs(newGUID); |
470 if (facade.createEvent(URI.create(SyncID), androidEvent.getIcsEvent().toString())) { |
485 |
471 //HINT: bugfix for google calendar replace("@", "%40") |
486 if (facade.createEvent(URI.create(SyncID), androidEvent.getIcsEvent() |
472 if (SyncID.contains("@")) |
487 .toString())) { |
473 SyncID = SyncID.replace("@", "%40"); |
488 //HINT: bugfix for google calendar replace("@", "%40") |
474 ContentValues values = new ContentValues(); |
489 if (SyncID.contains("@")) |
475 values.put(Events._SYNC_ID, SyncID); |
490 SyncID = SyncID.replace("@", "%40"); |
476 |
491 ContentValues values = new ContentValues(); |
477 //google doesn't send the etag after creation |
492 values.put(Events._SYNC_ID, SyncID); |
478 //HINT: my SabreDAV send always the same etag after putting a new event |
493 |
479 //String LastETag = facade.getLastETag(); |
494 //google doesn't send the etag after creation |
480 //if (!LastETag.equals("")) { |
495 //HINT: my SabreDAV send always the same etag after putting a new event |
481 // values.put(Event.ETAG, LastETag); |
496 //String LastETag = facade.getLastETag(); |
482 //} else { |
497 //if (!LastETag.equals("")) { |
483 //so get the etag with a new REPORT |
498 // values.put(Event.ETAG, LastETag); |
484 CalendarEvent calendarEvent = new CalendarEvent(account, provider); |
499 //} else { |
485 calendarEvent.calendarURL = caldavCalendarUri.toURL(); |
500 //so get the etag with a new REPORT |
486 URI SyncURI = new URI(SyncID); |
501 CalendarEvent calendarEvent = new CalendarEvent(account, provider); |
487 calendarEvent.setUri(SyncURI); |
502 calendarEvent.calendarURL = caldavCalendarUri.toURL(); |
488 CaldavFacade.getEvent(calendarEvent); |
503 URI SyncURI = new URI(SyncID); |
489 values.put(Event.ETAG, calendarEvent.getETag()); |
504 calendarEvent.setUri(SyncURI); |
490 //} |
505 CaldavFacade.getEvent(calendarEvent); |
491 values.put(Event.UID, newGUID); |
506 values.put(Event.ETAG, calendarEvent.getETag()); |
492 values.put(Events.DIRTY, 0); |
507 //} |
493 values.put(Event.RAWDATA, androidEvent.getIcsEvent().toString()); |
508 values.put(Event.UID, newGUID); |
494 |
509 values.put(Events.DIRTY, 0); |
495 int rowCount = provider.update(asSyncAdapter(androidEvent.getUri(), account.name, account.type), values, null, null); |
510 values.put(Event.RAWDATA, androidEvent.getIcsEvent().toString()); |
496 if (rowCount == 1) { |
511 |
497 rowInsert += 1; |
512 int rowCount = provider.update(asSyncAdapter(androidEvent.getUri(), account.name, account.type), values, null, null); |
498 notifyList.add(androidEvent.getUri()); |
513 if (rowCount == 1) { |
499 } |
514 rowInsert += 1; |
500 } |
515 notifyList.add(androidEvent.getUri()); |
501 } else if (Deleted) { |
516 } |
502 // deleted Android event |
517 } |
503 if (facade.deleteEvent(URI.create(SyncID), androidEvent.getETag())) { |
518 } else if (Deleted) { |
504 String mSelectionClause = "(" + Events._ID + "= ?)"; |
519 // deleted Android event |
505 String[] mSelectionArgs = {String.valueOf(EventID)}; |
520 if (facade.deleteEvent(URI.create(SyncID), androidEvent.getETag())) { |
506 |
521 String mSelectionClause = "(" + Events._ID + "= ?)"; |
507 int countDeleted = provider.delete(asSyncAdapter(Events.CONTENT_URI, account.name, account.type), mSelectionClause, mSelectionArgs); |
522 String[] mSelectionArgs = {String.valueOf(EventID)}; |
508 |
523 |
509 if (countDeleted == 1) { |
524 int countDeleted = provider.delete(asSyncAdapter(Events.CONTENT_URI, account.name, account.type), mSelectionClause, mSelectionArgs); |
510 rowDelete += 1; |
525 |
511 notifyList.add(androidEvent.getUri()); |
526 if (countDeleted == 1) { |
512 } |
527 rowDelete += 1; |
513 } |
528 notifyList.add(androidEvent.getUri()); |
514 } else { |
529 } |
515 //update the android event to the server |
530 } |
516 String uid = androidEvent.getUID(); |
531 } else { |
517 if ((uid == null) || (uid.equals(""))) { |
532 //update the android event to the server |
518 //COMPAT: this is needed because in the past, the UID was not stored in the android event |
533 String uid = androidEvent.getUID(); |
519 CalendarEvent calendarEvent = new CalendarEvent(account, provider); |
534 if ((uid == null) || (uid.equals(""))) { |
520 URI syncURI = new URI(SyncID); |
535 //COMPAT: this is needed because in the past, the UID was not stored in the android event |
521 calendarEvent.setUri(syncURI); |
536 CalendarEvent calendarEvent = new CalendarEvent(account, provider); |
522 calendarEvent.calendarURL = caldavCalendarUri.toURL(); |
537 URI syncURI = new URI(SyncID); |
523 if (calendarEvent.fetchBody()) { |
538 calendarEvent.setUri(syncURI); |
524 calendarEvent.readContentValues(); |
539 calendarEvent.calendarURL = caldavCalendarUri.toURL(); |
525 uid = calendarEvent.getUID(); |
540 if (calendarEvent.fetchBody()) { |
526 } |
541 calendarEvent.readContentValues(); |
527 } |
542 uid = calendarEvent.getUID(); |
528 if (uid != null) { |
543 } |
529 androidEvent.createIcs(uid); |
544 } |
530 |
545 if (uid != null) { |
531 if (facade.updateEvent(URI.create(SyncID), androidEvent.getIcsEvent().toString(), androidEvent.getETag())) { |
546 androidEvent.createIcs(uid); |
532 selection = "(" + Events._ID + "= ?)"; |
547 |
533 selectionArgs = new String[] {EventID.toString()}; |
548 if (facade.updateEvent(URI.create(SyncID), androidEvent.getIcsEvent() |
534 androidEvent.ContentValues.put(Events.DIRTY, 0); |
549 .toString(), androidEvent.getETag())) { |
535 |
550 selection = "(" + Events._ID + "= ?)"; |
536 //google doesn't send the etag after update |
551 selectionArgs = new String[]{EventID.toString()}; |
537 String LastETag = facade.getLastETag(); |
552 androidEvent.ContentValues.put(Events.DIRTY, 0); |
538 if (!LastETag.equals("")) { |
553 |
539 androidEvent.ContentValues.put(Event.ETAG, LastETag); |
554 //google doesn't send the etag after update |
540 } else { |
555 String LastETag = facade.getLastETag(); |
541 //so get the etag with a new REPORT |
556 if (!LastETag.equals("")) { |
542 CalendarEvent calendarEvent = new CalendarEvent(account, provider); |
557 androidEvent.ContentValues.put(Event.ETAG, LastETag); |
543 calendarEvent.calendarURL = caldavCalendarUri.toURL(); |
558 } else { |
544 URI SyncURI = new URI(SyncID); |
559 //so get the etag with a new REPORT |
545 calendarEvent.setUri(SyncURI); |
560 CalendarEvent calendarEvent = new CalendarEvent(account, provider); |
546 CaldavFacade.getEvent(calendarEvent); |
561 calendarEvent.calendarURL = caldavCalendarUri.toURL(); |
547 androidEvent.ContentValues.put(Event.ETAG, calendarEvent.getETag()); |
562 URI SyncURI = new URI(SyncID); |
548 } |
563 calendarEvent.setUri(SyncURI); |
549 androidEvent.ContentValues.put(Event.RAWDATA, androidEvent.getIcsEvent().toString()); |
564 CaldavFacade.getEvent(calendarEvent); |
550 int RowCount = provider.update(asSyncAdapter(androidEvent.getUri(), account.name, account.type), androidEvent.ContentValues, null, null); |
565 androidEvent.ContentValues.put(Event.ETAG, calendarEvent.getETag()); |
551 |
566 } |
552 if (RowCount == 1) { |
567 androidEvent.ContentValues.put(Event.RAWDATA, androidEvent.getIcsEvent() |
553 rowUpdate += 1; |
568 .toString()); |
554 notifyList.add(androidEvent.getUri()); |
569 int RowCount = provider.update(asSyncAdapter(androidEvent.getUri(), account.name, account.type), androidEvent.ContentValues, null, null); |
555 } |
570 |
556 } else { |
571 if (RowCount == 1) { |
557 rowDirty += 1; |
572 rowUpdate += 1; |
558 } |
573 notifyList.add(androidEvent.getUri()); |
559 } else { |
574 } |
560 rowDirty += 1; |
575 } else { |
561 } |
576 rowDirty += 1; |
562 } |
577 } |
563 } |
578 } else { |
564 curEvent.close(); |
579 rowDirty += 1; |
|
580 } |
|
581 } |
|
582 } |
|
583 curEvent.close(); |
565 |
584 |
566 /*if ((rowInsert > 0) || (rowUpdate > 0) || (rowDelete > 0) || (rowDirty > 0)) { |
585 /*if ((rowInsert > 0) || (rowUpdate > 0) || (rowDelete > 0) || (rowDirty > 0)) { |
567 Log.i(TAG,"Android Rows inserted: " + String.valueOf(rowInsert)); |
586 Log.i(TAG,"Android Rows inserted: " + String.valueOf(rowInsert)); |
568 Log.i(TAG,"Android Rows updated: " + String.valueOf(rowUpdate)); |
587 Log.i(TAG,"Android Rows updated: " + String.valueOf(rowUpdate)); |
569 Log.i(TAG,"Android Rows deleted: " + String.valueOf(rowDelete)); |
588 Log.i(TAG,"Android Rows deleted: " + String.valueOf(rowDelete)); |
570 Log.i(TAG,"Android Rows dirty: " + String.valueOf(rowDirty)); |
589 Log.i(TAG,"Android Rows dirty: " + String.valueOf(rowDirty)); |
571 }*/ |
590 }*/ |
572 |
591 |
573 stats.numInserts += rowInsert; |
592 stats.numInserts += rowInsert; |
574 stats.numUpdates += rowUpdate; |
593 stats.numUpdates += rowUpdate; |
575 stats.numDeletes += rowDelete; |
594 stats.numDeletes += rowDelete; |
576 stats.numSkippedEntries += rowDirty; |
595 stats.numSkippedEntries += rowDirty; |
577 stats.numEntries += rowInsert + rowUpdate + rowDelete; |
596 stats.numEntries += rowInsert + rowUpdate + rowDelete; |
578 } catch (RemoteException e) { |
597 } catch (RemoteException e) { |
579 e.printStackTrace(); |
598 e.printStackTrace(); |
580 } catch (URISyntaxException e) { |
599 } catch (URISyntaxException e) { |
581 // TODO Automatisch generierter Erfassungsblock |
600 // TODO Automatisch generierter Erfassungsblock |
582 e.printStackTrace(); |
601 e.printStackTrace(); |
583 } catch (ClientProtocolException e) { |
602 } catch (ClientProtocolException e) { |
584 // TODO Automatisch generierter Erfassungsblock |
603 // TODO Automatisch generierter Erfassungsblock |
585 e.printStackTrace(); |
604 e.printStackTrace(); |
586 } catch (IOException e) { |
605 } catch (IOException e) { |
587 // TODO Automatisch generierter Erfassungsblock |
606 // TODO Automatisch generierter Erfassungsblock |
588 e.printStackTrace(); |
607 e.printStackTrace(); |
589 } catch (CaldavProtocolException e) { |
608 } catch (CaldavProtocolException e) { |
590 // TODO Automatisch generierter Erfassungsblock |
609 // TODO Automatisch generierter Erfassungsblock |
591 e.printStackTrace(); |
610 e.printStackTrace(); |
592 } catch (ParserException e) { |
611 } catch (ParserException e) { |
593 // TODO Automatisch generierter Erfassungsblock |
612 // TODO Automatisch generierter Erfassungsblock |
594 e.printStackTrace(); |
613 e.printStackTrace(); |
595 } |
614 } |
596 |
615 |
597 return rowDirty; |
616 return rowDirty; |
598 } |
617 } |
599 |
618 |
600 /* private Account UpgradeAccount(Account OldAccount) { |
619 /* private Account UpgradeAccount(Account OldAccount) { |
601 String Username = OldAccount.name; |
620 String Username = OldAccount.name; |
602 String Type = OldAccount.type; |
621 String Type = OldAccount.type; |
603 String Password = this.mAccountManager.getPassword(OldAccount); |
622 String Password = this.mAccountManager.getPassword(OldAccount); |