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