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