1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.calendar; 18 19 import android.content.ContentResolver; 20 import android.content.ContentUris; 21 import android.content.Context; 22 import android.content.SharedPreferences; 23 import android.content.res.Resources; 24 import android.database.Cursor; 25 import android.graphics.Color; 26 import android.net.Uri; 27 import android.os.Debug; 28 import android.provider.CalendarContract; 29 import android.provider.CalendarContract.Attendees; 30 import android.provider.CalendarContract.Calendars; 31 import android.provider.CalendarContract.Events; 32 import android.provider.CalendarContract.Instances; 33 import android.text.TextUtils; 34 import android.text.format.DateUtils; 35 import android.text.format.Time; 36 import android.util.Log; 37 38 import java.util.ArrayList; 39 import java.util.Arrays; 40 import java.util.Iterator; 41 import java.util.concurrent.atomic.AtomicInteger; 42 43 // TODO: should Event be Parcelable so it can be passed via Intents? 44 public class Event implements Cloneable { 45 46 private static final String TAG = "CalEvent"; 47 private static final boolean PROFILE = false; 48 49 /** 50 * The sort order is: 51 * 1) events with an earlier start (begin for normal events, startday for allday) 52 * 2) events with a later end (end for normal events, endday for allday) 53 * 3) the title (unnecessary, but nice) 54 * 55 * The start and end day is sorted first so that all day events are 56 * sorted correctly with respect to events that are >24 hours (and 57 * therefore show up in the allday area). 58 */ 59 private static final String SORT_EVENTS_BY = 60 "begin ASC, end DESC, title ASC"; 61 private static final String SORT_ALLDAY_BY = 62 "startDay ASC, endDay DESC, title ASC"; 63 private static final String DISPLAY_AS_ALLDAY = "dispAllday"; 64 65 private static final String EVENTS_WHERE = DISPLAY_AS_ALLDAY + "=0"; 66 private static final String ALLDAY_WHERE = DISPLAY_AS_ALLDAY + "=1"; 67 68 // The projection to use when querying instances to build a list of events 69 public static final String[] EVENT_PROJECTION = new String[] { 70 Instances.TITLE, // 0 71 Instances.EVENT_LOCATION, // 1 72 Instances.ALL_DAY, // 2 73 Instances.CALENDAR_COLOR, // 3 74 Instances.EVENT_TIMEZONE, // 4 75 Instances.EVENT_ID, // 5 76 Instances.BEGIN, // 6 77 Instances.END, // 7 78 Instances._ID, // 8 79 Instances.START_DAY, // 9 80 Instances.END_DAY, // 10 81 Instances.START_MINUTE, // 11 82 Instances.END_MINUTE, // 12 83 Instances.HAS_ALARM, // 13 84 Instances.RRULE, // 14 85 Instances.RDATE, // 15 86 Instances.SELF_ATTENDEE_STATUS, // 16 87 Events.ORGANIZER, // 17 88 Events.GUESTS_CAN_MODIFY, // 18 89 Instances.ALL_DAY + "=1 OR (" + Instances.END + "-" + Instances.BEGIN + ")>=" 90 + DateUtils.DAY_IN_MILLIS + " AS " + DISPLAY_AS_ALLDAY, // 19 91 }; 92 93 // The indices for the projection array above. 94 private static final int PROJECTION_TITLE_INDEX = 0; 95 private static final int PROJECTION_LOCATION_INDEX = 1; 96 private static final int PROJECTION_ALL_DAY_INDEX = 2; 97 private static final int PROJECTION_COLOR_INDEX = 3; 98 private static final int PROJECTION_TIMEZONE_INDEX = 4; 99 private static final int PROJECTION_EVENT_ID_INDEX = 5; 100 private static final int PROJECTION_BEGIN_INDEX = 6; 101 private static final int PROJECTION_END_INDEX = 7; 102 private static final int PROJECTION_START_DAY_INDEX = 9; 103 private static final int PROJECTION_END_DAY_INDEX = 10; 104 private static final int PROJECTION_START_MINUTE_INDEX = 11; 105 private static final int PROJECTION_END_MINUTE_INDEX = 12; 106 private static final int PROJECTION_HAS_ALARM_INDEX = 13; 107 private static final int PROJECTION_RRULE_INDEX = 14; 108 private static final int PROJECTION_RDATE_INDEX = 15; 109 private static final int PROJECTION_SELF_ATTENDEE_STATUS_INDEX = 16; 110 private static final int PROJECTION_ORGANIZER_INDEX = 17; 111 private static final int PROJECTION_GUESTS_CAN_INVITE_OTHERS_INDEX = 18; 112 private static final int PROJECTION_DISPLAY_AS_ALLDAY = 19; 113 114 private static String mNoTitleString; 115 private static int mNoColorColor; 116 117 public long id; 118 public int color; 119 public CharSequence title; 120 public CharSequence location; 121 public boolean allDay; 122 public String organizer; 123 public boolean guestsCanModify; 124 125 public int startDay; // start Julian day 126 public int endDay; // end Julian day 127 public int startTime; // Start and end time are in minutes since midnight 128 public int endTime; 129 130 public long startMillis; // UTC milliseconds since the epoch 131 public long endMillis; // UTC milliseconds since the epoch 132 private int mColumn; 133 private int mMaxColumns; 134 135 public boolean hasAlarm; 136 public boolean isRepeating; 137 138 public int selfAttendeeStatus; 139 140 // The coordinates of the event rectangle drawn on the screen. 141 public float left; 142 public float right; 143 public float top; 144 public float bottom; 145 146 // These 4 fields are used for navigating among events within the selected 147 // hour in the Day and Week view. 148 public Event nextRight; 149 public Event nextLeft; 150 public Event nextUp; 151 public Event nextDown; 152 153 @Override 154 public final Object clone() throws CloneNotSupportedException { 155 super.clone(); 156 Event e = new Event(); 157 158 e.title = title; 159 e.color = color; 160 e.location = location; 161 e.allDay = allDay; 162 e.startDay = startDay; 163 e.endDay = endDay; 164 e.startTime = startTime; 165 e.endTime = endTime; 166 e.startMillis = startMillis; 167 e.endMillis = endMillis; 168 e.hasAlarm = hasAlarm; 169 e.isRepeating = isRepeating; 170 e.selfAttendeeStatus = selfAttendeeStatus; 171 e.organizer = organizer; 172 e.guestsCanModify = guestsCanModify; 173 174 return e; 175 } 176 177 public final void copyTo(Event dest) { 178 dest.id = id; 179 dest.title = title; 180 dest.color = color; 181 dest.location = location; 182 dest.allDay = allDay; 183 dest.startDay = startDay; 184 dest.endDay = endDay; 185 dest.startTime = startTime; 186 dest.endTime = endTime; 187 dest.startMillis = startMillis; 188 dest.endMillis = endMillis; 189 dest.hasAlarm = hasAlarm; 190 dest.isRepeating = isRepeating; 191 dest.selfAttendeeStatus = selfAttendeeStatus; 192 dest.organizer = organizer; 193 dest.guestsCanModify = guestsCanModify; 194 } 195 196 public static final Event newInstance() { 197 Event e = new Event(); 198 199 e.id = 0; 200 e.title = null; 201 e.color = 0; 202 e.location = null; 203 e.allDay = false; 204 e.startDay = 0; 205 e.endDay = 0; 206 e.startTime = 0; 207 e.endTime = 0; 208 e.startMillis = 0; 209 e.endMillis = 0; 210 e.hasAlarm = false; 211 e.isRepeating = false; 212 e.selfAttendeeStatus = Attendees.ATTENDEE_STATUS_NONE; 213 214 return e; 215 } 216 217 /** 218 * Loads <i>days</i> days worth of instances starting at <i>startDay</i>. 219 */ 220 public static void loadEvents(Context context, ArrayList<Event> events, int startDay, int days, 221 int requestId, AtomicInteger sequenceNumber) { 222 223 if (PROFILE) { 224 Debug.startMethodTracing("loadEvents"); 225 } 226 227 Cursor cEvents = null; 228 Cursor cAllday = null; 229 230 events.clear(); 231 try { 232 int endDay = startDay + days - 1; 233 234 // We use the byDay instances query to get a list of all events for 235 // the days we're interested in. 236 // The sort order is: events with an earlier start time occur 237 // first and if the start times are the same, then events with 238 // a later end time occur first. The later end time is ordered 239 // first so that long rectangles in the calendar views appear on 240 // the left side. If the start and end times of two events are 241 // the same then we sort alphabetically on the title. This isn't 242 // required for correctness, it just adds a nice touch. 243 244 // Respect the preference to show/hide declined events 245 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 246 boolean hideDeclined = prefs.getBoolean(GeneralPreferences.KEY_HIDE_DECLINED, 247 false); 248 249 String where = EVENTS_WHERE; 250 String whereAllday = ALLDAY_WHERE; 251 if (hideDeclined) { 252 String hideString = " AND " + Instances.SELF_ATTENDEE_STATUS + "!=" 253 + Attendees.ATTENDEE_STATUS_DECLINED; 254 where += hideString; 255 whereAllday += hideString; 256 } 257 258 cEvents = instancesQuery(context.getContentResolver(), EVENT_PROJECTION, startDay, 259 endDay, where, null, SORT_EVENTS_BY); 260 cAllday = instancesQuery(context.getContentResolver(), EVENT_PROJECTION, startDay, 261 endDay, whereAllday, null, SORT_ALLDAY_BY); 262 263 // Check if we should return early because there are more recent 264 // load requests waiting. 265 if (requestId != sequenceNumber.get()) { 266 return; 267 } 268 269 buildEventsFromCursor(events, cEvents, context, startDay, endDay); 270 buildEventsFromCursor(events, cAllday, context, startDay, endDay); 271 272 } finally { 273 if (cEvents != null) { 274 cEvents.close(); 275 } 276 if (cAllday != null) { 277 cAllday.close(); 278 } 279 if (PROFILE) { 280 Debug.stopMethodTracing(); 281 } 282 } 283 } 284 285 /** 286 * Performs a query to return all visible instances in the given range 287 * that match the given selection. This is a blocking function and 288 * should not be done on the UI thread. This will cause an expansion of 289 * recurring events to fill this time range if they are not already 290 * expanded and will slow down for larger time ranges with many 291 * recurring events. 292 * 293 * @param cr The ContentResolver to use for the query 294 * @param projection The columns to return 295 * @param begin The start of the time range to query in UTC millis since 296 * epoch 297 * @param end The end of the time range to query in UTC millis since 298 * epoch 299 * @param selection Filter on the query as an SQL WHERE statement 300 * @param selectionArgs Args to replace any '?'s in the selection 301 * @param orderBy How to order the rows as an SQL ORDER BY statement 302 * @return A Cursor of instances matching the selection 303 */ 304 private static final Cursor instancesQuery(ContentResolver cr, String[] projection, 305 int startDay, int endDay, String selection, String[] selectionArgs, String orderBy) { 306 String WHERE_CALENDARS_SELECTED = Calendars.VISIBLE + "=?"; 307 String[] WHERE_CALENDARS_ARGS = {"1"}; 308 String DEFAULT_SORT_ORDER = "begin ASC"; 309 310 Uri.Builder builder = Instances.CONTENT_BY_DAY_URI.buildUpon(); 311 ContentUris.appendId(builder, startDay); 312 ContentUris.appendId(builder, endDay); 313 if (TextUtils.isEmpty(selection)) { 314 selection = WHERE_CALENDARS_SELECTED; 315 selectionArgs = WHERE_CALENDARS_ARGS; 316 } else { 317 selection = "(" + selection + ") AND " + WHERE_CALENDARS_SELECTED; 318 if (selectionArgs != null && selectionArgs.length > 0) { 319 selectionArgs = Arrays.copyOf(selectionArgs, selectionArgs.length + 1); 320 selectionArgs[selectionArgs.length - 1] = WHERE_CALENDARS_ARGS[0]; 321 } else { 322 selectionArgs = WHERE_CALENDARS_ARGS; 323 } 324 } 325 return cr.query(builder.build(), projection, selection, selectionArgs, 326 orderBy == null ? DEFAULT_SORT_ORDER : orderBy); 327 } 328 329 /** 330 * Adds all the events from the cursors to the events list. 331 * 332 * @param events The list of events 333 * @param cEvents Events to add to the list 334 * @param context 335 * @param startDay 336 * @param endDay 337 */ 338 public static void buildEventsFromCursor( 339 ArrayList<Event> events, Cursor cEvents, Context context, int startDay, int endDay) { 340 if (cEvents == null || events == null) { 341 Log.e(TAG, "buildEventsFromCursor: null cursor or null events list!"); 342 return; 343 } 344 345 int count = cEvents.getCount(); 346 347 if (count == 0) { 348 return; 349 } 350 351 Resources res = context.getResources(); 352 mNoTitleString = res.getString(R.string.no_title_label); 353 mNoColorColor = res.getColor(R.color.event_center); 354 // Sort events in two passes so we ensure the allday and standard events 355 // get sorted in the correct order 356 while (cEvents.moveToNext()) { 357 Event e = generateEventFromCursor(cEvents); 358 if (e.startDay > endDay || e.endDay < startDay) { 359 continue; 360 } 361 events.add(e); 362 } 363 } 364 365 /** 366 * @param cEvents Cursor pointing at event 367 * @return An event created from the cursor 368 */ 369 private static Event generateEventFromCursor(Cursor cEvents) { 370 Event e = new Event(); 371 372 e.id = cEvents.getLong(PROJECTION_EVENT_ID_INDEX); 373 e.title = cEvents.getString(PROJECTION_TITLE_INDEX); 374 e.location = cEvents.getString(PROJECTION_LOCATION_INDEX); 375 e.allDay = cEvents.getInt(PROJECTION_ALL_DAY_INDEX) != 0; 376 e.organizer = cEvents.getString(PROJECTION_ORGANIZER_INDEX); 377 e.guestsCanModify = cEvents.getInt(PROJECTION_GUESTS_CAN_INVITE_OTHERS_INDEX) != 0; 378 379 if (e.title == null || e.title.length() == 0) { 380 e.title = mNoTitleString; 381 } 382 383 if (!cEvents.isNull(PROJECTION_COLOR_INDEX)) { 384 // Read the color from the database 385 e.color = Utils.getDisplayColorFromColor(cEvents.getInt(PROJECTION_COLOR_INDEX)); 386 } else { 387 e.color = mNoColorColor; 388 } 389 390 long eStart = cEvents.getLong(PROJECTION_BEGIN_INDEX); 391 long eEnd = cEvents.getLong(PROJECTION_END_INDEX); 392 393 e.startMillis = eStart; 394 e.startTime = cEvents.getInt(PROJECTION_START_MINUTE_INDEX); 395 e.startDay = cEvents.getInt(PROJECTION_START_DAY_INDEX); 396 397 e.endMillis = eEnd; 398 e.endTime = cEvents.getInt(PROJECTION_END_MINUTE_INDEX); 399 e.endDay = cEvents.getInt(PROJECTION_END_DAY_INDEX); 400 401 e.hasAlarm = cEvents.getInt(PROJECTION_HAS_ALARM_INDEX) != 0; 402 403 // Check if this is a repeating event 404 String rrule = cEvents.getString(PROJECTION_RRULE_INDEX); 405 String rdate = cEvents.getString(PROJECTION_RDATE_INDEX); 406 if (!TextUtils.isEmpty(rrule) || !TextUtils.isEmpty(rdate)) { 407 e.isRepeating = true; 408 } else { 409 e.isRepeating = false; 410 } 411 412 e.selfAttendeeStatus = cEvents.getInt(PROJECTION_SELF_ATTENDEE_STATUS_INDEX); 413 return e; 414 } 415 416 /** 417 * Computes a position for each event. Each event is displayed 418 * as a non-overlapping rectangle. For normal events, these rectangles 419 * are displayed in separate columns in the week view and day view. For 420 * all-day events, these rectangles are displayed in separate rows along 421 * the top. In both cases, each event is assigned two numbers: N, and 422 * Max, that specify that this event is the Nth event of Max number of 423 * events that are displayed in a group. The width and position of each 424 * rectangle depend on the maximum number of rectangles that occur at 425 * the same time. 426 * 427 * @param eventsList the list of events, sorted into increasing time order 428 * @param minimumDurationMillis minimum duration acceptable as cell height of each event 429 * rectangle in millisecond. Should be 0 when it is not determined. 430 */ 431 /* package */ static void computePositions(ArrayList<Event> eventsList, 432 long minimumDurationMillis) { 433 if (eventsList == null) { 434 return; 435 } 436 437 // Compute the column positions separately for the all-day events 438 doComputePositions(eventsList, minimumDurationMillis, false); 439 doComputePositions(eventsList, minimumDurationMillis, true); 440 } 441 442 private static void doComputePositions(ArrayList<Event> eventsList, 443 long minimumDurationMillis, boolean doAlldayEvents) { 444 final ArrayList<Event> activeList = new ArrayList<Event>(); 445 final ArrayList<Event> groupList = new ArrayList<Event>(); 446 447 if (minimumDurationMillis < 0) { 448 minimumDurationMillis = 0; 449 } 450 451 long colMask = 0; 452 int maxCols = 0; 453 for (Event event : eventsList) { 454 // Process all-day events separately 455 if (event.drawAsAllday() != doAlldayEvents) 456 continue; 457 458 if (!doAlldayEvents) { 459 colMask = removeNonAlldayActiveEvents( 460 event, activeList.iterator(), minimumDurationMillis, colMask); 461 } else { 462 colMask = removeAlldayActiveEvents(event, activeList.iterator(), colMask); 463 } 464 465 // If the active list is empty, then reset the max columns, clear 466 // the column bit mask, and empty the groupList. 467 if (activeList.isEmpty()) { 468 for (Event ev : groupList) { 469 ev.setMaxColumns(maxCols); 470 } 471 maxCols = 0; 472 colMask = 0; 473 groupList.clear(); 474 } 475 476 // Find the first empty column. Empty columns are represented by 477 // zero bits in the column mask "colMask". 478 int col = findFirstZeroBit(colMask); 479 if (col == 64) 480 col = 63; 481 colMask |= (1L << col); 482 event.setColumn(col); 483 activeList.add(event); 484 groupList.add(event); 485 int len = activeList.size(); 486 if (maxCols < len) 487 maxCols = len; 488 } 489 for (Event ev : groupList) { 490 ev.setMaxColumns(maxCols); 491 } 492 } 493 494 private static long removeAlldayActiveEvents(Event event, Iterator<Event> iter, long colMask) { 495 // Remove the inactive allday events. An event on the active list 496 // becomes inactive when the end day is less than the current event's 497 // start day. 498 while (iter.hasNext()) { 499 final Event active = iter.next(); 500 if (active.endDay < event.startDay) { 501 colMask &= ~(1L << active.getColumn()); 502 iter.remove(); 503 } 504 } 505 return colMask; 506 } 507 508 private static long removeNonAlldayActiveEvents( 509 Event event, Iterator<Event> iter, long minDurationMillis, long colMask) { 510 long start = event.getStartMillis(); 511 // Remove the inactive events. An event on the active list 512 // becomes inactive when its end time is less than or equal to 513 // the current event's start time. 514 while (iter.hasNext()) { 515 final Event active = iter.next(); 516 517 final long duration = Math.max( 518 active.getEndMillis() - active.getStartMillis(), minDurationMillis); 519 if ((active.getStartMillis() + duration) <= start) { 520 colMask &= ~(1L << active.getColumn()); 521 iter.remove(); 522 } 523 } 524 return colMask; 525 } 526 527 public static int findFirstZeroBit(long val) { 528 for (int ii = 0; ii < 64; ++ii) { 529 if ((val & (1L << ii)) == 0) 530 return ii; 531 } 532 return 64; 533 } 534 535 public final void dump() { 536 Log.e("Cal", "+-----------------------------------------+"); 537 Log.e("Cal", "+ id = " + id); 538 Log.e("Cal", "+ color = " + color); 539 Log.e("Cal", "+ title = " + title); 540 Log.e("Cal", "+ location = " + location); 541 Log.e("Cal", "+ allDay = " + allDay); 542 Log.e("Cal", "+ startDay = " + startDay); 543 Log.e("Cal", "+ endDay = " + endDay); 544 Log.e("Cal", "+ startTime = " + startTime); 545 Log.e("Cal", "+ endTime = " + endTime); 546 Log.e("Cal", "+ organizer = " + organizer); 547 Log.e("Cal", "+ guestwrt = " + guestsCanModify); 548 } 549 550 public final boolean intersects(int julianDay, int startMinute, 551 int endMinute) { 552 if (endDay < julianDay) { 553 return false; 554 } 555 556 if (startDay > julianDay) { 557 return false; 558 } 559 560 if (endDay == julianDay) { 561 if (endTime < startMinute) { 562 return false; 563 } 564 // An event that ends at the start minute should not be considered 565 // as intersecting the given time span, but don't exclude 566 // zero-length (or very short) events. 567 if (endTime == startMinute 568 && (startTime != endTime || startDay != endDay)) { 569 return false; 570 } 571 } 572 573 if (startDay == julianDay && startTime > endMinute) { 574 return false; 575 } 576 577 return true; 578 } 579 580 /** 581 * Returns the event title and location separated by a comma. If the 582 * location is already part of the title (at the end of the title), then 583 * just the title is returned. 584 * 585 * @return the event title and location as a String 586 */ 587 public String getTitleAndLocation() { 588 String text = title.toString(); 589 590 // Append the location to the title, unless the title ends with the 591 // location (for example, "meeting in building 42" ends with the 592 // location). 593 if (location != null) { 594 String locationString = location.toString(); 595 if (!text.endsWith(locationString)) { 596 text += ", " + locationString; 597 } 598 } 599 return text; 600 } 601 602 public void setColumn(int column) { 603 mColumn = column; 604 } 605 606 public int getColumn() { 607 return mColumn; 608 } 609 610 public void setMaxColumns(int maxColumns) { 611 mMaxColumns = maxColumns; 612 } 613 614 public int getMaxColumns() { 615 return mMaxColumns; 616 } 617 618 public void setStartMillis(long startMillis) { 619 this.startMillis = startMillis; 620 } 621 622 public long getStartMillis() { 623 return startMillis; 624 } 625 626 public void setEndMillis(long endMillis) { 627 this.endMillis = endMillis; 628 } 629 630 public long getEndMillis() { 631 return endMillis; 632 } 633 634 public boolean drawAsAllday() { 635 // Use >= so we'll pick up Exchange allday events 636 return allDay || endMillis - startMillis >= DateUtils.DAY_IN_MILLIS; 637 } 638 } 639