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