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