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