Tue, 10 Feb 2015 18:12:00 +0100
Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.
michael@0 | 1 | /** |
michael@0 | 2 | * Copyright (c) 2012-2013, Gerald Garcia, Timo Berger |
michael@0 | 3 | * |
michael@0 | 4 | * This file is part of Andoid Caldav Sync Adapter Free. |
michael@0 | 5 | * |
michael@0 | 6 | * Andoid Caldav Sync Adapter Free is free software: you can redistribute |
michael@0 | 7 | * it and/or modify it under the terms of the GNU General Public License |
michael@0 | 8 | * as published by the Free Software Foundation, either version 3 of the |
michael@0 | 9 | * License, or at your option any later version. |
michael@0 | 10 | * |
michael@0 | 11 | * Andoid Caldav Sync Adapter Free is distributed in the hope that |
michael@0 | 12 | * it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
michael@0 | 13 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
michael@0 | 14 | * GNU General Public License for more details. |
michael@0 | 15 | * |
michael@0 | 16 | * You should have received a copy of the GNU General Public License |
michael@0 | 17 | * along with Andoid Caldav Sync Adapter Free. |
michael@0 | 18 | * If not, see <http://www.gnu.org/licenses/>. |
michael@0 | 19 | * |
michael@0 | 20 | */ |
michael@0 | 21 | |
michael@0 | 22 | package org.gege.caldavsyncadapter.caldav.entities; |
michael@0 | 23 | |
michael@0 | 24 | import java.io.IOException; |
michael@0 | 25 | import java.net.URI; |
michael@0 | 26 | import java.net.URISyntaxException; |
michael@0 | 27 | import java.util.ArrayList; |
michael@0 | 28 | |
michael@0 | 29 | import javax.xml.parsers.ParserConfigurationException; |
michael@0 | 30 | |
michael@0 | 31 | import org.apache.http.client.ClientProtocolException; |
michael@0 | 32 | import org.gege.caldavsyncadapter.CalendarColors; |
michael@0 | 33 | import org.gege.caldavsyncadapter.Event; |
michael@0 | 34 | import org.gege.caldavsyncadapter.android.entities.AndroidEvent; |
michael@0 | 35 | import org.gege.caldavsyncadapter.caldav.CaldavFacade; |
michael@0 | 36 | import org.gege.caldavsyncadapter.syncadapter.SyncAdapter; |
michael@0 | 37 | import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper; |
michael@0 | 38 | import org.xml.sax.SAXException; |
michael@0 | 39 | |
michael@0 | 40 | import android.accounts.Account; |
michael@0 | 41 | import android.content.ContentProviderClient; |
michael@0 | 42 | import android.content.ContentUris; |
michael@0 | 43 | import android.content.ContentValues; |
michael@0 | 44 | import android.content.SyncStats; |
michael@0 | 45 | import android.database.Cursor; |
michael@0 | 46 | import android.net.Uri; |
michael@0 | 47 | import android.os.RemoteException; |
michael@0 | 48 | import android.provider.CalendarContract.Calendars; |
michael@0 | 49 | import android.provider.CalendarContract.Events; |
michael@0 | 50 | import android.util.Log; |
michael@0 | 51 | |
michael@0 | 52 | public class DavCalendar { |
michael@0 | 53 | public enum CalendarSource { |
michael@0 | 54 | undefined, Android, CalDAV |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | private static final String TAG = "Calendar"; |
michael@0 | 58 | |
michael@0 | 59 | /** |
michael@0 | 60 | * stores the CTAG of a calendar |
michael@0 | 61 | */ |
michael@0 | 62 | public static String CTAG = Calendars.CAL_SYNC1; |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * stores the URI of a calendar |
michael@0 | 66 | * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname |
michael@0 | 67 | */ |
michael@0 | 68 | public static String URI = Calendars._SYNC_ID; |
michael@0 | 69 | |
michael@0 | 70 | public static String SERVERURL = Calendars.CAL_SYNC2; |
michael@0 | 71 | |
michael@0 | 72 | private String strCalendarColor = ""; |
michael@0 | 73 | |
michael@0 | 74 | private ArrayList<Uri> mNotifyList = new ArrayList<Uri>(); |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * the event transformed into ContentValues |
michael@0 | 78 | */ |
michael@0 | 79 | public ContentValues ContentValues = new ContentValues(); |
michael@0 | 80 | |
michael@0 | 81 | private Account mAccount = null; |
michael@0 | 82 | private ContentProviderClient mProvider = null; |
michael@0 | 83 | |
michael@0 | 84 | public boolean foundServerSide = false; |
michael@0 | 85 | public boolean foundClientSide = false; |
michael@0 | 86 | public CalendarSource Source = CalendarSource.undefined; |
michael@0 | 87 | |
michael@0 | 88 | public String ServerUrl = ""; |
michael@0 | 89 | |
michael@0 | 90 | private ArrayList<CalendarEvent> mCalendarEvents = new ArrayList<CalendarEvent>(); |
michael@0 | 91 | |
michael@0 | 92 | private int mTagCounter = 1; |
michael@0 | 93 | |
michael@0 | 94 | /** |
michael@0 | 95 | * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname |
michael@0 | 96 | */ |
michael@0 | 97 | public URI getURI() { |
michael@0 | 98 | String strUri = this.getContentValueAsString(DavCalendar.URI); |
michael@0 | 99 | URI result = null; |
michael@0 | 100 | try { |
michael@0 | 101 | result = new URI(strUri); |
michael@0 | 102 | } catch (URISyntaxException e) { |
michael@0 | 103 | e.printStackTrace(); |
michael@0 | 104 | } |
michael@0 | 105 | return result; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname |
michael@0 | 110 | */ |
michael@0 | 111 | public void setURI(URI uri) { |
michael@0 | 112 | this.setContentValueAsString(DavCalendar.URI, uri.toString()); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | /** |
michael@0 | 116 | * example: Cleartext Display Name |
michael@0 | 117 | */ |
michael@0 | 118 | public String getCalendarDisplayName() { |
michael@0 | 119 | return this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | /** |
michael@0 | 123 | * example: Cleartext Display Name |
michael@0 | 124 | */ |
michael@0 | 125 | public void setCalendarDisplayName(String displayName) { |
michael@0 | 126 | this.setContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME, displayName); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | |
michael@0 | 130 | /** |
michael@0 | 131 | * example: 1143 |
michael@0 | 132 | */ |
michael@0 | 133 | public void setCTag(String cTag, boolean Update) { |
michael@0 | 134 | this.setContentValueAsString(DavCalendar.CTAG, cTag); |
michael@0 | 135 | if (Update) { |
michael@0 | 136 | //serverCalendar.updateAndroidCalendar(androidCalendarUri, Calendar.CTAG, serverCalendar.getcTag()); |
michael@0 | 137 | try { |
michael@0 | 138 | this.updateAndroidCalendar(this.getAndroidCalendarUri(), CTAG, cTag); |
michael@0 | 139 | } catch (RemoteException e) { |
michael@0 | 140 | e.printStackTrace(); |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * example: 1143 |
michael@0 | 147 | */ |
michael@0 | 148 | public String getcTag() { |
michael@0 | 149 | return this.getContentValueAsString(DavCalendar.CTAG); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | /** |
michael@0 | 153 | * example: #FFCCAA |
michael@0 | 154 | */ |
michael@0 | 155 | public void setCalendarColorAsString(String color) { |
michael@0 | 156 | int maxlen = 6; |
michael@0 | 157 | |
michael@0 | 158 | this.strCalendarColor = color; |
michael@0 | 159 | if (!color.equals("")) { |
michael@0 | 160 | String strColor = color.replace("#", ""); |
michael@0 | 161 | if (strColor.length() > maxlen) |
michael@0 | 162 | strColor = strColor.substring(0, maxlen); |
michael@0 | 163 | int intColor = Integer.parseInt(strColor, 16); |
michael@0 | 164 | this.setContentValueAsInt(Calendars.CALENDAR_COLOR, intColor); |
michael@0 | 165 | } |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | /** |
michael@0 | 169 | * example: #FFCCAA |
michael@0 | 170 | */ |
michael@0 | 171 | public String getCalendarColorAsString() { |
michael@0 | 172 | return this.strCalendarColor; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | /** |
michael@0 | 176 | * example 12345 |
michael@0 | 177 | */ |
michael@0 | 178 | public int getCalendarColor() { |
michael@0 | 179 | return this.getContentValueAsInt(Calendars.CALENDAR_COLOR); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | /** |
michael@0 | 183 | * example 12345 |
michael@0 | 184 | */ |
michael@0 | 185 | public void setCalendarColor(int color) { |
michael@0 | 186 | this.setContentValueAsInt(Calendars.CALENDAR_COLOR, color); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | /** |
michael@0 | 190 | * example: |
michael@0 | 191 | * should be: calendarname |
michael@0 | 192 | * but is: http://caldav.example.com/calendarserver.php/calendars/username/calendarname/ |
michael@0 | 193 | */ |
michael@0 | 194 | public String getCalendarName() { |
michael@0 | 195 | return this.getContentValueAsString(Calendars.NAME); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | /** |
michael@0 | 199 | * example: |
michael@0 | 200 | * should be: calendarname |
michael@0 | 201 | * but is: http://caldav.example.com/calendarserver.php/calendars/username/calendarname/ |
michael@0 | 202 | */ |
michael@0 | 203 | public void setCalendarName(String calendarName) { |
michael@0 | 204 | this.setContentValueAsString(Calendars.NAME, calendarName); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | /** |
michael@0 | 208 | * example: 8 |
michael@0 | 209 | */ |
michael@0 | 210 | public int getAndroidCalendarId() { |
michael@0 | 211 | return this.getContentValueAsInt(Calendars._ID); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | /** |
michael@0 | 215 | * example: 8 |
michael@0 | 216 | */ |
michael@0 | 217 | public void setAndroidCalendarId(int androidCalendarId) { |
michael@0 | 218 | this.setContentValueAsInt(Calendars._ID, androidCalendarId); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | /** |
michael@0 | 222 | * example: content://com.android.calendar/calendars/8 |
michael@0 | 223 | */ |
michael@0 | 224 | public Uri getAndroidCalendarUri() { |
michael@0 | 225 | return ContentUris.withAppendedId(Calendars.CONTENT_URI, this.getAndroidCalendarId()); |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | /** |
michael@0 | 229 | * empty constructor |
michael@0 | 230 | */ |
michael@0 | 231 | public DavCalendar(CalendarSource source) { |
michael@0 | 232 | this.Source = source; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | /** |
michael@0 | 236 | * creates an new instance from a cursor |
michael@0 | 237 | * @param cur must be a cursor from "ContentProviderClient" with Uri Calendars.CONTENT_URI |
michael@0 | 238 | */ |
michael@0 | 239 | public DavCalendar(Account account, ContentProviderClient provider, Cursor cur, CalendarSource source, String serverUrl) { |
michael@0 | 240 | this.mAccount = account; |
michael@0 | 241 | this.mProvider = provider; |
michael@0 | 242 | this.foundClientSide = true; |
michael@0 | 243 | this.Source = source; |
michael@0 | 244 | this.ServerUrl = serverUrl; |
michael@0 | 245 | |
michael@0 | 246 | String strSyncID = cur.getString(cur.getColumnIndex(Calendars._SYNC_ID)); |
michael@0 | 247 | String strName = cur.getString(cur.getColumnIndex(Calendars.NAME)); |
michael@0 | 248 | String strDisplayName = cur.getString(cur.getColumnIndex(Calendars.CALENDAR_DISPLAY_NAME)); |
michael@0 | 249 | String strCTAG = cur.getString(cur.getColumnIndex(DavCalendar.CTAG)); |
michael@0 | 250 | String strServerUrl = cur.getString(cur.getColumnIndex(DavCalendar.SERVERURL)); |
michael@0 | 251 | int intAndroidCalendarId = cur.getInt(cur.getColumnIndex(Calendars._ID)); |
michael@0 | 252 | |
michael@0 | 253 | this.setCalendarName(strName); |
michael@0 | 254 | this.setCalendarDisplayName(strDisplayName); |
michael@0 | 255 | this.setCTag(strCTAG, false); |
michael@0 | 256 | this.setAndroidCalendarId(intAndroidCalendarId); |
michael@0 | 257 | |
michael@0 | 258 | if (strSyncID == null) { |
michael@0 | 259 | this.correctSyncID(strName); |
michael@0 | 260 | strSyncID = strName; |
michael@0 | 261 | } |
michael@0 | 262 | if (strServerUrl == null) { |
michael@0 | 263 | this.correctServerUrl(serverUrl); |
michael@0 | 264 | } |
michael@0 | 265 | URI uri = null; |
michael@0 | 266 | try { |
michael@0 | 267 | uri = new URI(strSyncID); |
michael@0 | 268 | } catch (URISyntaxException e) { |
michael@0 | 269 | e.printStackTrace(); |
michael@0 | 270 | } |
michael@0 | 271 | this.setURI(uri); |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | /** |
michael@0 | 275 | * checks a given list of android calendars for a specific android calendar. |
michael@0 | 276 | * this calendar should be a server calendar as it is searched for. |
michael@0 | 277 | * if the calendar is not found, it will be created. |
michael@0 | 278 | * @param androidCalList the list of android calendars |
michael@0 | 279 | * @param context |
michael@0 | 280 | * @return the found android calendar or null of fails |
michael@0 | 281 | * @throws RemoteException |
michael@0 | 282 | */ |
michael@0 | 283 | public Uri checkAndroidCalendarList(CalendarList androidCalList, android.content.Context context) throws RemoteException { |
michael@0 | 284 | Uri androidCalendarUri = null; |
michael@0 | 285 | boolean isCalendarExist = false; |
michael@0 | 286 | |
michael@0 | 287 | DavCalendar androidCalendar = androidCalList.getCalendarByURI(this.getURI()); |
michael@0 | 288 | if (androidCalendar != null) { |
michael@0 | 289 | isCalendarExist = true; |
michael@0 | 290 | androidCalendar.foundServerSide = true; |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | |
michael@0 | 294 | if (!isCalendarExist) { |
michael@0 | 295 | DavCalendar newCal = this.createNewAndroidCalendar(this, androidCalList.getCalendarList().size(), context); |
michael@0 | 296 | if (newCal != null) { |
michael@0 | 297 | androidCalList.addCalendar(newCal); |
michael@0 | 298 | androidCalendarUri = newCal.getAndroidCalendarUri(); |
michael@0 | 299 | } |
michael@0 | 300 | } else { |
michael@0 | 301 | androidCalendarUri = androidCalendar.getAndroidCalendarUri(); |
michael@0 | 302 | if (!this.getCalendarColorAsString().equals("")) { |
michael@0 | 303 | //serverCalendar.updateCalendarColor(returnedCalendarUri, serverCalendar); |
michael@0 | 304 | this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_COLOR, this.getCalendarColor()); |
michael@0 | 305 | } |
michael@0 | 306 | if ((this.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME)) && |
michael@0 | 307 | (androidCalendar.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME))) { |
michael@0 | 308 | String serverDisplayName = this.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME); |
michael@0 | 309 | String clientDisplayName = androidCalendar.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME); |
michael@0 | 310 | if (!serverDisplayName.equals(clientDisplayName)) |
michael@0 | 311 | this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_DISPLAY_NAME, serverDisplayName); |
michael@0 | 312 | } |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | return androidCalendarUri; |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | /** |
michael@0 | 319 | * COMPAT: the calendar Uri was stored as calendar Name. this function updates the URI (_SYNC_ID) |
michael@0 | 320 | * @param calendarUri the real calendarUri |
michael@0 | 321 | * @return success of this function |
michael@0 | 322 | */ |
michael@0 | 323 | private boolean correctSyncID(String calendarUri) { |
michael@0 | 324 | boolean Result = false; |
michael@0 | 325 | Log.v(TAG, "correcting SyncID for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME)); |
michael@0 | 326 | |
michael@0 | 327 | ContentValues mUpdateValues = new ContentValues(); |
michael@0 | 328 | mUpdateValues.put(DavCalendar.URI, calendarUri); |
michael@0 | 329 | |
michael@0 | 330 | try { |
michael@0 | 331 | mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null); |
michael@0 | 332 | Result = true; |
michael@0 | 333 | } catch (RemoteException e) { |
michael@0 | 334 | e.printStackTrace(); |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | return Result; |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | /** |
michael@0 | 341 | * COMPAT: the serverurl (CAL_SYNC2) was not sored within a calendar. this fixes it. (see #98) |
michael@0 | 342 | * @param serverUrl the current serverurl |
michael@0 | 343 | * @return success of this function |
michael@0 | 344 | */ |
michael@0 | 345 | private boolean correctServerUrl(String serverUrl) { |
michael@0 | 346 | boolean Result = false; |
michael@0 | 347 | Log.v(TAG, "correcting ServerUrl for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME)); |
michael@0 | 348 | |
michael@0 | 349 | ContentValues mUpdateValues = new ContentValues(); |
michael@0 | 350 | mUpdateValues.put(DavCalendar.SERVERURL, serverUrl); |
michael@0 | 351 | |
michael@0 | 352 | try { |
michael@0 | 353 | mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null); |
michael@0 | 354 | Result = true; |
michael@0 | 355 | } catch (RemoteException e) { |
michael@0 | 356 | e.printStackTrace(); |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | return Result; |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | /** |
michael@0 | 363 | * creates a new androidCalendar |
michael@0 | 364 | * @param serverCalendar |
michael@0 | 365 | * @param index |
michael@0 | 366 | * @param context |
michael@0 | 367 | * @return the new androidCalendar or null if fails |
michael@0 | 368 | */ |
michael@0 | 369 | private DavCalendar createNewAndroidCalendar(DavCalendar serverCalendar, int index, android.content.Context context) { |
michael@0 | 370 | Uri newUri = null; |
michael@0 | 371 | DavCalendar Result = null; |
michael@0 | 372 | |
michael@0 | 373 | final ContentValues contentValues = new ContentValues(); |
michael@0 | 374 | contentValues.put(DavCalendar.URI, serverCalendar.getURI().toString()); |
michael@0 | 375 | contentValues.put(DavCalendar.SERVERURL, this.ServerUrl); |
michael@0 | 376 | |
michael@0 | 377 | contentValues.put(Calendars.VISIBLE, 1); |
michael@0 | 378 | contentValues.put(Calendars.CALENDAR_DISPLAY_NAME, serverCalendar.getCalendarDisplayName()); |
michael@0 | 379 | contentValues.put(Calendars.ACCOUNT_NAME, mAccount.name); |
michael@0 | 380 | contentValues.put(Calendars.ACCOUNT_TYPE, mAccount.type); |
michael@0 | 381 | contentValues.put(Calendars.OWNER_ACCOUNT, mAccount.name); |
michael@0 | 382 | contentValues.put(Calendars.SYNC_EVENTS, 1); |
michael@0 | 383 | contentValues.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER); |
michael@0 | 384 | |
michael@0 | 385 | if (!serverCalendar.getCalendarColorAsString().equals("")) { |
michael@0 | 386 | contentValues.put(Calendars.CALENDAR_COLOR, serverCalendar.getCalendarColor()); |
michael@0 | 387 | } else { |
michael@0 | 388 | // find a color |
michael@0 | 389 | //int index = mList.size(); |
michael@0 | 390 | index = index % CalendarColors.colors.length; |
michael@0 | 391 | contentValues.put(Calendars.CALENDAR_COLOR, CalendarColors.colors[index]); |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | try { |
michael@0 | 395 | newUri = mProvider.insert(asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type), contentValues); |
michael@0 | 396 | } catch (RemoteException e) { |
michael@0 | 397 | e.printStackTrace(); |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | // it is possible that this calendar already exists but the provider failed to find it within isCalendarExist() |
michael@0 | 401 | // the adapter would try to create a new calendar but the provider fails again to create a new calendar. |
michael@0 | 402 | if (newUri != null) { |
michael@0 | 403 | long newCalendarId = ContentUris.parseId(newUri); |
michael@0 | 404 | |
michael@0 | 405 | Cursor cur = null; |
michael@0 | 406 | Uri uri = Calendars.CONTENT_URI; |
michael@0 | 407 | String selection = "(" + Calendars._ID + " = ?)"; |
michael@0 | 408 | String[] selectionArgs = new String[] {String.valueOf(newCalendarId)}; |
michael@0 | 409 | |
michael@0 | 410 | // Submit the query and get a Cursor object back. |
michael@0 | 411 | try { |
michael@0 | 412 | cur = mProvider.query(uri, null, selection, selectionArgs, null); |
michael@0 | 413 | } catch (RemoteException e) { |
michael@0 | 414 | e.printStackTrace(); |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | if (cur != null) { |
michael@0 | 418 | while (cur.moveToNext()) { |
michael@0 | 419 | Result = new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl); |
michael@0 | 420 | Result.foundServerSide = true; |
michael@0 | 421 | } |
michael@0 | 422 | cur.close(); |
michael@0 | 423 | //if (Result != null) |
michael@0 | 424 | // this.mList.add(Result); |
michael@0 | 425 | } |
michael@0 | 426 | Log.i(TAG, "New calendar created : URI=" + Result.getAndroidCalendarUri()); |
michael@0 | 427 | NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "new calendar found: " + Result.getCalendarDisplayName()); |
michael@0 | 428 | mNotifyList.add(Result.getAndroidCalendarUri()); |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | return Result; |
michael@0 | 432 | } |
michael@0 | 433 | |
michael@0 | 434 | /** |
michael@0 | 435 | * there is no corresponding calendar on server side. time to delete this calendar on android side. |
michael@0 | 436 | * @return |
michael@0 | 437 | */ |
michael@0 | 438 | public boolean deleteAndroidCalendar() { |
michael@0 | 439 | boolean Result = false; |
michael@0 | 440 | |
michael@0 | 441 | String mSelectionClause = "(" + Calendars._ID + " = ?)"; |
michael@0 | 442 | int calendarId = this.getAndroidCalendarId(); |
michael@0 | 443 | String[] mSelectionArgs = {Long.toString(calendarId)}; |
michael@0 | 444 | |
michael@0 | 445 | int CountDeleted = 0; |
michael@0 | 446 | try { |
michael@0 | 447 | CountDeleted = mProvider.delete(this.SyncAdapter(), mSelectionClause, mSelectionArgs); |
michael@0 | 448 | Log.i(TAG,"Calendar deleted: " + String.valueOf(calendarId)); |
michael@0 | 449 | this.mNotifyList.add(this.getAndroidCalendarUri()); |
michael@0 | 450 | Result = true; |
michael@0 | 451 | } catch (RemoteException e) { |
michael@0 | 452 | e.printStackTrace(); |
michael@0 | 453 | } |
michael@0 | 454 | Log.d(TAG, "Android Calendars deleted: " + Integer.toString(CountDeleted)); |
michael@0 | 455 | |
michael@0 | 456 | return Result; |
michael@0 | 457 | } |
michael@0 | 458 | |
michael@0 | 459 | /** |
michael@0 | 460 | * updates the android calendar |
michael@0 | 461 | * @param calendarUri the uri of the androidCalendar |
michael@0 | 462 | * @param target must be from android.provider.CalendarContract.Calendars |
michael@0 | 463 | * @param value the new value for the target |
michael@0 | 464 | * @throws RemoteException |
michael@0 | 465 | */ |
michael@0 | 466 | private void updateAndroidCalendar(Uri calendarUri, String target, int value) throws RemoteException { |
michael@0 | 467 | ContentValues mUpdateValues = new ContentValues(); |
michael@0 | 468 | mUpdateValues.put(target, value); |
michael@0 | 469 | |
michael@0 | 470 | mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null); |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | /** |
michael@0 | 474 | * updates the android calendar |
michael@0 | 475 | * @param calendarUri the uri of the androidCalendar |
michael@0 | 476 | * @param target must be from android.provider.CalendarContract.Calendars |
michael@0 | 477 | * @param value the new value for the target |
michael@0 | 478 | * @throws RemoteException |
michael@0 | 479 | */ |
michael@0 | 480 | private void updateAndroidCalendar(Uri calendarUri, String target, String value) throws RemoteException { |
michael@0 | 481 | ContentValues mUpdateValues = new ContentValues(); |
michael@0 | 482 | mUpdateValues.put(target, value); |
michael@0 | 483 | |
michael@0 | 484 | mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null); |
michael@0 | 485 | } |
michael@0 | 486 | |
michael@0 | 487 | /** |
michael@0 | 488 | * marks the android event as already handled |
michael@0 | 489 | * @return |
michael@0 | 490 | * @see AndroidEvent#cInternalTag |
michael@0 | 491 | * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats) |
michael@0 | 492 | * @throws RemoteException |
michael@0 | 493 | */ |
michael@0 | 494 | public boolean tagAndroidEvent(AndroidEvent androidEvent) throws RemoteException { |
michael@0 | 495 | boolean Result = false; |
michael@0 | 496 | |
michael@0 | 497 | ContentValues values = new ContentValues(); |
michael@0 | 498 | //values.put(Event.INTERNALTAG, 1); |
michael@0 | 499 | values.put(Event.INTERNALTAG, mTagCounter); |
michael@0 | 500 | //values.put(Event.INTERNALTAG, String.valueOf(mTagCounter)); |
michael@0 | 501 | |
michael@0 | 502 | int RowCount = this.mProvider.update(asSyncAdapter(androidEvent.getUri(), this.mAccount.name, this.mAccount.type), values, null, null); |
michael@0 | 503 | //Log.v(TAG,"event tag nr: " + String.valueOf(mTagCounter)); |
michael@0 | 504 | //Log.v(TAG,"Rows updated: " + String.valueOf(RowCount)); |
michael@0 | 505 | |
michael@0 | 506 | if (RowCount == 1) { |
michael@0 | 507 | Result = true; |
michael@0 | 508 | mTagCounter += 1; |
michael@0 | 509 | } else { |
michael@0 | 510 | Log.v(TAG,"EVENT NOT TAGGED!"); |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | return Result; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | /** |
michael@0 | 517 | * removes the tag of all android events |
michael@0 | 518 | * @return |
michael@0 | 519 | * @see AndroidEvent#cInternalTag |
michael@0 | 520 | * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats) |
michael@0 | 521 | * @throws RemoteException |
michael@0 | 522 | */ |
michael@0 | 523 | public int untagAndroidEvents() throws RemoteException { |
michael@0 | 524 | int RowCount = 0; |
michael@0 | 525 | int Steps = 100; |
michael@0 | 526 | ContentValues values = new ContentValues(); |
michael@0 | 527 | values.put(Event.INTERNALTAG, 0); |
michael@0 | 528 | |
michael@0 | 529 | for (int i=1; i < this.mTagCounter; i = i + Steps) { |
michael@0 | 530 | String mSelectionClause = "(CAST(" + Event.INTERNALTAG + " AS INT) >= ?) AND (CAST(" + Event.INTERNALTAG + " AS INT) < ?) AND (" + Events.CALENDAR_ID + " = ?)"; |
michael@0 | 531 | String[] mSelectionArgs = {String.valueOf(i), String.valueOf(i + Steps), Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))}; |
michael@0 | 532 | RowCount += this.mProvider.update(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), values, mSelectionClause, mSelectionArgs); |
michael@0 | 533 | } |
michael@0 | 534 | /*String mSelectionClause = "(" + Event.INTERNALTAG + " > ?) AND (" + Events.CALENDAR_ID + " = ?)"; |
michael@0 | 535 | String[] mSelectionArgs = {"0", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))}; |
michael@0 | 536 | RowCount += this.mProvider.update(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), values, mSelectionClause, mSelectionArgs);*/ |
michael@0 | 537 | |
michael@0 | 538 | //Log.d(TAG, "Rows reseted: " + RowCount.toString()); |
michael@0 | 539 | return RowCount; |
michael@0 | 540 | } |
michael@0 | 541 | /** |
michael@0 | 542 | * Events not being tagged are for deletion |
michael@0 | 543 | * @return |
michael@0 | 544 | * @see AndroidEvent#cInternalTag |
michael@0 | 545 | * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats) |
michael@0 | 546 | * @throws RemoteException |
michael@0 | 547 | */ |
michael@0 | 548 | public int deleteUntaggedEvents() throws RemoteException { |
michael@0 | 549 | String mSelectionClause = "(" + Event.INTERNALTAG + " < ?) AND (" + Events.CALENDAR_ID + " = ?)"; |
michael@0 | 550 | String[] mSelectionArgs = {"1", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))}; |
michael@0 | 551 | |
michael@0 | 552 | int CountDeleted = this.mProvider.delete(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), mSelectionClause, mSelectionArgs); |
michael@0 | 553 | //Log.d(TAG, "Rows deleted: " + CountDeleted.toString()); |
michael@0 | 554 | return CountDeleted; |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | private Uri SyncAdapterCalendar() { |
michael@0 | 558 | return asSyncAdapter(this.getAndroidCalendarUri(), mAccount.name, mAccount.type); |
michael@0 | 559 | } |
michael@0 | 560 | private Uri SyncAdapter() { |
michael@0 | 561 | return asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type); |
michael@0 | 562 | } |
michael@0 | 563 | private static Uri asSyncAdapter(Uri uri, String account, String accountType) { |
michael@0 | 564 | return uri.buildUpon() |
michael@0 | 565 | .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true") |
michael@0 | 566 | .appendQueryParameter(Calendars.ACCOUNT_NAME, account) |
michael@0 | 567 | .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build(); |
michael@0 | 568 | } |
michael@0 | 569 | |
michael@0 | 570 | public void setAccount(Account account) { |
michael@0 | 571 | this.mAccount = account; |
michael@0 | 572 | } |
michael@0 | 573 | public void setProvider(ContentProviderClient provider) { |
michael@0 | 574 | this.mProvider = provider; |
michael@0 | 575 | } |
michael@0 | 576 | |
michael@0 | 577 | /** |
michael@0 | 578 | * general access function to ContentValues |
michael@0 | 579 | * @param Item the item name from Calendars.* |
michael@0 | 580 | * @return the value for the item |
michael@0 | 581 | */ |
michael@0 | 582 | private String getContentValueAsString(String Item) { |
michael@0 | 583 | String Result = ""; |
michael@0 | 584 | if (this.ContentValues.containsKey(Item)) |
michael@0 | 585 | Result = this.ContentValues.getAsString(Item); |
michael@0 | 586 | return Result; |
michael@0 | 587 | } |
michael@0 | 588 | /** |
michael@0 | 589 | * general access function to ContentValues |
michael@0 | 590 | * @param Item the item name from Calendars.* |
michael@0 | 591 | * @return the value for the item |
michael@0 | 592 | */ |
michael@0 | 593 | private int getContentValueAsInt(String Item) { |
michael@0 | 594 | int Result = 0; |
michael@0 | 595 | if (this.ContentValues.containsKey(Item)) |
michael@0 | 596 | Result = this.ContentValues.getAsInteger(Item); |
michael@0 | 597 | return Result; |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | /** |
michael@0 | 601 | * general access function to ContentValues |
michael@0 | 602 | * @param Item the item name from Calendars.* |
michael@0 | 603 | * @param Value the value for the item |
michael@0 | 604 | * @return success of this function |
michael@0 | 605 | */ |
michael@0 | 606 | private boolean setContentValueAsString(String Item, String Value) { |
michael@0 | 607 | boolean Result = false; |
michael@0 | 608 | |
michael@0 | 609 | if (this.ContentValues.containsKey(Item)) |
michael@0 | 610 | this.ContentValues.remove(Item); |
michael@0 | 611 | this.ContentValues.put(Item, Value); |
michael@0 | 612 | |
michael@0 | 613 | return Result; |
michael@0 | 614 | } |
michael@0 | 615 | |
michael@0 | 616 | /** |
michael@0 | 617 | * general access function to ContentValues |
michael@0 | 618 | * @param Item the item name from Calendars.* |
michael@0 | 619 | * @param Value the value for the item |
michael@0 | 620 | * @return success of this function |
michael@0 | 621 | */ |
michael@0 | 622 | private boolean setContentValueAsInt(String Item, int Value) { |
michael@0 | 623 | boolean Result = false; |
michael@0 | 624 | |
michael@0 | 625 | if (this.ContentValues.containsKey(Item)) |
michael@0 | 626 | this.ContentValues.remove(Item); |
michael@0 | 627 | this.ContentValues.put(Item, Value); |
michael@0 | 628 | |
michael@0 | 629 | return Result; |
michael@0 | 630 | } |
michael@0 | 631 | |
michael@0 | 632 | public ArrayList<Uri> getNotifyList() { |
michael@0 | 633 | return this.mNotifyList; |
michael@0 | 634 | } |
michael@0 | 635 | |
michael@0 | 636 | public ArrayList<CalendarEvent> getCalendarEvents() { |
michael@0 | 637 | return this.mCalendarEvents; |
michael@0 | 638 | } |
michael@0 | 639 | |
michael@0 | 640 | public boolean readCalendarEvents(CaldavFacade facade) { |
michael@0 | 641 | boolean Result = false; |
michael@0 | 642 | |
michael@0 | 643 | try { |
michael@0 | 644 | this.mCalendarEvents = facade.getCalendarEvents(this); |
michael@0 | 645 | Result = true; |
michael@0 | 646 | } catch (ClientProtocolException e) { |
michael@0 | 647 | e.printStackTrace(); |
michael@0 | 648 | Result = false; |
michael@0 | 649 | } catch (URISyntaxException e) { |
michael@0 | 650 | e.printStackTrace(); |
michael@0 | 651 | Result = false; |
michael@0 | 652 | } catch (IOException e) { |
michael@0 | 653 | e.printStackTrace(); |
michael@0 | 654 | Result = false; |
michael@0 | 655 | } catch (ParserConfigurationException e) { |
michael@0 | 656 | e.printStackTrace(); |
michael@0 | 657 | Result = false; |
michael@0 | 658 | } catch (SAXException e) { |
michael@0 | 659 | e.printStackTrace(); |
michael@0 | 660 | Result = false; |
michael@0 | 661 | } |
michael@0 | 662 | |
michael@0 | 663 | return Result; |
michael@0 | 664 | } |
michael@0 | 665 | |
michael@0 | 666 | } |