Utils.java revision 29dc76a401b073bcd69d610817e3781fc9ebe5fc
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 android.accounts.Account; 22import android.app.Activity; 23import android.app.SearchManager; 24import android.content.BroadcastReceiver; 25import android.content.ContentResolver; 26import android.content.Context; 27import android.content.Intent; 28import android.content.IntentFilter; 29import android.content.SharedPreferences; 30import android.content.pm.PackageManager; 31import android.content.res.Resources; 32import android.database.Cursor; 33import android.database.MatrixCursor; 34import android.graphics.Color; 35import android.graphics.drawable.Drawable; 36import android.graphics.drawable.LayerDrawable; 37import android.net.Uri; 38import android.os.Build; 39import android.os.Bundle; 40import android.os.Handler; 41import android.provider.CalendarContract.Calendars; 42import android.text.Spannable; 43import android.text.SpannableString; 44import android.text.Spanned; 45import android.text.TextUtils; 46import android.text.format.DateFormat; 47import android.text.format.DateUtils; 48import android.text.format.Time; 49import android.text.method.LinkMovementMethod; 50import android.text.method.MovementMethod; 51import android.text.style.URLSpan; 52import android.text.util.Linkify; 53import android.util.Log; 54import android.widget.SearchView; 55import android.widget.TextView; 56 57import com.android.calendar.CalendarController.ViewType; 58import com.android.calendar.CalendarUtils.TimeZoneUtils; 59 60import java.util.ArrayList; 61import java.util.Arrays; 62import java.util.Calendar; 63import java.util.Formatter; 64import java.util.HashMap; 65import java.util.Iterator; 66import java.util.LinkedHashSet; 67import java.util.LinkedList; 68import java.util.List; 69import java.util.Locale; 70import java.util.Map; 71import java.util.Set; 72import java.util.TimeZone; 73import java.util.regex.Matcher; 74import java.util.regex.Pattern; 75 76public class Utils { 77 private static final boolean DEBUG = false; 78 private static final String TAG = "CalUtils"; 79 80 // Set to 0 until we have UI to perform undo 81 public static final long UNDO_DELAY = 0; 82 83 // For recurring events which instances of the series are being modified 84 public static final int MODIFY_UNINITIALIZED = 0; 85 public static final int MODIFY_SELECTED = 1; 86 public static final int MODIFY_ALL_FOLLOWING = 2; 87 public static final int MODIFY_ALL = 3; 88 89 // When the edit event view finishes it passes back the appropriate exit 90 // code. 91 public static final int DONE_REVERT = 1 << 0; 92 public static final int DONE_SAVE = 1 << 1; 93 public static final int DONE_DELETE = 1 << 2; 94 // And should re run with DONE_EXIT if it should also leave the view, just 95 // exiting is identical to reverting 96 public static final int DONE_EXIT = 1 << 0; 97 98 public static final String OPEN_EMAIL_MARKER = " <"; 99 public static final String CLOSE_EMAIL_MARKER = ">"; 100 101 public static final String INTENT_KEY_DETAIL_VIEW = "DETAIL_VIEW"; 102 public static final String INTENT_KEY_VIEW_TYPE = "VIEW"; 103 public static final String INTENT_VALUE_VIEW_TYPE_DAY = "DAY"; 104 public static final String INTENT_KEY_HOME = "KEY_HOME"; 105 106 public static final int MONDAY_BEFORE_JULIAN_EPOCH = Time.EPOCH_JULIAN_DAY - 3; 107 public static final int DECLINED_EVENT_ALPHA = 0x66; 108 public static final int DECLINED_EVENT_TEXT_ALPHA = 0xC0; 109 110 private static final float SATURATION_ADJUST = 1.3f; 111 private static final float INTENSITY_ADJUST = 0.8f; 112 113 // Defines used by the DNA generation code 114 static final int DAY_IN_MINUTES = 60 * 24; 115 static final int WEEK_IN_MINUTES = DAY_IN_MINUTES * 7; 116 // The work day is being counted as 6am to 8pm 117 static int WORK_DAY_MINUTES = 14 * 60; 118 static int WORK_DAY_START_MINUTES = 6 * 60; 119 static int WORK_DAY_END_MINUTES = 20 * 60; 120 static int WORK_DAY_END_LENGTH = (24 * 60) - WORK_DAY_END_MINUTES; 121 static int CONFLICT_COLOR = 0xFF000000; 122 static boolean mMinutesLoaded = false; 123 124 // The name of the shared preferences file. This name must be maintained for 125 // historical 126 // reasons, as it's what PreferenceManager assigned the first time the file 127 // was created. 128 static final String SHARED_PREFS_NAME = "com.android.calendar_preferences"; 129 130 public static final String KEY_QUICK_RESPONSES = "preferences_quick_responses"; 131 132 public static final String APPWIDGET_DATA_TYPE = "vnd.android.data/update"; 133 134 static final String MACHINE_GENERATED_ADDRESS = "calendar.google.com"; 135 136 private static final TimeZoneUtils mTZUtils = new TimeZoneUtils(SHARED_PREFS_NAME); 137 private static boolean mAllowWeekForDetailView = false; 138 private static long mTardis = 0; 139 private static String sVersion = null; 140 141 private static final Pattern mWildcardPattern = Pattern.compile("^.*$"); 142 143 /** 144 * A coordinate must be of the following form for Google Maps to correctly use it: 145 * Latitude, Longitude 146 * 147 * This may be in decimal form: 148 * Latitude: {-90 to 90} 149 * Longitude: {-180 to 180} 150 * 151 * Or, in degrees, minutes, and seconds: 152 * Latitude: {-90 to 90}° {0 to 59}' {0 to 59}" 153 * Latitude: {-180 to 180}° {0 to 59}' {0 to 59}" 154 * + or - degrees may also be represented with N or n, S or s for latitude, and with 155 * E or e, W or w for longitude, where the direction may either precede or follow the value. 156 * 157 * Some examples of coordinates that will be accepted by the regex: 158 * 37.422081°, -122.084576° 159 * 37.422081,-122.084576 160 * +37°25'19.49", -122°5'4.47" 161 * 37°25'19.49"N, 122°5'4.47"W 162 * N 37° 25' 19.49", W 122° 5' 4.47" 163 **/ 164 private static final String COORD_DEGREES_LATITUDE = 165 "([-+NnSs]" + "(\\s)*)?" 166 + "[1-9]?[0-9](\u00B0)" + "(\\s)*" 167 + "([1-5]?[0-9]\')?" + "(\\s)*" 168 + "([1-5]?[0-9]" + "(\\.[0-9]+)?\")?" 169 + "((\\s)*" + "[NnSs])?"; 170 private static final String COORD_DEGREES_LONGITUDE = 171 "([-+EeWw]" + "(\\s)*)?" 172 + "(1)?[0-9]?[0-9](\u00B0)" + "(\\s)*" 173 + "([1-5]?[0-9]\')?" + "(\\s)*" 174 + "([1-5]?[0-9]" + "(\\.[0-9]+)?\")?" 175 + "((\\s)*" + "[EeWw])?"; 176 private static final String COORD_DEGREES_PATTERN = 177 COORD_DEGREES_LATITUDE 178 + "(\\s)*" + "," + "(\\s)*" 179 + COORD_DEGREES_LONGITUDE; 180 private static final String COORD_DECIMAL_LATITUDE = 181 "[+-]?" 182 + "[1-9]?[0-9]" + "(\\.[0-9]+)" 183 + "(\u00B0)?"; 184 private static final String COORD_DECIMAL_LONGITUDE = 185 "[+-]?" 186 + "(1)?[0-9]?[0-9]" + "(\\.[0-9]+)" 187 + "(\u00B0)?"; 188 private static final String COORD_DECIMAL_PATTERN = 189 COORD_DECIMAL_LATITUDE 190 + "(\\s)*" + "," + "(\\s)*" 191 + COORD_DECIMAL_LONGITUDE; 192 private static final Pattern COORD_PATTERN = 193 Pattern.compile(COORD_DEGREES_PATTERN + "|" + COORD_DECIMAL_PATTERN); 194 195 private static final String NANP_ALLOWED_SYMBOLS = "()+-*#."; 196 private static final int NANP_MIN_DIGITS = 7; 197 private static final int NANP_MAX_DIGITS = 11; 198 199 200 /** 201 * Returns whether the SDK is the Jellybean release or later. 202 */ 203 public static boolean isJellybeanOrLater() { 204 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; 205 } 206 207 public static int getViewTypeFromIntentAndSharedPref(Activity activity) { 208 Intent intent = activity.getIntent(); 209 Bundle extras = intent.getExtras(); 210 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(activity); 211 212 if (TextUtils.equals(intent.getAction(), Intent.ACTION_EDIT)) { 213 return ViewType.EDIT; 214 } 215 if (extras != null) { 216 if (extras.getBoolean(INTENT_KEY_DETAIL_VIEW, false)) { 217 // This is the "detail" view which is either agenda or day view 218 return prefs.getInt(GeneralPreferences.KEY_DETAILED_VIEW, 219 GeneralPreferences.DEFAULT_DETAILED_VIEW); 220 } else if (INTENT_VALUE_VIEW_TYPE_DAY.equals(extras.getString(INTENT_KEY_VIEW_TYPE))) { 221 // Not sure who uses this. This logic came from LaunchActivity 222 return ViewType.DAY; 223 } 224 } 225 226 // Default to the last view 227 return prefs.getInt( 228 GeneralPreferences.KEY_START_VIEW, GeneralPreferences.DEFAULT_START_VIEW); 229 } 230 231 /** 232 * Gets the intent action for telling the widget to update. 233 */ 234 public static String getWidgetUpdateAction(Context context) { 235 return context.getPackageName() + ".APPWIDGET_UPDATE"; 236 } 237 238 /** 239 * Gets the intent action for telling the widget to update. 240 */ 241 public static String getWidgetScheduledUpdateAction(Context context) { 242 return context.getPackageName() + ".APPWIDGET_SCHEDULED_UPDATE"; 243 } 244 245 /** 246 * Gets the intent action for telling the widget to update. 247 */ 248 public static String getSearchAuthority(Context context) { 249 return context.getPackageName() + ".CalendarRecentSuggestionsProvider"; 250 } 251 252 /** 253 * Writes a new home time zone to the db. Updates the home time zone in the 254 * db asynchronously and updates the local cache. Sending a time zone of 255 * **tbd** will cause it to be set to the device's time zone. null or empty 256 * tz will be ignored. 257 * 258 * @param context The calling activity 259 * @param timeZone The time zone to set Calendar to, or **tbd** 260 */ 261 public static void setTimeZone(Context context, String timeZone) { 262 mTZUtils.setTimeZone(context, timeZone); 263 } 264 265 /** 266 * Gets the time zone that Calendar should be displayed in This is a helper 267 * method to get the appropriate time zone for Calendar. If this is the 268 * first time this method has been called it will initiate an asynchronous 269 * query to verify that the data in preferences is correct. The callback 270 * supplied will only be called if this query returns a value other than 271 * what is stored in preferences and should cause the calling activity to 272 * refresh anything that depends on calling this method. 273 * 274 * @param context The calling activity 275 * @param callback The runnable that should execute if a query returns new 276 * values 277 * @return The string value representing the time zone Calendar should 278 * display 279 */ 280 public static String getTimeZone(Context context, Runnable callback) { 281 return mTZUtils.getTimeZone(context, callback); 282 } 283 284 /** 285 * Formats a date or a time range according to the local conventions. 286 * 287 * @param context the context is required only if the time is shown 288 * @param startMillis the start time in UTC milliseconds 289 * @param endMillis the end time in UTC milliseconds 290 * @param flags a bit mask of options See {@link DateUtils#formatDateRange(Context, Formatter, 291 * long, long, int, String) formatDateRange} 292 * @return a string containing the formatted date/time range. 293 */ 294 public static String formatDateRange( 295 Context context, long startMillis, long endMillis, int flags) { 296 return mTZUtils.formatDateRange(context, startMillis, endMillis, flags); 297 } 298 299 public static String[] getSharedPreference(Context context, String key, String[] defaultValue) { 300 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 301 Set<String> ss = prefs.getStringSet(key, null); 302 if (ss != null) { 303 String strings[] = new String[ss.size()]; 304 return ss.toArray(strings); 305 } 306 return defaultValue; 307 } 308 309 public static String getSharedPreference(Context context, String key, String defaultValue) { 310 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 311 return prefs.getString(key, defaultValue); 312 } 313 314 public static int getSharedPreference(Context context, String key, int defaultValue) { 315 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 316 return prefs.getInt(key, defaultValue); 317 } 318 319 public static boolean getSharedPreference(Context context, String key, boolean defaultValue) { 320 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 321 return prefs.getBoolean(key, defaultValue); 322 } 323 324 /** 325 * Asynchronously sets the preference with the given key to the given value 326 * 327 * @param context the context to use to get preferences from 328 * @param key the key of the preference to set 329 * @param value the value to set 330 */ 331 public static void setSharedPreference(Context context, String key, String value) { 332 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 333 prefs.edit().putString(key, value).apply(); 334 } 335 336 public static void setSharedPreference(Context context, String key, String[] values) { 337 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 338 LinkedHashSet<String> set = new LinkedHashSet<String>(); 339 for (String value : values) { 340 set.add(value); 341 } 342 prefs.edit().putStringSet(key, set).apply(); 343 } 344 345 protected static void tardis() { 346 mTardis = System.currentTimeMillis(); 347 } 348 349 protected static long getTardis() { 350 return mTardis; 351 } 352 353 public static void setSharedPreference(Context context, String key, boolean value) { 354 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 355 SharedPreferences.Editor editor = prefs.edit(); 356 editor.putBoolean(key, value); 357 editor.apply(); 358 } 359 360 static void setSharedPreference(Context context, String key, int value) { 361 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 362 SharedPreferences.Editor editor = prefs.edit(); 363 editor.putInt(key, value); 364 editor.apply(); 365 } 366 367 /** 368 * Save default agenda/day/week/month view for next time 369 * 370 * @param context 371 * @param viewId {@link CalendarController.ViewType} 372 */ 373 static void setDefaultView(Context context, int viewId) { 374 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 375 SharedPreferences.Editor editor = prefs.edit(); 376 377 boolean validDetailView = false; 378 if (mAllowWeekForDetailView && viewId == CalendarController.ViewType.WEEK) { 379 validDetailView = true; 380 } else { 381 validDetailView = viewId == CalendarController.ViewType.AGENDA 382 || viewId == CalendarController.ViewType.DAY; 383 } 384 385 if (validDetailView) { 386 // Record the detail start view 387 editor.putInt(GeneralPreferences.KEY_DETAILED_VIEW, viewId); 388 } 389 390 // Record the (new) start view 391 editor.putInt(GeneralPreferences.KEY_START_VIEW, viewId); 392 editor.apply(); 393 } 394 395 public static MatrixCursor matrixCursorFromCursor(Cursor cursor) { 396 if (cursor == null) { 397 return null; 398 } 399 400 String[] columnNames = cursor.getColumnNames(); 401 if (columnNames == null) { 402 columnNames = new String[] {}; 403 } 404 MatrixCursor newCursor = new MatrixCursor(columnNames); 405 int numColumns = cursor.getColumnCount(); 406 String data[] = new String[numColumns]; 407 cursor.moveToPosition(-1); 408 while (cursor.moveToNext()) { 409 for (int i = 0; i < numColumns; i++) { 410 data[i] = cursor.getString(i); 411 } 412 newCursor.addRow(data); 413 } 414 return newCursor; 415 } 416 417 /** 418 * Compares two cursors to see if they contain the same data. 419 * 420 * @return Returns true of the cursors contain the same data and are not 421 * null, false otherwise 422 */ 423 public static boolean compareCursors(Cursor c1, Cursor c2) { 424 if (c1 == null || c2 == null) { 425 return false; 426 } 427 428 int numColumns = c1.getColumnCount(); 429 if (numColumns != c2.getColumnCount()) { 430 return false; 431 } 432 433 if (c1.getCount() != c2.getCount()) { 434 return false; 435 } 436 437 c1.moveToPosition(-1); 438 c2.moveToPosition(-1); 439 while (c1.moveToNext() && c2.moveToNext()) { 440 for (int i = 0; i < numColumns; i++) { 441 if (!TextUtils.equals(c1.getString(i), c2.getString(i))) { 442 return false; 443 } 444 } 445 } 446 447 return true; 448 } 449 450 /** 451 * If the given intent specifies a time (in milliseconds since the epoch), 452 * then that time is returned. Otherwise, the current time is returned. 453 */ 454 public static final long timeFromIntentInMillis(Intent intent) { 455 // If the time was specified, then use that. Otherwise, use the current 456 // time. 457 Uri data = intent.getData(); 458 long millis = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1); 459 if (millis == -1 && data != null && data.isHierarchical()) { 460 List<String> path = data.getPathSegments(); 461 if (path.size() == 2 && path.get(0).equals("time")) { 462 try { 463 millis = Long.valueOf(data.getLastPathSegment()); 464 } catch (NumberFormatException e) { 465 Log.i("Calendar", "timeFromIntentInMillis: Data existed but no valid time " 466 + "found. Using current time."); 467 } 468 } 469 } 470 if (millis <= 0) { 471 millis = System.currentTimeMillis(); 472 } 473 return millis; 474 } 475 476 /** 477 * Formats the given Time object so that it gives the month and year (for 478 * example, "September 2007"). 479 * 480 * @param time the time to format 481 * @return the string containing the weekday and the date 482 */ 483 public static String formatMonthYear(Context context, Time time) { 484 int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY 485 | DateUtils.FORMAT_SHOW_YEAR; 486 long millis = time.toMillis(true); 487 return formatDateRange(context, millis, millis, flags); 488 } 489 490 /** 491 * Returns a list joined together by the provided delimiter, for example, 492 * ["a", "b", "c"] could be joined into "a,b,c" 493 * 494 * @param things the things to join together 495 * @param delim the delimiter to use 496 * @return a string contained the things joined together 497 */ 498 public static String join(List<?> things, String delim) { 499 StringBuilder builder = new StringBuilder(); 500 boolean first = true; 501 for (Object thing : things) { 502 if (first) { 503 first = false; 504 } else { 505 builder.append(delim); 506 } 507 builder.append(thing.toString()); 508 } 509 return builder.toString(); 510 } 511 512 /** 513 * Returns the week since {@link Time#EPOCH_JULIAN_DAY} (Jan 1, 1970) 514 * adjusted for first day of week. 515 * 516 * This takes a julian day and the week start day and calculates which 517 * week since {@link Time#EPOCH_JULIAN_DAY} that day occurs in, starting 518 * at 0. *Do not* use this to compute the ISO week number for the year. 519 * 520 * @param julianDay The julian day to calculate the week number for 521 * @param firstDayOfWeek Which week day is the first day of the week, 522 * see {@link Time#SUNDAY} 523 * @return Weeks since the epoch 524 */ 525 public static int getWeeksSinceEpochFromJulianDay(int julianDay, int firstDayOfWeek) { 526 int diff = Time.THURSDAY - firstDayOfWeek; 527 if (diff < 0) { 528 diff += 7; 529 } 530 int refDay = Time.EPOCH_JULIAN_DAY - diff; 531 return (julianDay - refDay) / 7; 532 } 533 534 /** 535 * Takes a number of weeks since the epoch and calculates the Julian day of 536 * the Monday for that week. 537 * 538 * This assumes that the week containing the {@link Time#EPOCH_JULIAN_DAY} 539 * is considered week 0. It returns the Julian day for the Monday 540 * {@code week} weeks after the Monday of the week containing the epoch. 541 * 542 * @param week Number of weeks since the epoch 543 * @return The julian day for the Monday of the given week since the epoch 544 */ 545 public static int getJulianMondayFromWeeksSinceEpoch(int week) { 546 return MONDAY_BEFORE_JULIAN_EPOCH + week * 7; 547 } 548 549 /** 550 * Get first day of week as android.text.format.Time constant. 551 * 552 * @return the first day of week in android.text.format.Time 553 */ 554 public static int getFirstDayOfWeek(Context context) { 555 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 556 String pref = prefs.getString( 557 GeneralPreferences.KEY_WEEK_START_DAY, GeneralPreferences.WEEK_START_DEFAULT); 558 559 int startDay; 560 if (GeneralPreferences.WEEK_START_DEFAULT.equals(pref)) { 561 startDay = Calendar.getInstance().getFirstDayOfWeek(); 562 } else { 563 startDay = Integer.parseInt(pref); 564 } 565 566 if (startDay == Calendar.SATURDAY) { 567 return Time.SATURDAY; 568 } else if (startDay == Calendar.MONDAY) { 569 return Time.MONDAY; 570 } else { 571 return Time.SUNDAY; 572 } 573 } 574 575 /** 576 * @return true when week number should be shown. 577 */ 578 public static boolean getShowWeekNumber(Context context) { 579 final SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 580 return prefs.getBoolean( 581 GeneralPreferences.KEY_SHOW_WEEK_NUM, GeneralPreferences.DEFAULT_SHOW_WEEK_NUM); 582 } 583 584 /** 585 * @return true when declined events should be hidden. 586 */ 587 public static boolean getHideDeclinedEvents(Context context) { 588 final SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 589 return prefs.getBoolean(GeneralPreferences.KEY_HIDE_DECLINED, false); 590 } 591 592 public static int getDaysPerWeek(Context context) { 593 final SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 594 return prefs.getInt(GeneralPreferences.KEY_DAYS_PER_WEEK, 7); 595 } 596 597 /** 598 * Determine whether the column position is Saturday or not. 599 * 600 * @param column the column position 601 * @param firstDayOfWeek the first day of week in android.text.format.Time 602 * @return true if the column is Saturday position 603 */ 604 public static boolean isSaturday(int column, int firstDayOfWeek) { 605 return (firstDayOfWeek == Time.SUNDAY && column == 6) 606 || (firstDayOfWeek == Time.MONDAY && column == 5) 607 || (firstDayOfWeek == Time.SATURDAY && column == 0); 608 } 609 610 /** 611 * Determine whether the column position is Sunday or not. 612 * 613 * @param column the column position 614 * @param firstDayOfWeek the first day of week in android.text.format.Time 615 * @return true if the column is Sunday position 616 */ 617 public static boolean isSunday(int column, int firstDayOfWeek) { 618 return (firstDayOfWeek == Time.SUNDAY && column == 0) 619 || (firstDayOfWeek == Time.MONDAY && column == 6) 620 || (firstDayOfWeek == Time.SATURDAY && column == 1); 621 } 622 623 /** 624 * Convert given UTC time into current local time. This assumes it is for an 625 * allday event and will adjust the time to be on a midnight boundary. 626 * 627 * @param recycle Time object to recycle, otherwise null. 628 * @param utcTime Time to convert, in UTC. 629 * @param tz The time zone to convert this time to. 630 */ 631 public static long convertAlldayUtcToLocal(Time recycle, long utcTime, String tz) { 632 if (recycle == null) { 633 recycle = new Time(); 634 } 635 recycle.timezone = Time.TIMEZONE_UTC; 636 recycle.set(utcTime); 637 recycle.timezone = tz; 638 return recycle.normalize(true); 639 } 640 641 public static long convertAlldayLocalToUTC(Time recycle, long localTime, String tz) { 642 if (recycle == null) { 643 recycle = new Time(); 644 } 645 recycle.timezone = tz; 646 recycle.set(localTime); 647 recycle.timezone = Time.TIMEZONE_UTC; 648 return recycle.normalize(true); 649 } 650 651 /** 652 * Finds and returns the next midnight after "theTime" in milliseconds UTC 653 * 654 * @param recycle - Time object to recycle, otherwise null. 655 * @param theTime - Time used for calculations (in UTC) 656 * @param tz The time zone to convert this time to. 657 */ 658 public static long getNextMidnight(Time recycle, long theTime, String tz) { 659 if (recycle == null) { 660 recycle = new Time(); 661 } 662 recycle.timezone = tz; 663 recycle.set(theTime); 664 recycle.monthDay ++; 665 recycle.hour = 0; 666 recycle.minute = 0; 667 recycle.second = 0; 668 return recycle.normalize(true); 669 } 670 671 /** 672 * Scan through a cursor of calendars and check if names are duplicated. 673 * This travels a cursor containing calendar display names and fills in the 674 * provided map with whether or not each name is repeated. 675 * 676 * @param isDuplicateName The map to put the duplicate check results in. 677 * @param cursor The query of calendars to check 678 * @param nameIndex The column of the query that contains the display name 679 */ 680 public static void checkForDuplicateNames( 681 Map<String, Boolean> isDuplicateName, Cursor cursor, int nameIndex) { 682 isDuplicateName.clear(); 683 cursor.moveToPosition(-1); 684 while (cursor.moveToNext()) { 685 String displayName = cursor.getString(nameIndex); 686 // Set it to true if we've seen this name before, false otherwise 687 if (displayName != null) { 688 isDuplicateName.put(displayName, isDuplicateName.containsKey(displayName)); 689 } 690 } 691 } 692 693 /** 694 * Null-safe object comparison 695 * 696 * @param s1 697 * @param s2 698 * @return 699 */ 700 public static boolean equals(Object o1, Object o2) { 701 return o1 == null ? o2 == null : o1.equals(o2); 702 } 703 704 public static void setAllowWeekForDetailView(boolean allowWeekView) { 705 mAllowWeekForDetailView = allowWeekView; 706 } 707 708 public static boolean getAllowWeekForDetailView() { 709 return mAllowWeekForDetailView; 710 } 711 712 public static boolean getConfigBool(Context c, int key) { 713 return c.getResources().getBoolean(key); 714 } 715 716 public static int getDisplayColorFromColor(int color) { 717 if (!isJellybeanOrLater()) { 718 return color; 719 } 720 721 float[] hsv = new float[3]; 722 Color.colorToHSV(color, hsv); 723 hsv[1] = Math.min(hsv[1] * SATURATION_ADJUST, 1.0f); 724 hsv[2] = hsv[2] * INTENSITY_ADJUST; 725 return Color.HSVToColor(hsv); 726 } 727 728 // This takes a color and computes what it would look like blended with 729 // white. The result is the color that should be used for declined events. 730 public static int getDeclinedColorFromColor(int color) { 731 int bg = 0xffffffff; 732 int a = DECLINED_EVENT_ALPHA; 733 int r = (((color & 0x00ff0000) * a) + ((bg & 0x00ff0000) * (0xff - a))) & 0xff000000; 734 int g = (((color & 0x0000ff00) * a) + ((bg & 0x0000ff00) * (0xff - a))) & 0x00ff0000; 735 int b = (((color & 0x000000ff) * a) + ((bg & 0x000000ff) * (0xff - a))) & 0x0000ff00; 736 return (0xff000000) | ((r | g | b) >> 8); 737 } 738 739 // A single strand represents one color of events. Events are divided up by 740 // color to make them convenient to draw. The black strand is special in 741 // that it holds conflicting events as well as color settings for allday on 742 // each day. 743 public static class DNAStrand { 744 public float[] points; 745 public int[] allDays; // color for the allday, 0 means no event 746 int position; 747 public int color; 748 int count; 749 } 750 751 // A segment is a single continuous length of time occupied by a single 752 // color. Segments should never span multiple days. 753 private static class DNASegment { 754 int startMinute; // in minutes since the start of the week 755 int endMinute; 756 int color; // Calendar color or black for conflicts 757 int day; // quick reference to the day this segment is on 758 } 759 760 /** 761 * Converts a list of events to a list of segments to draw. Assumes list is 762 * ordered by start time of the events. The function processes events for a 763 * range of days from firstJulianDay to firstJulianDay + dayXs.length - 1. 764 * The algorithm goes over all the events and creates a set of segments 765 * ordered by start time. This list of segments is then converted into a 766 * HashMap of strands which contain the draw points and are organized by 767 * color. The strands can then be drawn by setting the paint color to each 768 * strand's color and calling drawLines on its set of points. The points are 769 * set up using the following parameters. 770 * <ul> 771 * <li>Events between midnight and WORK_DAY_START_MINUTES are compressed 772 * into the first 1/8th of the space between top and bottom.</li> 773 * <li>Events between WORK_DAY_END_MINUTES and the following midnight are 774 * compressed into the last 1/8th of the space between top and bottom</li> 775 * <li>Events between WORK_DAY_START_MINUTES and WORK_DAY_END_MINUTES use 776 * the remaining 3/4ths of the space</li> 777 * <li>All segments drawn will maintain at least minPixels height, except 778 * for conflicts in the first or last 1/8th, which may be smaller</li> 779 * </ul> 780 * 781 * @param firstJulianDay The julian day of the first day of events 782 * @param events A list of events sorted by start time 783 * @param top The lowest y value the dna should be drawn at 784 * @param bottom The highest y value the dna should be drawn at 785 * @param dayXs An array of x values to draw the dna at, one for each day 786 * @param conflictColor the color to use for conflicts 787 * @return 788 */ 789 public static HashMap<Integer, DNAStrand> createDNAStrands(int firstJulianDay, 790 ArrayList<Event> events, int top, int bottom, int minPixels, int[] dayXs, 791 Context context) { 792 793 if (!mMinutesLoaded) { 794 if (context == null) { 795 Log.wtf(TAG, "No context and haven't loaded parameters yet! Can't create DNA."); 796 } 797 Resources res = context.getResources(); 798 CONFLICT_COLOR = res.getColor(R.color.month_dna_conflict_time_color); 799 WORK_DAY_START_MINUTES = res.getInteger(R.integer.work_start_minutes); 800 WORK_DAY_END_MINUTES = res.getInteger(R.integer.work_end_minutes); 801 WORK_DAY_END_LENGTH = DAY_IN_MINUTES - WORK_DAY_END_MINUTES; 802 WORK_DAY_MINUTES = WORK_DAY_END_MINUTES - WORK_DAY_START_MINUTES; 803 mMinutesLoaded = true; 804 } 805 806 if (events == null || events.isEmpty() || dayXs == null || dayXs.length < 1 807 || bottom - top < 8 || minPixels < 0) { 808 Log.e(TAG, 809 "Bad values for createDNAStrands! events:" + events + " dayXs:" 810 + Arrays.toString(dayXs) + " bot-top:" + (bottom - top) + " minPixels:" 811 + minPixels); 812 return null; 813 } 814 815 LinkedList<DNASegment> segments = new LinkedList<DNASegment>(); 816 HashMap<Integer, DNAStrand> strands = new HashMap<Integer, DNAStrand>(); 817 // add a black strand by default, other colors will get added in 818 // the loop 819 DNAStrand blackStrand = new DNAStrand(); 820 blackStrand.color = CONFLICT_COLOR; 821 strands.put(CONFLICT_COLOR, blackStrand); 822 // the min length is the number of minutes that will occupy 823 // MIN_SEGMENT_PIXELS in the 'work day' time slot. This computes the 824 // minutes/pixel * minpx where the number of pixels are 3/4 the total 825 // dna height: 4*(mins/(px * 3/4)) 826 int minMinutes = minPixels * 4 * WORK_DAY_MINUTES / (3 * (bottom - top)); 827 828 // There are slightly fewer than half as many pixels in 1/6 the space, 829 // so round to 2.5x for the min minutes in the non-work area 830 int minOtherMinutes = minMinutes * 5 / 2; 831 int lastJulianDay = firstJulianDay + dayXs.length - 1; 832 833 Event event = new Event(); 834 // Go through all the events for the week 835 for (Event currEvent : events) { 836 // if this event is outside the weeks range skip it 837 if (currEvent.endDay < firstJulianDay || currEvent.startDay > lastJulianDay) { 838 continue; 839 } 840 if (currEvent.drawAsAllday()) { 841 addAllDayToStrands(currEvent, strands, firstJulianDay, dayXs.length); 842 continue; 843 } 844 // Copy the event over so we can clip its start and end to our range 845 currEvent.copyTo(event); 846 if (event.startDay < firstJulianDay) { 847 event.startDay = firstJulianDay; 848 event.startTime = 0; 849 } 850 // If it starts after the work day make sure the start is at least 851 // minPixels from midnight 852 if (event.startTime > DAY_IN_MINUTES - minOtherMinutes) { 853 event.startTime = DAY_IN_MINUTES - minOtherMinutes; 854 } 855 if (event.endDay > lastJulianDay) { 856 event.endDay = lastJulianDay; 857 event.endTime = DAY_IN_MINUTES - 1; 858 } 859 // If the end time is before the work day make sure it ends at least 860 // minPixels after midnight 861 if (event.endTime < minOtherMinutes) { 862 event.endTime = minOtherMinutes; 863 } 864 // If the start and end are on the same day make sure they are at 865 // least minPixels apart. This only needs to be done for times 866 // outside the work day as the min distance for within the work day 867 // is enforced in the segment code. 868 if (event.startDay == event.endDay && 869 event.endTime - event.startTime < minOtherMinutes) { 870 // If it's less than minPixels in an area before the work 871 // day 872 if (event.startTime < WORK_DAY_START_MINUTES) { 873 // extend the end to the first easy guarantee that it's 874 // minPixels 875 event.endTime = Math.min(event.startTime + minOtherMinutes, 876 WORK_DAY_START_MINUTES + minMinutes); 877 // if it's in the area after the work day 878 } else if (event.endTime > WORK_DAY_END_MINUTES) { 879 // First try shifting the end but not past midnight 880 event.endTime = Math.min(event.endTime + minOtherMinutes, DAY_IN_MINUTES - 1); 881 // if it's still too small move the start back 882 if (event.endTime - event.startTime < minOtherMinutes) { 883 event.startTime = event.endTime - minOtherMinutes; 884 } 885 } 886 } 887 888 // This handles adding the first segment 889 if (segments.size() == 0) { 890 addNewSegment(segments, event, strands, firstJulianDay, 0, minMinutes); 891 continue; 892 } 893 // Now compare our current start time to the end time of the last 894 // segment in the list 895 DNASegment lastSegment = segments.getLast(); 896 int startMinute = (event.startDay - firstJulianDay) * DAY_IN_MINUTES + event.startTime; 897 int endMinute = Math.max((event.endDay - firstJulianDay) * DAY_IN_MINUTES 898 + event.endTime, startMinute + minMinutes); 899 900 if (startMinute < 0) { 901 startMinute = 0; 902 } 903 if (endMinute >= WEEK_IN_MINUTES) { 904 endMinute = WEEK_IN_MINUTES - 1; 905 } 906 // If we start before the last segment in the list ends we need to 907 // start going through the list as this may conflict with other 908 // events 909 if (startMinute < lastSegment.endMinute) { 910 int i = segments.size(); 911 // find the last segment this event intersects with 912 while (--i >= 0 && endMinute < segments.get(i).startMinute); 913 914 DNASegment currSegment; 915 // for each segment this event intersects with 916 for (; i >= 0 && startMinute <= (currSegment = segments.get(i)).endMinute; i--) { 917 // if the segment is already a conflict ignore it 918 if (currSegment.color == CONFLICT_COLOR) { 919 continue; 920 } 921 // if the event ends before the segment and wouldn't create 922 // a segment that is too small split off the right side 923 if (endMinute < currSegment.endMinute - minMinutes) { 924 DNASegment rhs = new DNASegment(); 925 rhs.endMinute = currSegment.endMinute; 926 rhs.color = currSegment.color; 927 rhs.startMinute = endMinute + 1; 928 rhs.day = currSegment.day; 929 currSegment.endMinute = endMinute; 930 segments.add(i + 1, rhs); 931 strands.get(rhs.color).count++; 932 if (DEBUG) { 933 Log.d(TAG, "Added rhs, curr:" + currSegment.toString() + " i:" 934 + segments.get(i).toString()); 935 } 936 } 937 // if the event starts after the segment and wouldn't create 938 // a segment that is too small split off the left side 939 if (startMinute > currSegment.startMinute + minMinutes) { 940 DNASegment lhs = new DNASegment(); 941 lhs.startMinute = currSegment.startMinute; 942 lhs.color = currSegment.color; 943 lhs.endMinute = startMinute - 1; 944 lhs.day = currSegment.day; 945 currSegment.startMinute = startMinute; 946 // increment i so that we are at the right position when 947 // referencing the segments to the right and left of the 948 // current segment. 949 segments.add(i++, lhs); 950 strands.get(lhs.color).count++; 951 if (DEBUG) { 952 Log.d(TAG, "Added lhs, curr:" + currSegment.toString() + " i:" 953 + segments.get(i).toString()); 954 } 955 } 956 // if the right side is black merge this with the segment to 957 // the right if they're on the same day and overlap 958 if (i + 1 < segments.size()) { 959 DNASegment rhs = segments.get(i + 1); 960 if (rhs.color == CONFLICT_COLOR && currSegment.day == rhs.day 961 && rhs.startMinute <= currSegment.endMinute + 1) { 962 rhs.startMinute = Math.min(currSegment.startMinute, rhs.startMinute); 963 segments.remove(currSegment); 964 strands.get(currSegment.color).count--; 965 // point at the new current segment 966 currSegment = rhs; 967 } 968 } 969 // if the left side is black merge this with the segment to 970 // the left if they're on the same day and overlap 971 if (i - 1 >= 0) { 972 DNASegment lhs = segments.get(i - 1); 973 if (lhs.color == CONFLICT_COLOR && currSegment.day == lhs.day 974 && lhs.endMinute >= currSegment.startMinute - 1) { 975 lhs.endMinute = Math.max(currSegment.endMinute, lhs.endMinute); 976 segments.remove(currSegment); 977 strands.get(currSegment.color).count--; 978 // point at the new current segment 979 currSegment = lhs; 980 // point i at the new current segment in case new 981 // code is added 982 i--; 983 } 984 } 985 // if we're still not black, decrement the count for the 986 // color being removed, change this to black, and increment 987 // the black count 988 if (currSegment.color != CONFLICT_COLOR) { 989 strands.get(currSegment.color).count--; 990 currSegment.color = CONFLICT_COLOR; 991 strands.get(CONFLICT_COLOR).count++; 992 } 993 } 994 995 } 996 // If this event extends beyond the last segment add a new segment 997 if (endMinute > lastSegment.endMinute) { 998 addNewSegment(segments, event, strands, firstJulianDay, lastSegment.endMinute, 999 minMinutes); 1000 } 1001 } 1002 weaveDNAStrands(segments, firstJulianDay, strands, top, bottom, dayXs); 1003 return strands; 1004 } 1005 1006 // This figures out allDay colors as allDay events are found 1007 private static void addAllDayToStrands(Event event, HashMap<Integer, DNAStrand> strands, 1008 int firstJulianDay, int numDays) { 1009 DNAStrand strand = getOrCreateStrand(strands, CONFLICT_COLOR); 1010 // if we haven't initialized the allDay portion create it now 1011 if (strand.allDays == null) { 1012 strand.allDays = new int[numDays]; 1013 } 1014 1015 // For each day this event is on update the color 1016 int end = Math.min(event.endDay - firstJulianDay, numDays - 1); 1017 for (int i = Math.max(event.startDay - firstJulianDay, 0); i <= end; i++) { 1018 if (strand.allDays[i] != 0) { 1019 // if this day already had a color, it is now a conflict 1020 strand.allDays[i] = CONFLICT_COLOR; 1021 } else { 1022 // else it's just the color of the event 1023 strand.allDays[i] = event.color; 1024 } 1025 } 1026 } 1027 1028 // This processes all the segments, sorts them by color, and generates a 1029 // list of points to draw 1030 private static void weaveDNAStrands(LinkedList<DNASegment> segments, int firstJulianDay, 1031 HashMap<Integer, DNAStrand> strands, int top, int bottom, int[] dayXs) { 1032 // First, get rid of any colors that ended up with no segments 1033 Iterator<DNAStrand> strandIterator = strands.values().iterator(); 1034 while (strandIterator.hasNext()) { 1035 DNAStrand strand = strandIterator.next(); 1036 if (strand.count < 1 && strand.allDays == null) { 1037 strandIterator.remove(); 1038 continue; 1039 } 1040 strand.points = new float[strand.count * 4]; 1041 strand.position = 0; 1042 } 1043 // Go through each segment and compute its points 1044 for (DNASegment segment : segments) { 1045 // Add the points to the strand of that color 1046 DNAStrand strand = strands.get(segment.color); 1047 int dayIndex = segment.day - firstJulianDay; 1048 int dayStartMinute = segment.startMinute % DAY_IN_MINUTES; 1049 int dayEndMinute = segment.endMinute % DAY_IN_MINUTES; 1050 int height = bottom - top; 1051 int workDayHeight = height * 3 / 4; 1052 int remainderHeight = (height - workDayHeight) / 2; 1053 1054 int x = dayXs[dayIndex]; 1055 int y0 = 0; 1056 int y1 = 0; 1057 1058 y0 = top + getPixelOffsetFromMinutes(dayStartMinute, workDayHeight, remainderHeight); 1059 y1 = top + getPixelOffsetFromMinutes(dayEndMinute, workDayHeight, remainderHeight); 1060 if (DEBUG) { 1061 Log.d(TAG, "Adding " + Integer.toHexString(segment.color) + " at x,y0,y1: " + x 1062 + " " + y0 + " " + y1 + " for " + dayStartMinute + " " + dayEndMinute); 1063 } 1064 strand.points[strand.position++] = x; 1065 strand.points[strand.position++] = y0; 1066 strand.points[strand.position++] = x; 1067 strand.points[strand.position++] = y1; 1068 } 1069 } 1070 1071 /** 1072 * Compute a pixel offset from the top for a given minute from the work day 1073 * height and the height of the top area. 1074 */ 1075 private static int getPixelOffsetFromMinutes(int minute, int workDayHeight, 1076 int remainderHeight) { 1077 int y; 1078 if (minute < WORK_DAY_START_MINUTES) { 1079 y = minute * remainderHeight / WORK_DAY_START_MINUTES; 1080 } else if (minute < WORK_DAY_END_MINUTES) { 1081 y = remainderHeight + (minute - WORK_DAY_START_MINUTES) * workDayHeight 1082 / WORK_DAY_MINUTES; 1083 } else { 1084 y = remainderHeight + workDayHeight + (minute - WORK_DAY_END_MINUTES) * remainderHeight 1085 / WORK_DAY_END_LENGTH; 1086 } 1087 return y; 1088 } 1089 1090 /** 1091 * Add a new segment based on the event provided. This will handle splitting 1092 * segments across day boundaries and ensures a minimum size for segments. 1093 */ 1094 private static void addNewSegment(LinkedList<DNASegment> segments, Event event, 1095 HashMap<Integer, DNAStrand> strands, int firstJulianDay, int minStart, int minMinutes) { 1096 if (event.startDay > event.endDay) { 1097 Log.wtf(TAG, "Event starts after it ends: " + event.toString()); 1098 } 1099 // If this is a multiday event split it up by day 1100 if (event.startDay != event.endDay) { 1101 Event lhs = new Event(); 1102 lhs.color = event.color; 1103 lhs.startDay = event.startDay; 1104 // the first day we want the start time to be the actual start time 1105 lhs.startTime = event.startTime; 1106 lhs.endDay = lhs.startDay; 1107 lhs.endTime = DAY_IN_MINUTES - 1; 1108 // Nearly recursive iteration! 1109 while (lhs.startDay != event.endDay) { 1110 addNewSegment(segments, lhs, strands, firstJulianDay, minStart, minMinutes); 1111 // The days in between are all day, even though that shouldn't 1112 // actually happen due to the allday filtering 1113 lhs.startDay++; 1114 lhs.endDay = lhs.startDay; 1115 lhs.startTime = 0; 1116 minStart = 0; 1117 } 1118 // The last day we want the end time to be the actual end time 1119 lhs.endTime = event.endTime; 1120 event = lhs; 1121 } 1122 // Create the new segment and compute its fields 1123 DNASegment segment = new DNASegment(); 1124 int dayOffset = (event.startDay - firstJulianDay) * DAY_IN_MINUTES; 1125 int endOfDay = dayOffset + DAY_IN_MINUTES - 1; 1126 // clip the start if needed 1127 segment.startMinute = Math.max(dayOffset + event.startTime, minStart); 1128 // and extend the end if it's too small, but not beyond the end of the 1129 // day 1130 int minEnd = Math.min(segment.startMinute + minMinutes, endOfDay); 1131 segment.endMinute = Math.max(dayOffset + event.endTime, minEnd); 1132 if (segment.endMinute > endOfDay) { 1133 segment.endMinute = endOfDay; 1134 } 1135 1136 segment.color = event.color; 1137 segment.day = event.startDay; 1138 segments.add(segment); 1139 // increment the count for the correct color or add a new strand if we 1140 // don't have that color yet 1141 DNAStrand strand = getOrCreateStrand(strands, segment.color); 1142 strand.count++; 1143 } 1144 1145 /** 1146 * Try to get a strand of the given color. Create it if it doesn't exist. 1147 */ 1148 private static DNAStrand getOrCreateStrand(HashMap<Integer, DNAStrand> strands, int color) { 1149 DNAStrand strand = strands.get(color); 1150 if (strand == null) { 1151 strand = new DNAStrand(); 1152 strand.color = color; 1153 strand.count = 0; 1154 strands.put(strand.color, strand); 1155 } 1156 return strand; 1157 } 1158 1159 /** 1160 * Sends an intent to launch the top level Calendar view. 1161 * 1162 * @param context 1163 */ 1164 public static void returnToCalendarHome(Context context) { 1165 Intent launchIntent = new Intent(context, AllInOneActivity.class); 1166 launchIntent.setAction(Intent.ACTION_DEFAULT); 1167 launchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 1168 launchIntent.putExtra(INTENT_KEY_HOME, true); 1169 context.startActivity(launchIntent); 1170 } 1171 1172 /** 1173 * This sets up a search view to use Calendar's search suggestions provider 1174 * and to allow refining the search. 1175 * 1176 * @param view The {@link SearchView} to set up 1177 * @param act The activity using the view 1178 */ 1179 public static void setUpSearchView(SearchView view, Activity act) { 1180 SearchManager searchManager = (SearchManager) act.getSystemService(Context.SEARCH_SERVICE); 1181 view.setSearchableInfo(searchManager.getSearchableInfo(act.getComponentName())); 1182 view.setQueryRefinementEnabled(true); 1183 } 1184 1185 /** 1186 * Given a context and a time in millis since unix epoch figures out the 1187 * correct week of the year for that time. 1188 * 1189 * @param millisSinceEpoch 1190 * @return 1191 */ 1192 public static int getWeekNumberFromTime(long millisSinceEpoch, Context context) { 1193 Time weekTime = new Time(getTimeZone(context, null)); 1194 weekTime.set(millisSinceEpoch); 1195 weekTime.normalize(true); 1196 int firstDayOfWeek = getFirstDayOfWeek(context); 1197 // if the date is on Saturday or Sunday and the start of the week 1198 // isn't Monday we may need to shift the date to be in the correct 1199 // week 1200 if (weekTime.weekDay == Time.SUNDAY 1201 && (firstDayOfWeek == Time.SUNDAY || firstDayOfWeek == Time.SATURDAY)) { 1202 weekTime.monthDay++; 1203 weekTime.normalize(true); 1204 } else if (weekTime.weekDay == Time.SATURDAY && firstDayOfWeek == Time.SATURDAY) { 1205 weekTime.monthDay += 2; 1206 weekTime.normalize(true); 1207 } 1208 return weekTime.getWeekNumber(); 1209 } 1210 1211 /** 1212 * Formats a day of the week string. This is either just the name of the day 1213 * or a combination of yesterday/today/tomorrow and the day of the week. 1214 * 1215 * @param julianDay The julian day to get the string for 1216 * @param todayJulianDay The julian day for today's date 1217 * @param millis A utc millis since epoch time that falls on julian day 1218 * @param context The calling context, used to get the timezone and do the 1219 * formatting 1220 * @return 1221 */ 1222 public static String getDayOfWeekString(int julianDay, int todayJulianDay, long millis, 1223 Context context) { 1224 getTimeZone(context, null); 1225 int flags = DateUtils.FORMAT_SHOW_WEEKDAY; 1226 String dayViewText; 1227 if (julianDay == todayJulianDay) { 1228 dayViewText = context.getString(R.string.agenda_today, 1229 mTZUtils.formatDateRange(context, millis, millis, flags).toString()); 1230 } else if (julianDay == todayJulianDay - 1) { 1231 dayViewText = context.getString(R.string.agenda_yesterday, 1232 mTZUtils.formatDateRange(context, millis, millis, flags).toString()); 1233 } else if (julianDay == todayJulianDay + 1) { 1234 dayViewText = context.getString(R.string.agenda_tomorrow, 1235 mTZUtils.formatDateRange(context, millis, millis, flags).toString()); 1236 } else { 1237 dayViewText = mTZUtils.formatDateRange(context, millis, millis, flags).toString(); 1238 } 1239 dayViewText = dayViewText.toUpperCase(); 1240 return dayViewText; 1241 } 1242 1243 // Calculate the time until midnight + 1 second and set the handler to 1244 // do run the runnable 1245 public static void setMidnightUpdater(Handler h, Runnable r, String timezone) { 1246 if (h == null || r == null || timezone == null) { 1247 return; 1248 } 1249 long now = System.currentTimeMillis(); 1250 Time time = new Time(timezone); 1251 time.set(now); 1252 long runInMillis = (24 * 3600 - time.hour * 3600 - time.minute * 60 - 1253 time.second + 1) * 1000; 1254 h.removeCallbacks(r); 1255 h.postDelayed(r, runInMillis); 1256 } 1257 1258 // Stop the midnight update thread 1259 public static void resetMidnightUpdater(Handler h, Runnable r) { 1260 if (h == null || r == null) { 1261 return; 1262 } 1263 h.removeCallbacks(r); 1264 } 1265 1266 /** 1267 * Returns a string description of the specified time interval. 1268 */ 1269 public static String getDisplayedDatetime(long startMillis, long endMillis, long currentMillis, 1270 String localTimezone, boolean allDay, Context context) { 1271 // Configure date/time formatting. 1272 int flagsDate = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY; 1273 int flagsTime = DateUtils.FORMAT_SHOW_TIME; 1274 if (DateFormat.is24HourFormat(context)) { 1275 flagsTime |= DateUtils.FORMAT_24HOUR; 1276 } 1277 1278 Time currentTime = new Time(localTimezone); 1279 currentTime.set(currentMillis); 1280 Resources resources = context.getResources(); 1281 String datetimeString = null; 1282 if (allDay) { 1283 // All day events require special timezone adjustment. 1284 long localStartMillis = convertAlldayUtcToLocal(null, startMillis, localTimezone); 1285 long localEndMillis = convertAlldayUtcToLocal(null, endMillis, localTimezone); 1286 if (singleDayEvent(localStartMillis, localEndMillis, currentTime.gmtoff)) { 1287 // If possible, use "Today" or "Tomorrow" instead of a full date string. 1288 int todayOrTomorrow = isTodayOrTomorrow(context.getResources(), 1289 localStartMillis, currentMillis, currentTime.gmtoff); 1290 if (TODAY == todayOrTomorrow) { 1291 datetimeString = resources.getString(R.string.today); 1292 } else if (TOMORROW == todayOrTomorrow) { 1293 datetimeString = resources.getString(R.string.tomorrow); 1294 } 1295 } 1296 if (datetimeString == null) { 1297 // For multi-day allday events or single-day all-day events that are not 1298 // today or tomorrow, use framework formatter. 1299 Formatter f = new Formatter(new StringBuilder(50), Locale.getDefault()); 1300 datetimeString = DateUtils.formatDateRange(context, f, startMillis, 1301 endMillis, flagsDate, Time.TIMEZONE_UTC).toString(); 1302 } 1303 } else { 1304 if (singleDayEvent(startMillis, endMillis, currentTime.gmtoff)) { 1305 // Format the time. 1306 String timeString = Utils.formatDateRange(context, startMillis, endMillis, 1307 flagsTime); 1308 1309 // If possible, use "Today" or "Tomorrow" instead of a full date string. 1310 int todayOrTomorrow = isTodayOrTomorrow(context.getResources(), startMillis, 1311 currentMillis, currentTime.gmtoff); 1312 if (TODAY == todayOrTomorrow) { 1313 // Example: "Today at 1:00pm - 2:00 pm" 1314 datetimeString = resources.getString(R.string.today_at_time_fmt, 1315 timeString); 1316 } else if (TOMORROW == todayOrTomorrow) { 1317 // Example: "Tomorrow at 1:00pm - 2:00 pm" 1318 datetimeString = resources.getString(R.string.tomorrow_at_time_fmt, 1319 timeString); 1320 } else { 1321 // Format the full date. Example: "Thursday, April 12, 1:00pm - 2:00pm" 1322 String dateString = Utils.formatDateRange(context, startMillis, endMillis, 1323 flagsDate); 1324 datetimeString = resources.getString(R.string.date_time_fmt, dateString, 1325 timeString); 1326 } 1327 } else { 1328 // For multiday events, shorten day/month names. 1329 // Example format: "Fri Apr 6, 5:00pm - Sun, Apr 8, 6:00pm" 1330 int flagsDatetime = flagsDate | flagsTime | DateUtils.FORMAT_ABBREV_MONTH | 1331 DateUtils.FORMAT_ABBREV_WEEKDAY; 1332 datetimeString = Utils.formatDateRange(context, startMillis, endMillis, 1333 flagsDatetime); 1334 } 1335 } 1336 return datetimeString; 1337 } 1338 1339 /** 1340 * Returns the timezone to display in the event info, if the local timezone is different 1341 * from the event timezone. Otherwise returns null. 1342 */ 1343 public static String getDisplayedTimezone(long startMillis, String localTimezone, 1344 String eventTimezone) { 1345 String tzDisplay = null; 1346 if (!TextUtils.equals(localTimezone, eventTimezone)) { 1347 // Figure out if this is in DST 1348 TimeZone tz = TimeZone.getTimeZone(localTimezone); 1349 if (tz == null || tz.getID().equals("GMT")) { 1350 tzDisplay = localTimezone; 1351 } else { 1352 Time startTime = new Time(localTimezone); 1353 startTime.set(startMillis); 1354 tzDisplay = tz.getDisplayName(startTime.isDst != 0, TimeZone.SHORT); 1355 } 1356 } 1357 return tzDisplay; 1358 } 1359 1360 /** 1361 * Returns whether the specified time interval is in a single day. 1362 */ 1363 private static boolean singleDayEvent(long startMillis, long endMillis, long localGmtOffset) { 1364 if (startMillis == endMillis) { 1365 return true; 1366 } 1367 1368 // An event ending at midnight should still be a single-day event, so check 1369 // time end-1. 1370 int startDay = Time.getJulianDay(startMillis, localGmtOffset); 1371 int endDay = Time.getJulianDay(endMillis - 1, localGmtOffset); 1372 return startDay == endDay; 1373 } 1374 1375 // Using int constants as a return value instead of an enum to minimize resources. 1376 private static final int TODAY = 1; 1377 private static final int TOMORROW = 2; 1378 private static final int NONE = 0; 1379 1380 /** 1381 * Returns TODAY or TOMORROW if applicable. Otherwise returns NONE. 1382 */ 1383 private static int isTodayOrTomorrow(Resources r, long dayMillis, 1384 long currentMillis, long localGmtOffset) { 1385 int startDay = Time.getJulianDay(dayMillis, localGmtOffset); 1386 int currentDay = Time.getJulianDay(currentMillis, localGmtOffset); 1387 1388 int days = startDay - currentDay; 1389 if (days == 1) { 1390 return TOMORROW; 1391 } else if (days == 0) { 1392 return TODAY; 1393 } else { 1394 return NONE; 1395 } 1396 } 1397 1398 /** 1399 * Create an intent for emailing attendees of an event. 1400 * 1401 * @param resources The resources for translating strings. 1402 * @param eventTitle The title of the event to use as the email subject. 1403 * @param body The default text for the email body. 1404 * @param toEmails The list of emails for the 'to' line. 1405 * @param ccEmails The list of emails for the 'cc' line. 1406 * @param ownerAccount The owner account to use as the email sender. 1407 */ 1408 public static Intent createEmailAttendeesIntent(Resources resources, String eventTitle, 1409 String body, List<String> toEmails, List<String> ccEmails, String ownerAccount) { 1410 List<String> toList = toEmails; 1411 List<String> ccList = ccEmails; 1412 if (toEmails.size() <= 0) { 1413 if (ccEmails.size() <= 0) { 1414 // TODO: Return a SEND intent if no one to email to, to at least populate 1415 // a draft email with the subject (and no recipients). 1416 throw new IllegalArgumentException("Both toEmails and ccEmails are empty."); 1417 } 1418 1419 // Email app does not work with no "to" recipient. Move all 'cc' to 'to' 1420 // in this case. 1421 toList = ccEmails; 1422 ccList = null; 1423 } 1424 1425 // Use the event title as the email subject (prepended with 'Re: '). 1426 String subject = null; 1427 if (eventTitle != null) { 1428 subject = resources.getString(R.string.email_subject_prefix) + eventTitle; 1429 } 1430 1431 // Use the SENDTO intent with a 'mailto' URI, because using SEND will cause 1432 // the picker to show apps like text messaging, which does not make sense 1433 // for email addresses. We put all data in the URI instead of using the extra 1434 // Intent fields (ie. EXTRA_CC, etc) because some email apps might not handle 1435 // those (though gmail does). 1436 Uri.Builder uriBuilder = new Uri.Builder(); 1437 uriBuilder.scheme("mailto"); 1438 1439 // We will append the first email to the 'mailto' field later (because the 1440 // current state of the Email app requires it). Add the remaining 'to' values 1441 // here. When the email codebase is updated, we can simplify this. 1442 if (toList.size() > 1) { 1443 for (int i = 1; i < toList.size(); i++) { 1444 // The Email app requires repeated parameter settings instead of 1445 // a single comma-separated list. 1446 uriBuilder.appendQueryParameter("to", toList.get(i)); 1447 } 1448 } 1449 1450 // Add the subject parameter. 1451 if (subject != null) { 1452 uriBuilder.appendQueryParameter("subject", subject); 1453 } 1454 1455 // Add the subject parameter. 1456 if (body != null) { 1457 uriBuilder.appendQueryParameter("body", body); 1458 } 1459 1460 // Add the cc parameters. 1461 if (ccList != null && ccList.size() > 0) { 1462 for (String email : ccList) { 1463 uriBuilder.appendQueryParameter("cc", email); 1464 } 1465 } 1466 1467 // Insert the first email after 'mailto:' in the URI manually since Uri.Builder 1468 // doesn't seem to have a way to do this. 1469 String uri = uriBuilder.toString(); 1470 if (uri.startsWith("mailto:")) { 1471 StringBuilder builder = new StringBuilder(uri); 1472 builder.insert(7, Uri.encode(toList.get(0))); 1473 uri = builder.toString(); 1474 } 1475 1476 // Start the email intent. Email from the account of the calendar owner in case there 1477 // are multiple email accounts. 1478 Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.parse(uri)); 1479 emailIntent.putExtra("fromAccountString", ownerAccount); 1480 1481 // Workaround a Email bug that overwrites the body with this intent extra. If not 1482 // set, it clears the body. 1483 if (body != null) { 1484 emailIntent.putExtra(Intent.EXTRA_TEXT, body); 1485 } 1486 1487 return Intent.createChooser(emailIntent, resources.getString(R.string.email_picker_label)); 1488 } 1489 1490 /** 1491 * Example fake email addresses used as attendee emails are resources like conference rooms, 1492 * or another calendar, etc. These all end in "calendar.google.com". 1493 */ 1494 public static boolean isValidEmail(String email) { 1495 return email != null && !email.endsWith(MACHINE_GENERATED_ADDRESS); 1496 } 1497 1498 /** 1499 * Returns true if: 1500 * (1) the email is not a resource like a conference room or another calendar. 1501 * Catch most of these by filtering out suffix calendar.google.com. 1502 * (2) the email is not equal to the sync account to prevent mailing himself. 1503 */ 1504 public static boolean isEmailableFrom(String email, String syncAccountName) { 1505 return Utils.isValidEmail(email) && !email.equals(syncAccountName); 1506 } 1507 1508 /** 1509 * Inserts a drawable with today's day into the today's icon in the option menu 1510 * @param icon - today's icon from the options menu 1511 */ 1512 public static void setTodayIcon(LayerDrawable icon, Context c, String timezone) { 1513 DayOfMonthDrawable today; 1514 1515 // Reuse current drawable if possible 1516 Drawable currentDrawable = icon.findDrawableByLayerId(R.id.today_icon_day); 1517 if (currentDrawable != null && currentDrawable instanceof DayOfMonthDrawable) { 1518 today = (DayOfMonthDrawable)currentDrawable; 1519 } else { 1520 today = new DayOfMonthDrawable(c); 1521 } 1522 // Set the day and update the icon 1523 Time now = new Time(timezone); 1524 now.setToNow(); 1525 now.normalize(false); 1526 today.setDayOfMonth(now.monthDay); 1527 icon.mutate(); 1528 icon.setDrawableByLayerId(R.id.today_icon_day, today); 1529 } 1530 1531 private static class CalendarBroadcastReceiver extends BroadcastReceiver { 1532 1533 Runnable mCallBack; 1534 1535 public CalendarBroadcastReceiver(Runnable callback) { 1536 super(); 1537 mCallBack = callback; 1538 } 1539 @Override 1540 public void onReceive(Context context, Intent intent) { 1541 if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED) || 1542 intent.getAction().equals(Intent.ACTION_TIME_CHANGED) || 1543 intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED) || 1544 intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) { 1545 if (mCallBack != null) { 1546 mCallBack.run(); 1547 } 1548 } 1549 } 1550 } 1551 1552 public static BroadcastReceiver setTimeChangesReceiver(Context c, Runnable callback) { 1553 IntentFilter filter = new IntentFilter(); 1554 filter.addAction(Intent.ACTION_TIME_CHANGED); 1555 filter.addAction(Intent.ACTION_DATE_CHANGED); 1556 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 1557 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 1558 1559 CalendarBroadcastReceiver r = new CalendarBroadcastReceiver(callback); 1560 c.registerReceiver(r, filter); 1561 return r; 1562 } 1563 1564 public static void clearTimeChangesReceiver(Context c, BroadcastReceiver r) { 1565 c.unregisterReceiver(r); 1566 } 1567 1568 /** 1569 * Get a list of quick responses used for emailing guests from the 1570 * SharedPreferences. If not are found, get the hard coded ones that shipped 1571 * with the app 1572 * 1573 * @param context 1574 * @return a list of quick responses. 1575 */ 1576 public static String[] getQuickResponses(Context context) { 1577 String[] s = Utils.getSharedPreference(context, KEY_QUICK_RESPONSES, (String[]) null); 1578 1579 if (s == null) { 1580 s = context.getResources().getStringArray(R.array.quick_response_defaults); 1581 } 1582 1583 return s; 1584 } 1585 1586 /** 1587 * Return the app version code. 1588 */ 1589 public static String getVersionCode(Context context) { 1590 if (sVersion == null) { 1591 try { 1592 sVersion = context.getPackageManager().getPackageInfo( 1593 context.getPackageName(), 0).versionName; 1594 } catch (PackageManager.NameNotFoundException e) { 1595 // Can't find version; just leave it blank. 1596 Log.e(TAG, "Error finding package " + context.getApplicationInfo().packageName); 1597 } 1598 } 1599 return sVersion; 1600 } 1601 1602 /** 1603 * Checks the server for an updated list of Calendars (in the background). 1604 * 1605 * If a Calendar is added on the web (and it is selected and not 1606 * hidden) then it will be added to the list of calendars on the phone 1607 * (when this finishes). When a new calendar from the 1608 * web is added to the phone, then the events for that calendar are also 1609 * downloaded from the web. 1610 * 1611 * This sync is done automatically in the background when the 1612 * SelectCalendars activity and fragment are started. 1613 * 1614 * @param account - The account to sync. May be null to sync all accounts. 1615 */ 1616 public static void startCalendarMetafeedSync(Account account) { 1617 Bundle extras = new Bundle(); 1618 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); 1619 extras.putBoolean("metafeedonly", true); 1620 ContentResolver.requestSync(account, Calendars.CONTENT_URI.getAuthority(), extras); 1621 } 1622 1623 /** 1624 * Replaces stretches of text that look like addresses and phone numbers with clickable 1625 * links. If lastDitchGeo is true, then if no links are found in the textview, the entire 1626 * string will be converted to a single geo link. Any spans that may have previously been 1627 * in the text will be cleared out. 1628 * <p> 1629 * This is really just an enhanced version of Linkify.addLinks(). 1630 * 1631 * @param text - The string to search for links. 1632 * @param lastDitchGeo - If no links are found, turn the entire string into one geo link. 1633 * @return Spannable object containing the list of URL spans found. 1634 */ 1635 public static Spannable extendedLinkify(String text, boolean lastDitchGeo) { 1636 // We use a copy of the string argument so it's available for later if necessary. 1637 Spannable spanText = SpannableString.valueOf(text); 1638 1639 /* 1640 * If the text includes a street address like "1600 Amphitheater Parkway, 94043", 1641 * the current Linkify code will identify "94043" as a phone number and invite 1642 * you to dial it (and not provide a map link for the address). For outside US, 1643 * use Linkify result iff it spans the entire text. Otherwise send the user to maps. 1644 */ 1645 String defaultPhoneRegion = System.getProperty("user.region", "US"); 1646 if (!defaultPhoneRegion.equals("US")) { 1647 Linkify.addLinks(spanText, Linkify.ALL); 1648 1649 // If Linkify links the entire text, use that result. 1650 URLSpan[] spans = spanText.getSpans(0, spanText.length(), URLSpan.class); 1651 if (spans.length == 1) { 1652 int linkStart = spanText.getSpanStart(spans[0]); 1653 int linkEnd = spanText.getSpanEnd(spans[0]); 1654 if (linkStart <= indexFirstNonWhitespaceChar(spanText) && 1655 linkEnd >= indexLastNonWhitespaceChar(spanText) + 1) { 1656 return spanText; 1657 } 1658 } 1659 1660 // Otherwise, to be cautious and to try to prevent false positives, reset the spannable. 1661 spanText = SpannableString.valueOf(text); 1662 // If lastDitchGeo is true, default the entire string to geo. 1663 if (lastDitchGeo && !text.isEmpty()) { 1664 Linkify.addLinks(spanText, mWildcardPattern, "geo:0,0?q="); 1665 } 1666 return spanText; 1667 } 1668 1669 /* 1670 * For within US, we want to have better recognition of phone numbers without losing 1671 * any of the existing annotations. Ideally this would be addressed by improving Linkify. 1672 * For now we manage it as a second pass over the text. 1673 * 1674 * URIs and e-mail addresses are pretty easy to pick out of text. Phone numbers 1675 * are a bit tricky because they have radically different formats in different 1676 * countries, in terms of both the digits and the way in which they are commonly 1677 * written or presented (e.g. the punctuation and spaces in "(650) 555-1212"). 1678 * The expected format of a street address is defined in WebView.findAddress(). It's 1679 * pretty narrowly defined, so it won't often match. 1680 * 1681 * The RFC 3966 specification defines the format of a "tel:" URI. 1682 * 1683 * Start by letting Linkify find anything that isn't a phone number. We have to let it 1684 * run first because every invocation removes all previous URLSpan annotations. 1685 * 1686 * Ideally we'd use the external/libphonenumber routines, but those aren't available 1687 * to unbundled applications. 1688 */ 1689 boolean linkifyFoundLinks = Linkify.addLinks(spanText, 1690 Linkify.ALL & ~(Linkify.PHONE_NUMBERS)); 1691 1692 /* 1693 * Get a list of any spans created by Linkify, for the coordinate overlapping span check. 1694 */ 1695 URLSpan[] existingSpans = spanText.getSpans(0, spanText.length(), URLSpan.class); 1696 1697 /* 1698 * Check for coordinates. 1699 * This must be done before phone numbers because longitude may look like a phone number. 1700 */ 1701 Matcher coordMatcher = COORD_PATTERN.matcher(spanText); 1702 int coordCount = 0; 1703 while (coordMatcher.find()) { 1704 int start = coordMatcher.start(); 1705 int end = coordMatcher.end(); 1706 if (spanWillOverlap(spanText, existingSpans, start, end)) { 1707 continue; 1708 } 1709 1710 URLSpan span = new URLSpan("geo:0,0?q=" + coordMatcher.group()); 1711 spanText.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 1712 coordCount++; 1713 } 1714 1715 /* 1716 * Update the list of existing spans, for the phone number overlapping span check. 1717 */ 1718 existingSpans = spanText.getSpans(0, spanText.length(), URLSpan.class); 1719 1720 /* 1721 * Search for phone numbers. 1722 * 1723 * Some URIs contain strings of digits that look like phone numbers. If both the URI 1724 * scanner and the phone number scanner find them, we want the URI link to win. Since 1725 * the URI scanner runs first, we just need to avoid creating overlapping spans. 1726 */ 1727 int[] phoneSequences = findNanpPhoneNumbers(text); 1728 1729 /* 1730 * Insert spans for the numbers we found. We generate "tel:" URIs. 1731 */ 1732 int phoneCount = 0; 1733 for (int match = 0; match < phoneSequences.length / 2; match++) { 1734 int start = phoneSequences[match*2]; 1735 int end = phoneSequences[match*2 + 1]; 1736 1737 if (spanWillOverlap(spanText, existingSpans, start, end)) { 1738 continue; 1739 } 1740 1741 /* 1742 * The Linkify code takes the matching span and strips out everything that isn't a 1743 * digit or '+' sign. We do the same here. Extension numbers will get appended 1744 * without a separator, but the dialer wasn't doing anything useful with ";ext=" 1745 * anyway. 1746 */ 1747 1748 //String dialStr = phoneUtil.format(match.number(), 1749 // PhoneNumberUtil.PhoneNumberFormat.RFC3966); 1750 StringBuilder dialBuilder = new StringBuilder(); 1751 for (int i = start; i < end; i++) { 1752 char ch = spanText.charAt(i); 1753 if (ch == '+' || Character.isDigit(ch)) { 1754 dialBuilder.append(ch); 1755 } 1756 } 1757 URLSpan span = new URLSpan("tel:" + dialBuilder.toString()); 1758 1759 spanText.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 1760 phoneCount++; 1761 } 1762 1763 /* 1764 * If lastDitchGeo, and no other links have been found, set the entire string as a geo link. 1765 */ 1766 if (lastDitchGeo && !text.isEmpty() && 1767 !linkifyFoundLinks && phoneCount == 0 && coordCount == 0) { 1768 if (Log.isLoggable(TAG, Log.VERBOSE)) { 1769 Log.v(TAG, "No linkification matches, using geo default"); 1770 } 1771 Linkify.addLinks(spanText, mWildcardPattern, "geo:0,0?q="); 1772 } 1773 1774 return spanText; 1775 } 1776 1777 private static int indexFirstNonWhitespaceChar(CharSequence str) { 1778 for (int i = 0; i < str.length(); i++) { 1779 if (!Character.isWhitespace(str.charAt(i))) { 1780 return i; 1781 } 1782 } 1783 return -1; 1784 } 1785 1786 private static int indexLastNonWhitespaceChar(CharSequence str) { 1787 for (int i = str.length() - 1; i >= 0; i--) { 1788 if (!Character.isWhitespace(str.charAt(i))) { 1789 return i; 1790 } 1791 } 1792 return -1; 1793 } 1794 1795 /** 1796 * Finds North American Numbering Plan (NANP) phone numbers in the input text. 1797 * 1798 * @param text The text to scan. 1799 * @return A list of [start, end) pairs indicating the positions of phone numbers in the input. 1800 */ 1801 // @VisibleForTesting 1802 static int[] findNanpPhoneNumbers(CharSequence text) { 1803 ArrayList<Integer> list = new ArrayList<Integer>(); 1804 1805 int startPos = 0; 1806 int endPos = text.length() - NANP_MIN_DIGITS + 1; 1807 if (endPos < 0) { 1808 return new int[] {}; 1809 } 1810 1811 /* 1812 * We can't just strip the whitespace out and crunch it down, because the whitespace 1813 * is significant. March through, trying to figure out where numbers start and end. 1814 */ 1815 while (startPos < endPos) { 1816 // skip whitespace 1817 while (Character.isWhitespace(text.charAt(startPos)) && startPos < endPos) { 1818 startPos++; 1819 } 1820 if (startPos == endPos) { 1821 break; 1822 } 1823 1824 // check for a match at this position 1825 int matchEnd = findNanpMatchEnd(text, startPos); 1826 if (matchEnd > startPos) { 1827 list.add(startPos); 1828 list.add(matchEnd); 1829 startPos = matchEnd; // skip past match 1830 } else { 1831 // skip to next whitespace char 1832 while (!Character.isWhitespace(text.charAt(startPos)) && startPos < endPos) { 1833 startPos++; 1834 } 1835 } 1836 } 1837 1838 int[] result = new int[list.size()]; 1839 for (int i = list.size() - 1; i >= 0; i--) { 1840 result[i] = list.get(i); 1841 } 1842 return result; 1843 } 1844 1845 /** 1846 * Checks to see if there is a valid phone number in the input, starting at the specified 1847 * offset. If so, the index of the last character + 1 is returned. The input is assumed 1848 * to begin with a non-whitespace character. 1849 * 1850 * @return Exclusive end position, or -1 if not a match. 1851 */ 1852 private static int findNanpMatchEnd(CharSequence text, int startPos) { 1853 /* 1854 * A few interesting cases: 1855 * 94043 # too short, ignore 1856 * 123456789012 # too long, ignore 1857 * +1 (650) 555-1212 # 11 digits, spaces 1858 * (650) 555 5555 # Second space, only when first is present. 1859 * (650) 555-1212, (650) 555-1213 # two numbers, return first 1860 * 1-650-555-1212 # 11 digits with leading '1' 1861 * *#650.555.1212#*! # 10 digits, include #*, ignore trailing '!' 1862 * 555.1212 # 7 digits 1863 * 1864 * For the most part we want to break on whitespace, but it's common to leave a space 1865 * between the initial '1' and/or after the area code. 1866 */ 1867 1868 // Check for "tel:" URI prefix. 1869 if (text.length() > startPos+4 1870 && text.subSequence(startPos, startPos+4).toString().equalsIgnoreCase("tel:")) { 1871 startPos += 4; 1872 } 1873 1874 int endPos = text.length(); 1875 int curPos = startPos; 1876 int foundDigits = 0; 1877 char firstDigit = 'x'; 1878 boolean foundWhiteSpaceAfterAreaCode = false; 1879 1880 while (curPos <= endPos) { 1881 char ch; 1882 if (curPos < endPos) { 1883 ch = text.charAt(curPos); 1884 } else { 1885 ch = 27; // fake invalid symbol at end to trigger loop break 1886 } 1887 1888 if (Character.isDigit(ch)) { 1889 if (foundDigits == 0) { 1890 firstDigit = ch; 1891 } 1892 foundDigits++; 1893 if (foundDigits > NANP_MAX_DIGITS) { 1894 // too many digits, stop early 1895 return -1; 1896 } 1897 } else if (Character.isWhitespace(ch)) { 1898 if ( (firstDigit == '1' && foundDigits == 4) || 1899 (foundDigits == 3)) { 1900 foundWhiteSpaceAfterAreaCode = true; 1901 } else if (firstDigit == '1' && foundDigits == 1) { 1902 } else if (foundWhiteSpaceAfterAreaCode 1903 && ( (firstDigit == '1' && (foundDigits == 7)) || (foundDigits == 6))) { 1904 } else { 1905 break; 1906 } 1907 } else if (NANP_ALLOWED_SYMBOLS.indexOf(ch) == -1) { 1908 break; 1909 } 1910 // else it's an allowed symbol 1911 1912 curPos++; 1913 } 1914 1915 if ((firstDigit != '1' && (foundDigits == 7 || foundDigits == 10)) || 1916 (firstDigit == '1' && foundDigits == 11)) { 1917 // match 1918 return curPos; 1919 } 1920 1921 return -1; 1922 } 1923 1924 /** 1925 * Determines whether a new span at [start,end) will overlap with any existing span. 1926 */ 1927 private static boolean spanWillOverlap(Spannable spanText, URLSpan[] spanList, int start, 1928 int end) { 1929 if (start == end) { 1930 // empty span, ignore 1931 return false; 1932 } 1933 for (URLSpan span : spanList) { 1934 int existingStart = spanText.getSpanStart(span); 1935 int existingEnd = spanText.getSpanEnd(span); 1936 if ((start >= existingStart && start < existingEnd) || 1937 end > existingStart && end <= existingEnd) { 1938 if (Log.isLoggable(TAG, Log.VERBOSE)) { 1939 CharSequence seq = spanText.subSequence(start, end); 1940 Log.v(TAG, "Not linkifying " + seq + " as phone number due to overlap"); 1941 } 1942 return true; 1943 } 1944 } 1945 1946 return false; 1947 } 1948 1949} 1950