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