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

changeset 0
fb9019fb1bf7
child 11
82795cac7832
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/gege/caldavsyncadapter/caldav/entities/CalendarList.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,155 @@
     1.4 +package org.gege.caldavsyncadapter.caldav.entities;
     1.5 +
     1.6 +import java.net.URI;
     1.7 +import java.util.ArrayList;
     1.8 +
     1.9 +//import org.gege.caldavsyncadapter.CalendarColors;
    1.10 +import org.gege.caldavsyncadapter.caldav.entities.DavCalendar.CalendarSource;
    1.11 +import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper;
    1.12 +
    1.13 +import android.accounts.Account;
    1.14 +import android.content.ContentProviderClient;
    1.15 +//import android.content.ContentUris;
    1.16 +//import android.content.ContentValues;
    1.17 +import android.database.Cursor;
    1.18 +import android.net.Uri;
    1.19 +import android.os.RemoteException;
    1.20 +import android.provider.CalendarContract.Calendars;
    1.21 +//import android.util.Log;
    1.22 +
    1.23 +public class CalendarList {
    1.24 +//	private static final String TAG = "CalendarList";
    1.25 +	
    1.26 +	private java.util.ArrayList<DavCalendar> mList = new java.util.ArrayList<DavCalendar>();
    1.27 +	
    1.28 +	private Account mAccount = null;
    1.29 +	private ContentProviderClient mProvider = null;
    1.30 +	
    1.31 +	public CalendarSource Source = CalendarSource.undefined;
    1.32 +	
    1.33 +	public String ServerUrl = "";
    1.34 +	
    1.35 +	public CalendarList(Account account, ContentProviderClient provider, CalendarSource source, String serverUrl) {
    1.36 +		this.mAccount = account;
    1.37 +		this.mProvider = provider;
    1.38 +		this.Source = source;
    1.39 +		this.ServerUrl = serverUrl;
    1.40 +	}
    1.41 +	
    1.42 +/*	public Calendar getCalendarByAndroidCalendarId(int calendarId) {
    1.43 +		Calendar Result = null;
    1.44 +		
    1.45 +		for (Calendar Item : mList) {
    1.46 +			if (Item.getAndroidCalendarId() == calendarId)
    1.47 +				Result = Item;
    1.48 +		}
    1.49 +		
    1.50 +		return Result;
    1.51 +	}*/
    1.52 +	
    1.53 +	public DavCalendar getCalendarByURI(URI calendarURI) {
    1.54 +		DavCalendar Result = null;
    1.55 +		
    1.56 +		for (DavCalendar Item : mList) {
    1.57 +			if (Item.getURI().equals(calendarURI))
    1.58 +				Result = Item;
    1.59 +		}
    1.60 +		
    1.61 +		return Result;
    1.62 +	}
    1.63 +	
    1.64 +	public DavCalendar getCalendarByAndroidUri(Uri androidCalendarUri) {
    1.65 +		DavCalendar Result = null;
    1.66 +		
    1.67 +		for (DavCalendar Item : mList) {
    1.68 +			if (Item.getAndroidCalendarUri().equals(androidCalendarUri))
    1.69 +				Result = Item;
    1.70 +		}
    1.71 +		
    1.72 +		return Result;
    1.73 +	}
    1.74 +	
    1.75 +	/**
    1.76 +	 * function to get all calendars from client side android
    1.77 +	 * @return
    1.78 +	 */
    1.79 +	public boolean readCalendarFromClient() {
    1.80 +		boolean Result = false;
    1.81 +		Cursor cur = null;
    1.82 +		
    1.83 +		Uri uri = Calendars.CONTENT_URI;
    1.84 +		
    1.85 +		/* COMPAT: in the past, the serverurl was not stored within a calendar. (see #98)
    1.86 +		 * so there was no chance to see which calendars belongs to a named account.
    1.87 +		 * username + serverurl have to be unique
    1.88 +		 * ((DavCalendar.SERVERURL = ?) OR (DavCalendar.SERVERURL IS NULL))
    1.89 +		 */
    1.90 +		String selection = "(" + "(" + Calendars.ACCOUNT_NAME +  " = ?) AND " + 
    1.91 +		                         "(" + Calendars.ACCOUNT_TYPE +  " = ?) AND " +
    1.92 +		                         "((" + DavCalendar.SERVERURL +   " = ?) OR " +
    1.93 +		                         "(" + DavCalendar.SERVERURL +   " IS NULL)) AND " +
    1.94 +		                         "(" + Calendars.OWNER_ACCOUNT + " = ?)" +
    1.95 +		                   ")";
    1.96 +		String[] selectionArgs = new String[] {	mAccount.name, 
    1.97 +												mAccount.type,
    1.98 +												this.ServerUrl,
    1.99 +												mAccount.name
   1.100 +											}; 
   1.101 +
   1.102 +		// Submit the query and get a Cursor object back. 
   1.103 +		try {
   1.104 +			cur = mProvider.query(uri, null, selection, selectionArgs, null);
   1.105 +		} catch (RemoteException e) {
   1.106 +			e.printStackTrace();
   1.107 +		}
   1.108 +		
   1.109 +		if (cur != null) {
   1.110 +			while (cur.moveToNext()) {
   1.111 +				mList.add(new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl));
   1.112 +			}
   1.113 +			cur.close();
   1.114 +			Result = true;
   1.115 +		}
   1.116 +		
   1.117 +		return Result;
   1.118 +	}
   1.119 +	
   1.120 +	public boolean deleteCalendarOnClientSideOnly(android.content.Context context) {
   1.121 +		boolean Result = false;
   1.122 +		
   1.123 +		for (DavCalendar androidCalendar : this.mList) {
   1.124 +			if (!androidCalendar.foundServerSide) {
   1.125 +				NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "calendar deleted: " + androidCalendar.getCalendarDisplayName());
   1.126 +				androidCalendar.deleteAndroidCalendar();
   1.127 +			}
   1.128 +		}
   1.129 +		
   1.130 +		return Result;
   1.131 +	}
   1.132 +	
   1.133 +	public void addCalendar(DavCalendar item) {
   1.134 +		item.setAccount(this.mAccount);
   1.135 +		item.setProvider(this.mProvider);
   1.136 +		item.ServerUrl = this.ServerUrl;
   1.137 +		this.mList.add(item);
   1.138 +	}
   1.139 +	public java.util.ArrayList<DavCalendar> getCalendarList() {
   1.140 +		return this.mList;
   1.141 +	}
   1.142 +	
   1.143 +	public void setAccount(Account account) {
   1.144 +		this.mAccount = account;
   1.145 +	}
   1.146 +	public void setProvider(ContentProviderClient provider) {
   1.147 +		this.mProvider = provider;
   1.148 +	}
   1.149 +	public ArrayList<Uri> getNotifyList() {
   1.150 +		ArrayList<Uri> Result = new ArrayList<Uri>();
   1.151 +		
   1.152 +		for (DavCalendar cal : this.mList) {
   1.153 +			Result.addAll(cal.getNotifyList());
   1.154 +		}
   1.155 +		
   1.156 +		return Result;
   1.157 +	}
   1.158 +}

mercurial