michael@0: package org.gege.caldavsyncadapter.caldav.entities; michael@0: michael@0: import java.net.URI; michael@0: import java.util.ArrayList; michael@0: michael@0: //import org.gege.caldavsyncadapter.CalendarColors; michael@0: import org.gege.caldavsyncadapter.caldav.entities.DavCalendar.CalendarSource; michael@0: import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper; michael@0: michael@0: import android.accounts.Account; michael@0: import android.content.ContentProviderClient; michael@0: //import android.content.ContentUris; michael@0: //import android.content.ContentValues; michael@0: import android.database.Cursor; michael@0: import android.net.Uri; michael@0: import android.os.RemoteException; michael@0: import android.provider.CalendarContract.Calendars; michael@0: //import android.util.Log; michael@0: michael@0: public class CalendarList { michael@0: // private static final String TAG = "CalendarList"; michael@0: michael@0: private java.util.ArrayList mList = new java.util.ArrayList(); michael@0: michael@0: private Account mAccount = null; michael@0: private ContentProviderClient mProvider = null; michael@0: michael@0: public CalendarSource Source = CalendarSource.undefined; michael@0: michael@0: public String ServerUrl = ""; michael@0: michael@0: public CalendarList(Account account, ContentProviderClient provider, CalendarSource source, String serverUrl) { michael@0: this.mAccount = account; michael@0: this.mProvider = provider; michael@0: this.Source = source; michael@0: this.ServerUrl = serverUrl; michael@0: } michael@0: michael@0: /* public Calendar getCalendarByAndroidCalendarId(int calendarId) { michael@0: Calendar Result = null; michael@0: michael@0: for (Calendar Item : mList) { michael@0: if (Item.getAndroidCalendarId() == calendarId) michael@0: Result = Item; michael@0: } michael@0: michael@0: return Result; michael@0: }*/ michael@0: michael@0: public DavCalendar getCalendarByURI(URI calendarURI) { michael@0: DavCalendar Result = null; michael@0: michael@0: for (DavCalendar Item : mList) { michael@0: if (Item.getURI().equals(calendarURI)) michael@0: Result = Item; michael@0: } michael@0: michael@0: return Result; michael@0: } michael@0: michael@0: public DavCalendar getCalendarByAndroidUri(Uri androidCalendarUri) { michael@0: DavCalendar Result = null; michael@0: michael@0: for (DavCalendar Item : mList) { michael@0: if (Item.getAndroidCalendarUri().equals(androidCalendarUri)) michael@0: Result = Item; michael@0: } michael@0: michael@0: return Result; michael@0: } michael@0: michael@0: /** michael@0: * function to get all calendars from client side android michael@0: * @return michael@0: */ michael@0: public boolean readCalendarFromClient() { michael@0: boolean Result = false; michael@0: Cursor cur = null; michael@0: michael@0: Uri uri = Calendars.CONTENT_URI; michael@0: michael@0: /* COMPAT: in the past, the serverurl was not stored within a calendar. (see #98) michael@0: * so there was no chance to see which calendars belongs to a named account. michael@0: * username + serverurl have to be unique michael@0: * ((DavCalendar.SERVERURL = ?) OR (DavCalendar.SERVERURL IS NULL)) michael@0: */ michael@0: String selection = "(" + "(" + Calendars.ACCOUNT_NAME + " = ?) AND " + michael@0: "(" + Calendars.ACCOUNT_TYPE + " = ?) AND " + michael@0: "((" + DavCalendar.SERVERURL + " = ?) OR " + michael@0: "(" + DavCalendar.SERVERURL + " IS NULL)) AND " + michael@0: "(" + Calendars.OWNER_ACCOUNT + " = ?)" + michael@0: ")"; michael@0: String[] selectionArgs = new String[] { mAccount.name, michael@0: mAccount.type, michael@0: this.ServerUrl, michael@0: mAccount.name michael@0: }; michael@0: michael@0: // Submit the query and get a Cursor object back. michael@0: try { michael@0: cur = mProvider.query(uri, null, selection, selectionArgs, null); michael@0: } catch (RemoteException e) { michael@0: e.printStackTrace(); michael@0: } michael@0: michael@0: if (cur != null) { michael@0: while (cur.moveToNext()) { michael@0: mList.add(new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl)); michael@0: } michael@0: cur.close(); michael@0: Result = true; michael@0: } michael@0: michael@0: return Result; michael@0: } michael@0: michael@0: public boolean deleteCalendarOnClientSideOnly(android.content.Context context) { michael@0: boolean Result = false; michael@0: michael@0: for (DavCalendar androidCalendar : this.mList) { michael@0: if (!androidCalendar.foundServerSide) { michael@0: NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "calendar deleted: " + androidCalendar.getCalendarDisplayName()); michael@0: androidCalendar.deleteAndroidCalendar(); michael@0: } michael@0: } michael@0: michael@0: return Result; michael@0: } michael@0: michael@0: public void addCalendar(DavCalendar item) { michael@0: item.setAccount(this.mAccount); michael@0: item.setProvider(this.mProvider); michael@0: item.ServerUrl = this.ServerUrl; michael@0: this.mList.add(item); michael@0: } michael@0: public java.util.ArrayList getCalendarList() { michael@0: return this.mList; michael@0: } michael@0: michael@0: public void setAccount(Account account) { michael@0: this.mAccount = account; michael@0: } michael@0: public void setProvider(ContentProviderClient provider) { michael@0: this.mProvider = provider; michael@0: } michael@0: public ArrayList getNotifyList() { michael@0: ArrayList Result = new ArrayList(); michael@0: michael@0: for (DavCalendar cal : this.mList) { michael@0: Result.addAll(cal.getNotifyList()); michael@0: } michael@0: michael@0: return Result; michael@0: } michael@0: }