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