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