src/org/gege/caldavsyncadapter/caldav/entities/DavCalendar.java

changeset 0
fb9019fb1bf7
child 8
ec8af0e3fbc2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/gege/caldavsyncadapter/caldav/entities/DavCalendar.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,666 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012-2013, Gerald Garcia, Timo Berger
     1.6 + * 
     1.7 + * This file is part of Andoid Caldav Sync Adapter Free.
     1.8 + *
     1.9 + * Andoid Caldav Sync Adapter Free is free software: you can redistribute 
    1.10 + * it and/or modify it under the terms of the GNU General Public License 
    1.11 + * as published by the Free Software Foundation, either version 3 of the 
    1.12 + * License, or at your option any later version.
    1.13 + *
    1.14 + * Andoid Caldav Sync Adapter Free is distributed in the hope that 
    1.15 + * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
    1.16 + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 + * GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License
    1.20 + * along with Andoid Caldav Sync Adapter Free.  
    1.21 + * If not, see <http://www.gnu.org/licenses/>.
    1.22 + * 
    1.23 + */
    1.24 +
    1.25 +package org.gege.caldavsyncadapter.caldav.entities;
    1.26 +
    1.27 +import java.io.IOException;
    1.28 +import java.net.URI;
    1.29 +import java.net.URISyntaxException;
    1.30 +import java.util.ArrayList;
    1.31 +
    1.32 +import javax.xml.parsers.ParserConfigurationException;
    1.33 +
    1.34 +import org.apache.http.client.ClientProtocolException;
    1.35 +import org.gege.caldavsyncadapter.CalendarColors;
    1.36 +import org.gege.caldavsyncadapter.Event;
    1.37 +import org.gege.caldavsyncadapter.android.entities.AndroidEvent;
    1.38 +import org.gege.caldavsyncadapter.caldav.CaldavFacade;
    1.39 +import org.gege.caldavsyncadapter.syncadapter.SyncAdapter;
    1.40 +import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper;
    1.41 +import org.xml.sax.SAXException;
    1.42 +
    1.43 +import android.accounts.Account;
    1.44 +import android.content.ContentProviderClient;
    1.45 +import android.content.ContentUris;
    1.46 +import android.content.ContentValues;
    1.47 +import android.content.SyncStats;
    1.48 +import android.database.Cursor;
    1.49 +import android.net.Uri;
    1.50 +import android.os.RemoteException;
    1.51 +import android.provider.CalendarContract.Calendars;
    1.52 +import android.provider.CalendarContract.Events;
    1.53 +import android.util.Log;
    1.54 +
    1.55 +public class DavCalendar {
    1.56 +	public enum CalendarSource {
    1.57 +		undefined, Android, CalDAV
    1.58 +	}
    1.59 +		
    1.60 +	private static final String TAG = "Calendar";
    1.61 +		
    1.62 +	/**
    1.63 +	 * stores the CTAG of a calendar
    1.64 +	 */
    1.65 +	public static String CTAG = Calendars.CAL_SYNC1;
    1.66 +	
    1.67 +	/**
    1.68 +	 * stores the URI of a calendar
    1.69 +	 * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
    1.70 +	 */
    1.71 +	public static String URI = Calendars._SYNC_ID;
    1.72 +	
    1.73 +	public static String SERVERURL = Calendars.CAL_SYNC2;
    1.74 +	
    1.75 +	private String strCalendarColor = "";
    1.76 +	
    1.77 +	private ArrayList<Uri> mNotifyList = new ArrayList<Uri>(); 
    1.78 +
    1.79 +	/**
    1.80 +	 * the event transformed into ContentValues
    1.81 +	 */
    1.82 +	public ContentValues ContentValues = new ContentValues();
    1.83 +	
    1.84 +	private Account mAccount = null;
    1.85 +	private ContentProviderClient mProvider = null;
    1.86 +	
    1.87 +	public boolean foundServerSide = false;
    1.88 +	public boolean foundClientSide = false;
    1.89 +	public CalendarSource Source = CalendarSource.undefined;
    1.90 +	
    1.91 +	public String ServerUrl = "";
    1.92 +	
    1.93 +	private ArrayList<CalendarEvent> mCalendarEvents = new ArrayList<CalendarEvent>();
    1.94 +	
    1.95 +	private int mTagCounter = 1;
    1.96 +	
    1.97 +	/**
    1.98 +	 * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
    1.99 +	 */
   1.100 +	public URI getURI() {
   1.101 +		String strUri = this.getContentValueAsString(DavCalendar.URI);
   1.102 +		URI result = null;
   1.103 +		try {
   1.104 +			result = new URI(strUri);
   1.105 +		} catch (URISyntaxException e) {
   1.106 +			e.printStackTrace();
   1.107 +		}
   1.108 +		return result;
   1.109 +	}
   1.110 +
   1.111 +	/**
   1.112 +	 * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
   1.113 +	 */
   1.114 +	public void setURI(URI uri) {
   1.115 +		this.setContentValueAsString(DavCalendar.URI, uri.toString());
   1.116 +	}
   1.117 +
   1.118 +	/**
   1.119 +	 * example: Cleartext Display Name 
   1.120 +	 */
   1.121 +	public String getCalendarDisplayName() {
   1.122 +		return this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME);
   1.123 +	}
   1.124 +
   1.125 +	/**
   1.126 +	 * example: Cleartext Display Name 
   1.127 +	 */
   1.128 +	public void setCalendarDisplayName(String displayName) {
   1.129 +		this.setContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME, displayName);
   1.130 +	}
   1.131 +	
   1.132 +
   1.133 +	/**
   1.134 +	 * example: 1143
   1.135 +	 */
   1.136 +	public void setCTag(String cTag, boolean Update) {
   1.137 +		this.setContentValueAsString(DavCalendar.CTAG, cTag);
   1.138 +		if (Update) {
   1.139 +			//serverCalendar.updateAndroidCalendar(androidCalendarUri, Calendar.CTAG, serverCalendar.getcTag());
   1.140 +			try {
   1.141 +				this.updateAndroidCalendar(this.getAndroidCalendarUri(), CTAG, cTag);
   1.142 +			} catch (RemoteException e) {
   1.143 +				e.printStackTrace();
   1.144 +			}
   1.145 +		}
   1.146 +	}
   1.147 +	
   1.148 +	/**
   1.149 +	 * example: 1143
   1.150 +	 */
   1.151 +	public String getcTag() {
   1.152 +		return this.getContentValueAsString(DavCalendar.CTAG);
   1.153 +	}
   1.154 +	
   1.155 +	/**
   1.156 +	 * example: #FFCCAA 
   1.157 +	 */
   1.158 +	public void setCalendarColorAsString(String color) {
   1.159 +		int maxlen = 6;
   1.160 +		
   1.161 +		this.strCalendarColor = color;
   1.162 +		if (!color.equals("")) {
   1.163 +			String strColor = color.replace("#", "");
   1.164 +			if (strColor.length() > maxlen)
   1.165 +				strColor = strColor.substring(0, maxlen);
   1.166 +			int intColor = Integer.parseInt(strColor, 16);
   1.167 +			this.setContentValueAsInt(Calendars.CALENDAR_COLOR, intColor);
   1.168 +		}
   1.169 +	}
   1.170 +
   1.171 +	/**
   1.172 +	 * example: #FFCCAA 
   1.173 +	 */
   1.174 +	public String getCalendarColorAsString() {
   1.175 +		return this.strCalendarColor;
   1.176 +	}
   1.177 +
   1.178 +	/**
   1.179 +	 * example 12345 
   1.180 +	 */
   1.181 +	public int getCalendarColor() {
   1.182 +		return this.getContentValueAsInt(Calendars.CALENDAR_COLOR);
   1.183 +	}
   1.184 +	
   1.185 +	/**
   1.186 +	 * example 12345 
   1.187 +	 */
   1.188 +	public void setCalendarColor(int color) {
   1.189 +		this.setContentValueAsInt(Calendars.CALENDAR_COLOR, color);
   1.190 +	}
   1.191 +
   1.192 +	/**
   1.193 +	 * example: 
   1.194 +	 * 		should be: calendarname
   1.195 +	 * 		but is:    http://caldav.example.com/calendarserver.php/calendars/username/calendarname/
   1.196 +	 */
   1.197 +	public String getCalendarName() {
   1.198 +		return this.getContentValueAsString(Calendars.NAME);
   1.199 +	}
   1.200 +
   1.201 +	/**
   1.202 +	 * example: 
   1.203 +	 * 		should be: calendarname
   1.204 +	 * 		but is:    http://caldav.example.com/calendarserver.php/calendars/username/calendarname/
   1.205 +	 */
   1.206 +	public void setCalendarName(String calendarName) {
   1.207 +		this.setContentValueAsString(Calendars.NAME, calendarName);
   1.208 +	}
   1.209 +
   1.210 +	/**
   1.211 +	 * example: 8
   1.212 +	 */
   1.213 +	public int getAndroidCalendarId() {
   1.214 +		return this.getContentValueAsInt(Calendars._ID);
   1.215 +	}
   1.216 +
   1.217 +	/**
   1.218 +	 * example: 8
   1.219 +	 */
   1.220 +	public void setAndroidCalendarId(int androidCalendarId) {
   1.221 +		this.setContentValueAsInt(Calendars._ID, androidCalendarId);
   1.222 +	}
   1.223 +
   1.224 +	/**
   1.225 +	 * example: content://com.android.calendar/calendars/8
   1.226 +	 */
   1.227 +	public Uri getAndroidCalendarUri() {
   1.228 +		return ContentUris.withAppendedId(Calendars.CONTENT_URI, this.getAndroidCalendarId());
   1.229 +	}
   1.230 +	
   1.231 +	/**
   1.232 +	 * empty constructor
   1.233 +	 */
   1.234 +	public DavCalendar(CalendarSource source) {
   1.235 +		this.Source = source;
   1.236 +	}
   1.237 +	
   1.238 +	/**
   1.239 +	 * creates an new instance from a cursor
   1.240 +	 * @param cur must be a cursor from "ContentProviderClient" with Uri Calendars.CONTENT_URI
   1.241 +	 */
   1.242 +	public DavCalendar(Account account, ContentProviderClient provider, Cursor cur, CalendarSource source, String serverUrl) {
   1.243 +		this.mAccount = account;
   1.244 +		this.mProvider = provider;
   1.245 +		this.foundClientSide = true;
   1.246 +		this.Source = source;
   1.247 +		this.ServerUrl = serverUrl;
   1.248 +
   1.249 +		String strSyncID = cur.getString(cur.getColumnIndex(Calendars._SYNC_ID));
   1.250 +		String strName = cur.getString(cur.getColumnIndex(Calendars.NAME));
   1.251 +		String strDisplayName = cur.getString(cur.getColumnIndex(Calendars.CALENDAR_DISPLAY_NAME));
   1.252 +		String strCTAG = cur.getString(cur.getColumnIndex(DavCalendar.CTAG));
   1.253 +		String strServerUrl = cur.getString(cur.getColumnIndex(DavCalendar.SERVERURL));
   1.254 +		int intAndroidCalendarId = cur.getInt(cur.getColumnIndex(Calendars._ID));
   1.255 +
   1.256 +		this.setCalendarName(strName);
   1.257 +		this.setCalendarDisplayName(strDisplayName);
   1.258 +		this.setCTag(strCTAG, false);
   1.259 +		this.setAndroidCalendarId(intAndroidCalendarId);
   1.260 +		
   1.261 +		if (strSyncID == null) {
   1.262 +			this.correctSyncID(strName);
   1.263 +			strSyncID = strName;
   1.264 +		}
   1.265 +		if (strServerUrl == null) {
   1.266 +			this.correctServerUrl(serverUrl);
   1.267 +		}
   1.268 +		URI uri = null;
   1.269 +		try {
   1.270 +			uri = new URI(strSyncID);
   1.271 +		} catch (URISyntaxException e) {
   1.272 +			e.printStackTrace();
   1.273 +		}
   1.274 +		this.setURI(uri);
   1.275 +	}
   1.276 +	
   1.277 +	/**
   1.278 +	 * checks a given list of android calendars for a specific android calendar.
   1.279 +	 * this calendar should be a server calendar as it is searched for.
   1.280 +	 * if the calendar is not found, it will be created.
   1.281 +	 * @param androidCalList the list of android calendars
   1.282 +	 * @param context
   1.283 +	 * @return the found android calendar or null of fails
   1.284 +	 * @throws RemoteException
   1.285 +	 */
   1.286 +	public Uri checkAndroidCalendarList(CalendarList androidCalList, android.content.Context context) throws RemoteException {
   1.287 +		Uri androidCalendarUri = null;
   1.288 +		boolean isCalendarExist = false;
   1.289 +		
   1.290 +		DavCalendar androidCalendar = androidCalList.getCalendarByURI(this.getURI());
   1.291 +		if (androidCalendar != null) {
   1.292 +			isCalendarExist = true;
   1.293 +			androidCalendar.foundServerSide = true;
   1.294 +		}
   1.295 +		
   1.296 +
   1.297 +		if (!isCalendarExist) {
   1.298 +			DavCalendar newCal = this.createNewAndroidCalendar(this, androidCalList.getCalendarList().size(), context);
   1.299 +			if (newCal != null) {
   1.300 +				androidCalList.addCalendar(newCal);
   1.301 +				androidCalendarUri = newCal.getAndroidCalendarUri();
   1.302 +			}
   1.303 +		} else {
   1.304 +			androidCalendarUri = androidCalendar.getAndroidCalendarUri();
   1.305 +			if (!this.getCalendarColorAsString().equals("")) {
   1.306 +				//serverCalendar.updateCalendarColor(returnedCalendarUri, serverCalendar);
   1.307 +				this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_COLOR, this.getCalendarColor());
   1.308 +			}
   1.309 +			if ((this.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME)) && 
   1.310 +				(androidCalendar.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME))) {
   1.311 +				String serverDisplayName = this.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME);
   1.312 +				String clientDisplayName = androidCalendar.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME);
   1.313 +				if (!serverDisplayName.equals(clientDisplayName))
   1.314 +					this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_DISPLAY_NAME, serverDisplayName);
   1.315 +			}
   1.316 +		}
   1.317 +		
   1.318 +		return androidCalendarUri;
   1.319 +	}
   1.320 +	
   1.321 +	/**
   1.322 +	 * COMPAT: the calendar Uri was stored as calendar Name. this function updates the URI (_SYNC_ID)
   1.323 +	 * @param calendarUri the real calendarUri
   1.324 +	 * @return success of this function
   1.325 +	 */
   1.326 +	private boolean correctSyncID(String calendarUri) {
   1.327 +		boolean Result = false;
   1.328 +		Log.v(TAG, "correcting SyncID for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME));
   1.329 +			
   1.330 +		ContentValues mUpdateValues = new ContentValues();
   1.331 +		mUpdateValues.put(DavCalendar.URI, calendarUri);
   1.332 +		
   1.333 +		try {
   1.334 +			mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null);
   1.335 +			Result = true;
   1.336 +		} catch (RemoteException e) {
   1.337 +			e.printStackTrace();
   1.338 +		}
   1.339 +		
   1.340 +		return Result;
   1.341 +	}
   1.342 +	
   1.343 +	/**
   1.344 +	 * COMPAT: the serverurl (CAL_SYNC2) was not sored within a calendar. this fixes it. (see #98)
   1.345 +	 * @param serverUrl the current serverurl
   1.346 +	 * @return success of this function
   1.347 +	 */
   1.348 +	private boolean correctServerUrl(String serverUrl) {
   1.349 +		boolean Result = false;
   1.350 +		Log.v(TAG, "correcting ServerUrl for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME));
   1.351 +			
   1.352 +		ContentValues mUpdateValues = new ContentValues();
   1.353 +		mUpdateValues.put(DavCalendar.SERVERURL, serverUrl);
   1.354 +		
   1.355 +		try {
   1.356 +			mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null);
   1.357 +			Result = true;
   1.358 +		} catch (RemoteException e) {
   1.359 +			e.printStackTrace();
   1.360 +		}
   1.361 +		
   1.362 +		return Result;
   1.363 +	}
   1.364 +	
   1.365 +	/**
   1.366 +	 * creates a new androidCalendar
   1.367 +	 * @param serverCalendar
   1.368 +	 * @param index
   1.369 +	 * @param context
   1.370 +	 * @return the new androidCalendar or null if fails
   1.371 +	 */
   1.372 +	private DavCalendar createNewAndroidCalendar(DavCalendar serverCalendar, int index, android.content.Context context) {
   1.373 +		Uri newUri = null;
   1.374 +		DavCalendar Result = null;
   1.375 +		
   1.376 +		final ContentValues contentValues = new ContentValues();
   1.377 +		contentValues.put(DavCalendar.URI, serverCalendar.getURI().toString());
   1.378 +		contentValues.put(DavCalendar.SERVERURL, this.ServerUrl);
   1.379 +
   1.380 +		contentValues.put(Calendars.VISIBLE, 1);
   1.381 +		contentValues.put(Calendars.CALENDAR_DISPLAY_NAME, serverCalendar.getCalendarDisplayName());
   1.382 +		contentValues.put(Calendars.ACCOUNT_NAME, mAccount.name);
   1.383 +		contentValues.put(Calendars.ACCOUNT_TYPE, mAccount.type);
   1.384 +		contentValues.put(Calendars.OWNER_ACCOUNT, mAccount.name);
   1.385 +		contentValues.put(Calendars.SYNC_EVENTS, 1);
   1.386 +		contentValues.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
   1.387 +		
   1.388 +		if (!serverCalendar.getCalendarColorAsString().equals("")) {
   1.389 +			contentValues.put(Calendars.CALENDAR_COLOR, serverCalendar.getCalendarColor());
   1.390 +		} else {
   1.391 +			// find a color
   1.392 +			//int index = mList.size();
   1.393 +			index = index % CalendarColors.colors.length;
   1.394 +			contentValues.put(Calendars.CALENDAR_COLOR, CalendarColors.colors[index]);
   1.395 +		}
   1.396 +
   1.397 +		try {
   1.398 +			newUri = mProvider.insert(asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type), contentValues);
   1.399 +		} catch (RemoteException e) {
   1.400 +			e.printStackTrace();
   1.401 +		}
   1.402 +
   1.403 +		// it is possible that this calendar already exists but the provider failed to find it within isCalendarExist()
   1.404 +		// the adapter would try to create a new calendar but the provider fails again to create a new calendar.
   1.405 +		if (newUri != null) {
   1.406 +			long newCalendarId = ContentUris.parseId(newUri);
   1.407 +
   1.408 +			Cursor cur = null;
   1.409 +			Uri uri = Calendars.CONTENT_URI;   
   1.410 +			String selection = "(" + Calendars._ID +  " = ?)";
   1.411 +			String[] selectionArgs = new String[] {String.valueOf(newCalendarId)}; 
   1.412 +
   1.413 +			// Submit the query and get a Cursor object back. 
   1.414 +			try {
   1.415 +				cur = mProvider.query(uri, null, selection, selectionArgs, null);
   1.416 +			} catch (RemoteException e) {
   1.417 +				e.printStackTrace();
   1.418 +			}
   1.419 +			
   1.420 +			if (cur != null) {
   1.421 +				while (cur.moveToNext()) {
   1.422 +					Result = new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl);
   1.423 +					Result.foundServerSide = true;
   1.424 +				}
   1.425 +				cur.close();
   1.426 +				//if (Result != null)
   1.427 +				//	this.mList.add(Result);
   1.428 +			}
   1.429 +			Log.i(TAG, "New calendar created : URI=" + Result.getAndroidCalendarUri());
   1.430 +			NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "new calendar found: " + Result.getCalendarDisplayName());
   1.431 +			mNotifyList.add(Result.getAndroidCalendarUri());
   1.432 +		}
   1.433 +		
   1.434 +		return Result;
   1.435 +	}
   1.436 +	
   1.437 +	/**
   1.438 +	 * there is no corresponding calendar on server side. time to delete this calendar on android side.
   1.439 +	 * @return 
   1.440 +	 */
   1.441 +	public boolean deleteAndroidCalendar() {
   1.442 +		boolean Result = false;
   1.443 +		
   1.444 +		String mSelectionClause = "(" + Calendars._ID + " = ?)";
   1.445 +		int calendarId  = this.getAndroidCalendarId();
   1.446 +		String[] mSelectionArgs = {Long.toString(calendarId)};
   1.447 +		
   1.448 +		int CountDeleted = 0;
   1.449 +		try {
   1.450 +			CountDeleted = mProvider.delete(this.SyncAdapter(), mSelectionClause, mSelectionArgs);
   1.451 +			Log.i(TAG,"Calendar deleted: " + String.valueOf(calendarId));
   1.452 +			this.mNotifyList.add(this.getAndroidCalendarUri());
   1.453 +			Result = true;
   1.454 +		} catch (RemoteException e) {
   1.455 +			e.printStackTrace();
   1.456 +		}	
   1.457 +		Log.d(TAG, "Android Calendars deleted: " + Integer.toString(CountDeleted));
   1.458 +		
   1.459 +		return Result;
   1.460 +	}
   1.461 +
   1.462 +	/**
   1.463 +	 * updates the android calendar
   1.464 +	 * @param calendarUri the uri of the androidCalendar
   1.465 +	 * @param target must be from android.provider.CalendarContract.Calendars
   1.466 +	 * @param value the new value for the target
   1.467 +	 * @throws RemoteException
   1.468 +	 */
   1.469 +	private void updateAndroidCalendar(Uri calendarUri, String target, int value) throws RemoteException {
   1.470 +		ContentValues mUpdateValues = new ContentValues();
   1.471 +		mUpdateValues.put(target, value);
   1.472 +		
   1.473 +		mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null);
   1.474 +	}
   1.475 +
   1.476 +	/**
   1.477 +	 * updates the android calendar
   1.478 +	 * @param calendarUri the uri of the androidCalendar
   1.479 +	 * @param target must be from android.provider.CalendarContract.Calendars
   1.480 +	 * @param value the new value for the target
   1.481 +	 * @throws RemoteException
   1.482 +	 */
   1.483 +	private void updateAndroidCalendar(Uri calendarUri, String target, String value) throws RemoteException {
   1.484 +		ContentValues mUpdateValues = new ContentValues();
   1.485 +		mUpdateValues.put(target, value);
   1.486 +		
   1.487 +		mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null);
   1.488 +	}
   1.489 +	
   1.490 +	/**
   1.491 +	 * marks the android event as already handled
   1.492 +	 * @return
   1.493 +	 * @see AndroidEvent#cInternalTag
   1.494 +	 * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
   1.495 +	 * @throws RemoteException
   1.496 +	 */
   1.497 +	public boolean tagAndroidEvent(AndroidEvent androidEvent) throws RemoteException {
   1.498 +		boolean Result = false;
   1.499 +		
   1.500 +		ContentValues values = new ContentValues();
   1.501 +		//values.put(Event.INTERNALTAG, 1);
   1.502 +		values.put(Event.INTERNALTAG, mTagCounter);
   1.503 +		//values.put(Event.INTERNALTAG, String.valueOf(mTagCounter));
   1.504 +		
   1.505 +		int RowCount = this.mProvider.update(asSyncAdapter(androidEvent.getUri(), this.mAccount.name, this.mAccount.type), values, null, null);
   1.506 +		//Log.v(TAG,"event tag nr: " + String.valueOf(mTagCounter));
   1.507 +		//Log.v(TAG,"Rows updated: " + String.valueOf(RowCount));
   1.508 +		
   1.509 +		if (RowCount == 1) {
   1.510 +			Result = true;
   1.511 +			mTagCounter += 1;
   1.512 +		} else {
   1.513 +			Log.v(TAG,"EVENT NOT TAGGED!");
   1.514 +		}
   1.515 +		
   1.516 +		return Result;
   1.517 +	} 
   1.518 +	
   1.519 +	/**
   1.520 +	 * removes the tag of all android events
   1.521 +	 * @return
   1.522 +	 * @see AndroidEvent#cInternalTag
   1.523 +	 * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
   1.524 +	 * @throws RemoteException
   1.525 +	 */
   1.526 +	public int untagAndroidEvents() throws RemoteException {
   1.527 +		int RowCount = 0;
   1.528 +		int Steps = 100;
   1.529 +		ContentValues values = new ContentValues();
   1.530 +		values.put(Event.INTERNALTAG, 0);
   1.531 +
   1.532 +		for (int i=1; i < this.mTagCounter; i = i + Steps) {
   1.533 +			String mSelectionClause = "(CAST(" + Event.INTERNALTAG +  " AS INT) >= ?) AND (CAST(" + Event.INTERNALTAG +  " AS INT) < ?) AND (" + Events.CALENDAR_ID + " = ?)";
   1.534 +			String[] mSelectionArgs = {String.valueOf(i), String.valueOf(i + Steps), Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
   1.535 +			RowCount += this.mProvider.update(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), values, mSelectionClause, mSelectionArgs);
   1.536 +		}
   1.537 +		/*String mSelectionClause = "(" + Event.INTERNALTAG +  " > ?) AND (" + Events.CALENDAR_ID + " = ?)";
   1.538 +		String[] mSelectionArgs = {"0", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
   1.539 +		RowCount += this.mProvider.update(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), values, mSelectionClause, mSelectionArgs);*/
   1.540 +		
   1.541 +		//Log.d(TAG, "Rows reseted: " + RowCount.toString());
   1.542 +		return RowCount;
   1.543 +	}
   1.544 +	/**
   1.545 +	 * Events not being tagged are for deletion 
   1.546 +	 * @return
   1.547 +	 * @see AndroidEvent#cInternalTag
   1.548 +	 * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
   1.549 +	 * @throws RemoteException
   1.550 +	 */
   1.551 +	public int deleteUntaggedEvents() throws RemoteException {
   1.552 +		String mSelectionClause = "(" + Event.INTERNALTAG +  " < ?) AND (" + Events.CALENDAR_ID + " = ?)";
   1.553 +		String[] mSelectionArgs = {"1", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
   1.554 +		
   1.555 +		int CountDeleted = this.mProvider.delete(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), mSelectionClause, mSelectionArgs);	
   1.556 +		//Log.d(TAG, "Rows deleted: " + CountDeleted.toString());
   1.557 +		return CountDeleted;
   1.558 +	}
   1.559 +	
   1.560 +	private Uri SyncAdapterCalendar() {
   1.561 +		return asSyncAdapter(this.getAndroidCalendarUri(), mAccount.name, mAccount.type);
   1.562 +	}
   1.563 +	private Uri SyncAdapter() {
   1.564 +		return asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type);
   1.565 +	}
   1.566 +	private static Uri asSyncAdapter(Uri uri, String account, String accountType) {
   1.567 +	    return uri.buildUpon()
   1.568 +	        .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
   1.569 +	        .appendQueryParameter(Calendars.ACCOUNT_NAME, account)
   1.570 +	        .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
   1.571 +	}
   1.572 +	
   1.573 +	public void setAccount(Account account) {
   1.574 +		this.mAccount = account;
   1.575 +	}
   1.576 +	public void setProvider(ContentProviderClient provider) {
   1.577 +		this.mProvider = provider;
   1.578 +	}
   1.579 +	
   1.580 +	/**
   1.581 +	 * general access function to ContentValues
   1.582 +	 * @param Item the item name from Calendars.*
   1.583 +	 * @return the value for the item
   1.584 +	 */
   1.585 +	private String getContentValueAsString(String Item) {
   1.586 +		String Result = "";
   1.587 +		if (this.ContentValues.containsKey(Item))
   1.588 +			Result = this.ContentValues.getAsString(Item);
   1.589 +		return Result;
   1.590 +	}
   1.591 +	/**
   1.592 +	 * general access function to ContentValues
   1.593 +	 * @param Item the item name from Calendars.*
   1.594 +	 * @return the value for the item
   1.595 +	 */
   1.596 +	private int getContentValueAsInt(String Item) {
   1.597 +		int Result = 0;
   1.598 +		if (this.ContentValues.containsKey(Item))
   1.599 +			Result = this.ContentValues.getAsInteger(Item);
   1.600 +		return Result;
   1.601 +	}
   1.602 +	
   1.603 +	/**
   1.604 +	 * general access function to ContentValues
   1.605 +	 * @param Item the item name from Calendars.*
   1.606 +	 * @param Value the value for the item
   1.607 +	 * @return success of this function
   1.608 +	 */
   1.609 +	private boolean setContentValueAsString(String Item, String Value) {
   1.610 +		boolean Result = false;
   1.611 +		
   1.612 +		if (this.ContentValues.containsKey(Item))
   1.613 +			this.ContentValues.remove(Item);
   1.614 +		this.ContentValues.put(Item, Value);
   1.615 +		
   1.616 +		return Result;
   1.617 +	}
   1.618 +	
   1.619 +	/**
   1.620 +	 * general access function to ContentValues
   1.621 +	 * @param Item the item name from Calendars.*
   1.622 +	 * @param Value the value for the item
   1.623 +	 * @return success of this function
   1.624 +	 */
   1.625 +	private boolean setContentValueAsInt(String Item, int Value) {
   1.626 +		boolean Result = false;
   1.627 +		
   1.628 +		if (this.ContentValues.containsKey(Item))
   1.629 +			this.ContentValues.remove(Item);
   1.630 +		this.ContentValues.put(Item, Value);
   1.631 +		
   1.632 +		return Result;
   1.633 +	}
   1.634 +	
   1.635 +	public ArrayList<Uri> getNotifyList() {
   1.636 +		return this.mNotifyList;
   1.637 +	}
   1.638 +
   1.639 +	public ArrayList<CalendarEvent> getCalendarEvents() {
   1.640 +		return this.mCalendarEvents;
   1.641 +	}
   1.642 +	
   1.643 +	public boolean readCalendarEvents(CaldavFacade facade) {
   1.644 +		boolean Result = false;
   1.645 +		
   1.646 +		try {
   1.647 +			this.mCalendarEvents = facade.getCalendarEvents(this);
   1.648 +			Result = true;
   1.649 +		} catch (ClientProtocolException e) {
   1.650 +			e.printStackTrace();
   1.651 +			Result = false;
   1.652 +		} catch (URISyntaxException e) {
   1.653 +			e.printStackTrace();
   1.654 +			Result = false;
   1.655 +		} catch (IOException e) {
   1.656 +			e.printStackTrace();
   1.657 +			Result = false;
   1.658 +		} catch (ParserConfigurationException e) {
   1.659 +			e.printStackTrace();
   1.660 +			Result = false;
   1.661 +		} catch (SAXException e) {
   1.662 +			e.printStackTrace();
   1.663 +			Result = false;
   1.664 +		}
   1.665 +		
   1.666 +		return Result;
   1.667 +	}
   1.668 +	
   1.669 +}

mercurial