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