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