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