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