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;
204dd81467e33a694138da6916fc68ca79501a9429Jason Monkimport android.content.res.Resources;
213b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlockimport android.net.Uri;
22056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport android.os.Parcel;
23056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport android.os.Parcelable;
24056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport android.text.TextUtils;
25856edebad73560e9b1cce021a7de9a0470d07176John Spurlockimport android.util.Slog;
26056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
27056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport org.xmlpull.v1.XmlPullParser;
28056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport org.xmlpull.v1.XmlPullParserException;
29056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport org.xmlpull.v1.XmlSerializer;
30056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
31056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport java.io.IOException;
323b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlockimport java.util.ArrayList;
333b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlockimport java.util.Arrays;
34ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlockimport java.util.Calendar;
35056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockimport java.util.Objects;
36056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
37056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock/**
38056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * Persisted configuration for zen mode.
39056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock *
40056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock * @hide
41056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock */
42056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlockpublic class ZenModeConfig implements Parcelable {
43856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    private static String TAG = "ZenModeConfig";
44056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
45056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static final String SLEEP_MODE_NIGHTS = "nights";
46056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static final String SLEEP_MODE_WEEKNIGHTS = "weeknights";
47ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static final String SLEEP_MODE_DAYS_PREFIX = "days:";
48056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
4999f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static final int SOURCE_ANYONE = 0;
5099f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static final int SOURCE_CONTACT = 1;
5199f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static final int SOURCE_STAR = 2;
5299f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static final int MAX_SOURCE = SOURCE_STAR;
5399f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren
54ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static final int[] ALL_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
55ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY };
56ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static final int[] WEEKNIGHT_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
57ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            Calendar.WEDNESDAY, Calendar.THURSDAY };
58ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock
594dd81467e33a694138da6916fc68ca79501a9429Jason Monk    public static final int[] MINUTE_BUCKETS = new int[] { 15, 30, 45, 60, 120, 180, 240, 480 };
604dd81467e33a694138da6916fc68ca79501a9429Jason Monk    private static final int SECONDS_MS = 1000;
614dd81467e33a694138da6916fc68ca79501a9429Jason Monk    private static final int MINUTES_MS = 60 * SECONDS_MS;
624dd81467e33a694138da6916fc68ca79501a9429Jason Monk    private static final int ZERO_VALUE_MS = 20 * SECONDS_MS;
634dd81467e33a694138da6916fc68ca79501a9429Jason Monk
642dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock    private static final boolean DEFAULT_ALLOW_EVENTS = true;
652dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock
66056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final int XML_VERSION = 1;
67056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ZEN_TAG = "zen";
68056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ZEN_ATT_VERSION = "version";
69056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ALLOW_TAG = "allow";
70056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ALLOW_ATT_CALLS = "calls";
71056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String ALLOW_ATT_MESSAGES = "messages";
7299f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    private static final String ALLOW_ATT_FROM = "from";
732dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock    private static final String ALLOW_ATT_EVENTS = "events";
74056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_TAG = "sleep";
75056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_MODE = "mode";
76056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
77056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_START_HR = "startHour";
78056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_START_MIN = "startMin";
79056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_END_HR = "endHour";
80056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static final String SLEEP_ATT_END_MIN = "endMin";
81056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
823b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static final String CONDITION_TAG = "condition";
833b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static final String CONDITION_ATT_COMPONENT = "component";
843b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static final String CONDITION_ATT_ID = "id";
854db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_SUMMARY = "summary";
864db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_LINE1 = "line1";
874db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_LINE2 = "line2";
884db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_ICON = "icon";
894db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_STATE = "state";
904db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String CONDITION_ATT_FLAGS = "flags";
913b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock
92856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    private static final String EXIT_CONDITION_TAG = "exitCondition";
9350806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock    private static final String EXIT_CONDITION_ATT_COMPONENT = "component";
94856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
95056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public boolean allowCalls;
96056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public boolean allowMessages;
972dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock    public boolean allowEvents = DEFAULT_ALLOW_EVENTS;
9899f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public int allowFrom = SOURCE_ANYONE;
99056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
100056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public String sleepMode;
1014db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public int sleepStartHour;   // 0-23
1024db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public int sleepStartMinute; // 0-59
103056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public int sleepEndHour;
104056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public int sleepEndMinute;
1053b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    public ComponentName[] conditionComponents;
1063b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    public Uri[] conditionIds;
1074db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public Condition exitCondition;
10850806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock    public ComponentName exitConditionComponent;
109056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
110056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public ZenModeConfig() { }
111056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
112056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public ZenModeConfig(Parcel source) {
113056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        allowCalls = source.readInt() == 1;
114056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        allowMessages = source.readInt() == 1;
1152dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock        allowEvents = source.readInt() == 1;
116056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (source.readInt() == 1) {
117056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            sleepMode = source.readString();
118056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
119056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        sleepStartHour = source.readInt();
120056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        sleepStartMinute = source.readInt();
121056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        sleepEndHour = source.readInt();
122056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        sleepEndMinute = source.readInt();
1233b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        int len = source.readInt();
1243b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (len > 0) {
1253b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            conditionComponents = new ComponentName[len];
1263b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            source.readTypedArray(conditionComponents, ComponentName.CREATOR);
1273b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
1283b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        len = source.readInt();
1293b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (len > 0) {
1303b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            conditionIds = new Uri[len];
1313b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            source.readTypedArray(conditionIds, Uri.CREATOR);
1323b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
13399f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        allowFrom = source.readInt();
1344db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        exitCondition = source.readParcelable(null);
13550806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock        exitConditionComponent = source.readParcelable(null);
136056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
137056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
138056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
139056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public void writeToParcel(Parcel dest, int flags) {
140056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(allowCalls ? 1 : 0);
141056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(allowMessages ? 1 : 0);
1422dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock        dest.writeInt(allowEvents ? 1 : 0);
143056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (sleepMode != null) {
144056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            dest.writeInt(1);
145056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            dest.writeString(sleepMode);
146056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        } else {
147056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            dest.writeInt(0);
148056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
149056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(sleepStartHour);
150056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(sleepStartMinute);
151056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(sleepEndHour);
152056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        dest.writeInt(sleepEndMinute);
1533b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (conditionComponents != null && conditionComponents.length > 0) {
1543b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeInt(conditionComponents.length);
1553b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeTypedArray(conditionComponents, 0);
1563b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        } else {
1573b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeInt(0);
1583b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
1593b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (conditionIds != null && conditionIds.length > 0) {
1603b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeInt(conditionIds.length);
1613b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeTypedArray(conditionIds, 0);
1623b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        } else {
1633b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            dest.writeInt(0);
1643b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
16599f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        dest.writeInt(allowFrom);
1664db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        dest.writeParcelable(exitCondition, 0);
16750806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock        dest.writeParcelable(exitConditionComponent, 0);
168056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
169056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
170056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
171056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public String toString() {
172056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[')
173056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append("allowCalls=").append(allowCalls)
174056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(",allowMessages=").append(allowMessages)
17599f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            .append(",allowFrom=").append(sourceToString(allowFrom))
1762dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock            .append(",allowEvents=").append(allowEvents)
177056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(",sleepMode=").append(sleepMode)
178056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(",sleepStart=").append(sleepStartHour).append('.').append(sleepStartMinute)
179056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(",sleepEnd=").append(sleepEndHour).append('.').append(sleepEndMinute)
1803b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            .append(",conditionComponents=")
1813b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            .append(conditionComponents == null ? null : TextUtils.join(",", conditionComponents))
1823b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            .append(",conditionIds=")
1833b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            .append(conditionIds == null ? null : TextUtils.join(",", conditionIds))
1844db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            .append(",exitCondition=").append(exitCondition)
18550806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock            .append(",exitConditionComponent=").append(exitConditionComponent)
186056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            .append(']').toString();
187056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
188056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
18999f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    public static String sourceToString(int source) {
19099f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        switch (source) {
19199f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            case SOURCE_ANYONE:
19299f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                return "anyone";
19399f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            case SOURCE_CONTACT:
19499f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                return "contacts";
19599f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            case SOURCE_STAR:
19699f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                return "stars";
19799f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren            default:
19899f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                return "UNKNOWN";
19999f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        }
20099f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren    }
20199f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren
202056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
203056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public boolean equals(Object o) {
204056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (!(o instanceof ZenModeConfig)) return false;
205056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (o == this) return true;
206056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final ZenModeConfig other = (ZenModeConfig) o;
207056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return other.allowCalls == allowCalls
208056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && other.allowMessages == allowMessages
20999f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                && other.allowFrom == allowFrom
2102dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock                && other.allowEvents == allowEvents
211056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && Objects.equals(other.sleepMode, sleepMode)
212056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && other.sleepStartHour == sleepStartHour
213056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && other.sleepStartMinute == sleepStartMinute
214056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && other.sleepEndHour == sleepEndHour
2153b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                && other.sleepEndMinute == sleepEndMinute
2163b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                && Objects.deepEquals(other.conditionComponents, conditionComponents)
217856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                && Objects.deepEquals(other.conditionIds, conditionIds)
2184db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                && Objects.equals(other.exitCondition, exitCondition)
21950806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock                && Objects.equals(other.exitConditionComponent, exitConditionComponent);
220056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
221056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
222056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
223056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public int hashCode() {
2242dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock        return Objects.hash(allowCalls, allowMessages, allowFrom, allowEvents, sleepMode,
22599f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                sleepStartHour, sleepStartMinute, sleepEndHour, sleepEndMinute,
226856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                Arrays.hashCode(conditionComponents), Arrays.hashCode(conditionIds),
2274db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                exitCondition, exitConditionComponent);
228056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
229056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
230056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public boolean isValid() {
231056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return isValidHour(sleepStartHour) && isValidMinute(sleepStartMinute)
232056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                && isValidHour(sleepEndHour) && isValidMinute(sleepEndMinute)
233ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock                && isValidSleepMode(sleepMode);
234ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    }
235ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock
236ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static boolean isValidSleepMode(String sleepMode) {
237ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        return sleepMode == null || sleepMode.equals(SLEEP_MODE_NIGHTS)
238ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock                || sleepMode.equals(SLEEP_MODE_WEEKNIGHTS) || tryParseDays(sleepMode) != null;
239ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    }
240ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock
241ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    public static int[] tryParseDays(String sleepMode) {
242ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (sleepMode == null) return null;
243ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        sleepMode = sleepMode.trim();
244ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (SLEEP_MODE_NIGHTS.equals(sleepMode)) return ALL_DAYS;
245ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (SLEEP_MODE_WEEKNIGHTS.equals(sleepMode)) return WEEKNIGHT_DAYS;
246ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (!sleepMode.startsWith(SLEEP_MODE_DAYS_PREFIX)) return null;
247ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (sleepMode.equals(SLEEP_MODE_DAYS_PREFIX)) return null;
248ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        final String[] tokens = sleepMode.substring(SLEEP_MODE_DAYS_PREFIX.length()).split(",");
249ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (tokens.length == 0) return null;
250ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        final int[] rt = new int[tokens.length];
251ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        for (int i = 0; i < tokens.length; i++) {
252ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            final int day = tryParseInt(tokens[i], -1);
253ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            if (day == -1) return null;
254ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            rt[i] = day;
255ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        }
256ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        return rt;
257ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    }
258ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock
259ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock    private static int tryParseInt(String value, int defValue) {
260ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        if (TextUtils.isEmpty(value)) return defValue;
261ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        try {
262ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            return Integer.valueOf(value);
263ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        } catch (NumberFormatException e) {
264ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock            return defValue;
265ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        }
266056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
267056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
268056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static ZenModeConfig readXml(XmlPullParser parser)
269056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            throws XmlPullParserException, IOException {
270056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        int type = parser.getEventType();
271056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (type != XmlPullParser.START_TAG) return null;
272056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        String tag = parser.getName();
273056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (!ZEN_TAG.equals(tag)) return null;
274056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final ZenModeConfig rt = new ZenModeConfig();
275ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        final int version = safeInt(parser, ZEN_ATT_VERSION, XML_VERSION);
2763b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final ArrayList<ComponentName> conditionComponents = new ArrayList<ComponentName>();
2773b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final ArrayList<Uri> conditionIds = new ArrayList<Uri>();
278056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
279056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            tag = parser.getName();
2803b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) {
2813b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                if (!conditionComponents.isEmpty()) {
2823b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    rt.conditionComponents = conditionComponents
2833b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                            .toArray(new ComponentName[conditionComponents.size()]);
2843b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    rt.conditionIds = conditionIds.toArray(new Uri[conditionIds.size()]);
2853b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                }
2863b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                return rt;
2873b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            }
288056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            if (type == XmlPullParser.START_TAG) {
289056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                if (ALLOW_TAG.equals(tag)) {
290056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false);
291056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false);
2922dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock                    rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS, DEFAULT_ALLOW_EVENTS);
29399f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                    rt.allowFrom = safeInt(parser, ALLOW_ATT_FROM, SOURCE_ANYONE);
29499f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                    if (rt.allowFrom < SOURCE_ANYONE || rt.allowFrom > MAX_SOURCE) {
29599f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                        throw new IndexOutOfBoundsException("bad source in config:" + rt.allowFrom);
29699f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren                    }
297056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                } else if (SLEEP_TAG.equals(tag)) {
298056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final String mode = parser.getAttributeValue(null, SLEEP_ATT_MODE);
299ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock                    rt.sleepMode = isValidSleepMode(mode)? mode : null;
300056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final int startHour = safeInt(parser, SLEEP_ATT_START_HR, 0);
301056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final int startMinute = safeInt(parser, SLEEP_ATT_START_MIN, 0);
302056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final int endHour = safeInt(parser, SLEEP_ATT_END_HR, 0);
303056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    final int endMinute = safeInt(parser, SLEEP_ATT_END_MIN, 0);
304056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.sleepStartHour = isValidHour(startHour) ? startHour : 0;
305056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.sleepStartMinute = isValidMinute(startMinute) ? startMinute : 0;
306056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.sleepEndHour = isValidHour(endHour) ? endHour : 0;
307056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                    rt.sleepEndMinute = isValidMinute(endMinute) ? endMinute : 0;
3083b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                } else if (CONDITION_TAG.equals(tag)) {
3093b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    final ComponentName component =
3103b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                            safeComponentName(parser, CONDITION_ATT_COMPONENT);
3113b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    final Uri conditionId = safeUri(parser, CONDITION_ATT_ID);
3123b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    if (component != null && conditionId != null) {
3133b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                        conditionComponents.add(component);
3143b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                        conditionIds.add(conditionId);
3153b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                    }
316856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                } else if (EXIT_CONDITION_TAG.equals(tag)) {
3174db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    rt.exitCondition = readConditionXml(parser);
3184db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    if (rt.exitCondition != null) {
3194db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                        rt.exitConditionComponent =
3204db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                                safeComponentName(parser, EXIT_CONDITION_ATT_COMPONENT);
3214db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    }
322056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock                }
323056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            }
324056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
3253b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        throw new IllegalStateException("Failed to reach END_DOCUMENT");
326056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
327056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
328056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public void writeXml(XmlSerializer out) throws IOException {
329056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.startTag(null, ZEN_TAG);
330056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, ZEN_ATT_VERSION, Integer.toString(XML_VERSION));
331056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
332056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.startTag(null, ALLOW_TAG);
333056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls));
334056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages));
3352dac62c2e985ce1848b0fff751d4ed2cc3885c0cJohn Spurlock        out.attribute(null, ALLOW_ATT_EVENTS, Boolean.toString(allowEvents));
33699f963ea04d7a86219ece00c356f3f6bce33b6d6Chris Wren        out.attribute(null, ALLOW_ATT_FROM, Integer.toString(allowFrom));
337056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.endTag(null, ALLOW_TAG);
338056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
339056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.startTag(null, SLEEP_TAG);
340056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (sleepMode != null) {
341056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            out.attribute(null, SLEEP_ATT_MODE, sleepMode);
342056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
343056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, SLEEP_ATT_START_HR, Integer.toString(sleepStartHour));
344056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, SLEEP_ATT_START_MIN, Integer.toString(sleepStartMinute));
345056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, SLEEP_ATT_END_HR, Integer.toString(sleepEndHour));
346056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.attribute(null, SLEEP_ATT_END_MIN, Integer.toString(sleepEndMinute));
347056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.endTag(null, SLEEP_TAG);
348056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
3493b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (conditionComponents != null && conditionIds != null
3503b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                && conditionComponents.length == conditionIds.length) {
3513b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            for (int i = 0; i < conditionComponents.length; i++) {
3523b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                out.startTag(null, CONDITION_TAG);
3533b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                out.attribute(null, CONDITION_ATT_COMPONENT,
3543b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                        conditionComponents[i].flattenToString());
3553b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                out.attribute(null, CONDITION_ATT_ID, conditionIds[i].toString());
3563b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock                out.endTag(null, CONDITION_TAG);
3573b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock            }
3583b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        }
3594db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (exitCondition != null && exitConditionComponent != null) {
360856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            out.startTag(null, EXIT_CONDITION_TAG);
36150806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock            out.attribute(null, EXIT_CONDITION_ATT_COMPONENT,
36250806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock                    exitConditionComponent.flattenToString());
3634db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            writeConditionXml(exitCondition, out);
364856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            out.endTag(null, EXIT_CONDITION_TAG);
365856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        }
366056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        out.endTag(null, ZEN_TAG);
367056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
368056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
3694db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static Condition readConditionXml(XmlPullParser parser) {
3704db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final Uri id = safeUri(parser, CONDITION_ATT_ID);
3714db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final String summary = parser.getAttributeValue(null, CONDITION_ATT_SUMMARY);
3724db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final String line1 = parser.getAttributeValue(null, CONDITION_ATT_LINE1);
3734db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final String line2 = parser.getAttributeValue(null, CONDITION_ATT_LINE2);
3744db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int icon = safeInt(parser, CONDITION_ATT_ICON, -1);
3754db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int state = safeInt(parser, CONDITION_ATT_STATE, -1);
3764db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int flags = safeInt(parser, CONDITION_ATT_FLAGS, -1);
3774db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        try {
3784db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return new Condition(id, summary, line1, line2, icon, state, flags);
3794db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        } catch (IllegalArgumentException e) {
3804db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            Slog.w(TAG, "Unable to read condition xml", e);
3814db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return null;
3824db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        }
3834db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
3844db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
3854db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static void writeConditionXml(Condition c, XmlSerializer out) throws IOException {
3864db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_ID, c.id.toString());
3874db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_SUMMARY, c.summary);
3884db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_LINE1, c.line1);
3894db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_LINE2, c.line2);
3904db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_ICON, Integer.toString(c.icon));
3914db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_STATE, Integer.toString(c.state));
3924db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        out.attribute(null, CONDITION_ATT_FLAGS, Integer.toString(c.flags));
3934db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
3944db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
395056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static boolean isValidHour(int val) {
396056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return val >= 0 && val < 24;
397056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
398056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
399056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static boolean isValidMinute(int val) {
400056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return val >= 0 && val < 60;
401056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
402056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
403056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) {
404056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final String val = parser.getAttributeValue(null, att);
405056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        if (TextUtils.isEmpty(val)) return defValue;
406056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return Boolean.valueOf(val);
407056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
408056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
409056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    private static int safeInt(XmlPullParser parser, String att, int defValue) {
410056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final String val = parser.getAttributeValue(null, att);
411ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock        return tryParseInt(val, defValue);
412056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
413056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
4143b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static ComponentName safeComponentName(XmlPullParser parser, String att) {
4153b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final String val = parser.getAttributeValue(null, att);
4163b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (TextUtils.isEmpty(val)) return null;
4173b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        return ComponentName.unflattenFromString(val);
4183b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    }
4193b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock
4203b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    private static Uri safeUri(XmlPullParser parser, String att) {
4213b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final String val = parser.getAttributeValue(null, att);
4223b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (TextUtils.isEmpty(val)) return null;
4233b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        return Uri.parse(val);
4243b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    }
4253b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock
426056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    @Override
427056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public int describeContents() {
428056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        return 0;
429056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
430056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
431056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public ZenModeConfig copy() {
432056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        final Parcel parcel = Parcel.obtain();
433056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        try {
434056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            writeToParcel(parcel, 0);
435056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            parcel.setDataPosition(0);
436056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            return new ZenModeConfig(parcel);
437056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        } finally {
438056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            parcel.recycle();
439056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
440056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    }
441056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
442056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    public static final Parcelable.Creator<ZenModeConfig> CREATOR
443056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            = new Parcelable.Creator<ZenModeConfig>() {
444056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        @Override
445056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        public ZenModeConfig createFromParcel(Parcel source) {
446056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            return new ZenModeConfig(source);
447056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
448056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock
449056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        @Override
450056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        public ZenModeConfig[] newArray(int size) {
451056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock            return new ZenModeConfig[size];
452056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock        }
453056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock    };
454856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
4554db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public DowntimeInfo toDowntimeInfo() {
4564db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final DowntimeInfo downtime = new DowntimeInfo();
4574db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.startHour = sleepStartHour;
4584db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.startMinute = sleepStartMinute;
4594db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.endHour = sleepEndHour;
4604db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.endMinute = sleepEndMinute;
4614db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return downtime;
4624db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
4634db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
4644dd81467e33a694138da6916fc68ca79501a9429Jason Monk    public static Condition toTimeCondition(int minutesFromNow) {
4654dd81467e33a694138da6916fc68ca79501a9429Jason Monk        final long now = System.currentTimeMillis();
4664dd81467e33a694138da6916fc68ca79501a9429Jason Monk        final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS;
4674dd81467e33a694138da6916fc68ca79501a9429Jason Monk        return toTimeCondition(now + millis, minutesFromNow);
4684dd81467e33a694138da6916fc68ca79501a9429Jason Monk    }
4694dd81467e33a694138da6916fc68ca79501a9429Jason Monk
4704dd81467e33a694138da6916fc68ca79501a9429Jason Monk    public static Condition toTimeCondition(long time, int minutes) {
4714dd81467e33a694138da6916fc68ca79501a9429Jason Monk        final int num = minutes < 60 ? minutes : Math.round(minutes / 60f);
4724dd81467e33a694138da6916fc68ca79501a9429Jason Monk        final int resId = minutes < 60
4734dd81467e33a694138da6916fc68ca79501a9429Jason Monk                ? com.android.internal.R.plurals.zen_mode_duration_minutes
4744dd81467e33a694138da6916fc68ca79501a9429Jason Monk                : com.android.internal.R.plurals.zen_mode_duration_hours;
4754dd81467e33a694138da6916fc68ca79501a9429Jason Monk        final String caption = Resources.getSystem().getQuantityString(resId, num, num);
4764dd81467e33a694138da6916fc68ca79501a9429Jason Monk        final Uri id = toCountdownConditionId(time);
4774dd81467e33a694138da6916fc68ca79501a9429Jason Monk        return new Condition(id, caption, "", "", 0, Condition.STATE_TRUE,
4784dd81467e33a694138da6916fc68ca79501a9429Jason Monk                Condition.FLAG_RELEVANT_NOW);
4794dd81467e33a694138da6916fc68ca79501a9429Jason Monk    }
4804dd81467e33a694138da6916fc68ca79501a9429Jason Monk
4814db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    // For built-in conditions
4824db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String SYSTEM_AUTHORITY = "android";
483856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
4844db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    // Built-in countdown conditions, e.g. condition://android/countdown/1399917958951
485856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    private static final String COUNTDOWN_PATH = "countdown";
486856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
487856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    public static Uri toCountdownConditionId(long time) {
488856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        return new Uri.Builder().scheme(Condition.SCHEME)
4894db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .authority(SYSTEM_AUTHORITY)
490856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                .appendPath(COUNTDOWN_PATH)
491856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                .appendPath(Long.toString(time))
492856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                .build();
493856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    }
494856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
495856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    public static long tryParseCountdownConditionId(Uri conditionId) {
4964db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)) return 0;
497856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        if (conditionId.getPathSegments().size() != 2
498856edebad73560e9b1cce021a7de9a0470d07176John Spurlock                || !COUNTDOWN_PATH.equals(conditionId.getPathSegments().get(0))) return 0;
499856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        try {
500856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            return Long.parseLong(conditionId.getPathSegments().get(1));
501856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        } catch (RuntimeException e) {
502856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            Slog.w(TAG, "Error parsing countdown condition: " + conditionId, e);
503856edebad73560e9b1cce021a7de9a0470d07176John Spurlock            return 0;
504856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        }
505856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    }
506856edebad73560e9b1cce021a7de9a0470d07176John Spurlock
507856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    public static boolean isValidCountdownConditionId(Uri conditionId) {
508856edebad73560e9b1cce021a7de9a0470d07176John Spurlock        return tryParseCountdownConditionId(conditionId) != 0;
509856edebad73560e9b1cce021a7de9a0470d07176John Spurlock    }
5104db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5114db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    // Built-in downtime conditions, e.g. condition://android/downtime?start=10.00&end=7.00
5124db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static final String DOWNTIME_PATH = "downtime";
5134db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5144db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static Uri toDowntimeConditionId(DowntimeInfo downtime) {
5154db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return new Uri.Builder().scheme(Condition.SCHEME)
5164db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .authority(SYSTEM_AUTHORITY)
5174db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .appendPath(DOWNTIME_PATH)
5184db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .appendQueryParameter("start", downtime.startHour + "." + downtime.startMinute)
5194db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .appendQueryParameter("end", downtime.endHour + "." + downtime.endMinute)
5204db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                .build();
5214db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
5224db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5234db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static DowntimeInfo tryParseDowntimeConditionId(Uri conditionId) {
5244db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)
5254db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                || conditionId.getPathSegments().size() != 1
5264db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                || !DOWNTIME_PATH.equals(conditionId.getPathSegments().get(0))) {
5274db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return null;
5284db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        }
5294db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int[] start = tryParseHourAndMinute(conditionId.getQueryParameter("start"));
5304db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int[] end = tryParseHourAndMinute(conditionId.getQueryParameter("end"));
5314db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (start == null || end == null) return null;
5324db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final DowntimeInfo downtime = new DowntimeInfo();
5334db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.startHour = start[0];
5344db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.startMinute = start[1];
5354db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.endHour = end[0];
5364db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        downtime.endMinute = end[1];
5374db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return downtime;
5384db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
5394db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5404db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    private static int[] tryParseHourAndMinute(String value) {
5414db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (TextUtils.isEmpty(value)) return null;
5424db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int i = value.indexOf('.');
5434db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        if (i < 1 || i >= value.length() - 1) return null;
5444db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int hour = tryParseInt(value.substring(0, i), -1);
5454db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        final int minute = tryParseInt(value.substring(i + 1), -1);
5464db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return isValidHour(hour) && isValidMinute(minute) ? new int[] { hour, minute } : null;
5474db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
5484db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5494db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static boolean isValidDowntimeConditionId(Uri conditionId) {
5504db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        return tryParseDowntimeConditionId(conditionId) != null;
5514db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
5524db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5534db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    public static class DowntimeInfo {
5544db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int startHour;   // 0-23
5554db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int startMinute; // 0-59
5564db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int endHour;
5574db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int endMinute;
5584db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5594db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        @Override
5604db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public int hashCode() {
5614db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return 0;
5624db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        }
5634db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock
5644db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        @Override
5654db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        public boolean equals(Object o) {
5664db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            if (!(o instanceof DowntimeInfo)) return false;
5674db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            final DowntimeInfo other = (DowntimeInfo) o;
5684db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock            return startHour == other.startHour
5694db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    && startMinute == other.startMinute
5704db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    && endHour == other.endHour
5714db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock                    && endMinute == other.endMinute;
5724db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock        }
5734db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock    }
574056c519df1dfb8fdc57daddfdf09bc0e1ffddac4John Spurlock}
575