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