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