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