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

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
child 11
82795cac7832
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

michael@0 1 package org.gege.caldavsyncadapter.caldav.entities;
michael@0 2
michael@0 3 import java.net.URI;
michael@0 4 import java.util.ArrayList;
michael@0 5
michael@0 6 //import org.gege.caldavsyncadapter.CalendarColors;
michael@0 7 import org.gege.caldavsyncadapter.caldav.entities.DavCalendar.CalendarSource;
michael@0 8 import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper;
michael@0 9
michael@0 10 import android.accounts.Account;
michael@0 11 import android.content.ContentProviderClient;
michael@0 12 //import android.content.ContentUris;
michael@0 13 //import android.content.ContentValues;
michael@0 14 import android.database.Cursor;
michael@0 15 import android.net.Uri;
michael@0 16 import android.os.RemoteException;
michael@0 17 import android.provider.CalendarContract.Calendars;
michael@0 18 //import android.util.Log;
michael@0 19
michael@0 20 public class CalendarList {
michael@0 21 // private static final String TAG = "CalendarList";
michael@0 22
michael@0 23 private java.util.ArrayList<DavCalendar> mList = new java.util.ArrayList<DavCalendar>();
michael@0 24
michael@0 25 private Account mAccount = null;
michael@0 26 private ContentProviderClient mProvider = null;
michael@0 27
michael@0 28 public CalendarSource Source = CalendarSource.undefined;
michael@0 29
michael@0 30 public String ServerUrl = "";
michael@0 31
michael@0 32 public CalendarList(Account account, ContentProviderClient provider, CalendarSource source, String serverUrl) {
michael@0 33 this.mAccount = account;
michael@0 34 this.mProvider = provider;
michael@0 35 this.Source = source;
michael@0 36 this.ServerUrl = serverUrl;
michael@0 37 }
michael@0 38
michael@0 39 /* public Calendar getCalendarByAndroidCalendarId(int calendarId) {
michael@0 40 Calendar Result = null;
michael@0 41
michael@0 42 for (Calendar Item : mList) {
michael@0 43 if (Item.getAndroidCalendarId() == calendarId)
michael@0 44 Result = Item;
michael@0 45 }
michael@0 46
michael@0 47 return Result;
michael@0 48 }*/
michael@0 49
michael@0 50 public DavCalendar getCalendarByURI(URI calendarURI) {
michael@0 51 DavCalendar Result = null;
michael@0 52
michael@0 53 for (DavCalendar Item : mList) {
michael@0 54 if (Item.getURI().equals(calendarURI))
michael@0 55 Result = Item;
michael@0 56 }
michael@0 57
michael@0 58 return Result;
michael@0 59 }
michael@0 60
michael@0 61 public DavCalendar getCalendarByAndroidUri(Uri androidCalendarUri) {
michael@0 62 DavCalendar Result = null;
michael@0 63
michael@0 64 for (DavCalendar Item : mList) {
michael@0 65 if (Item.getAndroidCalendarUri().equals(androidCalendarUri))
michael@0 66 Result = Item;
michael@0 67 }
michael@0 68
michael@0 69 return Result;
michael@0 70 }
michael@0 71
michael@0 72 /**
michael@0 73 * function to get all calendars from client side android
michael@0 74 * @return
michael@0 75 */
michael@0 76 public boolean readCalendarFromClient() {
michael@0 77 boolean Result = false;
michael@0 78 Cursor cur = null;
michael@0 79
michael@0 80 Uri uri = Calendars.CONTENT_URI;
michael@0 81
michael@0 82 /* COMPAT: in the past, the serverurl was not stored within a calendar. (see #98)
michael@0 83 * so there was no chance to see which calendars belongs to a named account.
michael@0 84 * username + serverurl have to be unique
michael@0 85 * ((DavCalendar.SERVERURL = ?) OR (DavCalendar.SERVERURL IS NULL))
michael@0 86 */
michael@0 87 String selection = "(" + "(" + Calendars.ACCOUNT_NAME + " = ?) AND " +
michael@0 88 "(" + Calendars.ACCOUNT_TYPE + " = ?) AND " +
michael@0 89 "((" + DavCalendar.SERVERURL + " = ?) OR " +
michael@0 90 "(" + DavCalendar.SERVERURL + " IS NULL)) AND " +
michael@0 91 "(" + Calendars.OWNER_ACCOUNT + " = ?)" +
michael@0 92 ")";
michael@0 93 String[] selectionArgs = new String[] { mAccount.name,
michael@0 94 mAccount.type,
michael@0 95 this.ServerUrl,
michael@0 96 mAccount.name
michael@0 97 };
michael@0 98
michael@0 99 // Submit the query and get a Cursor object back.
michael@0 100 try {
michael@0 101 cur = mProvider.query(uri, null, selection, selectionArgs, null);
michael@0 102 } catch (RemoteException e) {
michael@0 103 e.printStackTrace();
michael@0 104 }
michael@0 105
michael@0 106 if (cur != null) {
michael@0 107 while (cur.moveToNext()) {
michael@0 108 mList.add(new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl));
michael@0 109 }
michael@0 110 cur.close();
michael@0 111 Result = true;
michael@0 112 }
michael@0 113
michael@0 114 return Result;
michael@0 115 }
michael@0 116
michael@0 117 public boolean deleteCalendarOnClientSideOnly(android.content.Context context) {
michael@0 118 boolean Result = false;
michael@0 119
michael@0 120 for (DavCalendar androidCalendar : this.mList) {
michael@0 121 if (!androidCalendar.foundServerSide) {
michael@0 122 NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "calendar deleted: " + androidCalendar.getCalendarDisplayName());
michael@0 123 androidCalendar.deleteAndroidCalendar();
michael@0 124 }
michael@0 125 }
michael@0 126
michael@0 127 return Result;
michael@0 128 }
michael@0 129
michael@0 130 public void addCalendar(DavCalendar item) {
michael@0 131 item.setAccount(this.mAccount);
michael@0 132 item.setProvider(this.mProvider);
michael@0 133 item.ServerUrl = this.ServerUrl;
michael@0 134 this.mList.add(item);
michael@0 135 }
michael@0 136 public java.util.ArrayList<DavCalendar> getCalendarList() {
michael@0 137 return this.mList;
michael@0 138 }
michael@0 139
michael@0 140 public void setAccount(Account account) {
michael@0 141 this.mAccount = account;
michael@0 142 }
michael@0 143 public void setProvider(ContentProviderClient provider) {
michael@0 144 this.mProvider = provider;
michael@0 145 }
michael@0 146 public ArrayList<Uri> getNotifyList() {
michael@0 147 ArrayList<Uri> Result = new ArrayList<Uri>();
michael@0 148
michael@0 149 for (DavCalendar cal : this.mList) {
michael@0 150 Result.addAll(cal.getNotifyList());
michael@0 151 }
michael@0 152
michael@0 153 return Result;
michael@0 154 }
michael@0 155 }

mercurial