1056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock/**
2056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * Copyright (c) 2014, The Android Open Source Project
3056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock *
4056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * Licensed under the Apache License, Version 2.0 (the "License");
5056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * you may not use this file except in compliance with the License.
6056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * You may obtain a copy of the License at
7056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock *
8056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock *     http://www.apache.org/licenses/LICENSE-2.0
9056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock *
10056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * Unless required by applicable law or agreed to in writing, software
11056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * distributed under the License is distributed on an "AS IS" BASIS,
12056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * See the License for the specific language governing permissions and
14056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * limitations under the License.
15056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock */
16056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
17056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockpackage android.service.notification;
18056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
193b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlockimport android.content.ComponentName;
20c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlockimport android.content.Context;
214dd81467e33a694138da6916fc68ca79501a9429Jason Monkimport android.content.res.Resources;
223b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlockimport android.net.Uri;
23056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport android.os.Parcel;
24056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport android.os.Parcelable;
25056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport android.text.TextUtils;
26c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlockimport android.text.format.DateFormat;
27856edebad73560e9b1cce021a7de9a0470d07176John Spurlockimport android.util.Slog;
28056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
29056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport org.xmlpull.v1.XmlPullParser;
30056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport org.xmlpull.v1.XmlPullParserException;
31056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport org.xmlpull.v1.XmlSerializer;
32056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
33056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport java.io.IOException;
343b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlockimport java.util.ArrayList;
353b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlockimport java.util.Arrays;
36ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlockimport java.util.Calendar;
37c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlockimport java.util.Locale;
38056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport java.util.Objects;
39056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
40c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlockimport com.android.internal.R;
41c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock
42056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock/**
43056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * Persisted configuration for zen mode.
44056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock *
45056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * @hide
46056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock */
47056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockpublic class ZenModeConfig implements Parcelable {
48856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    private static String TAG = "ZenModeConfig";
49056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
50056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static final String SLEEP_MODE_NIGHTS = "nights";
51056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static final String SLEEP_MODE_WEEKNIGHTS = "weeknights";
52ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static final String SLEEP_MODE_DAYS_PREFIX = "days:";
53056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
5499f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static final int SOURCE_ANYONE = 0;
5599f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static final int SOURCE_CONTACT = 1;
5699f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static final int SOURCE_STAR = 2;
5799f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static final int MAX_SOURCE = SOURCE_STAR;
5899f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren
59ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static final int[] ALL_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
60ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY };
61ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static final int[] WEEKNIGHT_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
62ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            Calendar.WEDNESDAY, Calendar.THURSDAY };
63ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock
644dd81467e33a694138da6916fc68ca79501a9429Jason Monk    public static final int[] MINUTE_BUCKETS = new int[] { 15, 30, 45, 60, 120, 180, 240, 480 };
654dd81467e33a694138da6916fc68ca79501a9429Jason Monk    private static final int SECONDS_MS = 1000;
664dd81467e33a694138da6916fc68ca79501a9429Jason Monk    private static final int MINUTES_MS = 60 * SECONDS_MS;
67530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock    private static final int ZERO_VALUE_MS = 10 * SECONDS_MS;
684dd81467e33a694138da6916fc68ca79501a9429Jason Monk
692dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock    private static final boolean DEFAULT_ALLOW_EVENTS = true;
702dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock
71056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final int XML_VERSION = 1;
72056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ZEN_TAG = "zen";
73056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ZEN_ATT_VERSION = "version";
74056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ALLOW_TAG = "allow";
75056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ALLOW_ATT_CALLS = "calls";
76056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ALLOW_ATT_MESSAGES = "messages";
7799f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    private static final String ALLOW_ATT_FROM = "from";
782dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock    private static final String ALLOW_ATT_EVENTS = "events";
79056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_TAG = "sleep";
80056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_MODE = "mode";
81cc30c8d729c9d846012ac6dd03c1e51262936e8fJohn Spurlock    private static final String SLEEP_ATT_NONE = "none";
82056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
83056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_START_HR = "startHour";
84056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_START_MIN = "startMin";
85056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_END_HR = "endHour";
86056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_END_MIN = "endMin";
87056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
883b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static final String CONDITION_TAG = "condition";
893b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static final String CONDITION_ATT_COMPONENT = "component";
903b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static final String CONDITION_ATT_ID = "id";
914db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_SUMMARY = "summary";
924db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_LINE1 = "line1";
934db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_LINE2 = "line2";
944db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_ICON = "icon";
954db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_STATE = "state";
964db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_FLAGS = "flags";
973b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock
98856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    private static final String EXIT_CONDITION_TAG = "exitCondition";
9950806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock    private static final String EXIT_CONDITION_ATT_COMPONENT = "component";
100856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
101056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public boolean allowCalls;
102056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public boolean allowMessages;
1032dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock    public boolean allowEvents = DEFAULT_ALLOW_EVENTS;
10499f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public int allowFrom = SOURCE_ANYONE;
105056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
106056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public String sleepMode;
1074db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public int sleepStartHour;   // 0-23
1084db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public int sleepStartMinute; // 0-59
109056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public int sleepEndHour;
110056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public int sleepEndMinute;
111cc30c8d729c9d846012ac6dd03c1e51262936e8fJohn Spurlock    public boolean sleepNone;    // false = priority, true = none
1123b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    public ComponentName[] conditionComponents;
1133b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    public Uri[] conditionIds;
1144db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public Condition exitCondition;
11550806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock    public ComponentName exitConditionComponent;
116056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
117056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public ZenModeConfig() { }
118056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
119056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public ZenModeConfig(Parcel source) {
120056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        allowCalls = source.readInt() == 1;
121056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        allowMessages = source.readInt() == 1;
1222dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock        allowEvents = source.readInt() == 1;
123056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (source.readInt() == 1) {
124056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            sleepMode = source.readString();
125056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
126056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        sleepStartHour = source.readInt();
127056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        sleepStartMinute = source.readInt();
128056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        sleepEndHour = source.readInt();
129056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        sleepEndMinute = source.readInt();
130cc30c8d729c9d846012ac6dd03c1e51262936e8fJohn Spurlock        sleepNone = source.readInt() == 1;
1313b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        int len = source.readInt();
1323b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (len > 0) {
1333b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            conditionComponents = new ComponentName[len];
1343b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            source.readTypedArray(conditionComponents, ComponentName.CREATOR);
1353b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
1363b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        len = source.readInt();
1373b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (len > 0) {
1383b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            conditionIds = new Uri[len];
1393b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            source.readTypedArray(conditionIds, Uri.CREATOR);
1403b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
14199f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        allowFrom = source.readInt();
1424db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        exitCondition = source.readParcelable(null);
14350806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock        exitConditionComponent = source.readParcelable(null);
144056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
145056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
146056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
147056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public void writeToParcel(Parcel dest, int flags) {
148056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(allowCalls ? 1 : 0);
149056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(allowMessages ? 1 : 0);
1502dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock        dest.writeInt(allowEvents ? 1 : 0);
151056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (sleepMode != null) {
152056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            dest.writeInt(1);
153056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            dest.writeString(sleepMode);
154056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        } else {
155056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            dest.writeInt(0);
156056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
157056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(sleepStartHour);
158056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(sleepStartMinute);
159056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(sleepEndHour);
160056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(sleepEndMinute);
161cc30c8d729c9d846012ac6dd03c1e51262936e8fJohn Spurlock        dest.writeInt(sleepNone ? 1 : 0);
1623b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (conditionComponents != null && conditionComponents.length > 0) {
1633b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeInt(conditionComponents.length);
1643b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeTypedArray(conditionComponents, 0);
1653b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        } else {
1663b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeInt(0);
1673b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
1683b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (conditionIds != null && conditionIds.length > 0) {
1693b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeInt(conditionIds.length);
1703b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeTypedArray(conditionIds, 0);
1713b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        } else {
1723b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeInt(0);
1733b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
17499f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        dest.writeInt(allowFrom);
1754db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        dest.writeParcelable(exitCondition, 0);
17650806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock        dest.writeParcelable(exitConditionComponent, 0);
177056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
178056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
179056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
180056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public String toString() {
181056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[')
182056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append("allowCalls=").append(allowCalls)
183056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(",allowMessages=").append(allowMessages)
18499f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            .append(",allowFrom=").append(sourceToString(allowFrom))
1852dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock            .append(",allowEvents=").append(allowEvents)
186056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(",sleepMode=").append(sleepMode)
187056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(",sleepStart=").append(sleepStartHour).append('.').append(sleepStartMinute)
188056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(",sleepEnd=").append(sleepEndHour).append('.').append(sleepEndMinute)
189cc30c8d729c9d846012ac6dd03c1e51262936e8fJohn Spurlock            .append(",sleepNone=").append(sleepNone)
1903b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            .append(",conditionComponents=")
1913b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            .append(conditionComponents == null ? null : TextUtils.join(",", conditionComponents))
1923b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            .append(",conditionIds=")
1933b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            .append(conditionIds == null ? null : TextUtils.join(",", conditionIds))
1944db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            .append(",exitCondition=").append(exitCondition)
19550806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock            .append(",exitConditionComponent=").append(exitConditionComponent)
196056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(']').toString();
197056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
198056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
19999f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static String sourceToString(int source) {
20099f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        switch (source) {
20199f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            case SOURCE_ANYONE:
20299f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                return "anyone";
20399f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            case SOURCE_CONTACT:
20499f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                return "contacts";
20599f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            case SOURCE_STAR:
20699f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                return "stars";
20799f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            default:
20899f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                return "UNKNOWN";
20999f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        }
21099f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    }
21199f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren
212056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
213056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public boolean equals(Object o) {
214056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (!(o instanceof ZenModeConfig)) return false;
215056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (o == this) return true;
216056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final ZenModeConfig other = (ZenModeConfig) o;
217056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return other.allowCalls == allowCalls
218056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && other.allowMessages == allowMessages
21999f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                && other.allowFrom == allowFrom
2202dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock                && other.allowEvents == allowEvents
221056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && Objects.equals(other.sleepMode, sleepMode)
222cc30c8d729c9d846012ac6dd03c1e51262936e8fJohn Spurlock                && other.sleepNone == sleepNone
223056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && other.sleepStartHour == sleepStartHour
224056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && other.sleepStartMinute == sleepStartMinute
225056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && other.sleepEndHour == sleepEndHour
2263b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                && other.sleepEndMinute == sleepEndMinute
2273b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                && Objects.deepEquals(other.conditionComponents, conditionComponents)
228856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                && Objects.deepEquals(other.conditionIds, conditionIds)
2294db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                && Objects.equals(other.exitCondition, exitCondition)
23050806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock                && Objects.equals(other.exitConditionComponent, exitConditionComponent);
231056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
232056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
233056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
234056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public int hashCode() {
235cc30c8d729c9d846012ac6dd03c1e51262936e8fJohn Spurlock        return Objects.hash(allowCalls, allowMessages, allowFrom, allowEvents, sleepMode, sleepNone,
23699f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                sleepStartHour, sleepStartMinute, sleepEndHour, sleepEndMinute,
237856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                Arrays.hashCode(conditionComponents), Arrays.hashCode(conditionIds),
2384db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                exitCondition, exitConditionComponent);
239056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
240056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
241056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public boolean isValid() {
242056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return isValidHour(sleepStartHour) && isValidMinute(sleepStartMinute)
243056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && isValidHour(sleepEndHour) && isValidMinute(sleepEndMinute)
244ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock                && isValidSleepMode(sleepMode);
245ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    }
246ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock
247ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static boolean isValidSleepMode(String sleepMode) {
248ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        return sleepMode == null || sleepMode.equals(SLEEP_MODE_NIGHTS)
249ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock                || sleepMode.equals(SLEEP_MODE_WEEKNIGHTS) || tryParseDays(sleepMode) != null;
250ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    }
251ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock
252ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static int[] tryParseDays(String sleepMode) {
253ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (sleepMode == null) return null;
254ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        sleepMode = sleepMode.trim();
255ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (SLEEP_MODE_NIGHTS.equals(sleepMode)) return ALL_DAYS;
256ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (SLEEP_MODE_WEEKNIGHTS.equals(sleepMode)) return WEEKNIGHT_DAYS;
257ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (!sleepMode.startsWith(SLEEP_MODE_DAYS_PREFIX)) return null;
258ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (sleepMode.equals(SLEEP_MODE_DAYS_PREFIX)) return null;
259ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        final String[] tokens = sleepMode.substring(SLEEP_MODE_DAYS_PREFIX.length()).split(",");
260ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (tokens.length == 0) return null;
261ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        final int[] rt = new int[tokens.length];
262ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        for (int i = 0; i < tokens.length; i++) {
263ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            final int day = tryParseInt(tokens[i], -1);
264ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            if (day == -1) return null;
265ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            rt[i] = day;
266ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        }
267ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        return rt;
268ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    }
269ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock
270ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    private static int tryParseInt(String value, int defValue) {
271ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (TextUtils.isEmpty(value)) return defValue;
272ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        try {
273ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            return Integer.valueOf(value);
274ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        } catch (NumberFormatException e) {
275ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            return defValue;
276ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        }
277056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
278056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
279056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static ZenModeConfig readXml(XmlPullParser parser)
280056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            throws XmlPullParserException, IOException {
281056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        int type = parser.getEventType();
282056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (type != XmlPullParser.START_TAG) return null;
283056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        String tag = parser.getName();
284056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (!ZEN_TAG.equals(tag)) return null;
285056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final ZenModeConfig rt = new ZenModeConfig();
286ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        final int version = safeInt(parser, ZEN_ATT_VERSION, XML_VERSION);
2873b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final ArrayList<ComponentName> conditionComponents = new ArrayList<ComponentName>();
2883b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final ArrayList<Uri> conditionIds = new ArrayList<Uri>();
289056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
290056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            tag = parser.getName();
2913b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) {
2923b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                if (!conditionComponents.isEmpty()) {
2933b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    rt.conditionComponents = conditionComponents
2943b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                            .toArray(new ComponentName[conditionComponents.size()]);
2953b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    rt.conditionIds = conditionIds.toArray(new Uri[conditionIds.size()]);
2963b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                }
2973b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                return rt;
2983b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            }
299056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            if (type == XmlPullParser.START_TAG) {
300056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                if (ALLOW_TAG.equals(tag)) {
301056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false);
302056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false);
3032dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock                    rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS, DEFAULT_ALLOW_EVENTS);
30499f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                    rt.allowFrom = safeInt(parser, ALLOW_ATT_FROM, SOURCE_ANYONE);
30599f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                    if (rt.allowFrom < SOURCE_ANYONE || rt.allowFrom > MAX_SOURCE) {
30699f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                        throw new IndexOutOfBoundsException("bad source in config:" + rt.allowFrom);
30799f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                    }
308056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                } else if (SLEEP_TAG.equals(tag)) {
309056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final String mode = parser.getAttributeValue(null, SLEEP_ATT_MODE);
310ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock                    rt.sleepMode = isValidSleepMode(mode)? mode : null;
311cc30c8d729c9d846012ac6dd03c1e51262936e8fJohn Spurlock                    rt.sleepNone = safeBoolean(parser, SLEEP_ATT_NONE, false);
312056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final int startHour = safeInt(parser, SLEEP_ATT_START_HR, 0);
313056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final int startMinute = safeInt(parser, SLEEP_ATT_START_MIN, 0);
314056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final int endHour = safeInt(parser, SLEEP_ATT_END_HR, 0);
315056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final int endMinute = safeInt(parser, SLEEP_ATT_END_MIN, 0);
316056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.sleepStartHour = isValidHour(startHour) ? startHour : 0;
317056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.sleepStartMinute = isValidMinute(startMinute) ? startMinute : 0;
318056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.sleepEndHour = isValidHour(endHour) ? endHour : 0;
319056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.sleepEndMinute = isValidMinute(endMinute) ? endMinute : 0;
3203b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                } else if (CONDITION_TAG.equals(tag)) {
3213b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    final ComponentName component =
3223b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                            safeComponentName(parser, CONDITION_ATT_COMPONENT);
3233b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    final Uri conditionId = safeUri(parser, CONDITION_ATT_ID);
3243b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    if (component != null && conditionId != null) {
3253b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                        conditionComponents.add(component);
3263b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                        conditionIds.add(conditionId);
3273b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    }
328856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                } else if (EXIT_CONDITION_TAG.equals(tag)) {
3294db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    rt.exitCondition = readConditionXml(parser);
3304db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    if (rt.exitCondition != null) {
3314db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                        rt.exitConditionComponent =
3324db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                                safeComponentName(parser, EXIT_CONDITION_ATT_COMPONENT);
3334db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    }
334056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                }
335056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            }
336056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
3373b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        throw new IllegalStateException("Failed to reach END_DOCUMENT");
338056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
339056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
340056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public void writeXml(XmlSerializer out) throws IOException {
341056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.startTag(null, ZEN_TAG);
342056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, ZEN_ATT_VERSION, Integer.toString(XML_VERSION));
343056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
344056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.startTag(null, ALLOW_TAG);
345056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls));
346056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages));
3472dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock        out.attribute(null, ALLOW_ATT_EVENTS, Boolean.toString(allowEvents));
34899f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        out.attribute(null, ALLOW_ATT_FROM, Integer.toString(allowFrom));
349056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.endTag(null, ALLOW_TAG);
350056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
351056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.startTag(null, SLEEP_TAG);
352056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (sleepMode != null) {
353056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            out.attribute(null, SLEEP_ATT_MODE, sleepMode);
354056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
355cc30c8d729c9d846012ac6dd03c1e51262936e8fJohn Spurlock        out.attribute(null, SLEEP_ATT_NONE, Boolean.toString(sleepNone));
356056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, SLEEP_ATT_START_HR, Integer.toString(sleepStartHour));
357056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, SLEEP_ATT_START_MIN, Integer.toString(sleepStartMinute));
358056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, SLEEP_ATT_END_HR, Integer.toString(sleepEndHour));
359056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, SLEEP_ATT_END_MIN, Integer.toString(sleepEndMinute));
360056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.endTag(null, SLEEP_TAG);
361056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
3623b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (conditionComponents != null && conditionIds != null
3633b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                && conditionComponents.length == conditionIds.length) {
3643b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            for (int i = 0; i < conditionComponents.length; i++) {
3653b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                out.startTag(null, CONDITION_TAG);
3663b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                out.attribute(null, CONDITION_ATT_COMPONENT,
3673b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                        conditionComponents[i].flattenToString());
3683b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                out.attribute(null, CONDITION_ATT_ID, conditionIds[i].toString());
3693b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                out.endTag(null, CONDITION_TAG);
3703b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            }
3713b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
3724db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (exitCondition != null && exitConditionComponent != null) {
373856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            out.startTag(null, EXIT_CONDITION_TAG);
37450806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock            out.attribute(null, EXIT_CONDITION_ATT_COMPONENT,
37550806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock                    exitConditionComponent.flattenToString());
3764db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            writeConditionXml(exitCondition, out);
377856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            out.endTag(null, EXIT_CONDITION_TAG);
378856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        }
379056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.endTag(null, ZEN_TAG);
380056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
381056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
3824db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static Condition readConditionXml(XmlPullParser parser) {
3834db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final Uri id = safeUri(parser, CONDITION_ATT_ID);
3844db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final String summary = parser.getAttributeValue(null, CONDITION_ATT_SUMMARY);
3854db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final String line1 = parser.getAttributeValue(null, CONDITION_ATT_LINE1);
3864db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final String line2 = parser.getAttributeValue(null, CONDITION_ATT_LINE2);
3874db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int icon = safeInt(parser, CONDITION_ATT_ICON, -1);
3884db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int state = safeInt(parser, CONDITION_ATT_STATE, -1);
3894db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int flags = safeInt(parser, CONDITION_ATT_FLAGS, -1);
3904db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        try {
3914db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return new Condition(id, summary, line1, line2, icon, state, flags);
3924db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        } catch (IllegalArgumentException e) {
3934db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            Slog.w(TAG, "Unable to read condition xml", e);
3944db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return null;
3954db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        }
3964db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
3974db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
3984db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static void writeConditionXml(Condition c, XmlSerializer out) throws IOException {
3994db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_ID, c.id.toString());
4004db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_SUMMARY, c.summary);
4014db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_LINE1, c.line1);
4024db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_LINE2, c.line2);
4034db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_ICON, Integer.toString(c.icon));
4044db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_STATE, Integer.toString(c.state));
4054db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_FLAGS, Integer.toString(c.flags));
4064db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
4074db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
408056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static boolean isValidHour(int val) {
409056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return val >= 0 && val < 24;
410056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
411056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
412056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static boolean isValidMinute(int val) {
413056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return val >= 0 && val < 60;
414056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
415056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
416056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) {
417056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final String val = parser.getAttributeValue(null, att);
418056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (TextUtils.isEmpty(val)) return defValue;
419056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return Boolean.valueOf(val);
420056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
421056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
422056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static int safeInt(XmlPullParser parser, String att, int defValue) {
423056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final String val = parser.getAttributeValue(null, att);
424ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        return tryParseInt(val, defValue);
425056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
426056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
4273b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static ComponentName safeComponentName(XmlPullParser parser, String att) {
4283b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final String val = parser.getAttributeValue(null, att);
4293b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (TextUtils.isEmpty(val)) return null;
4303b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        return ComponentName.unflattenFromString(val);
4313b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    }
4323b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock
4333b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static Uri safeUri(XmlPullParser parser, String att) {
4343b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final String val = parser.getAttributeValue(null, att);
4353b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (TextUtils.isEmpty(val)) return null;
4363b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        return Uri.parse(val);
4373b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    }
4383b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock
439056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
440056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public int describeContents() {
441056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return 0;
442056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
443056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
444056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public ZenModeConfig copy() {
445056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final Parcel parcel = Parcel.obtain();
446056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        try {
447056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            writeToParcel(parcel, 0);
448056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            parcel.setDataPosition(0);
449056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            return new ZenModeConfig(parcel);
450056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        } finally {
451056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            parcel.recycle();
452056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
453056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
454056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
455056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static final Parcelable.Creator<ZenModeConfig> CREATOR
456056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            = new Parcelable.Creator<ZenModeConfig>() {
457056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        @Override
458056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        public ZenModeConfig createFromParcel(Parcel source) {
459056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            return new ZenModeConfig(source);
460056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
461056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
462056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        @Override
463056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        public ZenModeConfig[] newArray(int size) {
464056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            return new ZenModeConfig[size];
465056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
466056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    };
467856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
4684db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public DowntimeInfo toDowntimeInfo() {
4694db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final DowntimeInfo downtime = new DowntimeInfo();
4704db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.startHour = sleepStartHour;
4714db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.startMinute = sleepStartMinute;
4724db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.endHour = sleepEndHour;
4734db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.endMinute = sleepEndMinute;
474530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock        downtime.mode = sleepMode;
475530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock        downtime.none = sleepNone;
4764db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return downtime;
4774db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
4784db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
4799c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek    public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle) {
4804dd81467e33a694138da6916fc68ca79501a9429Jason Monk        final long now = System.currentTimeMillis();
4814dd81467e33a694138da6916fc68ca79501a9429Jason Monk        final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS;
4829c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek        return toTimeCondition(context, now + millis, minutesFromNow, now, userHandle);
4834dd81467e33a694138da6916fc68ca79501a9429Jason Monk    }
4844dd81467e33a694138da6916fc68ca79501a9429Jason Monk
4859c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek    public static Condition toTimeCondition(Context context, long time, int minutes, long now,
4869c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek            int userHandle) {
487c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        final int num, summaryResId, line1ResId;
488c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        if (minutes < 60) {
489c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock            // display as minutes
490c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock            num = minutes;
491c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock            summaryResId = R.plurals.zen_mode_duration_minutes_summary;
492c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock            line1ResId = R.plurals.zen_mode_duration_minutes;
493c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        } else {
494c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock            // display as hours
495c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock            num =  Math.round(minutes / 60f);
496c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock            summaryResId = com.android.internal.R.plurals.zen_mode_duration_hours_summary;
497c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock            line1ResId = com.android.internal.R.plurals.zen_mode_duration_hours;
498c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        }
4999c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek        final String skeleton = DateFormat.is24HourFormat(context, userHandle) ? "Hm" : "hma";
500c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        final String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
501c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        final CharSequence formattedTime = DateFormat.format(pattern, time);
502c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        final Resources res = context.getResources();
503c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        final String summary = res.getQuantityString(summaryResId, num, num, formattedTime);
504c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        final String line1 = res.getQuantityString(line1ResId, num, num, formattedTime);
505c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        final String line2 = res.getString(R.string.zen_mode_until, formattedTime);
5064dd81467e33a694138da6916fc68ca79501a9429Jason Monk        final Uri id = toCountdownConditionId(time);
507c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock        return new Condition(id, summary, line1, line2, 0, Condition.STATE_TRUE,
5084dd81467e33a694138da6916fc68ca79501a9429Jason Monk                Condition.FLAG_RELEVANT_NOW);
5094dd81467e33a694138da6916fc68ca79501a9429Jason Monk    }
5104dd81467e33a694138da6916fc68ca79501a9429Jason Monk
5114db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    // For built-in conditions
51237bc92cc2332eb6f864977381135c19d6a081a92John Spurlock    public static final String SYSTEM_AUTHORITY = "android";
513856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
5144db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    // Built-in countdown conditions, e.g. condition://android/countdown/1399917958951
515530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock    public static final String COUNTDOWN_PATH = "countdown";
516856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
517856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    public static Uri toCountdownConditionId(long time) {
518856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        return new Uri.Builder().scheme(Condition.SCHEME)
5194db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .authority(SYSTEM_AUTHORITY)
520856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                .appendPath(COUNTDOWN_PATH)
521856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                .appendPath(Long.toString(time))
522856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                .build();
523856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    }
524856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
525856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    public static long tryParseCountdownConditionId(Uri conditionId) {
5264db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)) return 0;
527856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        if (conditionId.getPathSegments().size() != 2
528856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                || !COUNTDOWN_PATH.equals(conditionId.getPathSegments().get(0))) return 0;
529856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        try {
530856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            return Long.parseLong(conditionId.getPathSegments().get(1));
531856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        } catch (RuntimeException e) {
532856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            Slog.w(TAG, "Error parsing countdown condition: " + conditionId, e);
533856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            return 0;
534856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        }
535856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    }
536856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
537856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    public static boolean isValidCountdownConditionId(Uri conditionId) {
538856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        return tryParseCountdownConditionId(conditionId) != 0;
539856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    }
5404db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
541530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock    // Built-in downtime conditions
542530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock    // e.g. condition://android/downtime?start=10.00&end=7.00&mode=days%3A5%2C6&none=false
543530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock    public static final String DOWNTIME_PATH = "downtime";
5444db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5454db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static Uri toDowntimeConditionId(DowntimeInfo downtime) {
5464db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return new Uri.Builder().scheme(Condition.SCHEME)
5474db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .authority(SYSTEM_AUTHORITY)
5484db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .appendPath(DOWNTIME_PATH)
5494db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .appendQueryParameter("start", downtime.startHour + "." + downtime.startMinute)
5504db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .appendQueryParameter("end", downtime.endHour + "." + downtime.endMinute)
551530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock                .appendQueryParameter("mode", downtime.mode)
552530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock                .appendQueryParameter("none", Boolean.toString(downtime.none))
5534db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .build();
5544db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
5554db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5564db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static DowntimeInfo tryParseDowntimeConditionId(Uri conditionId) {
5574db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)
5584db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                || conditionId.getPathSegments().size() != 1
5594db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                || !DOWNTIME_PATH.equals(conditionId.getPathSegments().get(0))) {
5604db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return null;
5614db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        }
5624db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int[] start = tryParseHourAndMinute(conditionId.getQueryParameter("start"));
5634db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int[] end = tryParseHourAndMinute(conditionId.getQueryParameter("end"));
5644db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (start == null || end == null) return null;
5654db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final DowntimeInfo downtime = new DowntimeInfo();
5664db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.startHour = start[0];
5674db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.startMinute = start[1];
5684db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.endHour = end[0];
5694db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.endMinute = end[1];
570530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock        downtime.mode = conditionId.getQueryParameter("mode");
571530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock        downtime.none = Boolean.toString(true).equals(conditionId.getQueryParameter("none"));
5724db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return downtime;
5734db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
5744db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5754db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static int[] tryParseHourAndMinute(String value) {
5764db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (TextUtils.isEmpty(value)) return null;
5774db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int i = value.indexOf('.');
5784db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (i < 1 || i >= value.length() - 1) return null;
5794db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int hour = tryParseInt(value.substring(0, i), -1);
5804db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int minute = tryParseInt(value.substring(i + 1), -1);
5814db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return isValidHour(hour) && isValidMinute(minute) ? new int[] { hour, minute } : null;
5824db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
5834db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5844db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static boolean isValidDowntimeConditionId(Uri conditionId) {
5854db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return tryParseDowntimeConditionId(conditionId) != null;
5864db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
5874db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5884db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static class DowntimeInfo {
5894db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int startHour;   // 0-23
5904db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int startMinute; // 0-59
5914db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int endHour;
5924db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int endMinute;
593530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock        public String mode;
594530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock        public boolean none;
5954db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5964db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        @Override
5974db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int hashCode() {
5984db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return 0;
5994db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        }
6004db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
6014db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        @Override
6024db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public boolean equals(Object o) {
6034db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            if (!(o instanceof DowntimeInfo)) return false;
6044db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            final DowntimeInfo other = (DowntimeInfo) o;
6054db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return startHour == other.startHour
6064db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    && startMinute == other.startMinute
6074db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    && endHour == other.endHour
608530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock                    && endMinute == other.endMinute
609530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock                    && Objects.equals(mode, other.mode)
610530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock                    && none == other.none;
6114db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        }
6124db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
613530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock
614530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock    // built-in next alarm conditions
615530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock    public static final String NEXT_ALARM_PATH = "next_alarm";
616056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock}
617