@@ -132,7 +132,7 @@ protected enum KeyIndex {
132132 CALENDARS_ID ,
133133 CALENDARS_NAME ,
134134 CALENDARS_VISIBLE ,
135- CALENDARS_DISPLAY_NAME ,
135+ CALENDARS_DISPLAY_NAME ,
136136 EVENTS_ID ,
137137 EVENTS_CALENDAR_ID ,
138138 EVENTS_DESCRIPTION ,
@@ -271,7 +271,7 @@ public final JSONArray getActiveCalendars() throws JSONException {
271271 new String []{
272272 this .getKey (KeyIndex .CALENDARS_ID ),
273273 this .getKey (KeyIndex .CALENDARS_NAME ),
274- this .getKey (KeyIndex .CALENDARS_DISPLAY_NAME )
274+ this .getKey (KeyIndex .CALENDARS_DISPLAY_NAME )
275275 },
276276 this .getKey (KeyIndex .CALENDARS_VISIBLE ) + "=1" , null , null
277277 );
@@ -284,20 +284,38 @@ public final JSONArray getActiveCalendars() throws JSONException {
284284 JSONObject calendar = new JSONObject ();
285285 calendar .put ("id" , cursor .getString (cursor .getColumnIndex (this .getKey (KeyIndex .CALENDARS_ID ))));
286286 calendar .put ("name" , cursor .getString (cursor .getColumnIndex (this .getKey (KeyIndex .CALENDARS_NAME ))));
287- calendar .put ("displayname" , cursor .getString (cursor .getColumnIndex (this .getKey (KeyIndex .CALENDARS_DISPLAY_NAME ))));
287+ calendar .put ("displayname" , cursor .getString (cursor .getColumnIndex (this .getKey (KeyIndex .CALENDARS_DISPLAY_NAME ))));
288288 calendarsWrapper .put (calendar );
289289 } while (cursor .moveToNext ());
290290 cursor .close ();
291291 }
292292 return calendarsWrapper ;
293293 }
294294
295- private Map <String , Event > fetchEventsAsMap (Event [] instances ) {
295+ private Map <String , Event > fetchEventsAsMap (Event [] instances , String calendarId ) {
296296 // Only selecting from active calendars, no active calendars = no events.
297- String [] activeCalendarIds = getActiveCalendarIds ();
298- if (activeCalendarIds .length == 0 ) {
297+ List < String > activeCalendarIds = Arrays . asList ( getActiveCalendarIds () );
298+ if (activeCalendarIds .isEmpty () ) {
299299 return null ;
300300 }
301+
302+ List <String > calendarsToSearch ;
303+
304+ if (calendarId !=null ){
305+ calendarsToSearch = new ArrayList <String >();
306+ if (activeCalendarIds .contains (calendarId )){
307+ calendarsToSearch .add (calendarId );
308+ }
309+
310+ }else {
311+ calendarsToSearch = activeCalendarIds ;
312+ }
313+
314+ if (calendarsToSearch .isEmpty ()){
315+ return null ;
316+ }
317+
318+
301319 String [] projection = new String []{
302320 this .getKey (KeyIndex .EVENTS_ID ),
303321 this .getKey (KeyIndex .EVENTS_DESCRIPTION ),
@@ -318,11 +336,14 @@ private Map<String, Event> fetchEventsAsMap(Event[] instances) {
318336 }
319337 select .append (") AND " + this .getKey (KeyIndex .EVENTS_CALENDAR_ID ) +
320338 " IN (" );
321- select .append (activeCalendarIds [0 ]);
322- for (int i = 1 ; i < activeCalendarIds .length ; i ++) {
323- select .append ("," );
324- select .append (activeCalendarIds [i ]);
339+
340+ String prefix ="" ;
341+ for (String calendarToFilterId :calendarsToSearch ) {
342+ select .append (prefix );
343+ prefix = "," ;
344+ select .append (calendarToFilterId );
325345 }
346+
326347 select .append (")" );
327348 Cursor cursor = queryEvents (projection , select .toString (), null , null );
328349 Map <String , Event > eventsMap = new HashMap <String , Event >();
@@ -428,23 +449,23 @@ private Map<String, ArrayList<Attendee>> fetchAttendeesForEventsAsMap(
428449 return attendeeMap ;
429450 }
430451
431- public JSONArray findEvents (String eventId , String title , String location , String notes , long startFrom , long startTo ) {
452+ public JSONArray findEvents (String eventId , String title , String location , String notes , long startFrom , long startTo , String calendarId ) {
432453 JSONArray result = new JSONArray ();
433454 // Fetch events from the instance table.
434455 Event [] instances = fetchEventInstances (eventId , title , location , notes , startFrom , startTo );
435456 if (instances == null ) {
436457 return result ;
437458 }
438459 // Fetch events from the events table for more event info.
439- Map <String , Event > eventMap = fetchEventsAsMap (instances );
460+ Map <String , Event > eventMap = fetchEventsAsMap (instances , calendarId );
440461 // Fetch event attendees
441462 Map <String , ArrayList <Attendee >> attendeeMap =
442463 fetchAttendeesForEventsAsMap (eventMap .keySet ().toArray (new String [0 ]));
443464 // Merge the event info with the instances and turn it into a JSONArray.
444- for (Event event : eventMap .values ()) {
465+ /* for (Event event : eventMap.values()) {
445466 result.put(event.toJSONObject());
446- }
447- /*
467+ }*/
468+
448469 for (Event instance : instances ) {
449470 Event event = eventMap .get (instance .eventId );
450471 if (event != null ) {
@@ -469,7 +490,7 @@ public JSONArray findEvents(String eventId, String title, String location, Strin
469490 instance .attendees = attendeeMap .get (instance .eventId );
470491 result .put (instance .toJSONObject ());
471492 }
472- }*/
493+ }
473494
474495 return result ;
475496 }
0 commit comments