Utils.java revision c0f6efee87520ce0c8b52246129169f94850177b
1146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project/*
2146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
3146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project *
4146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * you may not use this file except in compliance with the License.
6146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * You may obtain a copy of the License at
7146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project *
8146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project *
10146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * See the License for the specific language governing permissions and
14146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * limitations under the License.
15146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project */
16146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
17146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Projectpackage com.android.calendar;
18146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
19a27a886892fe3ec5edbc63c0b58e0a988623011aRoboErikimport static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
20e8aa59d4575d712601a133a9263acc23adbc8c17Michael Chan
21d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chanimport com.android.calendar.CalendarController.ViewType;
22d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan
23d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chanimport android.app.Activity;
24146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Projectimport android.content.Context;
25146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Projectimport android.content.Intent;
26e8aa59d4575d712601a133a9263acc23adbc8c17Michael Chanimport android.content.SharedPreferences;
275b2a907fa3a983f74bb48da86846e351c2a464d0Isaac Katzenelsonimport android.content.res.Configuration;
28092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErikimport android.content.res.Resources;
29ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chanimport android.database.Cursor;
30a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErikimport android.database.MatrixCursor;
31092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErikimport android.graphics.Color;
321ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erikimport android.net.Uri;
33d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chanimport android.os.Bundle;
34eb10fa8bee049e0052b5cb53dcfbdaccef9f2740Erikimport android.text.TextUtils;
3529190975b9238dd6841f822f82f2fb83b0557f36Daisuke Miyakawaimport android.text.format.DateUtils;
36146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Projectimport android.text.format.Time;
371ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erikimport android.util.Log;
38636269c7220a2b12e090cab43a91eb34922eb61fAndy McFaddenimport com.android.calendar.CalendarUtils.TimeZoneUtils;
39146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
4082400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelsonimport java.util.ArrayList;
41092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErikimport java.util.Arrays;
4256adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashiimport java.util.Calendar;
431427657d0bf7e69b831aa495828f67b45b69fd99Erikimport java.util.Formatter;
44092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErikimport java.util.HashMap;
4582400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelsonimport java.util.Iterator;
46092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErikimport java.util.LinkedList;
471ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erikimport java.util.List;
48ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chanimport java.util.Map;
4956adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi
50146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Projectpublic class Utils {
51092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    private static final boolean DEBUG = false;
523dc5e908a825b879978ba523d9099dc2255da9a5Erik    private static final String TAG = "CalUtils";
53bed0275111ecc6c4a3a638f90a9bac13bee594f4Michael Chan    // Set to 0 until we have UI to perform undo
54bed0275111ecc6c4a3a638f90a9bac13bee594f4Michael Chan    public static final long UNDO_DELAY = 0;
55bed0275111ecc6c4a3a638f90a9bac13bee594f4Michael Chan
5679f228124de7d98146ca526d743436f6419e2365Erik    // For recurring events which instances of the series are being modified
5779f228124de7d98146ca526d743436f6419e2365Erik    public static final int MODIFY_UNINITIALIZED = 0;
5879f228124de7d98146ca526d743436f6419e2365Erik    public static final int MODIFY_SELECTED = 1;
5979f228124de7d98146ca526d743436f6419e2365Erik    public static final int MODIFY_ALL_FOLLOWING = 2;
6079f228124de7d98146ca526d743436f6419e2365Erik    public static final int MODIFY_ALL = 3;
6179f228124de7d98146ca526d743436f6419e2365Erik
627b92da258a480284dcc15a518ea570072329a31dErik    // When the edit event view finishes it passes back the appropriate exit
637b92da258a480284dcc15a518ea570072329a31dErik    // code.
647b92da258a480284dcc15a518ea570072329a31dErik    public static final int DONE_REVERT = 1 << 0;
657b92da258a480284dcc15a518ea570072329a31dErik    public static final int DONE_SAVE = 1 << 1;
667b92da258a480284dcc15a518ea570072329a31dErik    public static final int DONE_DELETE = 1 << 2;
677b92da258a480284dcc15a518ea570072329a31dErik    // And should re run with DONE_EXIT if it should also leave the view, just
687b92da258a480284dcc15a518ea570072329a31dErik    // exiting is identical to reverting
697b92da258a480284dcc15a518ea570072329a31dErik    public static final int DONE_EXIT = 1 << 0;
7079f228124de7d98146ca526d743436f6419e2365Erik
71ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan    protected static final String OPEN_EMAIL_MARKER = " <";
72ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan    protected static final String CLOSE_EMAIL_MARKER = ">";
73ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan
74d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan    public static final String INTENT_KEY_DETAIL_VIEW = "DETAIL_VIEW";
75d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan    public static final String INTENT_KEY_VIEW_TYPE = "VIEW";
76d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan    public static final String INTENT_VALUE_VIEW_TYPE_DAY = "DAY";
77275232dae58bb24e3360a779ada9d24601a99bcfErik
78981874e61ecf29a96a77601a3172b2503b6537eeErik    public static final int MONDAY_BEFORE_JULIAN_EPOCH = Time.EPOCH_JULIAN_DAY - 3;
79981874e61ecf29a96a77601a3172b2503b6537eeErik
80092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // Defines used by the DNA generation code
81092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    static final int DAY_IN_MINUTES = 60 * 24;
82092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    static final int WEEK_IN_MINUTES = DAY_IN_MINUTES * 7;
83092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // The work day is being counted as 6am to 8pm
84092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    static int WORK_DAY_MINUTES = 14 * 60;
85092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    static int WORK_DAY_START_MINUTES = 6 * 60;
86092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    static int WORK_DAY_END_MINUTES = 20 * 60;
87092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    static int WORK_DAY_END_LENGTH = (24 * 60) - WORK_DAY_END_MINUTES;
88092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    static int CONFLICT_COLOR = 0xFF000000;
89092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    static boolean mMinutesLoaded = false;
90092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
91a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik    // The name of the shared preferences file. This name must be maintained for
92a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik    // historical
93a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik    // reasons, as it's what PreferenceManager assigned the first time the file
94a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik    // was created.
95a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik    private static final String SHARED_PREFS_NAME = "com.android.calendar_preferences";
9635d1362a75eac7cebbe9de23d08fea08c4aac817Erik
97a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik    private static final TimeZoneUtils mTZUtils = new TimeZoneUtils(SHARED_PREFS_NAME);
98b60218a31d948ea0a549daf6464063d20b48421fMichael Chan    private static boolean mAllowWeekForDetailView = false;
99ca4786769151f97069980443ce43d9c4f867ac5bErik    private static long mTardis = 0;
100d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan
101d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan    public static int getViewTypeFromIntentAndSharedPref(Activity activity) {
102dd95df57c8c5a58a85c4c0effad5652dec14f621Erik        Intent intent = activity.getIntent();
103dd95df57c8c5a58a85c4c0effad5652dec14f621Erik        Bundle extras = intent.getExtras();
1044b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(activity);
105d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan
1067b92da258a480284dcc15a518ea570072329a31dErik        if (TextUtils.equals(intent.getAction(), Intent.ACTION_EDIT)) {
107dd95df57c8c5a58a85c4c0effad5652dec14f621Erik            return ViewType.EDIT;
108dd95df57c8c5a58a85c4c0effad5652dec14f621Erik        }
109d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan        if (extras != null) {
110d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan            if (extras.getBoolean(INTENT_KEY_DETAIL_VIEW, false)) {
111d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan                // This is the "detail" view which is either agenda or day view
1124b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa                return prefs.getInt(GeneralPreferences.KEY_DETAILED_VIEW,
1134b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa                        GeneralPreferences.DEFAULT_DETAILED_VIEW);
114d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan            } else if (INTENT_VALUE_VIEW_TYPE_DAY.equals(extras.getString(INTENT_KEY_VIEW_TYPE))) {
115d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan                // Not sure who uses this. This logic came from LaunchActivity
116d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan                return ViewType.DAY;
117d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan            }
118d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan        }
119d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan
120d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan        // Default to the last view
1217b92da258a480284dcc15a518ea570072329a31dErik        return prefs.getInt(
1227b92da258a480284dcc15a518ea570072329a31dErik                GeneralPreferences.KEY_START_VIEW, GeneralPreferences.DEFAULT_START_VIEW);
123d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan    }
124ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan
125235d59cf61769ec8ab777d81cd1ceb2e7530f439Erik    /**
126064beb957ae443760118023a40fd7001eb11c1a1RoboErik     * Gets the intent action for telling the widget to update.
127064beb957ae443760118023a40fd7001eb11c1a1RoboErik     */
128064beb957ae443760118023a40fd7001eb11c1a1RoboErik    public static String getWidgetUpdateAction(Context context) {
129064beb957ae443760118023a40fd7001eb11c1a1RoboErik        return context.getPackageName() + ".APPWIDGET_UPDATE";
130064beb957ae443760118023a40fd7001eb11c1a1RoboErik    }
131064beb957ae443760118023a40fd7001eb11c1a1RoboErik
132064beb957ae443760118023a40fd7001eb11c1a1RoboErik    /**
133064beb957ae443760118023a40fd7001eb11c1a1RoboErik     * Gets the intent action for telling the widget to update.
134064beb957ae443760118023a40fd7001eb11c1a1RoboErik     */
135064beb957ae443760118023a40fd7001eb11c1a1RoboErik    public static String getWidgetScheduledUpdateAction(Context context) {
136064beb957ae443760118023a40fd7001eb11c1a1RoboErik        return context.getPackageName() + ".APPWIDGET_SCHEDULED_UPDATE";
137064beb957ae443760118023a40fd7001eb11c1a1RoboErik    }
138064beb957ae443760118023a40fd7001eb11c1a1RoboErik
139064beb957ae443760118023a40fd7001eb11c1a1RoboErik    /**
140064beb957ae443760118023a40fd7001eb11c1a1RoboErik     * Gets the intent action for telling the widget to update.
141064beb957ae443760118023a40fd7001eb11c1a1RoboErik     */
142064beb957ae443760118023a40fd7001eb11c1a1RoboErik    public static String getSearchAuthority(Context context) {
143064beb957ae443760118023a40fd7001eb11c1a1RoboErik        return context.getPackageName() + ".CalendarRecentSuggestionsProvider";
144064beb957ae443760118023a40fd7001eb11c1a1RoboErik    }
145064beb957ae443760118023a40fd7001eb11c1a1RoboErik
146064beb957ae443760118023a40fd7001eb11c1a1RoboErik    /**
1477b92da258a480284dcc15a518ea570072329a31dErik     * Writes a new home time zone to the db. Updates the home time zone in the
1487b92da258a480284dcc15a518ea570072329a31dErik     * db asynchronously and updates the local cache. Sending a time zone of
1497b92da258a480284dcc15a518ea570072329a31dErik     * **tbd** will cause it to be set to the device's time zone. null or empty
1507b92da258a480284dcc15a518ea570072329a31dErik     * tz will be ignored.
1513dc5e908a825b879978ba523d9099dc2255da9a5Erik     *
1523dc5e908a825b879978ba523d9099dc2255da9a5Erik     * @param context The calling activity
1533dc5e908a825b879978ba523d9099dc2255da9a5Erik     * @param timeZone The time zone to set Calendar to, or **tbd**
1543dc5e908a825b879978ba523d9099dc2255da9a5Erik     */
1553dc5e908a825b879978ba523d9099dc2255da9a5Erik    public static void setTimeZone(Context context, String timeZone) {
156a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik        mTZUtils.setTimeZone(context, timeZone);
1573dc5e908a825b879978ba523d9099dc2255da9a5Erik    }
1583dc5e908a825b879978ba523d9099dc2255da9a5Erik
1593dc5e908a825b879978ba523d9099dc2255da9a5Erik    /**
1607b92da258a480284dcc15a518ea570072329a31dErik     * Gets the time zone that Calendar should be displayed in This is a helper
1617b92da258a480284dcc15a518ea570072329a31dErik     * method to get the appropriate time zone for Calendar. If this is the
1627b92da258a480284dcc15a518ea570072329a31dErik     * first time this method has been called it will initiate an asynchronous
1637b92da258a480284dcc15a518ea570072329a31dErik     * query to verify that the data in preferences is correct. The callback
1647b92da258a480284dcc15a518ea570072329a31dErik     * supplied will only be called if this query returns a value other than
1657b92da258a480284dcc15a518ea570072329a31dErik     * what is stored in preferences and should cause the calling activity to
1667b92da258a480284dcc15a518ea570072329a31dErik     * refresh anything that depends on calling this method.
167235d59cf61769ec8ab777d81cd1ceb2e7530f439Erik     *
168235d59cf61769ec8ab777d81cd1ceb2e7530f439Erik     * @param context The calling activity
1697b92da258a480284dcc15a518ea570072329a31dErik     * @param callback The runnable that should execute if a query returns new
1707b92da258a480284dcc15a518ea570072329a31dErik     *            values
1717b92da258a480284dcc15a518ea570072329a31dErik     * @return The string value representing the time zone Calendar should
1727b92da258a480284dcc15a518ea570072329a31dErik     *         display
173235d59cf61769ec8ab777d81cd1ceb2e7530f439Erik     */
174235d59cf61769ec8ab777d81cd1ceb2e7530f439Erik    public static String getTimeZone(Context context, Runnable callback) {
175a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik        return mTZUtils.getTimeZone(context, callback);
17645efa09d6e06f5569b2c0ae0dae0436dbfe6cb28Michael Chan    }
17745efa09d6e06f5569b2c0ae0dae0436dbfe6cb28Michael Chan
1781427657d0bf7e69b831aa495828f67b45b69fd99Erik    /**
1791427657d0bf7e69b831aa495828f67b45b69fd99Erik     * Formats a date or a time range according to the local conventions.
1801427657d0bf7e69b831aa495828f67b45b69fd99Erik     *
1811427657d0bf7e69b831aa495828f67b45b69fd99Erik     * @param context the context is required only if the time is shown
1821427657d0bf7e69b831aa495828f67b45b69fd99Erik     * @param startMillis the start time in UTC milliseconds
1831427657d0bf7e69b831aa495828f67b45b69fd99Erik     * @param endMillis the end time in UTC milliseconds
18429190975b9238dd6841f822f82f2fb83b0557f36Daisuke Miyakawa     * @param flags a bit mask of options See {@link DateUtils#formatDateRange(Context, Formatter,
18529190975b9238dd6841f822f82f2fb83b0557f36Daisuke Miyakawa     * long, long, int, String) formatDateRange}
1861427657d0bf7e69b831aa495828f67b45b69fd99Erik     * @return a string containing the formatted date/time range.
1871427657d0bf7e69b831aa495828f67b45b69fd99Erik     */
1887b92da258a480284dcc15a518ea570072329a31dErik    public static String formatDateRange(
1897b92da258a480284dcc15a518ea570072329a31dErik            Context context, long startMillis, long endMillis, int flags) {
190a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik        return mTZUtils.formatDateRange(context, startMillis, endMillis, flags);
191a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik    }
192a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik
193a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik    public static String getSharedPreference(Context context, String key, String defaultValue) {
194a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
195a48b9d426236d8d26bd99602bf0a84315b3f1b09Erik        return prefs.getString(key, defaultValue);
1961427657d0bf7e69b831aa495828f67b45b69fd99Erik    }
1971427657d0bf7e69b831aa495828f67b45b69fd99Erik
198d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan    public static int getSharedPreference(Context context, String key, int defaultValue) {
1994b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
200d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan        return prefs.getInt(key, defaultValue);
201d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan    }
202d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan
203ca4786769151f97069980443ce43d9c4f867ac5bErik    public static boolean getSharedPreference(Context context, String key, boolean defaultValue) {
204ca4786769151f97069980443ce43d9c4f867ac5bErik        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
205ca4786769151f97069980443ce43d9c4f867ac5bErik        return prefs.getBoolean(key, defaultValue);
206ca4786769151f97069980443ce43d9c4f867ac5bErik    }
207ca4786769151f97069980443ce43d9c4f867ac5bErik
208f4ad4757de32ace6971cf4c3db7c395aa249001aMason Tang    /**
209f4ad4757de32ace6971cf4c3db7c395aa249001aMason Tang     * Asynchronously sets the preference with the given key to the given value
210f4ad4757de32ace6971cf4c3db7c395aa249001aMason Tang     *
211f4ad4757de32ace6971cf4c3db7c395aa249001aMason Tang     * @param context the context to use to get preferences from
212f4ad4757de32ace6971cf4c3db7c395aa249001aMason Tang     * @param key the key of the preference to set
213f4ad4757de32ace6971cf4c3db7c395aa249001aMason Tang     * @param value the value to set
214f4ad4757de32ace6971cf4c3db7c395aa249001aMason Tang     */
215fbce65e53c7a111955f638db5bf8bee35381e5b7Erik    public static void setSharedPreference(Context context, String key, String value) {
2164b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
21724fac46d6b87ce21d5e6a4b1c0fdcaa83d408997Brad Fitzpatrick        prefs.edit().putString(key, value).apply();
21845efa09d6e06f5569b2c0ae0dae0436dbfe6cb28Michael Chan    }
21945efa09d6e06f5569b2c0ae0dae0436dbfe6cb28Michael Chan
220ca4786769151f97069980443ce43d9c4f867ac5bErik    protected static void tardis() {
221ca4786769151f97069980443ce43d9c4f867ac5bErik        mTardis = System.currentTimeMillis();
222ca4786769151f97069980443ce43d9c4f867ac5bErik    }
223ca4786769151f97069980443ce43d9c4f867ac5bErik
224ca4786769151f97069980443ce43d9c4f867ac5bErik    protected static long getTardis() {
225ca4786769151f97069980443ce43d9c4f867ac5bErik        return mTardis;
226ca4786769151f97069980443ce43d9c4f867ac5bErik    }
227ca4786769151f97069980443ce43d9c4f867ac5bErik
2283dc5e908a825b879978ba523d9099dc2255da9a5Erik    static void setSharedPreference(Context context, String key, boolean value) {
2294b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
2303dc5e908a825b879978ba523d9099dc2255da9a5Erik        SharedPreferences.Editor editor = prefs.edit();
2313dc5e908a825b879978ba523d9099dc2255da9a5Erik        editor.putBoolean(key, value);
232275232dae58bb24e3360a779ada9d24601a99bcfErik        editor.apply();
2333dc5e908a825b879978ba523d9099dc2255da9a5Erik    }
2343dc5e908a825b879978ba523d9099dc2255da9a5Erik
235d885c1a5876735bbf2c0086956101bd70bac37e7Michael Chan    static void setSharedPreference(Context context, String key, int value) {
236d885c1a5876735bbf2c0086956101bd70bac37e7Michael Chan        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
237d885c1a5876735bbf2c0086956101bd70bac37e7Michael Chan        SharedPreferences.Editor editor = prefs.edit();
238d885c1a5876735bbf2c0086956101bd70bac37e7Michael Chan        editor.putInt(key, value);
239d885c1a5876735bbf2c0086956101bd70bac37e7Michael Chan        editor.apply();
240d885c1a5876735bbf2c0086956101bd70bac37e7Michael Chan    }
241d885c1a5876735bbf2c0086956101bd70bac37e7Michael Chan
242d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan    /**
243d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan     * Save default agenda/day/week/month view for next time
244d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan     *
245d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan     * @param context
246d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan     * @param viewId {@link CalendarController.ViewType}
247d6734dbbd704cdb1bc331d1bd74b7a3be58f69ffMichael Chan     */
248e8aa59d4575d712601a133a9263acc23adbc8c17Michael Chan    static void setDefaultView(Context context, int viewId) {
2494b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
250e8aa59d4575d712601a133a9263acc23adbc8c17Michael Chan        SharedPreferences.Editor editor = prefs.edit();
251f4ad4757de32ace6971cf4c3db7c395aa249001aMason Tang
252b60218a31d948ea0a549daf6464063d20b48421fMichael Chan        boolean validDetailView = false;
253b60218a31d948ea0a549daf6464063d20b48421fMichael Chan        if (mAllowWeekForDetailView && viewId == CalendarController.ViewType.WEEK) {
254b60218a31d948ea0a549daf6464063d20b48421fMichael Chan            validDetailView = true;
255b60218a31d948ea0a549daf6464063d20b48421fMichael Chan        } else {
256b60218a31d948ea0a549daf6464063d20b48421fMichael Chan            validDetailView = viewId == CalendarController.ViewType.AGENDA
257b60218a31d948ea0a549daf6464063d20b48421fMichael Chan                    || viewId == CalendarController.ViewType.DAY;
258b60218a31d948ea0a549daf6464063d20b48421fMichael Chan        }
259b60218a31d948ea0a549daf6464063d20b48421fMichael Chan
260b60218a31d948ea0a549daf6464063d20b48421fMichael Chan        if (validDetailView) {
261b60218a31d948ea0a549daf6464063d20b48421fMichael Chan            // Record the detail start view
2624b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa            editor.putInt(GeneralPreferences.KEY_DETAILED_VIEW, viewId);
263e8aa59d4575d712601a133a9263acc23adbc8c17Michael Chan        }
264e8aa59d4575d712601a133a9263acc23adbc8c17Michael Chan
265e8aa59d4575d712601a133a9263acc23adbc8c17Michael Chan        // Record the (new) start view
2664b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa        editor.putInt(GeneralPreferences.KEY_START_VIEW, viewId);
26724fac46d6b87ce21d5e6a4b1c0fdcaa83d408997Brad Fitzpatrick        editor.apply();
268e8aa59d4575d712601a133a9263acc23adbc8c17Michael Chan    }
269e8aa59d4575d712601a133a9263acc23adbc8c17Michael Chan
270a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik    public static MatrixCursor matrixCursorFromCursor(Cursor cursor) {
271a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        MatrixCursor newCursor = new MatrixCursor(cursor.getColumnNames());
272a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        int numColumns = cursor.getColumnCount();
273a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        String data[] = new String[numColumns];
274a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        cursor.moveToPosition(-1);
275a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        while (cursor.moveToNext()) {
276a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik            for (int i = 0; i < numColumns; i++) {
277a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik                data[i] = cursor.getString(i);
278a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik            }
279a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik            newCursor.addRow(data);
280a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        }
281a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        return newCursor;
282a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik    }
283a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik
284a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik    /**
285a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik     * Compares two cursors to see if they contain the same data.
286a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik     *
2877b92da258a480284dcc15a518ea570072329a31dErik     * @return Returns true of the cursors contain the same data and are not
2887b92da258a480284dcc15a518ea570072329a31dErik     *         null, false otherwise
289a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik     */
290a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik    public static boolean compareCursors(Cursor c1, Cursor c2) {
2917b92da258a480284dcc15a518ea570072329a31dErik        if (c1 == null || c2 == null) {
292a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik            return false;
293a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        }
294a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik
295a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        int numColumns = c1.getColumnCount();
296a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        if (numColumns != c2.getColumnCount()) {
297a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik            return false;
298a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        }
299a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik
300a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        if (c1.getCount() != c2.getCount()) {
301a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik            return false;
302a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        }
303a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik
304a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        c1.moveToPosition(-1);
305a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        c2.moveToPosition(-1);
3067b92da258a480284dcc15a518ea570072329a31dErik        while (c1.moveToNext() && c2.moveToNext()) {
3077b92da258a480284dcc15a518ea570072329a31dErik            for (int i = 0; i < numColumns; i++) {
3087b92da258a480284dcc15a518ea570072329a31dErik                if (!TextUtils.equals(c1.getString(i), c2.getString(i))) {
309a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik                    return false;
310a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik                }
311a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik            }
312a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        }
313a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik
314a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik        return true;
315a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik    }
316a144f86b41170e8ee7fe8d966cc51c5fc90cd44aErik
317146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    /**
318146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     * If the given intent specifies a time (in milliseconds since the epoch),
319146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     * then that time is returned. Otherwise, the current time is returned.
320146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     */
321146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    public static final long timeFromIntentInMillis(Intent intent) {
3227b92da258a480284dcc15a518ea570072329a31dErik        // If the time was specified, then use that. Otherwise, use the current
3237b92da258a480284dcc15a518ea570072329a31dErik        // time.
3241ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erik        Uri data = intent.getData();
325a27a886892fe3ec5edbc63c0b58e0a988623011aRoboErik        long millis = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1);
3261ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erik        if (millis == -1 && data != null && data.isHierarchical()) {
3271ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erik            List<String> path = data.getPathSegments();
3287b92da258a480284dcc15a518ea570072329a31dErik            if (path.size() == 2 && path.get(0).equals("time")) {
3291ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erik                try {
3301ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erik                    millis = Long.valueOf(data.getLastPathSegment());
3311ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erik                } catch (NumberFormatException e) {
3327b92da258a480284dcc15a518ea570072329a31dErik                    Log.i("Calendar", "timeFromIntentInMillis: Data existed but no valid time "
3337b92da258a480284dcc15a518ea570072329a31dErik                            + "found. Using current time.");
3341ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erik                }
3351ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erik            }
3361ef7f3ae2831dce8fa5e350f78ac4258c1a0a605Erik        }
33776727b7a9cf780f200414548b9d454bf9a701e3eErik        if (millis <= 0) {
338146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            millis = System.currentTimeMillis();
339146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
340146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        return millis;
341146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    }
342146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
343146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    /**
3447b92da258a480284dcc15a518ea570072329a31dErik     * Formats the given Time object so that it gives the month and year (for
3457b92da258a480284dcc15a518ea570072329a31dErik     * example, "September 2007").
346146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     *
347146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     * @param time the time to format
348146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     * @return the string containing the weekday and the date
349146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     */
350ad36a3c3cde7a2ec6d3a35d2529d46f03bd8d59dMichael Chan    public static String formatMonthYear(Context context, Time time) {
351cfa204ba5c59eb6ebd4b54788ceb31010e4a37a0RoboErik        int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY
352cfa204ba5c59eb6ebd4b54788ceb31010e4a37a0RoboErik                | DateUtils.FORMAT_SHOW_YEAR;
353cfa204ba5c59eb6ebd4b54788ceb31010e4a37a0RoboErik        long millis = time.toMillis(true);
354cfa204ba5c59eb6ebd4b54788ceb31010e4a37a0RoboErik        return formatDateRange(context, millis, millis, flags);
355146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    }
356146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
357146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    /**
3584c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang     * Returns a list joined together by the provided delimiter, for example,
3594c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang     * ["a", "b", "c"] could be joined into "a,b,c"
3604c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang     *
3614c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang     * @param things the things to join together
3624c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang     * @param delim the delimiter to use
3634c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang     * @return a string contained the things joined together
3644c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang     */
3654c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang    public static String join(List<?> things, String delim) {
3664c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang        StringBuilder builder = new StringBuilder();
3674c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang        boolean first = true;
3684c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang        for (Object thing : things) {
3694c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang            if (first) {
3704c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang                first = false;
3714c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang            } else {
3724c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang                builder.append(delim);
3734c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang            }
3744c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang            builder.append(thing.toString());
3754c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang        }
3764c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang        return builder.toString();
3774c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang    }
3784c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang
3794c8871bf5dee3b3586b375aee98effde31b781a8Mason Tang    /**
380981874e61ecf29a96a77601a3172b2503b6537eeErik     * Returns the week since {@link Time#EPOCH_JULIAN_DAY} (Jan 1, 1970)
381981874e61ecf29a96a77601a3172b2503b6537eeErik     * adjusted for first day of week.
382981874e61ecf29a96a77601a3172b2503b6537eeErik     *
383981874e61ecf29a96a77601a3172b2503b6537eeErik     * This takes a julian day and the week start day and calculates which
384981874e61ecf29a96a77601a3172b2503b6537eeErik     * week since {@link Time#EPOCH_JULIAN_DAY} that day occurs in, starting
385981874e61ecf29a96a77601a3172b2503b6537eeErik     * at 0. *Do not* use this to compute the ISO week number for the year.
386981874e61ecf29a96a77601a3172b2503b6537eeErik     *
387981874e61ecf29a96a77601a3172b2503b6537eeErik     * @param julianDay The julian day to calculate the week number for
388981874e61ecf29a96a77601a3172b2503b6537eeErik     * @param firstDayOfWeek Which week day is the first day of the week,
389981874e61ecf29a96a77601a3172b2503b6537eeErik     *          see {@link Time#SUNDAY}
390981874e61ecf29a96a77601a3172b2503b6537eeErik     * @return Weeks since the epoch
391981874e61ecf29a96a77601a3172b2503b6537eeErik     */
392981874e61ecf29a96a77601a3172b2503b6537eeErik    public static int getWeeksSinceEpochFromJulianDay(int julianDay, int firstDayOfWeek) {
393981874e61ecf29a96a77601a3172b2503b6537eeErik        int diff = Time.THURSDAY - firstDayOfWeek;
394981874e61ecf29a96a77601a3172b2503b6537eeErik        if (diff < 0) {
395981874e61ecf29a96a77601a3172b2503b6537eeErik            diff += 7;
396981874e61ecf29a96a77601a3172b2503b6537eeErik        }
397981874e61ecf29a96a77601a3172b2503b6537eeErik        int refDay = Time.EPOCH_JULIAN_DAY - diff;
398981874e61ecf29a96a77601a3172b2503b6537eeErik        return (julianDay - refDay) / 7;
399981874e61ecf29a96a77601a3172b2503b6537eeErik    }
400981874e61ecf29a96a77601a3172b2503b6537eeErik
401981874e61ecf29a96a77601a3172b2503b6537eeErik    /**
402981874e61ecf29a96a77601a3172b2503b6537eeErik     * Takes a number of weeks since the epoch and calculates the Julian day of
403981874e61ecf29a96a77601a3172b2503b6537eeErik     * the Monday for that week.
404981874e61ecf29a96a77601a3172b2503b6537eeErik     *
405981874e61ecf29a96a77601a3172b2503b6537eeErik     * This assumes that the week containing the {@link Time#EPOCH_JULIAN_DAY}
406981874e61ecf29a96a77601a3172b2503b6537eeErik     * is considered week 0. It returns the Julian day for the Monday
407981874e61ecf29a96a77601a3172b2503b6537eeErik     * {@code week} weeks after the Monday of the week containing the epoch.
408981874e61ecf29a96a77601a3172b2503b6537eeErik     *
409981874e61ecf29a96a77601a3172b2503b6537eeErik     * @param week Number of weeks since the epoch
410981874e61ecf29a96a77601a3172b2503b6537eeErik     * @return The julian day for the Monday of the given week since the epoch
411981874e61ecf29a96a77601a3172b2503b6537eeErik     */
412981874e61ecf29a96a77601a3172b2503b6537eeErik    public static int getJulianMondayFromWeeksSinceEpoch(int week) {
413981874e61ecf29a96a77601a3172b2503b6537eeErik        return MONDAY_BEFORE_JULIAN_EPOCH + week * 7;
414981874e61ecf29a96a77601a3172b2503b6537eeErik    }
415981874e61ecf29a96a77601a3172b2503b6537eeErik
416981874e61ecf29a96a77601a3172b2503b6537eeErik    /**
41756adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * Get first day of week as android.text.format.Time constant.
4187b92da258a480284dcc15a518ea570072329a31dErik     *
41956adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * @return the first day of week in android.text.format.Time
42056adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     */
4218e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang    public static int getFirstDayOfWeek(Context context) {
4224b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
4237b92da258a480284dcc15a518ea570072329a31dErik        String pref = prefs.getString(
4247b92da258a480284dcc15a518ea570072329a31dErik                GeneralPreferences.KEY_WEEK_START_DAY, GeneralPreferences.WEEK_START_DEFAULT);
4258e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang
4268e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang        int startDay;
4274b441bd6544fe6d11be75f974a41afd8fa040a4fDaisuke Miyakawa        if (GeneralPreferences.WEEK_START_DEFAULT.equals(pref)) {
4288e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang            startDay = Calendar.getInstance().getFirstDayOfWeek();
4298e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang        } else {
4308e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang            startDay = Integer.parseInt(pref);
4318e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang        }
4328e3d430a020744faa21bf4ca24f1a99c36ec5c4fMason Tang
43356adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi        if (startDay == Calendar.SATURDAY) {
43456adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi            return Time.SATURDAY;
43556adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi        } else if (startDay == Calendar.MONDAY) {
43656adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi            return Time.MONDAY;
43756adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi        } else {
43856adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi            return Time.SUNDAY;
43956adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi        }
44056adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi    }
44156adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi
44256adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi    /**
443d644b0df14ae6e204369b3454d16976fba32f15cDaisuke Miyakawa     * @return true when week number should be shown.
444981874e61ecf29a96a77601a3172b2503b6537eeErik     */
445981874e61ecf29a96a77601a3172b2503b6537eeErik    public static boolean getShowWeekNumber(Context context) {
446d644b0df14ae6e204369b3454d16976fba32f15cDaisuke Miyakawa        final SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
447981874e61ecf29a96a77601a3172b2503b6537eeErik        return prefs.getBoolean(
448981874e61ecf29a96a77601a3172b2503b6537eeErik                GeneralPreferences.KEY_SHOW_WEEK_NUM, GeneralPreferences.DEFAULT_SHOW_WEEK_NUM);
449981874e61ecf29a96a77601a3172b2503b6537eeErik    }
450981874e61ecf29a96a77601a3172b2503b6537eeErik
451981874e61ecf29a96a77601a3172b2503b6537eeErik    /**
45240bcd101b212c9863c3110c05a487a7ae6ebc3caErik     * @return true when declined events should be hidden.
45340bcd101b212c9863c3110c05a487a7ae6ebc3caErik     */
45440bcd101b212c9863c3110c05a487a7ae6ebc3caErik    public static boolean getHideDeclinedEvents(Context context) {
45540bcd101b212c9863c3110c05a487a7ae6ebc3caErik        final SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
45640bcd101b212c9863c3110c05a487a7ae6ebc3caErik        return prefs.getBoolean(GeneralPreferences.KEY_HIDE_DECLINED, false);
45740bcd101b212c9863c3110c05a487a7ae6ebc3caErik    }
45840bcd101b212c9863c3110c05a487a7ae6ebc3caErik
45991b01ed605e36fc5a7a924c226597a62c789b50dErik    public static int getDaysPerWeek(Context context) {
46091b01ed605e36fc5a7a924c226597a62c789b50dErik        final SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
46191b01ed605e36fc5a7a924c226597a62c789b50dErik        return prefs.getInt(GeneralPreferences.KEY_DAYS_PER_WEEK, 7);
46291b01ed605e36fc5a7a924c226597a62c789b50dErik    }
46391b01ed605e36fc5a7a924c226597a62c789b50dErik
46440bcd101b212c9863c3110c05a487a7ae6ebc3caErik    /**
46556adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * Determine whether the column position is Saturday or not.
4667b92da258a480284dcc15a518ea570072329a31dErik     *
46756adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * @param column the column position
46856adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * @param firstDayOfWeek the first day of week in android.text.format.Time
46956adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * @return true if the column is Saturday position
47056adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     */
47156adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi    public static boolean isSaturday(int column, int firstDayOfWeek) {
47256adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi        return (firstDayOfWeek == Time.SUNDAY && column == 6)
4737b92da258a480284dcc15a518ea570072329a31dErik                || (firstDayOfWeek == Time.MONDAY && column == 5)
4747b92da258a480284dcc15a518ea570072329a31dErik                || (firstDayOfWeek == Time.SATURDAY && column == 0);
47556adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi    }
47656adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi
47756adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi    /**
47856adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * Determine whether the column position is Sunday or not.
4797b92da258a480284dcc15a518ea570072329a31dErik     *
48056adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * @param column the column position
48156adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * @param firstDayOfWeek the first day of week in android.text.format.Time
48256adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     * @return true if the column is Sunday position
48356adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi     */
48456adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi    public static boolean isSunday(int column, int firstDayOfWeek) {
48556adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi        return (firstDayOfWeek == Time.SUNDAY && column == 0)
4867b92da258a480284dcc15a518ea570072329a31dErik                || (firstDayOfWeek == Time.MONDAY && column == 6)
4877b92da258a480284dcc15a518ea570072329a31dErik                || (firstDayOfWeek == Time.SATURDAY && column == 1);
48856adc7b3f9e62ada7f3708c5c7228e8ac5af1755Takaoka G. Tadashi    }
489ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan
490ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan    /**
4919da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik     * Convert given UTC time into current local time. This assumes it is for an
4929da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik     * allday event and will adjust the time to be on a midnight boundary.
4933ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang     *
4943ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang     * @param recycle Time object to recycle, otherwise null.
4953ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang     * @param utcTime Time to convert, in UTC.
4969da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik     * @param tz The time zone to convert this time to.
4973ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang     */
4989da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik    public static long convertAlldayUtcToLocal(Time recycle, long utcTime, String tz) {
4993ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang        if (recycle == null) {
5003ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang            recycle = new Time();
5013ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang        }
5023ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang        recycle.timezone = Time.TIMEZONE_UTC;
5033ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang        recycle.set(utcTime);
5049da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik        recycle.timezone = tz;
5059da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik        return recycle.normalize(true);
5069da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik    }
5079da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik
5089da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik    public static long convertAlldayLocalToUTC(Time recycle, long localTime, String tz) {
5099da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik        if (recycle == null) {
5109da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik            recycle = new Time();
5119da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik        }
5129da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik        recycle.timezone = tz;
5139da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik        recycle.set(localTime);
5149da910f65de0a325d4268d88f0cf2e2fd653f24aRoboErik        recycle.timezone = Time.TIMEZONE_UTC;
5153ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang        return recycle.normalize(true);
5163ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang    }
5173ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang
5183ea333d41c04fd5f3a5d45f540c17894874429e8Mason Tang    /**
519ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan     * Scan through a cursor of calendars and check if names are duplicated.
5207b92da258a480284dcc15a518ea570072329a31dErik     * This travels a cursor containing calendar display names and fills in the
5217b92da258a480284dcc15a518ea570072329a31dErik     * provided map with whether or not each name is repeated.
522ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan     *
523ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan     * @param isDuplicateName The map to put the duplicate check results in.
524ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan     * @param cursor The query of calendars to check
525ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan     * @param nameIndex The column of the query that contains the display name
526ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan     */
5277b92da258a480284dcc15a518ea570072329a31dErik    public static void checkForDuplicateNames(
5287b92da258a480284dcc15a518ea570072329a31dErik            Map<String, Boolean> isDuplicateName, Cursor cursor, int nameIndex) {
529ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan        isDuplicateName.clear();
530ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan        cursor.moveToPosition(-1);
531ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan        while (cursor.moveToNext()) {
532ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan            String displayName = cursor.getString(nameIndex);
533ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan            // Set it to true if we've seen this name before, false otherwise
534ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan            if (displayName != null) {
535ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan                isDuplicateName.put(displayName, isDuplicateName.containsKey(displayName));
536ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan            }
537ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan        }
538ff6be831fc682374be6b78c13ecf5daca81f86d9Michael Chan    }
5399138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang
5409138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang    /**
5419138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang     * Null-safe object comparison
5427b92da258a480284dcc15a518ea570072329a31dErik     *
5439138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang     * @param s1
5449138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang     * @param s2
5459138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang     * @return
5469138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang     */
5479138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang    public static boolean equals(Object o1, Object o2) {
5489138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang        return o1 == null ? o2 == null : o1.equals(o2);
5499138ce8a14924612c014da2b6e727b4117ba1a92Mason Tang    }
550b60218a31d948ea0a549daf6464063d20b48421fMichael Chan
55163cd053150e00fde045b019dbe0f48b8a9ed3559Erik    public static void setAllowWeekForDetailView(boolean allowWeekView) {
552b60218a31d948ea0a549daf6464063d20b48421fMichael Chan        mAllowWeekForDetailView  = allowWeekView;
553b60218a31d948ea0a549daf6464063d20b48421fMichael Chan    }
55463cd053150e00fde045b019dbe0f48b8a9ed3559Erik
55563cd053150e00fde045b019dbe0f48b8a9ed3559Erik    public static boolean getAllowWeekForDetailView() {
55663cd053150e00fde045b019dbe0f48b8a9ed3559Erik        return mAllowWeekForDetailView;
55763cd053150e00fde045b019dbe0f48b8a9ed3559Erik    }
5580b1bd10dbf90e55821a555c68a8a444854e31252Isaac Katzenelson
5590b1bd10dbf90e55821a555c68a8a444854e31252Isaac Katzenelson    public static boolean isMultiPaneConfiguration (Context c) {
5600b1bd10dbf90e55821a555c68a8a444854e31252Isaac Katzenelson        return (c.getResources().getConfiguration().screenLayout &
5610b1bd10dbf90e55821a555c68a8a444854e31252Isaac Katzenelson                Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0;
5620b1bd10dbf90e55821a555c68a8a444854e31252Isaac Katzenelson    }
563ff5c4345f794ec12b0a92f284434ddc1c88ff3a8Isaac Katzenelson
564ff5c4345f794ec12b0a92f284434ddc1c88ff3a8Isaac Katzenelson    public static boolean getConfigBool(Context c, int key) {
565ff5c4345f794ec12b0a92f284434ddc1c88ff3a8Isaac Katzenelson        return c.getResources().getBoolean(key);
566ff5c4345f794ec12b0a92f284434ddc1c88ff3a8Isaac Katzenelson    }
56782400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson
568092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // A single strand represents one color of events. Events are divided up by
569092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // color to make them convenient to draw. The black strand is special in
570092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // that it holds conflicting events as well as color settings for allday on
571092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // each day.
572092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    public static class DNAStrand {
573092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        public float[] points;
574092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        public int[] allDays; // color for the allday, 0 means no event
575092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int position;
576092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        public int color;
577092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int count;
578092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    }
57982400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson
580092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // A segment is a single continuous length of time occupied by a single
581092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // color. Segments should never span multiple days.
582092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    private static class DNASegment {
583092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int startMinute; // in minutes since the start of the week
584092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int endMinute;
585092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int color; // Calendar color or black for conflicts
586092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int day; // quick reference to the day this segment is on
58772a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson    }
58872a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson
58972a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson    /**
590092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * Converts a list of events to a list of segments to draw. Assumes list is
591092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * ordered by start time of the events. The function processes events for a
592092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * range of days from firstJulianDay to firstJulianDay + dayXs.length - 1.
593092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * The algorithm goes over all the events and creates a set of segments
594092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * ordered by start time. This list of segments is then converted into a
595092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * HashMap of strands which contain the draw points and are organized by
596092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * color. The strands can then be drawn by setting the paint color to each
597092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * strand's color and calling drawLines on its set of points. The points are
598092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * set up using the following parameters.
599092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * <ul>
600092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * <li>Events between midnight and WORK_DAY_START_MINUTES are compressed
601092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * into the first 1/8th of the space between top and bottom.</li>
602092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * <li>Events between WORK_DAY_END_MINUTES and the following midnight are
603092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * compressed into the last 1/8th of the space between top and bottom</li>
604092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * <li>Events between WORK_DAY_START_MINUTES and WORK_DAY_END_MINUTES use
605092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * the remaining 3/4ths of the space</li>
606092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * <li>All segments drawn will maintain at least minPixels height, except
607092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * for conflicts in the first or last 1/8th, which may be smaller</li>
608092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * </ul>
60971b9ce3a20ebaff8f7e40fc0e5ec2a388bcc394cIsaac Katzenelson     *
610092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * @param firstJulianDay The julian day of the first day of events
611092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * @param events A list of events sorted by start time
612092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * @param top The lowest y value the dna should be drawn at
613092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * @param bottom The highest y value the dna should be drawn at
614092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * @param dayXs An array of x values to draw the dna at, one for each day
615092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * @param conflictColor the color to use for conflicts
616092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * @return
61772a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson     */
618092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    public static HashMap<Integer, DNAStrand> createDNAStrands(int firstJulianDay,
619092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            ArrayList<Event> events, int top, int bottom, int minPixels, int[] dayXs,
620092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            Context context) {
62172a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson
622092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        if (!mMinutesLoaded) {
623092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (context == null) {
624092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                Log.wtf(TAG, "No context and haven't loaded parameters yet! Can't create DNA.");
62571b9ce3a20ebaff8f7e40fc0e5ec2a388bcc394cIsaac Katzenelson            }
626092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            Resources res = context.getResources();
627092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            CONFLICT_COLOR = res.getColor(R.color.month_dna_conflict_time_color);
628092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            WORK_DAY_START_MINUTES = res.getInteger(R.integer.work_start_minutes);
629092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            WORK_DAY_END_MINUTES = res.getInteger(R.integer.work_end_minutes);
630092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            WORK_DAY_END_LENGTH = DAY_IN_MINUTES - WORK_DAY_END_MINUTES;
631092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            WORK_DAY_MINUTES = WORK_DAY_END_MINUTES - WORK_DAY_START_MINUTES;
632092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            mMinutesLoaded = true;
63372a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson        }
63472a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson
635092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        if (events == null || events.isEmpty() || dayXs == null || dayXs.length < 1
636092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                || bottom - top < 8 || minPixels < 0) {
637092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            Log.e(TAG,
638092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    "Bad values for createDNAStrands! events:" + events + " dayXs:"
639092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            + Arrays.toString(dayXs) + " bot-top:" + (bottom - top) + " minPixels:"
640092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            + minPixels);
641092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            return null;
64271b9ce3a20ebaff8f7e40fc0e5ec2a388bcc394cIsaac Katzenelson        }
64371b9ce3a20ebaff8f7e40fc0e5ec2a388bcc394cIsaac Katzenelson
644092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        LinkedList<DNASegment> segments = new LinkedList<DNASegment>();
645092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        HashMap<Integer, DNAStrand> strands = new HashMap<Integer, DNAStrand>();
646092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // add a black strand by default, other colors will get added in
647092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // the loop
648092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        DNAStrand blackStrand = new DNAStrand();
649092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        blackStrand.color = CONFLICT_COLOR;
650092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        strands.put(CONFLICT_COLOR, blackStrand);
651092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // the min length is the number of minutes that will occupy
652092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // MIN_SEGMENT_PIXELS in the 'work day' time slot. This computes the
653092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // minutes/pixel * minpx where the number of pixels are 3/4 the total
654092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // dna height: 4*(mins/(px * 3/4))
655092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int minMinutes = minPixels * 4 * WORK_DAY_MINUTES / (3 * (bottom - top));
656092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
657092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // There are slightly fewer than half as many pixels in 1/6 the space,
658092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // so round to 2.5x for the min minutes in the non-work area
659092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int minOtherMinutes = minMinutes * 5 / 2;
660092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int lastJulianDay = firstJulianDay + dayXs.length - 1;
661092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
662092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        Event event = new Event();
663092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // Go through all the events for the week
664092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        for (Event currEvent : events) {
665092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // if this event is outside the weeks range skip it
666092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (currEvent.endDay < firstJulianDay || currEvent.startDay > lastJulianDay) {
667092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                continue;
66872a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson            }
669092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (currEvent.drawAsAllday()) {
670092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                addAllDayToStrands(currEvent, strands, firstJulianDay, dayXs.length);
671092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                continue;
67271b9ce3a20ebaff8f7e40fc0e5ec2a388bcc394cIsaac Katzenelson            }
673092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // Copy the event over so we can clip its start and end to our range
674092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            currEvent.copyTo(event);
675092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (event.startDay < firstJulianDay) {
676092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                event.startDay = firstJulianDay;
677092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                event.startTime = 0;
67871b9ce3a20ebaff8f7e40fc0e5ec2a388bcc394cIsaac Katzenelson            }
679092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // If it starts after the work day make sure the start is at least
680092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // minPixels from midnight
681092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (event.startTime > DAY_IN_MINUTES - minOtherMinutes) {
682092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                event.startTime = DAY_IN_MINUTES - minOtherMinutes;
68382400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson            }
684092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (event.endDay > lastJulianDay) {
685092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                event.endDay = lastJulianDay;
686092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                event.endTime = DAY_IN_MINUTES - 1;
68772a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson            }
688092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // If the end time is before the work day make sure it ends at least
689092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // minPixels after midnight
690092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (event.endTime < minOtherMinutes) {
691092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                event.endTime = minOtherMinutes;
69282400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson            }
693092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // If the start and end are on the same day make sure they are at
694092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // least minPixels apart. This only needs to be done for times
695092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // outside the work day as the min distance for within the work day
696092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // is enforced in the segment code.
697092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (event.startDay == event.endDay &&
698092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    event.endTime - event.startTime < minOtherMinutes) {
699092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                // If it's less than minPixels in an area before the work
700092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                // day
701092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                if (event.startTime < WORK_DAY_START_MINUTES) {
702092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // extend the end to the first easy guarantee that it's
703092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // minPixels
704092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    event.endTime = Math.min(event.startTime + minOtherMinutes,
705092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            WORK_DAY_START_MINUTES + minMinutes);
706092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // if it's in the area after the work day
707092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                } else if (event.endTime > WORK_DAY_END_MINUTES) {
708092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // First try shifting the end but not past midnight
709092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    event.endTime = Math.min(event.endTime + minOtherMinutes, DAY_IN_MINUTES - 1);
710092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // if it's still too small move the start back
711092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    if (event.endTime - event.startTime < minOtherMinutes) {
712092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        event.startTime = event.endTime - minOtherMinutes;
713092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    }
714092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                }
71582400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson            }
71682400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson
717092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // This handles adding the first segment
718092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (segments.size() == 0) {
719092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                addNewSegment(segments, event, strands, firstJulianDay, 0, minMinutes);
72082400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson                continue;
72182400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson            }
722092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // Now compare our current start time to the end time of the last
723092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // segment in the list
724092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            DNASegment lastSegment = segments.getLast();
725092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int startMinute = (event.startDay - firstJulianDay) * DAY_IN_MINUTES + event.startTime;
726092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int endMinute = Math.max((event.endDay - firstJulianDay) * DAY_IN_MINUTES
727092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    + event.endTime, startMinute + minMinutes);
728092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
729092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (startMinute < 0) {
730092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                startMinute = 0;
731092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            }
732092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (endMinute >= WEEK_IN_MINUTES) {
733092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                endMinute = WEEK_IN_MINUTES - 1;
734092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            }
735092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // If we start before the last segment in the list ends we need to
736092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // start going through the list as this may conflict with other
737092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // events
738092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (startMinute < lastSegment.endMinute) {
739092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                int i = segments.size();
740092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                // find the last segment this event intersects with
741092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                while (--i >= 0 && endMinute < segments.get(i).startMinute);
742092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
743092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                DNASegment currSegment;
744092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                // for each segment this event intersects with
745092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                for (; i >= 0 && startMinute <= (currSegment = segments.get(i)).endMinute; i--) {
746092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // if the segment is already a conflict ignore it
747092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    if (currSegment.color == CONFLICT_COLOR) {
748092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        continue;
74972a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson                    }
750092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // if the event ends before the segment and wouldn't create
751092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // a segment that is too small split off the right side
752092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    if (endMinute < currSegment.endMinute - minMinutes) {
753092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        DNASegment rhs = new DNASegment();
754092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        rhs.endMinute = currSegment.endMinute;
755092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        rhs.color = currSegment.color;
756092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        rhs.startMinute = endMinute + 1;
757092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        rhs.day = currSegment.day;
758092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        currSegment.endMinute = endMinute;
759092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        segments.add(i + 1, rhs);
760092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        strands.get(rhs.color).count++;
761092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        if (DEBUG) {
762092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            Log.d(TAG, "Added rhs, curr:" + currSegment.toString() + " i:"
763092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                                    + segments.get(i).toString());
764092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        }
765092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    }
766092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // if the event starts after the segment and wouldn't create
767092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // a segment that is too small split off the left side
768092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    if (startMinute > currSegment.startMinute + minMinutes) {
769092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        DNASegment lhs = new DNASegment();
770092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        lhs.startMinute = currSegment.startMinute;
771092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        lhs.color = currSegment.color;
772092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        lhs.endMinute = startMinute - 1;
773092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        lhs.day = currSegment.day;
774092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        currSegment.startMinute = startMinute;
775092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        // increment i so that we are at the right position when
776092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        // referencing the segments to the right and left of the
777092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        // current segment.
778092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        segments.add(i++, lhs);
779092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        strands.get(lhs.color).count++;
780092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        if (DEBUG) {
781092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            Log.d(TAG, "Added lhs, curr:" + currSegment.toString() + " i:"
782092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                                    + segments.get(i).toString());
783092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        }
784092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    }
785092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // if the right side is black merge this with the segment to
786092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // the right if they're on the same day and overlap
787092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    if (i + 1 < segments.size()) {
788092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        DNASegment rhs = segments.get(i + 1);
789092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        if (rhs.color == CONFLICT_COLOR && currSegment.day == rhs.day
790092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                                && rhs.startMinute <= currSegment.endMinute + 1) {
791092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            rhs.startMinute = Math.min(currSegment.startMinute, rhs.startMinute);
792092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            segments.remove(currSegment);
793092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            strands.get(currSegment.color).count--;
794092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            // point at the new current segment
795092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            currSegment = rhs;
796092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        }
797092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    }
798092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // if the left side is black merge this with the segment to
799092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // the left if they're on the same day and overlap
800092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    if (i - 1 >= 0) {
801092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        DNASegment lhs = segments.get(i - 1);
802092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        if (lhs.color == CONFLICT_COLOR && currSegment.day == lhs.day
803092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                                && lhs.endMinute >= currSegment.startMinute - 1) {
804092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            lhs.endMinute = Math.max(currSegment.endMinute, lhs.endMinute);
805092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            segments.remove(currSegment);
806092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            strands.get(currSegment.color).count--;
807092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            // point at the new current segment
808092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            currSegment = lhs;
809092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            // point i at the new current segment in case new
810092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            // code is added
811092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                            i--;
812092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        }
813092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    }
814092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // if we're still not black, decrement the count for the
815092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // color being removed, change this to black, and increment
816092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    // the black count
817092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    if (currSegment.color != CONFLICT_COLOR) {
818092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        strands.get(currSegment.color).count--;
819092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        currSegment.color = CONFLICT_COLOR;
820092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        strands.get(CONFLICT_COLOR).count++;
82172a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson                    }
82272a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson                }
823092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
82472a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson            }
825092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // If this event extends beyond the last segment add a new segment
826092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (endMinute > lastSegment.endMinute) {
827092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                addNewSegment(segments, event, strands, firstJulianDay, lastSegment.endMinute,
828092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        minMinutes);
82982400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson            }
83082400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson        }
831092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        weaveDNAStrands(segments, firstJulianDay, strands, top, bottom, dayXs);
832092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        return strands;
833092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    }
83482400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson
835092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // This figures out allDay colors as allDay events are found
836092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    private static void addAllDayToStrands(Event event, HashMap<Integer, DNAStrand> strands,
837092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int firstJulianDay, int numDays) {
838092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        DNAStrand strand = getOrCreateStrand(strands, CONFLICT_COLOR);
839092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // if we haven't initialized the allDay portion create it now
840092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        if (strand.allDays == null) {
841092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand.allDays = new int[numDays];
84272a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson        }
84372a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson
844092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // For each day this event is on update the color
845092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int end = Math.min(event.endDay - firstJulianDay, numDays - 1);
846092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        for (int i = Math.max(event.startDay - firstJulianDay, 0); i <= end; i++) {
847092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (strand.allDays[i] != 0) {
848092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                // if this day already had a color, it is now a conflict
849092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                strand.allDays[i] = CONFLICT_COLOR;
850092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            } else {
851092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                // else it's just the color of the event
852092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                strand.allDays[i] = event.color;
853092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            }
854092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        }
855092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    }
856092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
857092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // This processes all the segments, sorts them by color, and generates a
858092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    // list of points to draw
859092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    private static void weaveDNAStrands(LinkedList<DNASegment> segments, int firstJulianDay,
860092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            HashMap<Integer, DNAStrand> strands, int top, int bottom, int[] dayXs) {
861092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // First, get rid of any colors that ended up with no segments
862092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        Iterator<DNAStrand> strandIterator = strands.values().iterator();
863092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        while (strandIterator.hasNext()) {
864092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            DNAStrand strand = strandIterator.next();
865092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (strand.count < 1 && strand.allDays == null) {
866092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                strandIterator.remove();
86772a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson                continue;
86872a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson            }
869092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand.points = new float[strand.count * 4];
870092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand.position = 0;
871092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        }
872092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // Go through each segment and compute its points
873092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        for (DNASegment segment : segments) {
874092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // Add the points to the strand of that color
875092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            DNAStrand strand = strands.get(segment.color);
876092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int dayIndex = segment.day - firstJulianDay;
877092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int dayStartMinute = segment.startMinute % DAY_IN_MINUTES;
878092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int dayEndMinute = segment.endMinute % DAY_IN_MINUTES;
879092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int height = bottom - top;
880092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int workDayHeight = height * 3 / 4;
881092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int remainderHeight = (height - workDayHeight) / 2;
882092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
883092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int x = dayXs[dayIndex];
884092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int y0 = 0;
885092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int y1 = 0;
886092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
887092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            y0 = top + getPixelOffsetFromMinutes(dayStartMinute, workDayHeight, remainderHeight);
888092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            y1 = top + getPixelOffsetFromMinutes(dayEndMinute, workDayHeight, remainderHeight);
889092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            if (DEBUG) {
890092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                Log.d(TAG, "Adding " + Integer.toHexString(segment.color) + " at x,y0,y1: " + x
891092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                        + " " + y0 + " " + y1 + " for " + dayStartMinute + " " + dayEndMinute);
89272a9459e1f4cec02ad9e8dbdf824d66920b762eeIsaac Katzenelson            }
893092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand.points[strand.position++] = x;
894092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand.points[strand.position++] = y0;
895092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand.points[strand.position++] = x;
896092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand.points[strand.position++] = y1;
89782400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson        }
89882400dd70331df7885dd59b809c4bc0667046320Isaac Katzenelson    }
899092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
900092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    /**
901092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * Compute a pixel offset from the top for a given minute from the work day
902092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * height and the height of the top area.
903092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     */
904092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    private static int getPixelOffsetFromMinutes(int minute, int workDayHeight,
905092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            int remainderHeight) {
906092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int y;
907092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        if (minute < WORK_DAY_START_MINUTES) {
908092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            y = minute * remainderHeight / WORK_DAY_START_MINUTES;
909092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        } else if (minute < WORK_DAY_END_MINUTES) {
910092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            y = remainderHeight + (minute - WORK_DAY_START_MINUTES) * workDayHeight
911092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    / WORK_DAY_MINUTES;
912092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        } else {
913092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            y = remainderHeight + workDayHeight + (minute - WORK_DAY_END_MINUTES) * remainderHeight
914092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                    / WORK_DAY_END_LENGTH;
915092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        }
916092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        return y;
917092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    }
918092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
919092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    /**
920092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * Add a new segment based on the event provided. This will handle splitting
921092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * segments across day boundaries and ensures a minimum size for segments.
922092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     */
923092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    private static void addNewSegment(LinkedList<DNASegment> segments, Event event,
924092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            HashMap<Integer, DNAStrand> strands, int firstJulianDay, int minStart, int minMinutes) {
925092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        if (event.startDay > event.endDay) {
926092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            Log.wtf(TAG, "Event starts after it ends: " + event.toString());
927092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        }
928092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // If this is a multiday event split it up by day
929092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        if (event.startDay != event.endDay) {
930092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            Event lhs = new Event();
931092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            lhs.color = event.color;
932092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            lhs.startDay = event.startDay;
933092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // the first day we want the start time to be the actual start time
934092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            lhs.startTime = event.startTime;
935092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            lhs.endDay = lhs.startDay;
936092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            lhs.endTime = DAY_IN_MINUTES - 1;
937092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // Nearly recursive iteration!
938092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            while (lhs.startDay != event.endDay) {
939092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                addNewSegment(segments, lhs, strands, firstJulianDay, minStart, minMinutes);
940092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                // The days in between are all day, even though that shouldn't
941092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                // actually happen due to the allday filtering
942092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                lhs.startDay++;
943092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                lhs.endDay = lhs.startDay;
944092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                lhs.startTime = 0;
945092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik                minStart = 0;
946092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            }
947092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            // The last day we want the end time to be the actual end time
948092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            lhs.endTime = event.endTime;
949092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            event = lhs;
950092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        }
951092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // Create the new segment and compute its fields
952092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        DNASegment segment = new DNASegment();
953092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int dayOffset = (event.startDay - firstJulianDay) * DAY_IN_MINUTES;
954092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int endOfDay = dayOffset + DAY_IN_MINUTES - 1;
955092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // clip the start if needed
956092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        segment.startMinute = Math.max(dayOffset + event.startTime, minStart);
957092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // and extend the end if it's too small, but not beyond the end of the
958092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // day
959092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        int minEnd = Math.min(segment.startMinute + minMinutes, endOfDay);
960092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        segment.endMinute = Math.max(dayOffset + event.endTime, minEnd);
961092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        if (segment.endMinute > endOfDay) {
962092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            segment.endMinute = endOfDay;
963092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        }
964092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
965092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        segment.color = event.color;
966092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        segment.day = event.startDay;
967092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        segments.add(segment);
968092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // increment the count for the correct color or add a new strand if we
969092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        // don't have that color yet
970092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        DNAStrand strand = getOrCreateStrand(strands, segment.color);
971092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        strand.count++;
972092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    }
973092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
974092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    /**
975092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     * Try to get a strand of the given color. Create it if it doesn't exist.
976092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik     */
977092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    private static DNAStrand getOrCreateStrand(HashMap<Integer, DNAStrand> strands, int color) {
978092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        DNAStrand strand = strands.get(color);
979092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        if (strand == null) {
980092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand = new DNAStrand();
981092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand.color = color;
982092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strand.count = 0;
983092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik            strands.put(strand.color, strand);
984092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        }
985092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik        return strand;
986092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik    }
987092caec951caa0fabcd51729678e3ddcd6ca2f03RoboErik
988c0f6efee87520ce0c8b52246129169f94850177bRoboErik    /**
989c0f6efee87520ce0c8b52246129169f94850177bRoboErik     * Sends an intent to launch the top level Calendar view.
990c0f6efee87520ce0c8b52246129169f94850177bRoboErik     *
991c0f6efee87520ce0c8b52246129169f94850177bRoboErik     * @param context
992c0f6efee87520ce0c8b52246129169f94850177bRoboErik     */
993c0f6efee87520ce0c8b52246129169f94850177bRoboErik    public static void returnToCalendarHome(Context context) {
994c0f6efee87520ce0c8b52246129169f94850177bRoboErik        Intent launchIntent = new Intent();
995c0f6efee87520ce0c8b52246129169f94850177bRoboErik        launchIntent.setAction(Intent.ACTION_VIEW);
996c0f6efee87520ce0c8b52246129169f94850177bRoboErik        launchIntent.setData(Uri.parse("content://com.android.calendar/time"));
997c0f6efee87520ce0c8b52246129169f94850177bRoboErik        launchIntent.setClass(context, AllInOneActivity.class);
998c0f6efee87520ce0c8b52246129169f94850177bRoboErik        launchIntent.setFlags(
999c0f6efee87520ce0c8b52246129169f94850177bRoboErik                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);
1000c0f6efee87520ce0c8b52246129169f94850177bRoboErik        context.startActivity(launchIntent);
1001c0f6efee87520ce0c8b52246129169f94850177bRoboErik    }
1002c0f6efee87520ce0c8b52246129169f94850177bRoboErik
1003146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project}
1004