michael@0: /**
michael@0: * Copyright (c) 2012-2013, Gerald Garcia, Timo Berger
michael@8: *
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@8: *
michael@0: */
michael@0:
michael@0: package org.gege.caldavsyncadapter.caldav.entities;
michael@0:
michael@0: import android.accounts.Account;
michael@0: import android.content.ContentProviderClient;
michael@0: import android.content.ContentUris;
michael@0: import android.content.ContentValues;
michael@0: import android.content.SyncStats;
michael@0: import android.database.Cursor;
michael@0: import android.net.Uri;
michael@0: import android.os.RemoteException;
michael@0: import android.provider.CalendarContract.Calendars;
michael@0: import android.provider.CalendarContract.Events;
michael@0: import android.util.Log;
michael@0:
michael@8: import org.apache.http.client.ClientProtocolException;
michael@8: import org.gege.caldavsyncadapter.CalendarColors;
michael@8: import org.gege.caldavsyncadapter.Event;
michael@8: import org.gege.caldavsyncadapter.android.entities.AndroidEvent;
michael@8: import org.gege.caldavsyncadapter.caldav.CaldavFacade;
michael@8: import org.gege.caldavsyncadapter.syncadapter.SyncAdapter;
michael@8: import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper;
michael@8: import org.xml.sax.SAXException;
michael@8:
michael@8: import java.io.IOException;
michael@8: import java.net.URI;
michael@8: import java.net.URISyntaxException;
michael@8: import java.util.ArrayList;
michael@8: import java.util.regex.Matcher;
michael@8: import java.util.regex.Pattern;
michael@8:
michael@8: import javax.xml.parsers.ParserConfigurationException;
michael@8:
michael@0: public class DavCalendar {
michael@8: public enum CalendarSource {
michael@8: undefined, Android, CalDAV
michael@8: }
michael@0:
michael@8: private static final String TAG = "Calendar";
michael@0:
michael@8: /**
michael@8: * stores the CTAG of a calendar
michael@8: */
michael@8: public static String CTAG = Calendars.CAL_SYNC1;
michael@0:
michael@8: /**
michael@8: * stores the URI of a calendar
michael@8: * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
michael@8: */
michael@8: public static String URI = Calendars._SYNC_ID;
michael@0:
michael@8: public static String SERVERURL = Calendars.CAL_SYNC2;
michael@0:
michael@8: private String strCalendarColor = "";
michael@0:
michael@8: private ArrayList mNotifyList = new ArrayList();
michael@0:
michael@8: /**
michael@8: * the event transformed into ContentValues
michael@8: */
michael@8: public ContentValues ContentValues = new ContentValues();
michael@0:
michael@8: private Account mAccount = null;
michael@8: private ContentProviderClient mProvider = null;
michael@0:
michael@8: public boolean foundServerSide = false;
michael@8: public boolean foundClientSide = false;
michael@8: public CalendarSource Source = CalendarSource.undefined;
michael@0:
michael@8: public String ServerUrl = "";
michael@0:
michael@8: private ArrayList mCalendarEvents = new ArrayList();
michael@0:
michael@8: private int mTagCounter = 1;
michael@0:
michael@8: /**
michael@8: * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
michael@8: */
michael@8: public URI getURI() {
michael@8: String strUri = this.getContentValueAsString(DavCalendar.URI);
michael@8: URI result = null;
michael@8: try {
michael@8: result = new URI(strUri);
michael@8: } catch (URISyntaxException e) {
michael@8: e.printStackTrace();
michael@8: }
michael@8: return result;
michael@8: }
michael@0:
michael@8: /**
michael@8: * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
michael@8: */
michael@8: public void setURI(URI uri) {
michael@8: this.setContentValueAsString(DavCalendar.URI, uri.toString());
michael@8: }
michael@0:
michael@8: /**
michael@8: * example: Cleartext Display Name
michael@8: */
michael@8: public String getCalendarDisplayName() {
michael@8: return this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME);
michael@8: }
michael@0:
michael@8: /**
michael@8: * example: Cleartext Display Name
michael@8: */
michael@8: public void setCalendarDisplayName(String displayName) {
michael@8: this.setContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME, displayName);
michael@8: }
michael@0:
michael@0:
michael@8: /**
michael@8: * example: 1143
michael@8: */
michael@8: public void setCTag(String cTag, boolean Update) {
michael@8: this.setContentValueAsString(DavCalendar.CTAG, cTag);
michael@8: if (Update) {
michael@8: //serverCalendar.updateAndroidCalendar(androidCalendarUri, Calendar.CTAG, serverCalendar.getcTag());
michael@8: try {
michael@8: this.updateAndroidCalendar(this.getAndroidCalendarUri(), CTAG, cTag);
michael@8: } catch (RemoteException e) {
michael@8: e.printStackTrace();
michael@8: }
michael@8: }
michael@8: }
michael@0:
michael@8: /**
michael@8: * example: 1143
michael@8: */
michael@8: public String getcTag() {
michael@8: return this.getContentValueAsString(DavCalendar.CTAG);
michael@8: }
michael@0:
michael@8: /**
michael@8: * example: #FFCCAA
michael@8: */
michael@8: public void setCalendarColorAsString(String color) {
michael@8: int maxlen = 6;
michael@0:
michael@8: this.strCalendarColor = color;
michael@8: if (!color.equals("")) {
michael@8: String strColor = color.replace("#", "");
michael@8: if (strColor.length() > maxlen)
michael@8: strColor = strColor.substring(0, maxlen);
michael@8: int intColor = Integer.parseInt(strColor, 16);
michael@8: this.setContentValueAsInt(Calendars.CALENDAR_COLOR, intColor);
michael@8: }
michael@8: }
michael@0:
michael@8: /**
michael@8: * example: #FFCCAA
michael@8: */
michael@8: public String getCalendarColorAsString() {
michael@8: return this.strCalendarColor;
michael@8: }
michael@0:
michael@8: /**
michael@8: * example 12345
michael@8: */
michael@8: public int getCalendarColor() {
michael@8: return this.getContentValueAsInt(Calendars.CALENDAR_COLOR);
michael@8: }
michael@8:
michael@8: /**
michael@8: * example 12345
michael@8: */
michael@8: public void setCalendarColor(int color) {
michael@8: this.setContentValueAsInt(Calendars.CALENDAR_COLOR, color);
michael@8: }
michael@8:
michael@8: /**
michael@8: * example:
michael@8: * should be: calendarname
michael@8: * but is: http://caldav.example.com/calendarserver.php/calendars/username/calendarname/
michael@8: */
michael@8: public String getCalendarName() {
michael@8: return this.getContentValueAsString(Calendars.NAME);
michael@8: }
michael@8:
michael@8: /**
michael@8: * example:
michael@8: * should be: calendarname
michael@8: * but is: http://caldav.example.com/calendarserver.php/calendars/username/calendarname/
michael@8: */
michael@8: public void setCalendarName(String calendarName) {
michael@8: this.setContentValueAsString(Calendars.NAME, calendarName);
michael@8: }
michael@8:
michael@8: /**
michael@8: * example: 8
michael@8: */
michael@8: public int getAndroidCalendarId() {
michael@8: return this.getContentValueAsInt(Calendars._ID);
michael@8: }
michael@8:
michael@8: /**
michael@8: * example: 8
michael@8: */
michael@8: public void setAndroidCalendarId(int androidCalendarId) {
michael@8: this.setContentValueAsInt(Calendars._ID, androidCalendarId);
michael@8: }
michael@8:
michael@8: /**
michael@8: * example: content://com.android.calendar/calendars/8
michael@8: */
michael@8: public Uri getAndroidCalendarUri() {
michael@8: return ContentUris.withAppendedId(Calendars.CONTENT_URI, this.getAndroidCalendarId());
michael@8: }
michael@8:
michael@8: /**
michael@8: * empty constructor
michael@8: */
michael@8: public DavCalendar(CalendarSource source) {
michael@8: this.Source = source;
michael@8: }
michael@8:
michael@8: /**
michael@8: * creates an new instance from a cursor
michael@8: *
michael@8: * @param cur must be a cursor from "ContentProviderClient" with Uri Calendars.CONTENT_URI
michael@8: */
michael@8: public DavCalendar(Account account, ContentProviderClient provider, Cursor cur, CalendarSource source, String serverUrl) {
michael@8: this.mAccount = account;
michael@8: this.mProvider = provider;
michael@8: this.foundClientSide = true;
michael@8: this.Source = source;
michael@8: this.ServerUrl = serverUrl;
michael@8:
michael@8: String strSyncID = cur.getString(cur.getColumnIndex(Calendars._SYNC_ID));
michael@8: String strName = cur.getString(cur.getColumnIndex(Calendars.NAME));
michael@8: String strDisplayName = cur.getString(cur.getColumnIndex(Calendars.CALENDAR_DISPLAY_NAME));
michael@8: String strCTAG = cur.getString(cur.getColumnIndex(DavCalendar.CTAG));
michael@8: String strServerUrl = cur.getString(cur.getColumnIndex(DavCalendar.SERVERURL));
michael@8: String strCalendarColor = cur.getString(cur.getColumnIndex(Calendars.CALENDAR_COLOR));
michael@8: int intAndroidCalendarId = cur.getInt(cur.getColumnIndex(Calendars._ID));
michael@8:
michael@8: this.setCalendarName(strName);
michael@8: this.setCalendarDisplayName(strDisplayName);
michael@8: this.setCTag(strCTAG, false);
michael@8: this.setAndroidCalendarId(intAndroidCalendarId);
michael@8: this.setCalendarColor(Integer.parseInt(strCalendarColor));
michael@8:
michael@8: if (strSyncID == null) {
michael@8: this.correctSyncID(strName);
michael@8: strSyncID = strName;
michael@8: }
michael@8: if (strServerUrl == null) {
michael@8: this.correctServerUrl(serverUrl);
michael@8: }
michael@8: URI uri = null;
michael@8: try {
michael@8: uri = new URI(strSyncID);
michael@8: } catch (URISyntaxException e) {
michael@8: e.printStackTrace();
michael@8: }
michael@8: this.setURI(uri);
michael@8: }
michael@8:
michael@8: /**
michael@8: * checks a given list of android calendars for a specific android calendar.
michael@8: * this calendar should be a server calendar as it is searched for.
michael@8: * if the calendar is not found, it will be created.
michael@8: *
michael@8: * @param androidCalList the list of android calendars
michael@8: * @param context
michael@8: * @return the found android calendar or null of fails
michael@8: * @throws RemoteException
michael@8: */
michael@8: public Uri checkAndroidCalendarList(CalendarList androidCalList, android.content.Context context) throws RemoteException {
michael@8: Uri androidCalendarUri = null;
michael@8: boolean isCalendarExist = false;
michael@8:
michael@8: DavCalendar androidCalendar = androidCalList.getCalendarByURI(this.getURI());
michael@8: if (androidCalendar != null) {
michael@8: isCalendarExist = true;
michael@8: androidCalendar.foundServerSide = true;
michael@8: }
michael@8:
michael@8:
michael@8: if (!isCalendarExist) {
michael@8: DavCalendar newCal = this.createNewAndroidCalendar(this, androidCalList.getCalendarList()
michael@8: .size(), context);
michael@8: if (newCal != null) {
michael@8: androidCalList.addCalendar(newCal);
michael@8: androidCalendarUri = newCal.getAndroidCalendarUri();
michael@8: }
michael@8: } else {
michael@8: androidCalendarUri = androidCalendar.getAndroidCalendarUri();
michael@8: if (!this.getCalendarColorAsString().equals("")) {
michael@8: this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_COLOR, getColorAsHex(this.getCalendarColorAsString()));
michael@8: }
michael@8: if ((this.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME)) &&
michael@8: (androidCalendar.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME))) {
michael@8: String serverDisplayName = this.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME);
michael@8: String clientDisplayName = androidCalendar.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME);
michael@8: if (!serverDisplayName.equals(clientDisplayName))
michael@8: this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_DISPLAY_NAME, serverDisplayName);
michael@8: }
michael@8: }
michael@8:
michael@8: return androidCalendarUri;
michael@8: }
michael@8:
michael@8: /**
michael@8: * COMPAT: the calendar Uri was stored as calendar Name. this function updates the URI (_SYNC_ID)
michael@8: *
michael@8: * @param calendarUri the real calendarUri
michael@8: * @return success of this function
michael@8: */
michael@8: private boolean correctSyncID(String calendarUri) {
michael@8: boolean Result = false;
michael@8: Log.v(TAG, "correcting SyncID for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME));
michael@8:
michael@8: ContentValues mUpdateValues = new ContentValues();
michael@8: mUpdateValues.put(DavCalendar.URI, calendarUri);
michael@8:
michael@8: try {
michael@8: mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null);
michael@8: Result = true;
michael@8: } catch (RemoteException e) {
michael@8: e.printStackTrace();
michael@8: }
michael@8:
michael@8: return Result;
michael@8: }
michael@8:
michael@8: /**
michael@8: * COMPAT: the serverurl (CAL_SYNC2) was not sored within a calendar. this fixes it. (see #98)
michael@8: *
michael@8: * @param serverUrl the current serverurl
michael@8: * @return success of this function
michael@8: */
michael@8: private boolean correctServerUrl(String serverUrl) {
michael@8: boolean Result = false;
michael@8: Log.v(TAG, "correcting ServerUrl for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME));
michael@8:
michael@8: ContentValues mUpdateValues = new ContentValues();
michael@8: mUpdateValues.put(DavCalendar.SERVERURL, serverUrl);
michael@8:
michael@8: try {
michael@8: mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null);
michael@8: Result = true;
michael@8: } catch (RemoteException e) {
michael@8: e.printStackTrace();
michael@8: }
michael@8:
michael@8: return Result;
michael@8: }
michael@8:
michael@8: /**
michael@8: * creates a new androidCalendar
michael@8: *
michael@8: * @param serverCalendar
michael@8: * @param index
michael@8: * @param context
michael@8: * @return the new androidCalendar or null if fails
michael@8: */
michael@8: private DavCalendar createNewAndroidCalendar(DavCalendar serverCalendar, int index, android.content.Context context) {
michael@8: Uri newUri = null;
michael@8: DavCalendar Result = null;
michael@8:
michael@8: final ContentValues contentValues = new ContentValues();
michael@8: contentValues.put(DavCalendar.URI, serverCalendar.getURI().toString());
michael@8: contentValues.put(DavCalendar.SERVERURL, this.ServerUrl);
michael@8:
michael@8: contentValues.put(Calendars.VISIBLE, 1);
michael@8: contentValues.put(Calendars.CALENDAR_DISPLAY_NAME, serverCalendar.getCalendarDisplayName());
michael@8: contentValues.put(Calendars.ACCOUNT_NAME, mAccount.name);
michael@8: contentValues.put(Calendars.ACCOUNT_TYPE, mAccount.type);
michael@8: contentValues.put(Calendars.OWNER_ACCOUNT, mAccount.name);
michael@8: contentValues.put(Calendars.SYNC_EVENTS, 1);
michael@8: contentValues.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
michael@8:
michael@8: String calendarColorAsString = serverCalendar.getCalendarColorAsString();
michael@8: if (!calendarColorAsString.isEmpty()) {
michael@8: int color = getColorAsHex(calendarColorAsString);
michael@8: contentValues.put(Calendars.CALENDAR_COLOR, color);
michael@8: } else {
michael@8: // find a color
michael@8: //int index = mList.size();
michael@8: index = index % CalendarColors.colors.length;
michael@8: contentValues.put(Calendars.CALENDAR_COLOR, CalendarColors.colors[2]);
michael@8: }
michael@8:
michael@8: try {
michael@8: newUri = mProvider.insert(asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type), contentValues);
michael@8: } catch (RemoteException e) {
michael@8: e.printStackTrace();
michael@8: }
michael@8:
michael@8: // it is possible that this calendar already exists but the provider failed to find it within isCalendarExist()
michael@8: // the adapter would try to create a new calendar but the provider fails again to create a new calendar.
michael@8: if (newUri != null) {
michael@8: long newCalendarId = ContentUris.parseId(newUri);
michael@8:
michael@8: Cursor cur = null;
michael@8: Uri uri = Calendars.CONTENT_URI;
michael@8: String selection = "(" + Calendars._ID + " = ?)";
michael@8: String[] selectionArgs = new String[]{String.valueOf(newCalendarId)};
michael@8:
michael@8: // Submit the query and get a Cursor object back.
michael@8: try {
michael@8: cur = mProvider.query(uri, null, selection, selectionArgs, null);
michael@8: } catch (RemoteException e) {
michael@8: e.printStackTrace();
michael@8: }
michael@8:
michael@8: if (cur != null) {
michael@8: while (cur.moveToNext()) {
michael@8: Result = new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl);
michael@8: Result.foundServerSide = true;
michael@8: }
michael@8: cur.close();
michael@8: //if (Result != null)
michael@8: // this.mList.add(Result);
michael@8: }
michael@8: Log.i(TAG, "New calendar created : URI=" + Result.getAndroidCalendarUri());
michael@8: NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "new calendar found: " + Result
michael@8: .getCalendarDisplayName());
michael@8: mNotifyList.add(Result.getAndroidCalendarUri());
michael@8: }
michael@8:
michael@8: return Result;
michael@8: }
michael@8:
michael@8: private int getColorAsHex(String calendarColorAsString) {
michael@8: int color = 0x0000FF00;
michael@8: Pattern p = Pattern.compile("#?(\\p{XDigit}{6})(\\p{XDigit}{2})?");
michael@8: Matcher m = p.matcher(calendarColorAsString);
michael@8: if (m.find()) {
michael@8: int color_rgb = Integer.parseInt(m.group(1), 16);
michael@8: int color_alpha = m.group(2) != null ? (Integer.parseInt(m.group(2), 16) & 0xFF) : 0xFF;
michael@8: color = (color_alpha << 24) | color_rgb;
michael@8: }
michael@8: return color;
michael@8: }
michael@8:
michael@8: /**
michael@8: * there is no corresponding calendar on server side. time to delete this calendar on android side.
michael@8: *
michael@8: * @return
michael@8: */
michael@8: public boolean deleteAndroidCalendar() {
michael@8: boolean Result = false;
michael@8:
michael@8: String mSelectionClause = "(" + Calendars._ID + " = ?)";
michael@8: int calendarId = this.getAndroidCalendarId();
michael@8: String[] mSelectionArgs = {Long.toString(calendarId)};
michael@8:
michael@8: int CountDeleted = 0;
michael@8: try {
michael@8: CountDeleted = mProvider.delete(this.SyncAdapter(), mSelectionClause, mSelectionArgs);
michael@8: Log.i(TAG, "Calendar deleted: " + String.valueOf(calendarId));
michael@8: this.mNotifyList.add(this.getAndroidCalendarUri());
michael@8: Result = true;
michael@8: } catch (RemoteException e) {
michael@8: e.printStackTrace();
michael@8: }
michael@8: Log.d(TAG, "Android Calendars deleted: " + Integer.toString(CountDeleted));
michael@8:
michael@8: return Result;
michael@8: }
michael@8:
michael@8: /**
michael@8: * updates the android calendar
michael@8: *
michael@8: * @param calendarUri the uri of the androidCalendar
michael@8: * @param target must be from android.provider.CalendarContract.Calendars
michael@8: * @param value the new value for the target
michael@8: * @throws RemoteException
michael@8: */
michael@8: private void updateAndroidCalendar(Uri calendarUri, String target, int value) throws RemoteException {
michael@8: ContentValues mUpdateValues = new ContentValues();
michael@8: mUpdateValues.put(target, value);
michael@8:
michael@8: mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null);
michael@8: }
michael@8:
michael@8: /**
michael@8: * updates the android calendar
michael@8: *
michael@8: * @param calendarUri the uri of the androidCalendar
michael@8: * @param target must be from android.provider.CalendarContract.Calendars
michael@8: * @param value the new value for the target
michael@8: * @throws RemoteException
michael@8: */
michael@8: private void updateAndroidCalendar(Uri calendarUri, String target, String value) throws RemoteException {
michael@8: ContentValues mUpdateValues = new ContentValues();
michael@8: mUpdateValues.put(target, value);
michael@8:
michael@8: mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null);
michael@8: }
michael@8:
michael@8: /**
michael@8: * marks the android event as already handled
michael@8: *
michael@8: * @return
michael@8: * @throws RemoteException
michael@8: * @see AndroidEvent#cInternalTag
michael@8: * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
michael@8: */
michael@8: public boolean tagAndroidEvent(AndroidEvent androidEvent) throws RemoteException {
michael@8: boolean Result = false;
michael@8:
michael@8: ContentValues values = new ContentValues();
michael@8: //values.put(Event.INTERNALTAG, 1);
michael@8: values.put(Event.INTERNALTAG, mTagCounter);
michael@8: //values.put(Event.INTERNALTAG, String.valueOf(mTagCounter));
michael@8:
michael@8: int RowCount = this.mProvider.update(asSyncAdapter(androidEvent.getUri(), this.mAccount.name, this.mAccount.type), values, null, null);
michael@8: //Log.v(TAG,"event tag nr: " + String.valueOf(mTagCounter));
michael@8: //Log.v(TAG,"Rows updated: " + String.valueOf(RowCount));
michael@8:
michael@8: if (RowCount == 1) {
michael@8: Result = true;
michael@8: mTagCounter += 1;
michael@8: } else {
michael@8: Log.v(TAG, "EVENT NOT TAGGED!");
michael@8: }
michael@8:
michael@8: return Result;
michael@8: }
michael@8:
michael@8: /**
michael@8: * removes the tag of all android events
michael@8: *
michael@8: * @return
michael@8: * @throws RemoteException
michael@8: * @see AndroidEvent#cInternalTag
michael@8: * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
michael@8: */
michael@8: public int untagAndroidEvents() throws RemoteException {
michael@8: int RowCount = 0;
michael@8: int Steps = 100;
michael@8: ContentValues values = new ContentValues();
michael@8: values.put(Event.INTERNALTAG, 0);
michael@8:
michael@8: for (int i = 1; i < this.mTagCounter; i = i + Steps) {
michael@8: String mSelectionClause = "(CAST(" + Event.INTERNALTAG + " AS INT) >= ?) AND (CAST(" + Event.INTERNALTAG + " AS INT) < ?) AND (" + Events.CALENDAR_ID + " = ?)";
michael@8: String[] mSelectionArgs = {String.valueOf(i), String.valueOf(i + Steps), Long.toString(ContentUris
michael@8: .parseId(this.getAndroidCalendarUri()))};
michael@8: RowCount += this.mProvider.update(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), values, mSelectionClause, mSelectionArgs);
michael@8: }
michael@8: /*String mSelectionClause = "(" + Event.INTERNALTAG + " > ?) AND (" + Events.CALENDAR_ID + " = ?)";
michael@8: String[] mSelectionArgs = {"0", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
michael@0: RowCount += this.mProvider.update(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), values, mSelectionClause, mSelectionArgs);*/
michael@0:
michael@8: //Log.d(TAG, "Rows reseted: " + RowCount.toString());
michael@8: return RowCount;
michael@8: }
michael@8:
michael@8: /**
michael@8: * Events not being tagged are for deletion
michael@8: *
michael@8: * @return
michael@8: * @throws RemoteException
michael@8: * @see AndroidEvent#cInternalTag
michael@8: * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
michael@8: */
michael@8: public int deleteUntaggedEvents() throws RemoteException {
michael@8: String mSelectionClause = "(" + Event.INTERNALTAG + " < ?) AND (" + Events.CALENDAR_ID + " = ?)";
michael@8: String[] mSelectionArgs = {"1", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
michael@8:
michael@8: int CountDeleted = this.mProvider.delete(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), mSelectionClause, mSelectionArgs);
michael@8: //Log.d(TAG, "Rows deleted: " + CountDeleted.toString());
michael@8: return CountDeleted;
michael@8: }
michael@8:
michael@8: private Uri SyncAdapterCalendar() {
michael@8: return asSyncAdapter(this.getAndroidCalendarUri(), mAccount.name, mAccount.type);
michael@8: }
michael@8:
michael@8: private Uri SyncAdapter() {
michael@8: return asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type);
michael@8: }
michael@8:
michael@8: private static Uri asSyncAdapter(Uri uri, String account, String accountType) {
michael@8: return uri.buildUpon()
michael@8: .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
michael@8: .appendQueryParameter(Calendars.ACCOUNT_NAME, account)
michael@8: .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
michael@8: }
michael@8:
michael@8: public void setAccount(Account account) {
michael@8: this.mAccount = account;
michael@8: }
michael@8:
michael@8: public void setProvider(ContentProviderClient provider) {
michael@8: this.mProvider = provider;
michael@8: }
michael@8:
michael@8: /**
michael@8: * general access function to ContentValues
michael@8: *
michael@8: * @param Item the item name from Calendars.*
michael@8: * @return the value for the item
michael@8: */
michael@8: private String getContentValueAsString(String Item) {
michael@8: String Result = "";
michael@8: if (this.ContentValues.containsKey(Item))
michael@8: Result = this.ContentValues.getAsString(Item);
michael@8: return Result;
michael@8: }
michael@8:
michael@8: /**
michael@8: * general access function to ContentValues
michael@8: *
michael@8: * @param Item the item name from Calendars.*
michael@8: * @return the value for the item
michael@8: */
michael@8: private int getContentValueAsInt(String Item) {
michael@8: int Result = 0;
michael@8: if (this.ContentValues.containsKey(Item))
michael@8: Result = this.ContentValues.getAsInteger(Item);
michael@8: return Result;
michael@8: }
michael@8:
michael@8: /**
michael@8: * general access function to ContentValues
michael@8: *
michael@8: * @param Item the item name from Calendars.*
michael@8: * @param Value the value for the item
michael@8: * @return success of this function
michael@8: */
michael@8: private boolean setContentValueAsString(String Item, String Value) {
michael@8: boolean Result = false;
michael@8:
michael@8: if (this.ContentValues.containsKey(Item))
michael@8: this.ContentValues.remove(Item);
michael@8: this.ContentValues.put(Item, Value);
michael@8:
michael@8: return Result;
michael@8: }
michael@8:
michael@8: /**
michael@8: * general access function to ContentValues
michael@8: *
michael@8: * @param Item the item name from Calendars.*
michael@8: * @param Value the value for the item
michael@8: * @return success of this function
michael@8: */
michael@8: private boolean setContentValueAsInt(String Item, int Value) {
michael@8: boolean Result = false;
michael@8:
michael@8: if (this.ContentValues.containsKey(Item))
michael@8: this.ContentValues.remove(Item);
michael@8: this.ContentValues.put(Item, Value);
michael@8:
michael@8: return Result;
michael@8: }
michael@8:
michael@8: public ArrayList getNotifyList() {
michael@8: return this.mNotifyList;
michael@8: }
michael@8:
michael@8: public ArrayList getCalendarEvents() {
michael@8: return this.mCalendarEvents;
michael@8: }
michael@8:
michael@8: public boolean readCalendarEvents(CaldavFacade facade) {
michael@8: boolean Result = false;
michael@8:
michael@8: try {
michael@8: this.mCalendarEvents = facade.getCalendarEvents(this);
michael@8: Result = true;
michael@8: } catch (ClientProtocolException e) {
michael@8: e.printStackTrace();
michael@8: Result = false;
michael@8: } catch (URISyntaxException e) {
michael@8: e.printStackTrace();
michael@8: Result = false;
michael@8: } catch (IOException e) {
michael@8: e.printStackTrace();
michael@8: Result = false;
michael@8: } catch (ParserConfigurationException e) {
michael@8: e.printStackTrace();
michael@8: Result = false;
michael@8: } catch (SAXException e) {
michael@8: e.printStackTrace();
michael@8: Result = false;
michael@8: }
michael@8:
michael@8: return Result;
michael@8: }
michael@8:
michael@0: }