Utils.java revision 2aeb8d988aa4b65d3402374832613ab977e009dc
1/* 2 * Copyright (C) 2006 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 17package com.android.calendar; 18 19import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME; 20 21import com.android.calendar.CalendarController.ViewType; 22 23import android.app.Activity; 24import android.content.Context; 25import android.content.Intent; 26import android.content.SharedPreferences; 27import android.content.res.Configuration; 28import android.content.res.Resources; 29import android.database.Cursor; 30import android.database.MatrixCursor; 31import android.net.Uri; 32import android.os.Bundle; 33import android.text.TextUtils; 34import android.text.format.DateUtils; 35import android.text.format.Time; 36import android.util.Log; 37import com.android.calendar.CalendarUtils.TimeZoneUtils; 38 39import java.util.ArrayList; 40import java.util.Arrays; 41import java.util.Calendar; 42import java.util.Formatter; 43import java.util.HashMap; 44import java.util.Iterator; 45import java.util.LinkedList; 46import java.util.List; 47import java.util.Map; 48 49public class Utils { 50 private static final boolean DEBUG = false; 51 private static final String TAG = "CalUtils"; 52 // Set to 0 until we have UI to perform undo 53 public static final long UNDO_DELAY = 0; 54 55 // For recurring events which instances of the series are being modified 56 public static final int MODIFY_UNINITIALIZED = 0; 57 public static final int MODIFY_SELECTED = 1; 58 public static final int MODIFY_ALL_FOLLOWING = 2; 59 public static final int MODIFY_ALL = 3; 60 61 // When the edit event view finishes it passes back the appropriate exit 62 // code. 63 public static final int DONE_REVERT = 1 << 0; 64 public static final int DONE_SAVE = 1 << 1; 65 public static final int DONE_DELETE = 1 << 2; 66 // And should re run with DONE_EXIT if it should also leave the view, just 67 // exiting is identical to reverting 68 public static final int DONE_EXIT = 1 << 0; 69 70 public static final String OPEN_EMAIL_MARKER = " <"; 71 public static final String CLOSE_EMAIL_MARKER = ">"; 72 73 public static final String INTENT_KEY_DETAIL_VIEW = "DETAIL_VIEW"; 74 public static final String INTENT_KEY_VIEW_TYPE = "VIEW"; 75 public static final String INTENT_VALUE_VIEW_TYPE_DAY = "DAY"; 76 77 public static final int MONDAY_BEFORE_JULIAN_EPOCH = Time.EPOCH_JULIAN_DAY - 3; 78 79 // Defines used by the DNA generation code 80 static final int DAY_IN_MINUTES = 60 * 24; 81 static final int WEEK_IN_MINUTES = DAY_IN_MINUTES * 7; 82 // The work day is being counted as 6am to 8pm 83 static int WORK_DAY_MINUTES = 14 * 60; 84 static int WORK_DAY_START_MINUTES = 6 * 60; 85 static int WORK_DAY_END_MINUTES = 20 * 60; 86 static int WORK_DAY_END_LENGTH = (24 * 60) - WORK_DAY_END_MINUTES; 87 static int CONFLICT_COLOR = 0xFF000000; 88 static boolean mMinutesLoaded = false; 89 90 // The name of the shared preferences file. This name must be maintained for 91 // historical 92 // reasons, as it's what PreferenceManager assigned the first time the file 93 // was created. 94 private static final String SHARED_PREFS_NAME = "com.android.calendar_preferences"; 95 96 public static final String APPWIDGET_DATA_TYPE = "vnd.android.data/update"; 97 98 private static final TimeZoneUtils mTZUtils = new TimeZoneUtils(SHARED_PREFS_NAME); 99 private static boolean mAllowWeekForDetailView = false; 100 private static long mTardis = 0; 101 102 public static int getViewTypeFromIntentAndSharedPref(Activity activity) { 103 Intent intent = activity.getIntent(); 104 Bundle extras = intent.getExtras(); 105 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(activity); 106 107 if (TextUtils.equals(intent.getAction(), Intent.ACTION_EDIT)) { 108 return ViewType.EDIT; 109 } 110 if (extras != null) { 111 if (extras.getBoolean(INTENT_KEY_DETAIL_VIEW, false)) { 112 // This is the "detail" view which is either agenda or day view 113 return prefs.getInt(GeneralPreferences.KEY_DETAILED_VIEW, 114 GeneralPreferences.DEFAULT_DETAILED_VIEW); 115 } else if (INTENT_VALUE_VIEW_TYPE_DAY.equals(extras.getString(INTENT_KEY_VIEW_TYPE))) { 116 // Not sure who uses this. This logic came from LaunchActivity 117 return ViewType.DAY; 118 } 119 } 120 121 // Default to the last view 122 return prefs.getInt( 123 GeneralPreferences.KEY_START_VIEW, GeneralPreferences.DEFAULT_START_VIEW); 124 } 125 126 /** 127 * Gets the intent action for telling the widget to update. 128 */ 129 public static String getWidgetUpdateAction(Context context) { 130 return context.getPackageName() + ".APPWIDGET_UPDATE"; 131 } 132 133 /** 134 * Gets the intent action for telling the widget to update. 135 */ 136 public static String getWidgetScheduledUpdateAction(Context context) { 137 return context.getPackageName() + ".APPWIDGET_SCHEDULED_UPDATE"; 138 } 139 140 /** 141 * Gets the intent action for telling the widget to update. 142 */ 143 public static String getSearchAuthority(Context context) { 144 return context.getPackageName() + ".CalendarRecentSuggestionsProvider"; 145 } 146 147 /** 148 * Writes a new home time zone to the db. Updates the home time zone in the 149 * db asynchronously and updates the local cache. Sending a time zone of 150 * **tbd** will cause it to be set to the device's time zone. null or empty 151 * tz will be ignored. 152 * 153 * @param context The calling activity 154 * @param timeZone The time zone to set Calendar to, or **tbd** 155 */ 156 public static void setTimeZone(Context context, String timeZone) { 157 mTZUtils.setTimeZone(context, timeZone); 158 } 159 160 /** 161 * Gets the time zone that Calendar should be displayed in This is a helper 162 * method to get the appropriate time zone for Calendar. If this is the 163 * first time this method has been called it will initiate an asynchronous 164 * query to verify that the data in preferences is correct. The callback 165 * supplied will only be called if this query returns a value other than 166 * what is stored in preferences and should cause the calling activity to 167 * refresh anything that depends on calling this method. 168 * 169 * @param context The calling activity 170 * @param callback The runnable that should execute if a query returns new 171 * values 172 * @return The string value representing the time zone Calendar should 173 * display 174 */ 175 public static String getTimeZone(Context context, Runnable callback) { 176 return mTZUtils.getTimeZone(context, callback); 177 } 178 179 /** 180 * Formats a date or a time range according to the local conventions. 181 * 182 * @param context the context is required only if the time is shown 183 * @param startMillis the start time in UTC milliseconds 184 * @param endMillis the end time in UTC milliseconds 185 * @param flags a bit mask of options See {@link DateUtils#formatDateRange(Context, Formatter, 186 * long, long, int, String) formatDateRange} 187 * @return a string containing the formatted date/time range. 188 */ 189 public static String formatDateRange( 190 Context context, long startMillis, long endMillis, int flags) { 191 return mTZUtils.formatDateRange(context, startMillis, endMillis, flags); 192 } 193 194 public static String getSharedPreference(Context context, String key, String defaultValue) { 195 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 196 return prefs.getString(key, defaultValue); 197 } 198 199 public static int getSharedPreference(Context context, String key, int defaultValue) { 200 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 201 return prefs.getInt(key, defaultValue); 202 } 203 204 public static boolean getSharedPreference(Context context, String key, boolean defaultValue) { 205 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 206 return prefs.getBoolean(key, defaultValue); 207 } 208 209 /** 210 * Asynchronously sets the preference with the given key to the given value 211 * 212 * @param context the context to use to get preferences from 213 * @param key the key of the preference to set 214 * @param value the value to set 215 */ 216 public static void setSharedPreference(Context context, String key, String value) { 217 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 218 prefs.edit().putString(key, value).apply(); 219 } 220 221 protected static void tardis() { 222 mTardis = System.currentTimeMillis(); 223 } 224 225 protected static long getTardis() { 226 return mTardis; 227 } 228 229 static void setSharedPreference(Context context, String key, boolean value) { 230 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 231 SharedPreferences.Editor editor = prefs.edit(); 232 editor.putBoolean(key, value); 233 editor.apply(); 234 } 235 236 static void setSharedPreference(Context context, String key, int value) { 237 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 238 SharedPreferences.Editor editor = prefs.edit(); 239 editor.putInt(key, value); 240 editor.apply(); 241 } 242 243 /** 244 * Save default agenda/day/week/month view for next time 245 * 246 * @param context 247 * @param viewId {@link CalendarController.ViewType} 248 */ 249 static void setDefaultView(Context context, int viewId) { 250 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 251 SharedPreferences.Editor editor = prefs.edit(); 252 253 boolean validDetailView = false; 254 if (mAllowWeekForDetailView && viewId == CalendarController.ViewType.WEEK) { 255 validDetailView = true; 256 } else { 257 validDetailView = viewId == CalendarController.ViewType.AGENDA 258 || viewId == CalendarController.ViewType.DAY; 259 } 260 261 if (validDetailView) { 262 // Record the detail start view 263 editor.putInt(GeneralPreferences.KEY_DETAILED_VIEW, viewId); 264 } 265 266 // Record the (new) start view 267 editor.putInt(GeneralPreferences.KEY_START_VIEW, viewId); 268 editor.apply(); 269 } 270 271 public static MatrixCursor matrixCursorFromCursor(Cursor cursor) { 272 MatrixCursor newCursor = new MatrixCursor(cursor.getColumnNames()); 273 int numColumns = cursor.getColumnCount(); 274 String data[] = new String[numColumns]; 275 cursor.moveToPosition(-1); 276 while (cursor.moveToNext()) { 277 for (int i = 0; i < numColumns; i++) { 278 data[i] = cursor.getString(i); 279 } 280 newCursor.addRow(data); 281 } 282 return newCursor; 283 } 284 285 /** 286 * Compares two cursors to see if they contain the same data. 287 * 288 * @return Returns true of the cursors contain the same data and are not 289 * null, false otherwise 290 */ 291 public static boolean compareCursors(Cursor c1, Cursor c2) { 292 if (c1 == null || c2 == null) { 293 return false; 294 } 295 296 int numColumns = c1.getColumnCount(); 297 if (numColumns != c2.getColumnCount()) { 298 return false; 299 } 300 301 if (c1.getCount() != c2.getCount()) { 302 return false; 303 } 304 305 c1.moveToPosition(-1); 306 c2.moveToPosition(-1); 307 while (c1.moveToNext() && c2.moveToNext()) { 308 for (int i = 0; i < numColumns; i++) { 309 if (!TextUtils.equals(c1.getString(i), c2.getString(i))) { 310 return false; 311 } 312 } 313 } 314 315 return true; 316 } 317 318 /** 319 * If the given intent specifies a time (in milliseconds since the epoch), 320 * then that time is returned. Otherwise, the current time is returned. 321 */ 322 public static final long timeFromIntentInMillis(Intent intent) { 323 // If the time was specified, then use that. Otherwise, use the current 324 // time. 325 Uri data = intent.getData(); 326 long millis = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1); 327 if (millis == -1 && data != null && data.isHierarchical()) { 328 List<String> path = data.getPathSegments(); 329 if (path.size() == 2 && path.get(0).equals("time")) { 330 try { 331 millis = Long.valueOf(data.getLastPathSegment()); 332 } catch (NumberFormatException e) { 333 Log.i("Calendar", "timeFromIntentInMillis: Data existed but no valid time " 334 + "found. Using current time."); 335 } 336 } 337 } 338 if (millis <= 0) { 339 millis = System.currentTimeMillis(); 340 } 341 return millis; 342 } 343 344 /** 345 * Formats the given Time object so that it gives the month and year (for 346 * example, "September 2007"). 347 * 348 * @param time the time to format 349 * @return the string containing the weekday and the date 350 */ 351 public static String formatMonthYear(Context context, Time time) { 352 int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY 353 | DateUtils.FORMAT_SHOW_YEAR; 354 long millis = time.toMillis(true); 355 return formatDateRange(context, millis, millis, flags); 356 } 357 358 /** 359 * Returns a list joined together by the provided delimiter, for example, 360 * ["a", "b", "c"] could be joined into "a,b,c" 361 * 362 * @param things the things to join together 363 * @param delim the delimiter to use 364 * @return a string contained the things joined together 365 */ 366 public static String join(List<?> things, String delim) { 367 StringBuilder builder = new StringBuilder(); 368 boolean first = true; 369 for (Object thing : things) { 370 if (first) { 371 first = false; 372 } else { 373 builder.append(delim); 374 } 375 builder.append(thing.toString()); 376 } 377 return builder.toString(); 378 } 379 380 /** 381 * Returns the week since {@link Time#EPOCH_JULIAN_DAY} (Jan 1, 1970) 382 * adjusted for first day of week. 383 * 384 * This takes a julian day and the week start day and calculates which 385 * week since {@link Time#EPOCH_JULIAN_DAY} that day occurs in, starting 386 * at 0. *Do not* use this to compute the ISO week number for the year. 387 * 388 * @param julianDay The julian day to calculate the week number for 389 * @param firstDayOfWeek Which week day is the first day of the week, 390 * see {@link Time#SUNDAY} 391 * @return Weeks since the epoch 392 */ 393 public static int getWeeksSinceEpochFromJulianDay(int julianDay, int firstDayOfWeek) { 394 int diff = Time.THURSDAY - firstDayOfWeek; 395 if (diff < 0) { 396 diff += 7; 397 } 398 int refDay = Time.EPOCH_JULIAN_DAY - diff; 399 return (julianDay - refDay) / 7; 400 } 401 402 /** 403 * Takes a number of weeks since the epoch and calculates the Julian day of 404 * the Monday for that week. 405 * 406 * This assumes that the week containing the {@link Time#EPOCH_JULIAN_DAY} 407 * is considered week 0. It returns the Julian day for the Monday 408 * {@code week} weeks after the Monday of the week containing the epoch. 409 * 410 * @param week Number of weeks since the epoch 411 * @return The julian day for the Monday of the given week since the epoch 412 */ 413 public static int getJulianMondayFromWeeksSinceEpoch(int week) { 414 return MONDAY_BEFORE_JULIAN_EPOCH + week * 7; 415 } 416 417 /** 418 * Get first day of week as android.text.format.Time constant. 419 * 420 * @return the first day of week in android.text.format.Time 421 */ 422 public static int getFirstDayOfWeek(Context context) { 423 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 424 String pref = prefs.getString( 425 GeneralPreferences.KEY_WEEK_START_DAY, GeneralPreferences.WEEK_START_DEFAULT); 426 427 int startDay; 428 if (GeneralPreferences.WEEK_START_DEFAULT.equals(pref)) { 429 startDay = Calendar.getInstance().getFirstDayOfWeek(); 430 } else { 431 startDay = Integer.parseInt(pref); 432 } 433 434 if (startDay == Calendar.SATURDAY) { 435 return Time.SATURDAY; 436 } else if (startDay == Calendar.MONDAY) { 437 return Time.MONDAY; 438 } else { 439 return Time.SUNDAY; 440 } 441 } 442 443 /** 444 * @return true when week number should be shown. 445 */ 446 public static boolean getShowWeekNumber(Context context) { 447 final SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 448 return prefs.getBoolean( 449 GeneralPreferences.KEY_SHOW_WEEK_NUM, GeneralPreferences.DEFAULT_SHOW_WEEK_NUM); 450 } 451 452 /** 453 * @return true when declined events should be hidden. 454 */ 455 public static boolean getHideDeclinedEvents(Context context) { 456 final SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 457 return prefs.getBoolean(GeneralPreferences.KEY_HIDE_DECLINED, false); 458 } 459 460 public static int getDaysPerWeek(Context context) { 461 final SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 462 return prefs.getInt(GeneralPreferences.KEY_DAYS_PER_WEEK, 7); 463 } 464 465 /** 466 * Determine whether the column position is Saturday or not. 467 * 468 * @param column the column position 469 * @param firstDayOfWeek the first day of week in android.text.format.Time 470 * @return true if the column is Saturday position 471 */ 472 public static boolean isSaturday(int column, int firstDayOfWeek) { 473 return (firstDayOfWeek == Time.SUNDAY && column == 6) 474 || (firstDayOfWeek == Time.MONDAY && column == 5) 475 || (firstDayOfWeek == Time.SATURDAY && column == 0); 476 } 477 478 /** 479 * Determine whether the column position is Sunday or not. 480 * 481 * @param column the column position 482 * @param firstDayOfWeek the first day of week in android.text.format.Time 483 * @return true if the column is Sunday position 484 */ 485 public static boolean isSunday(int column, int firstDayOfWeek) { 486 return (firstDayOfWeek == Time.SUNDAY && column == 0) 487 || (firstDayOfWeek == Time.MONDAY && column == 6) 488 || (firstDayOfWeek == Time.SATURDAY && column == 1); 489 } 490 491 /** 492 * Convert given UTC time into current local time. This assumes it is for an 493 * allday event and will adjust the time to be on a midnight boundary. 494 * 495 * @param recycle Time object to recycle, otherwise null. 496 * @param utcTime Time to convert, in UTC. 497 * @param tz The time zone to convert this time to. 498 */ 499 public static long convertAlldayUtcToLocal(Time recycle, long utcTime, String tz) { 500 if (recycle == null) { 501 recycle = new Time(); 502 } 503 recycle.timezone = Time.TIMEZONE_UTC; 504 recycle.set(utcTime); 505 recycle.timezone = tz; 506 return recycle.normalize(true); 507 } 508 509 public static long convertAlldayLocalToUTC(Time recycle, long localTime, String tz) { 510 if (recycle == null) { 511 recycle = new Time(); 512 } 513 recycle.timezone = tz; 514 recycle.set(localTime); 515 recycle.timezone = Time.TIMEZONE_UTC; 516 return recycle.normalize(true); 517 } 518 519 /** 520 * Scan through a cursor of calendars and check if names are duplicated. 521 * This travels a cursor containing calendar display names and fills in the 522 * provided map with whether or not each name is repeated. 523 * 524 * @param isDuplicateName The map to put the duplicate check results in. 525 * @param cursor The query of calendars to check 526 * @param nameIndex The column of the query that contains the display name 527 */ 528 public static void checkForDuplicateNames( 529 Map<String, Boolean> isDuplicateName, Cursor cursor, int nameIndex) { 530 isDuplicateName.clear(); 531 cursor.moveToPosition(-1); 532 while (cursor.moveToNext()) { 533 String displayName = cursor.getString(nameIndex); 534 // Set it to true if we've seen this name before, false otherwise 535 if (displayName != null) { 536 isDuplicateName.put(displayName, isDuplicateName.containsKey(displayName)); 537 } 538 } 539 } 540 541 /** 542 * Null-safe object comparison 543 * 544 * @param s1 545 * @param s2 546 * @return 547 */ 548 public static boolean equals(Object o1, Object o2) { 549 return o1 == null ? o2 == null : o1.equals(o2); 550 } 551 552 public static void setAllowWeekForDetailView(boolean allowWeekView) { 553 mAllowWeekForDetailView = allowWeekView; 554 } 555 556 public static boolean getAllowWeekForDetailView() { 557 return mAllowWeekForDetailView; 558 } 559 560 public static boolean isMultiPaneConfiguration (Context c) { 561 return (c.getResources().getConfiguration().screenLayout & 562 Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0; 563 } 564 565 public static boolean getConfigBool(Context c, int key) { 566 return c.getResources().getBoolean(key); 567 } 568 569 // A single strand represents one color of events. Events are divided up by 570 // color to make them convenient to draw. The black strand is special in 571 // that it holds conflicting events as well as color settings for allday on 572 // each day. 573 public static class DNAStrand { 574 public float[] points; 575 public int[] allDays; // color for the allday, 0 means no event 576 int position; 577 public int color; 578 int count; 579 } 580 581 // A segment is a single continuous length of time occupied by a single 582 // color. Segments should never span multiple days. 583 private static class DNASegment { 584 int startMinute; // in minutes since the start of the week 585 int endMinute; 586 int color; // Calendar color or black for conflicts 587 int day; // quick reference to the day this segment is on 588 } 589 590 /** 591 * Converts a list of events to a list of segments to draw. Assumes list is 592 * ordered by start time of the events. The function processes events for a 593 * range of days from firstJulianDay to firstJulianDay + dayXs.length - 1. 594 * The algorithm goes over all the events and creates a set of segments 595 * ordered by start time. This list of segments is then converted into a 596 * HashMap of strands which contain the draw points and are organized by 597 * color. The strands can then be drawn by setting the paint color to each 598 * strand's color and calling drawLines on its set of points. The points are 599 * set up using the following parameters. 600 * <ul> 601 * <li>Events between midnight and WORK_DAY_START_MINUTES are compressed 602 * into the first 1/8th of the space between top and bottom.</li> 603 * <li>Events between WORK_DAY_END_MINUTES and the following midnight are 604 * compressed into the last 1/8th of the space between top and bottom</li> 605 * <li>Events between WORK_DAY_START_MINUTES and WORK_DAY_END_MINUTES use 606 * the remaining 3/4ths of the space</li> 607 * <li>All segments drawn will maintain at least minPixels height, except 608 * for conflicts in the first or last 1/8th, which may be smaller</li> 609 * </ul> 610 * 611 * @param firstJulianDay The julian day of the first day of events 612 * @param events A list of events sorted by start time 613 * @param top The lowest y value the dna should be drawn at 614 * @param bottom The highest y value the dna should be drawn at 615 * @param dayXs An array of x values to draw the dna at, one for each day 616 * @param conflictColor the color to use for conflicts 617 * @return 618 */ 619 public static HashMap<Integer, DNAStrand> createDNAStrands(int firstJulianDay, 620 ArrayList<Event> events, int top, int bottom, int minPixels, int[] dayXs, 621 Context context) { 622 623 if (!mMinutesLoaded) { 624 if (context == null) { 625 Log.wtf(TAG, "No context and haven't loaded parameters yet! Can't create DNA."); 626 } 627 Resources res = context.getResources(); 628 CONFLICT_COLOR = res.getColor(R.color.month_dna_conflict_time_color); 629 WORK_DAY_START_MINUTES = res.getInteger(R.integer.work_start_minutes); 630 WORK_DAY_END_MINUTES = res.getInteger(R.integer.work_end_minutes); 631 WORK_DAY_END_LENGTH = DAY_IN_MINUTES - WORK_DAY_END_MINUTES; 632 WORK_DAY_MINUTES = WORK_DAY_END_MINUTES - WORK_DAY_START_MINUTES; 633 mMinutesLoaded = true; 634 } 635 636 if (events == null || events.isEmpty() || dayXs == null || dayXs.length < 1 637 || bottom - top < 8 || minPixels < 0) { 638 Log.e(TAG, 639 "Bad values for createDNAStrands! events:" + events + " dayXs:" 640 + Arrays.toString(dayXs) + " bot-top:" + (bottom - top) + " minPixels:" 641 + minPixels); 642 return null; 643 } 644 645 LinkedList<DNASegment> segments = new LinkedList<DNASegment>(); 646 HashMap<Integer, DNAStrand> strands = new HashMap<Integer, DNAStrand>(); 647 // add a black strand by default, other colors will get added in 648 // the loop 649 DNAStrand blackStrand = new DNAStrand(); 650 blackStrand.color = CONFLICT_COLOR; 651 strands.put(CONFLICT_COLOR, blackStrand); 652 // the min length is the number of minutes that will occupy 653 // MIN_SEGMENT_PIXELS in the 'work day' time slot. This computes the 654 // minutes/pixel * minpx where the number of pixels are 3/4 the total 655 // dna height: 4*(mins/(px * 3/4)) 656 int minMinutes = minPixels * 4 * WORK_DAY_MINUTES / (3 * (bottom - top)); 657 658 // There are slightly fewer than half as many pixels in 1/6 the space, 659 // so round to 2.5x for the min minutes in the non-work area 660 int minOtherMinutes = minMinutes * 5 / 2; 661 int lastJulianDay = firstJulianDay + dayXs.length - 1; 662 663 Event event = new Event(); 664 // Go through all the events for the week 665 for (Event currEvent : events) { 666 // if this event is outside the weeks range skip it 667 if (currEvent.endDay < firstJulianDay || currEvent.startDay > lastJulianDay) { 668 continue; 669 } 670 if (currEvent.drawAsAllday()) { 671 addAllDayToStrands(currEvent, strands, firstJulianDay, dayXs.length); 672 continue; 673 } 674 // Copy the event over so we can clip its start and end to our range 675 currEvent.copyTo(event); 676 if (event.startDay < firstJulianDay) { 677 event.startDay = firstJulianDay; 678 event.startTime = 0; 679 } 680 // If it starts after the work day make sure the start is at least 681 // minPixels from midnight 682 if (event.startTime > DAY_IN_MINUTES - minOtherMinutes) { 683 event.startTime = DAY_IN_MINUTES - minOtherMinutes; 684 } 685 if (event.endDay > lastJulianDay) { 686 event.endDay = lastJulianDay; 687 event.endTime = DAY_IN_MINUTES - 1; 688 } 689 // If the end time is before the work day make sure it ends at least 690 // minPixels after midnight 691 if (event.endTime < minOtherMinutes) { 692 event.endTime = minOtherMinutes; 693 } 694 // If the start and end are on the same day make sure they are at 695 // least minPixels apart. This only needs to be done for times 696 // outside the work day as the min distance for within the work day 697 // is enforced in the segment code. 698 if (event.startDay == event.endDay && 699 event.endTime - event.startTime < minOtherMinutes) { 700 // If it's less than minPixels in an area before the work 701 // day 702 if (event.startTime < WORK_DAY_START_MINUTES) { 703 // extend the end to the first easy guarantee that it's 704 // minPixels 705 event.endTime = Math.min(event.startTime + minOtherMinutes, 706 WORK_DAY_START_MINUTES + minMinutes); 707 // if it's in the area after the work day 708 } else if (event.endTime > WORK_DAY_END_MINUTES) { 709 // First try shifting the end but not past midnight 710 event.endTime = Math.min(event.endTime + minOtherMinutes, DAY_IN_MINUTES - 1); 711 // if it's still too small move the start back 712 if (event.endTime - event.startTime < minOtherMinutes) { 713 event.startTime = event.endTime - minOtherMinutes; 714 } 715 } 716 } 717 718 // This handles adding the first segment 719 if (segments.size() == 0) { 720 addNewSegment(segments, event, strands, firstJulianDay, 0, minMinutes); 721 continue; 722 } 723 // Now compare our current start time to the end time of the last 724 // segment in the list 725 DNASegment lastSegment = segments.getLast(); 726 int startMinute = (event.startDay - firstJulianDay) * DAY_IN_MINUTES + event.startTime; 727 int endMinute = Math.max((event.endDay - firstJulianDay) * DAY_IN_MINUTES 728 + event.endTime, startMinute + minMinutes); 729 730 if (startMinute < 0) { 731 startMinute = 0; 732 } 733 if (endMinute >= WEEK_IN_MINUTES) { 734 endMinute = WEEK_IN_MINUTES - 1; 735 } 736 // If we start before the last segment in the list ends we need to 737 // start going through the list as this may conflict with other 738 // events 739 if (startMinute < lastSegment.endMinute) { 740 int i = segments.size(); 741 // find the last segment this event intersects with 742 while (--i >= 0 && endMinute < segments.get(i).startMinute); 743 744 DNASegment currSegment; 745 // for each segment this event intersects with 746 for (; i >= 0 && startMinute <= (currSegment = segments.get(i)).endMinute; i--) { 747 // if the segment is already a conflict ignore it 748 if (currSegment.color == CONFLICT_COLOR) { 749 continue; 750 } 751 // if the event ends before the segment and wouldn't create 752 // a segment that is too small split off the right side 753 if (endMinute < currSegment.endMinute - minMinutes) { 754 DNASegment rhs = new DNASegment(); 755 rhs.endMinute = currSegment.endMinute; 756 rhs.color = currSegment.color; 757 rhs.startMinute = endMinute + 1; 758 rhs.day = currSegment.day; 759 currSegment.endMinute = endMinute; 760 segments.add(i + 1, rhs); 761 strands.get(rhs.color).count++; 762 if (DEBUG) { 763 Log.d(TAG, "Added rhs, curr:" + currSegment.toString() + " i:" 764 + segments.get(i).toString()); 765 } 766 } 767 // if the event starts after the segment and wouldn't create 768 // a segment that is too small split off the left side 769 if (startMinute > currSegment.startMinute + minMinutes) { 770 DNASegment lhs = new DNASegment(); 771 lhs.startMinute = currSegment.startMinute; 772 lhs.color = currSegment.color; 773 lhs.endMinute = startMinute - 1; 774 lhs.day = currSegment.day; 775 currSegment.startMinute = startMinute; 776 // increment i so that we are at the right position when 777 // referencing the segments to the right and left of the 778 // current segment. 779 segments.add(i++, lhs); 780 strands.get(lhs.color).count++; 781 if (DEBUG) { 782 Log.d(TAG, "Added lhs, curr:" + currSegment.toString() + " i:" 783 + segments.get(i).toString()); 784 } 785 } 786 // if the right side is black merge this with the segment to 787 // the right if they're on the same day and overlap 788 if (i + 1 < segments.size()) { 789 DNASegment rhs = segments.get(i + 1); 790 if (rhs.color == CONFLICT_COLOR && currSegment.day == rhs.day 791 && rhs.startMinute <= currSegment.endMinute + 1) { 792 rhs.startMinute = Math.min(currSegment.startMinute, rhs.startMinute); 793 segments.remove(currSegment); 794 strands.get(currSegment.color).count--; 795 // point at the new current segment 796 currSegment = rhs; 797 } 798 } 799 // if the left side is black merge this with the segment to 800 // the left if they're on the same day and overlap 801 if (i - 1 >= 0) { 802 DNASegment lhs = segments.get(i - 1); 803 if (lhs.color == CONFLICT_COLOR && currSegment.day == lhs.day 804 && lhs.endMinute >= currSegment.startMinute - 1) { 805 lhs.endMinute = Math.max(currSegment.endMinute, lhs.endMinute); 806 segments.remove(currSegment); 807 strands.get(currSegment.color).count--; 808 // point at the new current segment 809 currSegment = lhs; 810 // point i at the new current segment in case new 811 // code is added 812 i--; 813 } 814 } 815 // if we're still not black, decrement the count for the 816 // color being removed, change this to black, and increment 817 // the black count 818 if (currSegment.color != CONFLICT_COLOR) { 819 strands.get(currSegment.color).count--; 820 currSegment.color = CONFLICT_COLOR; 821 strands.get(CONFLICT_COLOR).count++; 822 } 823 } 824 825 } 826 // If this event extends beyond the last segment add a new segment 827 if (endMinute > lastSegment.endMinute) { 828 addNewSegment(segments, event, strands, firstJulianDay, lastSegment.endMinute, 829 minMinutes); 830 } 831 } 832 weaveDNAStrands(segments, firstJulianDay, strands, top, bottom, dayXs); 833 return strands; 834 } 835 836 // This figures out allDay colors as allDay events are found 837 private static void addAllDayToStrands(Event event, HashMap<Integer, DNAStrand> strands, 838 int firstJulianDay, int numDays) { 839 DNAStrand strand = getOrCreateStrand(strands, CONFLICT_COLOR); 840 // if we haven't initialized the allDay portion create it now 841 if (strand.allDays == null) { 842 strand.allDays = new int[numDays]; 843 } 844 845 // For each day this event is on update the color 846 int end = Math.min(event.endDay - firstJulianDay, numDays - 1); 847 for (int i = Math.max(event.startDay - firstJulianDay, 0); i <= end; i++) { 848 if (strand.allDays[i] != 0) { 849 // if this day already had a color, it is now a conflict 850 strand.allDays[i] = CONFLICT_COLOR; 851 } else { 852 // else it's just the color of the event 853 strand.allDays[i] = event.color; 854 } 855 } 856 } 857 858 // This processes all the segments, sorts them by color, and generates a 859 // list of points to draw 860 private static void weaveDNAStrands(LinkedList<DNASegment> segments, int firstJulianDay, 861 HashMap<Integer, DNAStrand> strands, int top, int bottom, int[] dayXs) { 862 // First, get rid of any colors that ended up with no segments 863 Iterator<DNAStrand> strandIterator = strands.values().iterator(); 864 while (strandIterator.hasNext()) { 865 DNAStrand strand = strandIterator.next(); 866 if (strand.count < 1 && strand.allDays == null) { 867 strandIterator.remove(); 868 continue; 869 } 870 strand.points = new float[strand.count * 4]; 871 strand.position = 0; 872 } 873 // Go through each segment and compute its points 874 for (DNASegment segment : segments) { 875 // Add the points to the strand of that color 876 DNAStrand strand = strands.get(segment.color); 877 int dayIndex = segment.day - firstJulianDay; 878 int dayStartMinute = segment.startMinute % DAY_IN_MINUTES; 879 int dayEndMinute = segment.endMinute % DAY_IN_MINUTES; 880 int height = bottom - top; 881 int workDayHeight = height * 3 / 4; 882 int remainderHeight = (height - workDayHeight) / 2; 883 884 int x = dayXs[dayIndex]; 885 int y0 = 0; 886 int y1 = 0; 887 888 y0 = top + getPixelOffsetFromMinutes(dayStartMinute, workDayHeight, remainderHeight); 889 y1 = top + getPixelOffsetFromMinutes(dayEndMinute, workDayHeight, remainderHeight); 890 if (DEBUG) { 891 Log.d(TAG, "Adding " + Integer.toHexString(segment.color) + " at x,y0,y1: " + x 892 + " " + y0 + " " + y1 + " for " + dayStartMinute + " " + dayEndMinute); 893 } 894 strand.points[strand.position++] = x; 895 strand.points[strand.position++] = y0; 896 strand.points[strand.position++] = x; 897 strand.points[strand.position++] = y1; 898 } 899 } 900 901 /** 902 * Compute a pixel offset from the top for a given minute from the work day 903 * height and the height of the top area. 904 */ 905 private static int getPixelOffsetFromMinutes(int minute, int workDayHeight, 906 int remainderHeight) { 907 int y; 908 if (minute < WORK_DAY_START_MINUTES) { 909 y = minute * remainderHeight / WORK_DAY_START_MINUTES; 910 } else if (minute < WORK_DAY_END_MINUTES) { 911 y = remainderHeight + (minute - WORK_DAY_START_MINUTES) * workDayHeight 912 / WORK_DAY_MINUTES; 913 } else { 914 y = remainderHeight + workDayHeight + (minute - WORK_DAY_END_MINUTES) * remainderHeight 915 / WORK_DAY_END_LENGTH; 916 } 917 return y; 918 } 919 920 /** 921 * Add a new segment based on the event provided. This will handle splitting 922 * segments across day boundaries and ensures a minimum size for segments. 923 */ 924 private static void addNewSegment(LinkedList<DNASegment> segments, Event event, 925 HashMap<Integer, DNAStrand> strands, int firstJulianDay, int minStart, int minMinutes) { 926 if (event.startDay > event.endDay) { 927 Log.wtf(TAG, "Event starts after it ends: " + event.toString()); 928 } 929 // If this is a multiday event split it up by day 930 if (event.startDay != event.endDay) { 931 Event lhs = new Event(); 932 lhs.color = event.color; 933 lhs.startDay = event.startDay; 934 // the first day we want the start time to be the actual start time 935 lhs.startTime = event.startTime; 936 lhs.endDay = lhs.startDay; 937 lhs.endTime = DAY_IN_MINUTES - 1; 938 // Nearly recursive iteration! 939 while (lhs.startDay != event.endDay) { 940 addNewSegment(segments, lhs, strands, firstJulianDay, minStart, minMinutes); 941 // The days in between are all day, even though that shouldn't 942 // actually happen due to the allday filtering 943 lhs.startDay++; 944 lhs.endDay = lhs.startDay; 945 lhs.startTime = 0; 946 minStart = 0; 947 } 948 // The last day we want the end time to be the actual end time 949 lhs.endTime = event.endTime; 950 event = lhs; 951 } 952 // Create the new segment and compute its fields 953 DNASegment segment = new DNASegment(); 954 int dayOffset = (event.startDay - firstJulianDay) * DAY_IN_MINUTES; 955 int endOfDay = dayOffset + DAY_IN_MINUTES - 1; 956 // clip the start if needed 957 segment.startMinute = Math.max(dayOffset + event.startTime, minStart); 958 // and extend the end if it's too small, but not beyond the end of the 959 // day 960 int minEnd = Math.min(segment.startMinute + minMinutes, endOfDay); 961 segment.endMinute = Math.max(dayOffset + event.endTime, minEnd); 962 if (segment.endMinute > endOfDay) { 963 segment.endMinute = endOfDay; 964 } 965 966 segment.color = event.color; 967 segment.day = event.startDay; 968 segments.add(segment); 969 // increment the count for the correct color or add a new strand if we 970 // don't have that color yet 971 DNAStrand strand = getOrCreateStrand(strands, segment.color); 972 strand.count++; 973 } 974 975 /** 976 * Try to get a strand of the given color. Create it if it doesn't exist. 977 */ 978 private static DNAStrand getOrCreateStrand(HashMap<Integer, DNAStrand> strands, int color) { 979 DNAStrand strand = strands.get(color); 980 if (strand == null) { 981 strand = new DNAStrand(); 982 strand.color = color; 983 strand.count = 0; 984 strands.put(strand.color, strand); 985 } 986 return strand; 987 } 988 989 /** 990 * Sends an intent to launch the top level Calendar view. 991 * 992 * @param context 993 */ 994 public static void returnToCalendarHome(Context context) { 995 Intent launchIntent = new Intent(); 996 launchIntent.setAction(Intent.ACTION_VIEW); 997 launchIntent.setData(Uri.parse("content://com.android.calendar/time")); 998 launchIntent.setClass(context, AllInOneActivity.class); 999 launchIntent.setFlags( 1000 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP); 1001 context.startActivity(launchIntent); 1002 } 1003} 1004