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

changeset 8
ec8af0e3fbc2
parent 0
fb9019fb1bf7
     1.1 --- a/src/org/gege/caldavsyncadapter/caldav/entities/DavCalendar.java	Tue Feb 10 21:55:00 2015 +0100
     1.2 +++ b/src/org/gege/caldavsyncadapter/caldav/entities/DavCalendar.java	Tue Feb 10 22:40:00 2015 +0100
     1.3 @@ -1,6 +1,6 @@
     1.4  /**
     1.5   * Copyright (c) 2012-2013, Gerald Garcia, Timo Berger
     1.6 - * 
     1.7 + *
     1.8   * This file is part of Andoid Caldav Sync Adapter Free.
     1.9   *
    1.10   * Andoid Caldav Sync Adapter Free is free software: you can redistribute 
    1.11 @@ -16,27 +16,11 @@
    1.12   * You should have received a copy of the GNU General Public License
    1.13   * along with Andoid Caldav Sync Adapter Free.  
    1.14   * If not, see <http://www.gnu.org/licenses/>.
    1.15 - * 
    1.16 + *
    1.17   */
    1.18  
    1.19  package org.gege.caldavsyncadapter.caldav.entities;
    1.20  
    1.21 -import java.io.IOException;
    1.22 -import java.net.URI;
    1.23 -import java.net.URISyntaxException;
    1.24 -import java.util.ArrayList;
    1.25 -
    1.26 -import javax.xml.parsers.ParserConfigurationException;
    1.27 -
    1.28 -import org.apache.http.client.ClientProtocolException;
    1.29 -import org.gege.caldavsyncadapter.CalendarColors;
    1.30 -import org.gege.caldavsyncadapter.Event;
    1.31 -import org.gege.caldavsyncadapter.android.entities.AndroidEvent;
    1.32 -import org.gege.caldavsyncadapter.caldav.CaldavFacade;
    1.33 -import org.gege.caldavsyncadapter.syncadapter.SyncAdapter;
    1.34 -import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper;
    1.35 -import org.xml.sax.SAXException;
    1.36 -
    1.37  import android.accounts.Account;
    1.38  import android.content.ContentProviderClient;
    1.39  import android.content.ContentUris;
    1.40 @@ -49,618 +33,674 @@
    1.41  import android.provider.CalendarContract.Events;
    1.42  import android.util.Log;
    1.43  
    1.44 +import org.apache.http.client.ClientProtocolException;
    1.45 +import org.gege.caldavsyncadapter.CalendarColors;
    1.46 +import org.gege.caldavsyncadapter.Event;
    1.47 +import org.gege.caldavsyncadapter.android.entities.AndroidEvent;
    1.48 +import org.gege.caldavsyncadapter.caldav.CaldavFacade;
    1.49 +import org.gege.caldavsyncadapter.syncadapter.SyncAdapter;
    1.50 +import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper;
    1.51 +import org.xml.sax.SAXException;
    1.52 +
    1.53 +import java.io.IOException;
    1.54 +import java.net.URI;
    1.55 +import java.net.URISyntaxException;
    1.56 +import java.util.ArrayList;
    1.57 +import java.util.regex.Matcher;
    1.58 +import java.util.regex.Pattern;
    1.59 +
    1.60 +import javax.xml.parsers.ParserConfigurationException;
    1.61 +
    1.62  public class DavCalendar {
    1.63 -	public enum CalendarSource {
    1.64 -		undefined, Android, CalDAV
    1.65 -	}
    1.66 -		
    1.67 -	private static final String TAG = "Calendar";
    1.68 -		
    1.69 -	/**
    1.70 -	 * stores the CTAG of a calendar
    1.71 -	 */
    1.72 -	public static String CTAG = Calendars.CAL_SYNC1;
    1.73 -	
    1.74 -	/**
    1.75 -	 * stores the URI of a calendar
    1.76 -	 * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
    1.77 -	 */
    1.78 -	public static String URI = Calendars._SYNC_ID;
    1.79 -	
    1.80 -	public static String SERVERURL = Calendars.CAL_SYNC2;
    1.81 -	
    1.82 -	private String strCalendarColor = "";
    1.83 -	
    1.84 -	private ArrayList<Uri> mNotifyList = new ArrayList<Uri>(); 
    1.85 +    public enum CalendarSource {
    1.86 +        undefined, Android, CalDAV
    1.87 +    }
    1.88  
    1.89 -	/**
    1.90 -	 * the event transformed into ContentValues
    1.91 -	 */
    1.92 -	public ContentValues ContentValues = new ContentValues();
    1.93 -	
    1.94 -	private Account mAccount = null;
    1.95 -	private ContentProviderClient mProvider = null;
    1.96 -	
    1.97 -	public boolean foundServerSide = false;
    1.98 -	public boolean foundClientSide = false;
    1.99 -	public CalendarSource Source = CalendarSource.undefined;
   1.100 -	
   1.101 -	public String ServerUrl = "";
   1.102 -	
   1.103 -	private ArrayList<CalendarEvent> mCalendarEvents = new ArrayList<CalendarEvent>();
   1.104 -	
   1.105 -	private int mTagCounter = 1;
   1.106 -	
   1.107 -	/**
   1.108 -	 * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
   1.109 -	 */
   1.110 -	public URI getURI() {
   1.111 -		String strUri = this.getContentValueAsString(DavCalendar.URI);
   1.112 -		URI result = null;
   1.113 -		try {
   1.114 -			result = new URI(strUri);
   1.115 -		} catch (URISyntaxException e) {
   1.116 -			e.printStackTrace();
   1.117 -		}
   1.118 -		return result;
   1.119 -	}
   1.120 +    private static final String TAG = "Calendar";
   1.121  
   1.122 -	/**
   1.123 -	 * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
   1.124 -	 */
   1.125 -	public void setURI(URI uri) {
   1.126 -		this.setContentValueAsString(DavCalendar.URI, uri.toString());
   1.127 -	}
   1.128 +    /**
   1.129 +     * stores the CTAG of a calendar
   1.130 +     */
   1.131 +    public static String CTAG = Calendars.CAL_SYNC1;
   1.132  
   1.133 -	/**
   1.134 -	 * example: Cleartext Display Name 
   1.135 -	 */
   1.136 -	public String getCalendarDisplayName() {
   1.137 -		return this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME);
   1.138 -	}
   1.139 +    /**
   1.140 +     * stores the URI of a calendar
   1.141 +     * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
   1.142 +     */
   1.143 +    public static String URI = Calendars._SYNC_ID;
   1.144  
   1.145 -	/**
   1.146 -	 * example: Cleartext Display Name 
   1.147 -	 */
   1.148 -	public void setCalendarDisplayName(String displayName) {
   1.149 -		this.setContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME, displayName);
   1.150 -	}
   1.151 -	
   1.152 +    public static String SERVERURL = Calendars.CAL_SYNC2;
   1.153  
   1.154 -	/**
   1.155 -	 * example: 1143
   1.156 -	 */
   1.157 -	public void setCTag(String cTag, boolean Update) {
   1.158 -		this.setContentValueAsString(DavCalendar.CTAG, cTag);
   1.159 -		if (Update) {
   1.160 -			//serverCalendar.updateAndroidCalendar(androidCalendarUri, Calendar.CTAG, serverCalendar.getcTag());
   1.161 -			try {
   1.162 -				this.updateAndroidCalendar(this.getAndroidCalendarUri(), CTAG, cTag);
   1.163 -			} catch (RemoteException e) {
   1.164 -				e.printStackTrace();
   1.165 -			}
   1.166 -		}
   1.167 -	}
   1.168 -	
   1.169 -	/**
   1.170 -	 * example: 1143
   1.171 -	 */
   1.172 -	public String getcTag() {
   1.173 -		return this.getContentValueAsString(DavCalendar.CTAG);
   1.174 -	}
   1.175 -	
   1.176 -	/**
   1.177 -	 * example: #FFCCAA 
   1.178 -	 */
   1.179 -	public void setCalendarColorAsString(String color) {
   1.180 -		int maxlen = 6;
   1.181 -		
   1.182 -		this.strCalendarColor = color;
   1.183 -		if (!color.equals("")) {
   1.184 -			String strColor = color.replace("#", "");
   1.185 -			if (strColor.length() > maxlen)
   1.186 -				strColor = strColor.substring(0, maxlen);
   1.187 -			int intColor = Integer.parseInt(strColor, 16);
   1.188 -			this.setContentValueAsInt(Calendars.CALENDAR_COLOR, intColor);
   1.189 -		}
   1.190 -	}
   1.191 +    private String strCalendarColor = "";
   1.192  
   1.193 -	/**
   1.194 -	 * example: #FFCCAA 
   1.195 -	 */
   1.196 -	public String getCalendarColorAsString() {
   1.197 -		return this.strCalendarColor;
   1.198 -	}
   1.199 +    private ArrayList<Uri> mNotifyList = new ArrayList<Uri>();
   1.200  
   1.201 -	/**
   1.202 -	 * example 12345 
   1.203 -	 */
   1.204 -	public int getCalendarColor() {
   1.205 -		return this.getContentValueAsInt(Calendars.CALENDAR_COLOR);
   1.206 -	}
   1.207 -	
   1.208 -	/**
   1.209 -	 * example 12345 
   1.210 -	 */
   1.211 -	public void setCalendarColor(int color) {
   1.212 -		this.setContentValueAsInt(Calendars.CALENDAR_COLOR, color);
   1.213 -	}
   1.214 +    /**
   1.215 +     * the event transformed into ContentValues
   1.216 +     */
   1.217 +    public ContentValues ContentValues = new ContentValues();
   1.218  
   1.219 -	/**
   1.220 -	 * example: 
   1.221 -	 * 		should be: calendarname
   1.222 -	 * 		but is:    http://caldav.example.com/calendarserver.php/calendars/username/calendarname/
   1.223 -	 */
   1.224 -	public String getCalendarName() {
   1.225 -		return this.getContentValueAsString(Calendars.NAME);
   1.226 -	}
   1.227 +    private Account mAccount = null;
   1.228 +    private ContentProviderClient mProvider = null;
   1.229  
   1.230 -	/**
   1.231 -	 * example: 
   1.232 -	 * 		should be: calendarname
   1.233 -	 * 		but is:    http://caldav.example.com/calendarserver.php/calendars/username/calendarname/
   1.234 -	 */
   1.235 -	public void setCalendarName(String calendarName) {
   1.236 -		this.setContentValueAsString(Calendars.NAME, calendarName);
   1.237 -	}
   1.238 +    public boolean foundServerSide = false;
   1.239 +    public boolean foundClientSide = false;
   1.240 +    public CalendarSource Source = CalendarSource.undefined;
   1.241  
   1.242 -	/**
   1.243 -	 * example: 8
   1.244 -	 */
   1.245 -	public int getAndroidCalendarId() {
   1.246 -		return this.getContentValueAsInt(Calendars._ID);
   1.247 -	}
   1.248 +    public String ServerUrl = "";
   1.249  
   1.250 -	/**
   1.251 -	 * example: 8
   1.252 -	 */
   1.253 -	public void setAndroidCalendarId(int androidCalendarId) {
   1.254 -		this.setContentValueAsInt(Calendars._ID, androidCalendarId);
   1.255 -	}
   1.256 +    private ArrayList<CalendarEvent> mCalendarEvents = new ArrayList<CalendarEvent>();
   1.257  
   1.258 -	/**
   1.259 -	 * example: content://com.android.calendar/calendars/8
   1.260 -	 */
   1.261 -	public Uri getAndroidCalendarUri() {
   1.262 -		return ContentUris.withAppendedId(Calendars.CONTENT_URI, this.getAndroidCalendarId());
   1.263 -	}
   1.264 -	
   1.265 -	/**
   1.266 -	 * empty constructor
   1.267 -	 */
   1.268 -	public DavCalendar(CalendarSource source) {
   1.269 -		this.Source = source;
   1.270 -	}
   1.271 -	
   1.272 -	/**
   1.273 -	 * creates an new instance from a cursor
   1.274 -	 * @param cur must be a cursor from "ContentProviderClient" with Uri Calendars.CONTENT_URI
   1.275 -	 */
   1.276 -	public DavCalendar(Account account, ContentProviderClient provider, Cursor cur, CalendarSource source, String serverUrl) {
   1.277 -		this.mAccount = account;
   1.278 -		this.mProvider = provider;
   1.279 -		this.foundClientSide = true;
   1.280 -		this.Source = source;
   1.281 -		this.ServerUrl = serverUrl;
   1.282 +    private int mTagCounter = 1;
   1.283  
   1.284 -		String strSyncID = cur.getString(cur.getColumnIndex(Calendars._SYNC_ID));
   1.285 -		String strName = cur.getString(cur.getColumnIndex(Calendars.NAME));
   1.286 -		String strDisplayName = cur.getString(cur.getColumnIndex(Calendars.CALENDAR_DISPLAY_NAME));
   1.287 -		String strCTAG = cur.getString(cur.getColumnIndex(DavCalendar.CTAG));
   1.288 -		String strServerUrl = cur.getString(cur.getColumnIndex(DavCalendar.SERVERURL));
   1.289 -		int intAndroidCalendarId = cur.getInt(cur.getColumnIndex(Calendars._ID));
   1.290 +    /**
   1.291 +     * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
   1.292 +     */
   1.293 +    public URI getURI() {
   1.294 +        String strUri = this.getContentValueAsString(DavCalendar.URI);
   1.295 +        URI result = null;
   1.296 +        try {
   1.297 +            result = new URI(strUri);
   1.298 +        } catch (URISyntaxException e) {
   1.299 +            e.printStackTrace();
   1.300 +        }
   1.301 +        return result;
   1.302 +    }
   1.303  
   1.304 -		this.setCalendarName(strName);
   1.305 -		this.setCalendarDisplayName(strDisplayName);
   1.306 -		this.setCTag(strCTAG, false);
   1.307 -		this.setAndroidCalendarId(intAndroidCalendarId);
   1.308 -		
   1.309 -		if (strSyncID == null) {
   1.310 -			this.correctSyncID(strName);
   1.311 -			strSyncID = strName;
   1.312 -		}
   1.313 -		if (strServerUrl == null) {
   1.314 -			this.correctServerUrl(serverUrl);
   1.315 -		}
   1.316 -		URI uri = null;
   1.317 -		try {
   1.318 -			uri = new URI(strSyncID);
   1.319 -		} catch (URISyntaxException e) {
   1.320 -			e.printStackTrace();
   1.321 -		}
   1.322 -		this.setURI(uri);
   1.323 -	}
   1.324 -	
   1.325 -	/**
   1.326 -	 * checks a given list of android calendars for a specific android calendar.
   1.327 -	 * this calendar should be a server calendar as it is searched for.
   1.328 -	 * if the calendar is not found, it will be created.
   1.329 -	 * @param androidCalList the list of android calendars
   1.330 -	 * @param context
   1.331 -	 * @return the found android calendar or null of fails
   1.332 -	 * @throws RemoteException
   1.333 -	 */
   1.334 -	public Uri checkAndroidCalendarList(CalendarList androidCalList, android.content.Context context) throws RemoteException {
   1.335 -		Uri androidCalendarUri = null;
   1.336 -		boolean isCalendarExist = false;
   1.337 -		
   1.338 -		DavCalendar androidCalendar = androidCalList.getCalendarByURI(this.getURI());
   1.339 -		if (androidCalendar != null) {
   1.340 -			isCalendarExist = true;
   1.341 -			androidCalendar.foundServerSide = true;
   1.342 -		}
   1.343 -		
   1.344 +    /**
   1.345 +     * example: http://caldav.example.com/calendarserver.php/calendars/username/calendarname
   1.346 +     */
   1.347 +    public void setURI(URI uri) {
   1.348 +        this.setContentValueAsString(DavCalendar.URI, uri.toString());
   1.349 +    }
   1.350  
   1.351 -		if (!isCalendarExist) {
   1.352 -			DavCalendar newCal = this.createNewAndroidCalendar(this, androidCalList.getCalendarList().size(), context);
   1.353 -			if (newCal != null) {
   1.354 -				androidCalList.addCalendar(newCal);
   1.355 -				androidCalendarUri = newCal.getAndroidCalendarUri();
   1.356 -			}
   1.357 -		} else {
   1.358 -			androidCalendarUri = androidCalendar.getAndroidCalendarUri();
   1.359 -			if (!this.getCalendarColorAsString().equals("")) {
   1.360 -				//serverCalendar.updateCalendarColor(returnedCalendarUri, serverCalendar);
   1.361 -				this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_COLOR, this.getCalendarColor());
   1.362 -			}
   1.363 -			if ((this.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME)) && 
   1.364 -				(androidCalendar.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME))) {
   1.365 -				String serverDisplayName = this.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME);
   1.366 -				String clientDisplayName = androidCalendar.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME);
   1.367 -				if (!serverDisplayName.equals(clientDisplayName))
   1.368 -					this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_DISPLAY_NAME, serverDisplayName);
   1.369 -			}
   1.370 -		}
   1.371 -		
   1.372 -		return androidCalendarUri;
   1.373 -	}
   1.374 -	
   1.375 -	/**
   1.376 -	 * COMPAT: the calendar Uri was stored as calendar Name. this function updates the URI (_SYNC_ID)
   1.377 -	 * @param calendarUri the real calendarUri
   1.378 -	 * @return success of this function
   1.379 -	 */
   1.380 -	private boolean correctSyncID(String calendarUri) {
   1.381 -		boolean Result = false;
   1.382 -		Log.v(TAG, "correcting SyncID for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME));
   1.383 -			
   1.384 -		ContentValues mUpdateValues = new ContentValues();
   1.385 -		mUpdateValues.put(DavCalendar.URI, calendarUri);
   1.386 -		
   1.387 -		try {
   1.388 -			mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null);
   1.389 -			Result = true;
   1.390 -		} catch (RemoteException e) {
   1.391 -			e.printStackTrace();
   1.392 -		}
   1.393 -		
   1.394 -		return Result;
   1.395 -	}
   1.396 -	
   1.397 -	/**
   1.398 -	 * COMPAT: the serverurl (CAL_SYNC2) was not sored within a calendar. this fixes it. (see #98)
   1.399 -	 * @param serverUrl the current serverurl
   1.400 -	 * @return success of this function
   1.401 -	 */
   1.402 -	private boolean correctServerUrl(String serverUrl) {
   1.403 -		boolean Result = false;
   1.404 -		Log.v(TAG, "correcting ServerUrl for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME));
   1.405 -			
   1.406 -		ContentValues mUpdateValues = new ContentValues();
   1.407 -		mUpdateValues.put(DavCalendar.SERVERURL, serverUrl);
   1.408 -		
   1.409 -		try {
   1.410 -			mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null);
   1.411 -			Result = true;
   1.412 -		} catch (RemoteException e) {
   1.413 -			e.printStackTrace();
   1.414 -		}
   1.415 -		
   1.416 -		return Result;
   1.417 -	}
   1.418 -	
   1.419 -	/**
   1.420 -	 * creates a new androidCalendar
   1.421 -	 * @param serverCalendar
   1.422 -	 * @param index
   1.423 -	 * @param context
   1.424 -	 * @return the new androidCalendar or null if fails
   1.425 -	 */
   1.426 -	private DavCalendar createNewAndroidCalendar(DavCalendar serverCalendar, int index, android.content.Context context) {
   1.427 -		Uri newUri = null;
   1.428 -		DavCalendar Result = null;
   1.429 -		
   1.430 -		final ContentValues contentValues = new ContentValues();
   1.431 -		contentValues.put(DavCalendar.URI, serverCalendar.getURI().toString());
   1.432 -		contentValues.put(DavCalendar.SERVERURL, this.ServerUrl);
   1.433 +    /**
   1.434 +     * example: Cleartext Display Name
   1.435 +     */
   1.436 +    public String getCalendarDisplayName() {
   1.437 +        return this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME);
   1.438 +    }
   1.439  
   1.440 -		contentValues.put(Calendars.VISIBLE, 1);
   1.441 -		contentValues.put(Calendars.CALENDAR_DISPLAY_NAME, serverCalendar.getCalendarDisplayName());
   1.442 -		contentValues.put(Calendars.ACCOUNT_NAME, mAccount.name);
   1.443 -		contentValues.put(Calendars.ACCOUNT_TYPE, mAccount.type);
   1.444 -		contentValues.put(Calendars.OWNER_ACCOUNT, mAccount.name);
   1.445 -		contentValues.put(Calendars.SYNC_EVENTS, 1);
   1.446 -		contentValues.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
   1.447 -		
   1.448 -		if (!serverCalendar.getCalendarColorAsString().equals("")) {
   1.449 -			contentValues.put(Calendars.CALENDAR_COLOR, serverCalendar.getCalendarColor());
   1.450 -		} else {
   1.451 -			// find a color
   1.452 -			//int index = mList.size();
   1.453 -			index = index % CalendarColors.colors.length;
   1.454 -			contentValues.put(Calendars.CALENDAR_COLOR, CalendarColors.colors[index]);
   1.455 -		}
   1.456 +    /**
   1.457 +     * example: Cleartext Display Name
   1.458 +     */
   1.459 +    public void setCalendarDisplayName(String displayName) {
   1.460 +        this.setContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME, displayName);
   1.461 +    }
   1.462  
   1.463 -		try {
   1.464 -			newUri = mProvider.insert(asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type), contentValues);
   1.465 -		} catch (RemoteException e) {
   1.466 -			e.printStackTrace();
   1.467 -		}
   1.468  
   1.469 -		// it is possible that this calendar already exists but the provider failed to find it within isCalendarExist()
   1.470 -		// the adapter would try to create a new calendar but the provider fails again to create a new calendar.
   1.471 -		if (newUri != null) {
   1.472 -			long newCalendarId = ContentUris.parseId(newUri);
   1.473 +    /**
   1.474 +     * example: 1143
   1.475 +     */
   1.476 +    public void setCTag(String cTag, boolean Update) {
   1.477 +        this.setContentValueAsString(DavCalendar.CTAG, cTag);
   1.478 +        if (Update) {
   1.479 +            //serverCalendar.updateAndroidCalendar(androidCalendarUri, Calendar.CTAG, serverCalendar.getcTag());
   1.480 +            try {
   1.481 +                this.updateAndroidCalendar(this.getAndroidCalendarUri(), CTAG, cTag);
   1.482 +            } catch (RemoteException e) {
   1.483 +                e.printStackTrace();
   1.484 +            }
   1.485 +        }
   1.486 +    }
   1.487  
   1.488 -			Cursor cur = null;
   1.489 -			Uri uri = Calendars.CONTENT_URI;   
   1.490 -			String selection = "(" + Calendars._ID +  " = ?)";
   1.491 -			String[] selectionArgs = new String[] {String.valueOf(newCalendarId)}; 
   1.492 +    /**
   1.493 +     * example: 1143
   1.494 +     */
   1.495 +    public String getcTag() {
   1.496 +        return this.getContentValueAsString(DavCalendar.CTAG);
   1.497 +    }
   1.498  
   1.499 -			// Submit the query and get a Cursor object back. 
   1.500 -			try {
   1.501 -				cur = mProvider.query(uri, null, selection, selectionArgs, null);
   1.502 -			} catch (RemoteException e) {
   1.503 -				e.printStackTrace();
   1.504 -			}
   1.505 -			
   1.506 -			if (cur != null) {
   1.507 -				while (cur.moveToNext()) {
   1.508 -					Result = new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl);
   1.509 -					Result.foundServerSide = true;
   1.510 -				}
   1.511 -				cur.close();
   1.512 -				//if (Result != null)
   1.513 -				//	this.mList.add(Result);
   1.514 -			}
   1.515 -			Log.i(TAG, "New calendar created : URI=" + Result.getAndroidCalendarUri());
   1.516 -			NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "new calendar found: " + Result.getCalendarDisplayName());
   1.517 -			mNotifyList.add(Result.getAndroidCalendarUri());
   1.518 -		}
   1.519 -		
   1.520 -		return Result;
   1.521 -	}
   1.522 -	
   1.523 -	/**
   1.524 -	 * there is no corresponding calendar on server side. time to delete this calendar on android side.
   1.525 -	 * @return 
   1.526 -	 */
   1.527 -	public boolean deleteAndroidCalendar() {
   1.528 -		boolean Result = false;
   1.529 -		
   1.530 -		String mSelectionClause = "(" + Calendars._ID + " = ?)";
   1.531 -		int calendarId  = this.getAndroidCalendarId();
   1.532 -		String[] mSelectionArgs = {Long.toString(calendarId)};
   1.533 -		
   1.534 -		int CountDeleted = 0;
   1.535 -		try {
   1.536 -			CountDeleted = mProvider.delete(this.SyncAdapter(), mSelectionClause, mSelectionArgs);
   1.537 -			Log.i(TAG,"Calendar deleted: " + String.valueOf(calendarId));
   1.538 -			this.mNotifyList.add(this.getAndroidCalendarUri());
   1.539 -			Result = true;
   1.540 -		} catch (RemoteException e) {
   1.541 -			e.printStackTrace();
   1.542 -		}	
   1.543 -		Log.d(TAG, "Android Calendars deleted: " + Integer.toString(CountDeleted));
   1.544 -		
   1.545 -		return Result;
   1.546 -	}
   1.547 +    /**
   1.548 +     * example: #FFCCAA
   1.549 +     */
   1.550 +    public void setCalendarColorAsString(String color) {
   1.551 +        int maxlen = 6;
   1.552  
   1.553 -	/**
   1.554 -	 * updates the android calendar
   1.555 -	 * @param calendarUri the uri of the androidCalendar
   1.556 -	 * @param target must be from android.provider.CalendarContract.Calendars
   1.557 -	 * @param value the new value for the target
   1.558 -	 * @throws RemoteException
   1.559 -	 */
   1.560 -	private void updateAndroidCalendar(Uri calendarUri, String target, int value) throws RemoteException {
   1.561 -		ContentValues mUpdateValues = new ContentValues();
   1.562 -		mUpdateValues.put(target, value);
   1.563 -		
   1.564 -		mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null);
   1.565 -	}
   1.566 +        this.strCalendarColor = color;
   1.567 +        if (!color.equals("")) {
   1.568 +            String strColor = color.replace("#", "");
   1.569 +            if (strColor.length() > maxlen)
   1.570 +                strColor = strColor.substring(0, maxlen);
   1.571 +            int intColor = Integer.parseInt(strColor, 16);
   1.572 +            this.setContentValueAsInt(Calendars.CALENDAR_COLOR, intColor);
   1.573 +        }
   1.574 +    }
   1.575  
   1.576 -	/**
   1.577 -	 * updates the android calendar
   1.578 -	 * @param calendarUri the uri of the androidCalendar
   1.579 -	 * @param target must be from android.provider.CalendarContract.Calendars
   1.580 -	 * @param value the new value for the target
   1.581 -	 * @throws RemoteException
   1.582 -	 */
   1.583 -	private void updateAndroidCalendar(Uri calendarUri, String target, String value) throws RemoteException {
   1.584 -		ContentValues mUpdateValues = new ContentValues();
   1.585 -		mUpdateValues.put(target, value);
   1.586 -		
   1.587 -		mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null);
   1.588 -	}
   1.589 -	
   1.590 -	/**
   1.591 -	 * marks the android event as already handled
   1.592 -	 * @return
   1.593 -	 * @see AndroidEvent#cInternalTag
   1.594 -	 * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
   1.595 -	 * @throws RemoteException
   1.596 -	 */
   1.597 -	public boolean tagAndroidEvent(AndroidEvent androidEvent) throws RemoteException {
   1.598 -		boolean Result = false;
   1.599 -		
   1.600 -		ContentValues values = new ContentValues();
   1.601 -		//values.put(Event.INTERNALTAG, 1);
   1.602 -		values.put(Event.INTERNALTAG, mTagCounter);
   1.603 -		//values.put(Event.INTERNALTAG, String.valueOf(mTagCounter));
   1.604 -		
   1.605 -		int RowCount = this.mProvider.update(asSyncAdapter(androidEvent.getUri(), this.mAccount.name, this.mAccount.type), values, null, null);
   1.606 -		//Log.v(TAG,"event tag nr: " + String.valueOf(mTagCounter));
   1.607 -		//Log.v(TAG,"Rows updated: " + String.valueOf(RowCount));
   1.608 -		
   1.609 -		if (RowCount == 1) {
   1.610 -			Result = true;
   1.611 -			mTagCounter += 1;
   1.612 -		} else {
   1.613 -			Log.v(TAG,"EVENT NOT TAGGED!");
   1.614 -		}
   1.615 -		
   1.616 -		return Result;
   1.617 -	} 
   1.618 -	
   1.619 -	/**
   1.620 -	 * removes the tag of all android events
   1.621 -	 * @return
   1.622 -	 * @see AndroidEvent#cInternalTag
   1.623 -	 * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
   1.624 -	 * @throws RemoteException
   1.625 -	 */
   1.626 -	public int untagAndroidEvents() throws RemoteException {
   1.627 -		int RowCount = 0;
   1.628 -		int Steps = 100;
   1.629 -		ContentValues values = new ContentValues();
   1.630 -		values.put(Event.INTERNALTAG, 0);
   1.631 +    /**
   1.632 +     * example: #FFCCAA
   1.633 +     */
   1.634 +    public String getCalendarColorAsString() {
   1.635 +        return this.strCalendarColor;
   1.636 +    }
   1.637  
   1.638 -		for (int i=1; i < this.mTagCounter; i = i + Steps) {
   1.639 -			String mSelectionClause = "(CAST(" + Event.INTERNALTAG +  " AS INT) >= ?) AND (CAST(" + Event.INTERNALTAG +  " AS INT) < ?) AND (" + Events.CALENDAR_ID + " = ?)";
   1.640 -			String[] mSelectionArgs = {String.valueOf(i), String.valueOf(i + Steps), Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
   1.641 -			RowCount += this.mProvider.update(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), values, mSelectionClause, mSelectionArgs);
   1.642 -		}
   1.643 -		/*String mSelectionClause = "(" + Event.INTERNALTAG +  " > ?) AND (" + Events.CALENDAR_ID + " = ?)";
   1.644 -		String[] mSelectionArgs = {"0", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
   1.645 +    /**
   1.646 +     * example 12345
   1.647 +     */
   1.648 +    public int getCalendarColor() {
   1.649 +        return this.getContentValueAsInt(Calendars.CALENDAR_COLOR);
   1.650 +    }
   1.651 +
   1.652 +    /**
   1.653 +     * example 12345
   1.654 +     */
   1.655 +    public void setCalendarColor(int color) {
   1.656 +        this.setContentValueAsInt(Calendars.CALENDAR_COLOR, color);
   1.657 +    }
   1.658 +
   1.659 +    /**
   1.660 +     * example:
   1.661 +     * should be: calendarname
   1.662 +     * but is:    http://caldav.example.com/calendarserver.php/calendars/username/calendarname/
   1.663 +     */
   1.664 +    public String getCalendarName() {
   1.665 +        return this.getContentValueAsString(Calendars.NAME);
   1.666 +    }
   1.667 +
   1.668 +    /**
   1.669 +     * example:
   1.670 +     * should be: calendarname
   1.671 +     * but is:    http://caldav.example.com/calendarserver.php/calendars/username/calendarname/
   1.672 +     */
   1.673 +    public void setCalendarName(String calendarName) {
   1.674 +        this.setContentValueAsString(Calendars.NAME, calendarName);
   1.675 +    }
   1.676 +
   1.677 +    /**
   1.678 +     * example: 8
   1.679 +     */
   1.680 +    public int getAndroidCalendarId() {
   1.681 +        return this.getContentValueAsInt(Calendars._ID);
   1.682 +    }
   1.683 +
   1.684 +    /**
   1.685 +     * example: 8
   1.686 +     */
   1.687 +    public void setAndroidCalendarId(int androidCalendarId) {
   1.688 +        this.setContentValueAsInt(Calendars._ID, androidCalendarId);
   1.689 +    }
   1.690 +
   1.691 +    /**
   1.692 +     * example: content://com.android.calendar/calendars/8
   1.693 +     */
   1.694 +    public Uri getAndroidCalendarUri() {
   1.695 +        return ContentUris.withAppendedId(Calendars.CONTENT_URI, this.getAndroidCalendarId());
   1.696 +    }
   1.697 +
   1.698 +    /**
   1.699 +     * empty constructor
   1.700 +     */
   1.701 +    public DavCalendar(CalendarSource source) {
   1.702 +        this.Source = source;
   1.703 +    }
   1.704 +
   1.705 +    /**
   1.706 +     * creates an new instance from a cursor
   1.707 +     *
   1.708 +     * @param cur must be a cursor from "ContentProviderClient" with Uri Calendars.CONTENT_URI
   1.709 +     */
   1.710 +    public DavCalendar(Account account, ContentProviderClient provider, Cursor cur, CalendarSource source, String serverUrl) {
   1.711 +        this.mAccount = account;
   1.712 +        this.mProvider = provider;
   1.713 +        this.foundClientSide = true;
   1.714 +        this.Source = source;
   1.715 +        this.ServerUrl = serverUrl;
   1.716 +
   1.717 +        String strSyncID = cur.getString(cur.getColumnIndex(Calendars._SYNC_ID));
   1.718 +        String strName = cur.getString(cur.getColumnIndex(Calendars.NAME));
   1.719 +        String strDisplayName = cur.getString(cur.getColumnIndex(Calendars.CALENDAR_DISPLAY_NAME));
   1.720 +        String strCTAG = cur.getString(cur.getColumnIndex(DavCalendar.CTAG));
   1.721 +        String strServerUrl = cur.getString(cur.getColumnIndex(DavCalendar.SERVERURL));
   1.722 +        String strCalendarColor = cur.getString(cur.getColumnIndex(Calendars.CALENDAR_COLOR));
   1.723 +        int intAndroidCalendarId = cur.getInt(cur.getColumnIndex(Calendars._ID));
   1.724 +
   1.725 +        this.setCalendarName(strName);
   1.726 +        this.setCalendarDisplayName(strDisplayName);
   1.727 +        this.setCTag(strCTAG, false);
   1.728 +        this.setAndroidCalendarId(intAndroidCalendarId);
   1.729 +        this.setCalendarColor(Integer.parseInt(strCalendarColor));
   1.730 +
   1.731 +        if (strSyncID == null) {
   1.732 +            this.correctSyncID(strName);
   1.733 +            strSyncID = strName;
   1.734 +        }
   1.735 +        if (strServerUrl == null) {
   1.736 +            this.correctServerUrl(serverUrl);
   1.737 +        }
   1.738 +        URI uri = null;
   1.739 +        try {
   1.740 +            uri = new URI(strSyncID);
   1.741 +        } catch (URISyntaxException e) {
   1.742 +            e.printStackTrace();
   1.743 +        }
   1.744 +        this.setURI(uri);
   1.745 +    }
   1.746 +
   1.747 +    /**
   1.748 +     * checks a given list of android calendars for a specific android calendar.
   1.749 +     * this calendar should be a server calendar as it is searched for.
   1.750 +     * if the calendar is not found, it will be created.
   1.751 +     *
   1.752 +     * @param androidCalList the list of android calendars
   1.753 +     * @param context
   1.754 +     * @return the found android calendar or null of fails
   1.755 +     * @throws RemoteException
   1.756 +     */
   1.757 +    public Uri checkAndroidCalendarList(CalendarList androidCalList, android.content.Context context) throws RemoteException {
   1.758 +        Uri androidCalendarUri = null;
   1.759 +        boolean isCalendarExist = false;
   1.760 +
   1.761 +        DavCalendar androidCalendar = androidCalList.getCalendarByURI(this.getURI());
   1.762 +        if (androidCalendar != null) {
   1.763 +            isCalendarExist = true;
   1.764 +            androidCalendar.foundServerSide = true;
   1.765 +        }
   1.766 +
   1.767 +
   1.768 +        if (!isCalendarExist) {
   1.769 +            DavCalendar newCal = this.createNewAndroidCalendar(this, androidCalList.getCalendarList()
   1.770 +                    .size(), context);
   1.771 +            if (newCal != null) {
   1.772 +                androidCalList.addCalendar(newCal);
   1.773 +                androidCalendarUri = newCal.getAndroidCalendarUri();
   1.774 +            }
   1.775 +        } else {
   1.776 +            androidCalendarUri = androidCalendar.getAndroidCalendarUri();
   1.777 +            if (!this.getCalendarColorAsString().equals("")) {
   1.778 +                this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_COLOR, getColorAsHex(this.getCalendarColorAsString()));
   1.779 +            }
   1.780 +            if ((this.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME)) &&
   1.781 +                    (androidCalendar.ContentValues.containsKey(Calendars.CALENDAR_DISPLAY_NAME))) {
   1.782 +                String serverDisplayName = this.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME);
   1.783 +                String clientDisplayName = androidCalendar.ContentValues.getAsString(Calendars.CALENDAR_DISPLAY_NAME);
   1.784 +                if (!serverDisplayName.equals(clientDisplayName))
   1.785 +                    this.updateAndroidCalendar(androidCalendarUri, Calendars.CALENDAR_DISPLAY_NAME, serverDisplayName);
   1.786 +            }
   1.787 +        }
   1.788 +
   1.789 +        return androidCalendarUri;
   1.790 +    }
   1.791 +
   1.792 +    /**
   1.793 +     * COMPAT: the calendar Uri was stored as calendar Name. this function updates the URI (_SYNC_ID)
   1.794 +     *
   1.795 +     * @param calendarUri the real calendarUri
   1.796 +     * @return success of this function
   1.797 +     */
   1.798 +    private boolean correctSyncID(String calendarUri) {
   1.799 +        boolean Result = false;
   1.800 +        Log.v(TAG, "correcting SyncID for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME));
   1.801 +
   1.802 +        ContentValues mUpdateValues = new ContentValues();
   1.803 +        mUpdateValues.put(DavCalendar.URI, calendarUri);
   1.804 +
   1.805 +        try {
   1.806 +            mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null);
   1.807 +            Result = true;
   1.808 +        } catch (RemoteException e) {
   1.809 +            e.printStackTrace();
   1.810 +        }
   1.811 +
   1.812 +        return Result;
   1.813 +    }
   1.814 +
   1.815 +    /**
   1.816 +     * COMPAT: the serverurl (CAL_SYNC2) was not sored within a calendar. this fixes it. (see #98)
   1.817 +     *
   1.818 +     * @param serverUrl the current serverurl
   1.819 +     * @return success of this function
   1.820 +     */
   1.821 +    private boolean correctServerUrl(String serverUrl) {
   1.822 +        boolean Result = false;
   1.823 +        Log.v(TAG, "correcting ServerUrl for calendar:" + this.getContentValueAsString(Calendars.CALENDAR_DISPLAY_NAME));
   1.824 +
   1.825 +        ContentValues mUpdateValues = new ContentValues();
   1.826 +        mUpdateValues.put(DavCalendar.SERVERURL, serverUrl);
   1.827 +
   1.828 +        try {
   1.829 +            mProvider.update(this.SyncAdapterCalendar(), mUpdateValues, null, null);
   1.830 +            Result = true;
   1.831 +        } catch (RemoteException e) {
   1.832 +            e.printStackTrace();
   1.833 +        }
   1.834 +
   1.835 +        return Result;
   1.836 +    }
   1.837 +
   1.838 +    /**
   1.839 +     * creates a new androidCalendar
   1.840 +     *
   1.841 +     * @param serverCalendar
   1.842 +     * @param index
   1.843 +     * @param context
   1.844 +     * @return the new androidCalendar or null if fails
   1.845 +     */
   1.846 +    private DavCalendar createNewAndroidCalendar(DavCalendar serverCalendar, int index, android.content.Context context) {
   1.847 +        Uri newUri = null;
   1.848 +        DavCalendar Result = null;
   1.849 +
   1.850 +        final ContentValues contentValues = new ContentValues();
   1.851 +        contentValues.put(DavCalendar.URI, serverCalendar.getURI().toString());
   1.852 +        contentValues.put(DavCalendar.SERVERURL, this.ServerUrl);
   1.853 +
   1.854 +        contentValues.put(Calendars.VISIBLE, 1);
   1.855 +        contentValues.put(Calendars.CALENDAR_DISPLAY_NAME, serverCalendar.getCalendarDisplayName());
   1.856 +        contentValues.put(Calendars.ACCOUNT_NAME, mAccount.name);
   1.857 +        contentValues.put(Calendars.ACCOUNT_TYPE, mAccount.type);
   1.858 +        contentValues.put(Calendars.OWNER_ACCOUNT, mAccount.name);
   1.859 +        contentValues.put(Calendars.SYNC_EVENTS, 1);
   1.860 +        contentValues.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
   1.861 +
   1.862 +        String calendarColorAsString = serverCalendar.getCalendarColorAsString();
   1.863 +        if (!calendarColorAsString.isEmpty()) {
   1.864 +            int color = getColorAsHex(calendarColorAsString);
   1.865 +            contentValues.put(Calendars.CALENDAR_COLOR, color);
   1.866 +        } else {
   1.867 +            // find a color
   1.868 +            //int index = mList.size();
   1.869 +            index = index % CalendarColors.colors.length;
   1.870 +            contentValues.put(Calendars.CALENDAR_COLOR, CalendarColors.colors[2]);
   1.871 +        }
   1.872 +
   1.873 +        try {
   1.874 +            newUri = mProvider.insert(asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type), contentValues);
   1.875 +        } catch (RemoteException e) {
   1.876 +            e.printStackTrace();
   1.877 +        }
   1.878 +
   1.879 +        // it is possible that this calendar already exists but the provider failed to find it within isCalendarExist()
   1.880 +        // the adapter would try to create a new calendar but the provider fails again to create a new calendar.
   1.881 +        if (newUri != null) {
   1.882 +            long newCalendarId = ContentUris.parseId(newUri);
   1.883 +
   1.884 +            Cursor cur = null;
   1.885 +            Uri uri = Calendars.CONTENT_URI;
   1.886 +            String selection = "(" + Calendars._ID + " = ?)";
   1.887 +            String[] selectionArgs = new String[]{String.valueOf(newCalendarId)};
   1.888 +
   1.889 +            // Submit the query and get a Cursor object back.
   1.890 +            try {
   1.891 +                cur = mProvider.query(uri, null, selection, selectionArgs, null);
   1.892 +            } catch (RemoteException e) {
   1.893 +                e.printStackTrace();
   1.894 +            }
   1.895 +
   1.896 +            if (cur != null) {
   1.897 +                while (cur.moveToNext()) {
   1.898 +                    Result = new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl);
   1.899 +                    Result.foundServerSide = true;
   1.900 +                }
   1.901 +                cur.close();
   1.902 +                //if (Result != null)
   1.903 +                //	this.mList.add(Result);
   1.904 +            }
   1.905 +            Log.i(TAG, "New calendar created : URI=" + Result.getAndroidCalendarUri());
   1.906 +            NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "new calendar found: " + Result
   1.907 +                    .getCalendarDisplayName());
   1.908 +            mNotifyList.add(Result.getAndroidCalendarUri());
   1.909 +        }
   1.910 +
   1.911 +        return Result;
   1.912 +    }
   1.913 +
   1.914 +    private int getColorAsHex(String calendarColorAsString) {
   1.915 +        int color = 0x0000FF00;
   1.916 +        Pattern p = Pattern.compile("#?(\\p{XDigit}{6})(\\p{XDigit}{2})?");
   1.917 +        Matcher m = p.matcher(calendarColorAsString);
   1.918 +        if (m.find()) {
   1.919 +            int color_rgb = Integer.parseInt(m.group(1), 16);
   1.920 +            int color_alpha = m.group(2) != null ? (Integer.parseInt(m.group(2), 16) & 0xFF) : 0xFF;
   1.921 +            color = (color_alpha << 24) | color_rgb;
   1.922 +        }
   1.923 +        return color;
   1.924 +    }
   1.925 +
   1.926 +    /**
   1.927 +     * there is no corresponding calendar on server side. time to delete this calendar on android side.
   1.928 +     *
   1.929 +     * @return
   1.930 +     */
   1.931 +    public boolean deleteAndroidCalendar() {
   1.932 +        boolean Result = false;
   1.933 +
   1.934 +        String mSelectionClause = "(" + Calendars._ID + " = ?)";
   1.935 +        int calendarId = this.getAndroidCalendarId();
   1.936 +        String[] mSelectionArgs = {Long.toString(calendarId)};
   1.937 +
   1.938 +        int CountDeleted = 0;
   1.939 +        try {
   1.940 +            CountDeleted = mProvider.delete(this.SyncAdapter(), mSelectionClause, mSelectionArgs);
   1.941 +            Log.i(TAG, "Calendar deleted: " + String.valueOf(calendarId));
   1.942 +            this.mNotifyList.add(this.getAndroidCalendarUri());
   1.943 +            Result = true;
   1.944 +        } catch (RemoteException e) {
   1.945 +            e.printStackTrace();
   1.946 +        }
   1.947 +        Log.d(TAG, "Android Calendars deleted: " + Integer.toString(CountDeleted));
   1.948 +
   1.949 +        return Result;
   1.950 +    }
   1.951 +
   1.952 +    /**
   1.953 +     * updates the android calendar
   1.954 +     *
   1.955 +     * @param calendarUri the uri of the androidCalendar
   1.956 +     * @param target      must be from android.provider.CalendarContract.Calendars
   1.957 +     * @param value       the new value for the target
   1.958 +     * @throws RemoteException
   1.959 +     */
   1.960 +    private void updateAndroidCalendar(Uri calendarUri, String target, int value) throws RemoteException {
   1.961 +        ContentValues mUpdateValues = new ContentValues();
   1.962 +        mUpdateValues.put(target, value);
   1.963 +
   1.964 +        mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null);
   1.965 +    }
   1.966 +
   1.967 +    /**
   1.968 +     * updates the android calendar
   1.969 +     *
   1.970 +     * @param calendarUri the uri of the androidCalendar
   1.971 +     * @param target      must be from android.provider.CalendarContract.Calendars
   1.972 +     * @param value       the new value for the target
   1.973 +     * @throws RemoteException
   1.974 +     */
   1.975 +    private void updateAndroidCalendar(Uri calendarUri, String target, String value) throws RemoteException {
   1.976 +        ContentValues mUpdateValues = new ContentValues();
   1.977 +        mUpdateValues.put(target, value);
   1.978 +
   1.979 +        mProvider.update(asSyncAdapter(calendarUri, mAccount.name, mAccount.type), mUpdateValues, null, null);
   1.980 +    }
   1.981 +
   1.982 +    /**
   1.983 +     * marks the android event as already handled
   1.984 +     *
   1.985 +     * @return
   1.986 +     * @throws RemoteException
   1.987 +     * @see AndroidEvent#cInternalTag
   1.988 +     * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
   1.989 +     */
   1.990 +    public boolean tagAndroidEvent(AndroidEvent androidEvent) throws RemoteException {
   1.991 +        boolean Result = false;
   1.992 +
   1.993 +        ContentValues values = new ContentValues();
   1.994 +        //values.put(Event.INTERNALTAG, 1);
   1.995 +        values.put(Event.INTERNALTAG, mTagCounter);
   1.996 +        //values.put(Event.INTERNALTAG, String.valueOf(mTagCounter));
   1.997 +
   1.998 +        int RowCount = this.mProvider.update(asSyncAdapter(androidEvent.getUri(), this.mAccount.name, this.mAccount.type), values, null, null);
   1.999 +        //Log.v(TAG,"event tag nr: " + String.valueOf(mTagCounter));
  1.1000 +        //Log.v(TAG,"Rows updated: " + String.valueOf(RowCount));
  1.1001 +
  1.1002 +        if (RowCount == 1) {
  1.1003 +            Result = true;
  1.1004 +            mTagCounter += 1;
  1.1005 +        } else {
  1.1006 +            Log.v(TAG, "EVENT NOT TAGGED!");
  1.1007 +        }
  1.1008 +
  1.1009 +        return Result;
  1.1010 +    }
  1.1011 +
  1.1012 +    /**
  1.1013 +     * removes the tag of all android events
  1.1014 +     *
  1.1015 +     * @return
  1.1016 +     * @throws RemoteException
  1.1017 +     * @see AndroidEvent#cInternalTag
  1.1018 +     * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
  1.1019 +     */
  1.1020 +    public int untagAndroidEvents() throws RemoteException {
  1.1021 +        int RowCount = 0;
  1.1022 +        int Steps = 100;
  1.1023 +        ContentValues values = new ContentValues();
  1.1024 +        values.put(Event.INTERNALTAG, 0);
  1.1025 +
  1.1026 +        for (int i = 1; i < this.mTagCounter; i = i + Steps) {
  1.1027 +            String mSelectionClause = "(CAST(" + Event.INTERNALTAG + " AS INT) >= ?) AND (CAST(" + Event.INTERNALTAG + " AS INT) < ?) AND (" + Events.CALENDAR_ID + " = ?)";
  1.1028 +            String[] mSelectionArgs = {String.valueOf(i), String.valueOf(i + Steps), Long.toString(ContentUris
  1.1029 +                    .parseId(this.getAndroidCalendarUri()))};
  1.1030 +            RowCount += this.mProvider.update(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), values, mSelectionClause, mSelectionArgs);
  1.1031 +        }
  1.1032 +        /*String mSelectionClause = "(" + Event.INTERNALTAG +  " > ?) AND (" + Events.CALENDAR_ID + " = ?)";
  1.1033 +        String[] mSelectionArgs = {"0", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
  1.1034  		RowCount += this.mProvider.update(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), values, mSelectionClause, mSelectionArgs);*/
  1.1035 -		
  1.1036 -		//Log.d(TAG, "Rows reseted: " + RowCount.toString());
  1.1037 -		return RowCount;
  1.1038 -	}
  1.1039 -	/**
  1.1040 -	 * Events not being tagged are for deletion 
  1.1041 -	 * @return
  1.1042 -	 * @see AndroidEvent#cInternalTag
  1.1043 -	 * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
  1.1044 -	 * @throws RemoteException
  1.1045 -	 */
  1.1046 -	public int deleteUntaggedEvents() throws RemoteException {
  1.1047 -		String mSelectionClause = "(" + Event.INTERNALTAG +  " < ?) AND (" + Events.CALENDAR_ID + " = ?)";
  1.1048 -		String[] mSelectionArgs = {"1", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
  1.1049 -		
  1.1050 -		int CountDeleted = this.mProvider.delete(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), mSelectionClause, mSelectionArgs);	
  1.1051 -		//Log.d(TAG, "Rows deleted: " + CountDeleted.toString());
  1.1052 -		return CountDeleted;
  1.1053 -	}
  1.1054 -	
  1.1055 -	private Uri SyncAdapterCalendar() {
  1.1056 -		return asSyncAdapter(this.getAndroidCalendarUri(), mAccount.name, mAccount.type);
  1.1057 -	}
  1.1058 -	private Uri SyncAdapter() {
  1.1059 -		return asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type);
  1.1060 -	}
  1.1061 -	private static Uri asSyncAdapter(Uri uri, String account, String accountType) {
  1.1062 -	    return uri.buildUpon()
  1.1063 -	        .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
  1.1064 -	        .appendQueryParameter(Calendars.ACCOUNT_NAME, account)
  1.1065 -	        .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
  1.1066 -	}
  1.1067 -	
  1.1068 -	public void setAccount(Account account) {
  1.1069 -		this.mAccount = account;
  1.1070 -	}
  1.1071 -	public void setProvider(ContentProviderClient provider) {
  1.1072 -		this.mProvider = provider;
  1.1073 -	}
  1.1074 -	
  1.1075 -	/**
  1.1076 -	 * general access function to ContentValues
  1.1077 -	 * @param Item the item name from Calendars.*
  1.1078 -	 * @return the value for the item
  1.1079 -	 */
  1.1080 -	private String getContentValueAsString(String Item) {
  1.1081 -		String Result = "";
  1.1082 -		if (this.ContentValues.containsKey(Item))
  1.1083 -			Result = this.ContentValues.getAsString(Item);
  1.1084 -		return Result;
  1.1085 -	}
  1.1086 -	/**
  1.1087 -	 * general access function to ContentValues
  1.1088 -	 * @param Item the item name from Calendars.*
  1.1089 -	 * @return the value for the item
  1.1090 -	 */
  1.1091 -	private int getContentValueAsInt(String Item) {
  1.1092 -		int Result = 0;
  1.1093 -		if (this.ContentValues.containsKey(Item))
  1.1094 -			Result = this.ContentValues.getAsInteger(Item);
  1.1095 -		return Result;
  1.1096 -	}
  1.1097 -	
  1.1098 -	/**
  1.1099 -	 * general access function to ContentValues
  1.1100 -	 * @param Item the item name from Calendars.*
  1.1101 -	 * @param Value the value for the item
  1.1102 -	 * @return success of this function
  1.1103 -	 */
  1.1104 -	private boolean setContentValueAsString(String Item, String Value) {
  1.1105 -		boolean Result = false;
  1.1106 -		
  1.1107 -		if (this.ContentValues.containsKey(Item))
  1.1108 -			this.ContentValues.remove(Item);
  1.1109 -		this.ContentValues.put(Item, Value);
  1.1110 -		
  1.1111 -		return Result;
  1.1112 -	}
  1.1113 -	
  1.1114 -	/**
  1.1115 -	 * general access function to ContentValues
  1.1116 -	 * @param Item the item name from Calendars.*
  1.1117 -	 * @param Value the value for the item
  1.1118 -	 * @return success of this function
  1.1119 -	 */
  1.1120 -	private boolean setContentValueAsInt(String Item, int Value) {
  1.1121 -		boolean Result = false;
  1.1122 -		
  1.1123 -		if (this.ContentValues.containsKey(Item))
  1.1124 -			this.ContentValues.remove(Item);
  1.1125 -		this.ContentValues.put(Item, Value);
  1.1126 -		
  1.1127 -		return Result;
  1.1128 -	}
  1.1129 -	
  1.1130 -	public ArrayList<Uri> getNotifyList() {
  1.1131 -		return this.mNotifyList;
  1.1132 -	}
  1.1133  
  1.1134 -	public ArrayList<CalendarEvent> getCalendarEvents() {
  1.1135 -		return this.mCalendarEvents;
  1.1136 -	}
  1.1137 -	
  1.1138 -	public boolean readCalendarEvents(CaldavFacade facade) {
  1.1139 -		boolean Result = false;
  1.1140 -		
  1.1141 -		try {
  1.1142 -			this.mCalendarEvents = facade.getCalendarEvents(this);
  1.1143 -			Result = true;
  1.1144 -		} catch (ClientProtocolException e) {
  1.1145 -			e.printStackTrace();
  1.1146 -			Result = false;
  1.1147 -		} catch (URISyntaxException e) {
  1.1148 -			e.printStackTrace();
  1.1149 -			Result = false;
  1.1150 -		} catch (IOException e) {
  1.1151 -			e.printStackTrace();
  1.1152 -			Result = false;
  1.1153 -		} catch (ParserConfigurationException e) {
  1.1154 -			e.printStackTrace();
  1.1155 -			Result = false;
  1.1156 -		} catch (SAXException e) {
  1.1157 -			e.printStackTrace();
  1.1158 -			Result = false;
  1.1159 -		}
  1.1160 -		
  1.1161 -		return Result;
  1.1162 -	}
  1.1163 -	
  1.1164 +        //Log.d(TAG, "Rows reseted: " + RowCount.toString());
  1.1165 +        return RowCount;
  1.1166 +    }
  1.1167 +
  1.1168 +    /**
  1.1169 +     * Events not being tagged are for deletion
  1.1170 +     *
  1.1171 +     * @return
  1.1172 +     * @throws RemoteException
  1.1173 +     * @see AndroidEvent#cInternalTag
  1.1174 +     * @see SyncAdapter#synchroniseEvents(CaldavFacade, Account, ContentProviderClient, Uri, DavCalendar, SyncStats)
  1.1175 +     */
  1.1176 +    public int deleteUntaggedEvents() throws RemoteException {
  1.1177 +        String mSelectionClause = "(" + Event.INTERNALTAG + " < ?) AND (" + Events.CALENDAR_ID + " = ?)";
  1.1178 +        String[] mSelectionArgs = {"1", Long.toString(ContentUris.parseId(this.getAndroidCalendarUri()))};
  1.1179 +
  1.1180 +        int CountDeleted = this.mProvider.delete(asSyncAdapter(Events.CONTENT_URI, this.mAccount.name, this.mAccount.type), mSelectionClause, mSelectionArgs);
  1.1181 +        //Log.d(TAG, "Rows deleted: " + CountDeleted.toString());
  1.1182 +        return CountDeleted;
  1.1183 +    }
  1.1184 +
  1.1185 +    private Uri SyncAdapterCalendar() {
  1.1186 +        return asSyncAdapter(this.getAndroidCalendarUri(), mAccount.name, mAccount.type);
  1.1187 +    }
  1.1188 +
  1.1189 +    private Uri SyncAdapter() {
  1.1190 +        return asSyncAdapter(Calendars.CONTENT_URI, mAccount.name, mAccount.type);
  1.1191 +    }
  1.1192 +
  1.1193 +    private static Uri asSyncAdapter(Uri uri, String account, String accountType) {
  1.1194 +        return uri.buildUpon()
  1.1195 +                .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
  1.1196 +                .appendQueryParameter(Calendars.ACCOUNT_NAME, account)
  1.1197 +                .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
  1.1198 +    }
  1.1199 +
  1.1200 +    public void setAccount(Account account) {
  1.1201 +        this.mAccount = account;
  1.1202 +    }
  1.1203 +
  1.1204 +    public void setProvider(ContentProviderClient provider) {
  1.1205 +        this.mProvider = provider;
  1.1206 +    }
  1.1207 +
  1.1208 +    /**
  1.1209 +     * general access function to ContentValues
  1.1210 +     *
  1.1211 +     * @param Item the item name from Calendars.*
  1.1212 +     * @return the value for the item
  1.1213 +     */
  1.1214 +    private String getContentValueAsString(String Item) {
  1.1215 +        String Result = "";
  1.1216 +        if (this.ContentValues.containsKey(Item))
  1.1217 +            Result = this.ContentValues.getAsString(Item);
  1.1218 +        return Result;
  1.1219 +    }
  1.1220 +
  1.1221 +    /**
  1.1222 +     * general access function to ContentValues
  1.1223 +     *
  1.1224 +     * @param Item the item name from Calendars.*
  1.1225 +     * @return the value for the item
  1.1226 +     */
  1.1227 +    private int getContentValueAsInt(String Item) {
  1.1228 +        int Result = 0;
  1.1229 +        if (this.ContentValues.containsKey(Item))
  1.1230 +            Result = this.ContentValues.getAsInteger(Item);
  1.1231 +        return Result;
  1.1232 +    }
  1.1233 +
  1.1234 +    /**
  1.1235 +     * general access function to ContentValues
  1.1236 +     *
  1.1237 +     * @param Item  the item name from Calendars.*
  1.1238 +     * @param Value the value for the item
  1.1239 +     * @return success of this function
  1.1240 +     */
  1.1241 +    private boolean setContentValueAsString(String Item, String Value) {
  1.1242 +        boolean Result = false;
  1.1243 +
  1.1244 +        if (this.ContentValues.containsKey(Item))
  1.1245 +            this.ContentValues.remove(Item);
  1.1246 +        this.ContentValues.put(Item, Value);
  1.1247 +
  1.1248 +        return Result;
  1.1249 +    }
  1.1250 +
  1.1251 +    /**
  1.1252 +     * general access function to ContentValues
  1.1253 +     *
  1.1254 +     * @param Item  the item name from Calendars.*
  1.1255 +     * @param Value the value for the item
  1.1256 +     * @return success of this function
  1.1257 +     */
  1.1258 +    private boolean setContentValueAsInt(String Item, int Value) {
  1.1259 +        boolean Result = false;
  1.1260 +
  1.1261 +        if (this.ContentValues.containsKey(Item))
  1.1262 +            this.ContentValues.remove(Item);
  1.1263 +        this.ContentValues.put(Item, Value);
  1.1264 +
  1.1265 +        return Result;
  1.1266 +    }
  1.1267 +
  1.1268 +    public ArrayList<Uri> getNotifyList() {
  1.1269 +        return this.mNotifyList;
  1.1270 +    }
  1.1271 +
  1.1272 +    public ArrayList<CalendarEvent> getCalendarEvents() {
  1.1273 +        return this.mCalendarEvents;
  1.1274 +    }
  1.1275 +
  1.1276 +    public boolean readCalendarEvents(CaldavFacade facade) {
  1.1277 +        boolean Result = false;
  1.1278 +
  1.1279 +        try {
  1.1280 +            this.mCalendarEvents = facade.getCalendarEvents(this);
  1.1281 +            Result = true;
  1.1282 +        } catch (ClientProtocolException e) {
  1.1283 +            e.printStackTrace();
  1.1284 +            Result = false;
  1.1285 +        } catch (URISyntaxException e) {
  1.1286 +            e.printStackTrace();
  1.1287 +            Result = false;
  1.1288 +        } catch (IOException e) {
  1.1289 +            e.printStackTrace();
  1.1290 +            Result = false;
  1.1291 +        } catch (ParserConfigurationException e) {
  1.1292 +            e.printStackTrace();
  1.1293 +            Result = false;
  1.1294 +        } catch (SAXException e) {
  1.1295 +            e.printStackTrace();
  1.1296 +            Result = false;
  1.1297 +        }
  1.1298 +
  1.1299 +        return Result;
  1.1300 +    }
  1.1301 +
  1.1302  }

mercurial