ZenModeConfig.java revision 2dac62c2e985ce1848b0fff751d4ed2cc3885c0c
1/**
2 * Copyright (c) 2014, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.service.notification;
18
19import android.content.ComponentName;
20import android.content.res.Resources;
21import android.net.Uri;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.text.TextUtils;
25import android.util.Slog;
26
27import org.xmlpull.v1.XmlPullParser;
28import org.xmlpull.v1.XmlPullParserException;
29import org.xmlpull.v1.XmlSerializer;
30
31import java.io.IOException;
32import java.util.ArrayList;
33import java.util.Arrays;
34import java.util.Calendar;
35import java.util.Objects;
36
37/**
38 * Persisted configuration for zen mode.
39 *
40 * @hide
41 */
42public class ZenModeConfig implements Parcelable {
43    private static String TAG = "ZenModeConfig";
44
45    public static final String SLEEP_MODE_NIGHTS = "nights";
46    public static final String SLEEP_MODE_WEEKNIGHTS = "weeknights";
47    public static final String SLEEP_MODE_DAYS_PREFIX = "days:";
48
49    public static final int SOURCE_ANYONE = 0;
50    public static final int SOURCE_CONTACT = 1;
51    public static final int SOURCE_STAR = 2;
52    public static final int MAX_SOURCE = SOURCE_STAR;
53
54    public static final int[] ALL_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
55            Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY };
56    public static final int[] WEEKNIGHT_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
57            Calendar.WEDNESDAY, Calendar.THURSDAY };
58
59    public static final int[] MINUTE_BUCKETS = new int[] { 15, 30, 45, 60, 120, 180, 240, 480 };
60    private static final int SECONDS_MS = 1000;
61    private static final int MINUTES_MS = 60 * SECONDS_MS;
62    private static final int ZERO_VALUE_MS = 20 * SECONDS_MS;
63
64    private static final boolean DEFAULT_ALLOW_EVENTS = true;
65
66    private static final int XML_VERSION = 1;
67    private static final String ZEN_TAG = "zen";
68    private static final String ZEN_ATT_VERSION = "version";
69    private static final String ALLOW_TAG = "allow";
70    private static final String ALLOW_ATT_CALLS = "calls";
71    private static final String ALLOW_ATT_MESSAGES = "messages";
72    private static final String ALLOW_ATT_FROM = "from";
73    private static final String ALLOW_ATT_EVENTS = "events";
74    private static final String SLEEP_TAG = "sleep";
75    private static final String SLEEP_ATT_MODE = "mode";
76
77    private static final String SLEEP_ATT_START_HR = "startHour";
78    private static final String SLEEP_ATT_START_MIN = "startMin";
79    private static final String SLEEP_ATT_END_HR = "endHour";
80    private static final String SLEEP_ATT_END_MIN = "endMin";
81
82    private static final String CONDITION_TAG = "condition";
83    private static final String CONDITION_ATT_COMPONENT = "component";
84    private static final String CONDITION_ATT_ID = "id";
85    private static final String CONDITION_ATT_SUMMARY = "summary";
86    private static final String CONDITION_ATT_LINE1 = "line1";
87    private static final String CONDITION_ATT_LINE2 = "line2";
88    private static final String CONDITION_ATT_ICON = "icon";
89    private static final String CONDITION_ATT_STATE = "state";
90    private static final String CONDITION_ATT_FLAGS = "flags";
91
92    private static final String EXIT_CONDITION_TAG = "exitCondition";
93    private static final String EXIT_CONDITION_ATT_COMPONENT = "component";
94
95    public boolean allowCalls;
96    public boolean allowMessages;
97    public boolean allowEvents = DEFAULT_ALLOW_EVENTS;
98    public int allowFrom = SOURCE_ANYONE;
99
100    public String sleepMode;
101    public int sleepStartHour;   // 0-23
102    public int sleepStartMinute; // 0-59
103    public int sleepEndHour;
104    public int sleepEndMinute;
105    public ComponentName[] conditionComponents;
106    public Uri[] conditionIds;
107    public Condition exitCondition;
108    public ComponentName exitConditionComponent;
109
110    public ZenModeConfig() { }
111
112    public ZenModeConfig(Parcel source) {
113        allowCalls = source.readInt() == 1;
114        allowMessages = source.readInt() == 1;
115        allowEvents = source.readInt() == 1;
116        if (source.readInt() == 1) {
117            sleepMode = source.readString();
118        }
119        sleepStartHour = source.readInt();
120        sleepStartMinute = source.readInt();
121        sleepEndHour = source.readInt();
122        sleepEndMinute = source.readInt();
123        int len = source.readInt();
124        if (len > 0) {
125            conditionComponents = new ComponentName[len];
126            source.readTypedArray(conditionComponents, ComponentName.CREATOR);
127        }
128        len = source.readInt();
129        if (len > 0) {
130            conditionIds = new Uri[len];
131            source.readTypedArray(conditionIds, Uri.CREATOR);
132        }
133        allowFrom = source.readInt();
134        exitCondition = source.readParcelable(null);
135        exitConditionComponent = source.readParcelable(null);
136    }
137
138    @Override
139    public void writeToParcel(Parcel dest, int flags) {
140        dest.writeInt(allowCalls ? 1 : 0);
141        dest.writeInt(allowMessages ? 1 : 0);
142        dest.writeInt(allowEvents ? 1 : 0);
143        if (sleepMode != null) {
144            dest.writeInt(1);
145            dest.writeString(sleepMode);
146        } else {
147            dest.writeInt(0);
148        }
149        dest.writeInt(sleepStartHour);
150        dest.writeInt(sleepStartMinute);
151        dest.writeInt(sleepEndHour);
152        dest.writeInt(sleepEndMinute);
153        if (conditionComponents != null && conditionComponents.length > 0) {
154            dest.writeInt(conditionComponents.length);
155            dest.writeTypedArray(conditionComponents, 0);
156        } else {
157            dest.writeInt(0);
158        }
159        if (conditionIds != null && conditionIds.length > 0) {
160            dest.writeInt(conditionIds.length);
161            dest.writeTypedArray(conditionIds, 0);
162        } else {
163            dest.writeInt(0);
164        }
165        dest.writeInt(allowFrom);
166        dest.writeParcelable(exitCondition, 0);
167        dest.writeParcelable(exitConditionComponent, 0);
168    }
169
170    @Override
171    public String toString() {
172        return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[')
173            .append("allowCalls=").append(allowCalls)
174            .append(",allowMessages=").append(allowMessages)
175            .append(",allowFrom=").append(sourceToString(allowFrom))
176            .append(",allowEvents=").append(allowEvents)
177            .append(",sleepMode=").append(sleepMode)
178            .append(",sleepStart=").append(sleepStartHour).append('.').append(sleepStartMinute)
179            .append(",sleepEnd=").append(sleepEndHour).append('.').append(sleepEndMinute)
180            .append(",conditionComponents=")
181            .append(conditionComponents == null ? null : TextUtils.join(",", conditionComponents))
182            .append(",conditionIds=")
183            .append(conditionIds == null ? null : TextUtils.join(",", conditionIds))
184            .append(",exitCondition=").append(exitCondition)
185            .append(",exitConditionComponent=").append(exitConditionComponent)
186            .append(']').toString();
187    }
188
189    public static String sourceToString(int source) {
190        switch (source) {
191            case SOURCE_ANYONE:
192                return "anyone";
193            case SOURCE_CONTACT:
194                return "contacts";
195            case SOURCE_STAR:
196                return "stars";
197            default:
198                return "UNKNOWN";
199        }
200    }
201
202    @Override
203    public boolean equals(Object o) {
204        if (!(o instanceof ZenModeConfig)) return false;
205        if (o == this) return true;
206        final ZenModeConfig other = (ZenModeConfig) o;
207        return other.allowCalls == allowCalls
208                && other.allowMessages == allowMessages
209                && other.allowFrom == allowFrom
210                && other.allowEvents == allowEvents
211                && Objects.equals(other.sleepMode, sleepMode)
212                && other.sleepStartHour == sleepStartHour
213                && other.sleepStartMinute == sleepStartMinute
214                && other.sleepEndHour == sleepEndHour
215                && other.sleepEndMinute == sleepEndMinute
216                && Objects.deepEquals(other.conditionComponents, conditionComponents)
217                && Objects.deepEquals(other.conditionIds, conditionIds)
218                && Objects.equals(other.exitCondition, exitCondition)
219                && Objects.equals(other.exitConditionComponent, exitConditionComponent);
220    }
221
222    @Override
223    public int hashCode() {
224        return Objects.hash(allowCalls, allowMessages, allowFrom, allowEvents, sleepMode,
225                sleepStartHour, sleepStartMinute, sleepEndHour, sleepEndMinute,
226                Arrays.hashCode(conditionComponents), Arrays.hashCode(conditionIds),
227                exitCondition, exitConditionComponent);
228    }
229
230    public boolean isValid() {
231        return isValidHour(sleepStartHour) && isValidMinute(sleepStartMinute)
232                && isValidHour(sleepEndHour) && isValidMinute(sleepEndMinute)
233                && isValidSleepMode(sleepMode);
234    }
235
236    public static boolean isValidSleepMode(String sleepMode) {
237        return sleepMode == null || sleepMode.equals(SLEEP_MODE_NIGHTS)
238                || sleepMode.equals(SLEEP_MODE_WEEKNIGHTS) || tryParseDays(sleepMode) != null;
239    }
240
241    public static int[] tryParseDays(String sleepMode) {
242        if (sleepMode == null) return null;
243        sleepMode = sleepMode.trim();
244        if (SLEEP_MODE_NIGHTS.equals(sleepMode)) return ALL_DAYS;
245        if (SLEEP_MODE_WEEKNIGHTS.equals(sleepMode)) return WEEKNIGHT_DAYS;
246        if (!sleepMode.startsWith(SLEEP_MODE_DAYS_PREFIX)) return null;
247        if (sleepMode.equals(SLEEP_MODE_DAYS_PREFIX)) return null;
248        final String[] tokens = sleepMode.substring(SLEEP_MODE_DAYS_PREFIX.length()).split(",");
249        if (tokens.length == 0) return null;
250        final int[] rt = new int[tokens.length];
251        for (int i = 0; i < tokens.length; i++) {
252            final int day = tryParseInt(tokens[i], -1);
253            if (day == -1) return null;
254            rt[i] = day;
255        }
256        return rt;
257    }
258
259    private static int tryParseInt(String value, int defValue) {
260        if (TextUtils.isEmpty(value)) return defValue;
261        try {
262            return Integer.valueOf(value);
263        } catch (NumberFormatException e) {
264            return defValue;
265        }
266    }
267
268    public static ZenModeConfig readXml(XmlPullParser parser)
269            throws XmlPullParserException, IOException {
270        int type = parser.getEventType();
271        if (type != XmlPullParser.START_TAG) return null;
272        String tag = parser.getName();
273        if (!ZEN_TAG.equals(tag)) return null;
274        final ZenModeConfig rt = new ZenModeConfig();
275        final int version = safeInt(parser, ZEN_ATT_VERSION, XML_VERSION);
276        final ArrayList<ComponentName> conditionComponents = new ArrayList<ComponentName>();
277        final ArrayList<Uri> conditionIds = new ArrayList<Uri>();
278        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
279            tag = parser.getName();
280            if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) {
281                if (!conditionComponents.isEmpty()) {
282                    rt.conditionComponents = conditionComponents
283                            .toArray(new ComponentName[conditionComponents.size()]);
284                    rt.conditionIds = conditionIds.toArray(new Uri[conditionIds.size()]);
285                }
286                return rt;
287            }
288            if (type == XmlPullParser.START_TAG) {
289                if (ALLOW_TAG.equals(tag)) {
290                    rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false);
291                    rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false);
292                    rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS, DEFAULT_ALLOW_EVENTS);
293                    rt.allowFrom = safeInt(parser, ALLOW_ATT_FROM, SOURCE_ANYONE);
294                    if (rt.allowFrom < SOURCE_ANYONE || rt.allowFrom > MAX_SOURCE) {
295                        throw new IndexOutOfBoundsException("bad source in config:" + rt.allowFrom);
296                    }
297                } else if (SLEEP_TAG.equals(tag)) {
298                    final String mode = parser.getAttributeValue(null, SLEEP_ATT_MODE);
299                    rt.sleepMode = isValidSleepMode(mode)? mode : null;
300                    final int startHour = safeInt(parser, SLEEP_ATT_START_HR, 0);
301                    final int startMinute = safeInt(parser, SLEEP_ATT_START_MIN, 0);
302                    final int endHour = safeInt(parser, SLEEP_ATT_END_HR, 0);
303                    final int endMinute = safeInt(parser, SLEEP_ATT_END_MIN, 0);
304                    rt.sleepStartHour = isValidHour(startHour) ? startHour : 0;
305                    rt.sleepStartMinute = isValidMinute(startMinute) ? startMinute : 0;
306                    rt.sleepEndHour = isValidHour(endHour) ? endHour : 0;
307                    rt.sleepEndMinute = isValidMinute(endMinute) ? endMinute : 0;
308                } else if (CONDITION_TAG.equals(tag)) {
309                    final ComponentName component =
310                            safeComponentName(parser, CONDITION_ATT_COMPONENT);
311                    final Uri conditionId = safeUri(parser, CONDITION_ATT_ID);
312                    if (component != null && conditionId != null) {
313                        conditionComponents.add(component);
314                        conditionIds.add(conditionId);
315                    }
316                } else if (EXIT_CONDITION_TAG.equals(tag)) {
317                    rt.exitCondition = readConditionXml(parser);
318                    if (rt.exitCondition != null) {
319                        rt.exitConditionComponent =
320                                safeComponentName(parser, EXIT_CONDITION_ATT_COMPONENT);
321                    }
322                }
323            }
324        }
325        throw new IllegalStateException("Failed to reach END_DOCUMENT");
326    }
327
328    public void writeXml(XmlSerializer out) throws IOException {
329        out.startTag(null, ZEN_TAG);
330        out.attribute(null, ZEN_ATT_VERSION, Integer.toString(XML_VERSION));
331
332        out.startTag(null, ALLOW_TAG);
333        out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls));
334        out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages));
335        out.attribute(null, ALLOW_ATT_EVENTS, Boolean.toString(allowEvents));
336        out.attribute(null, ALLOW_ATT_FROM, Integer.toString(allowFrom));
337        out.endTag(null, ALLOW_TAG);
338
339        out.startTag(null, SLEEP_TAG);
340        if (sleepMode != null) {
341            out.attribute(null, SLEEP_ATT_MODE, sleepMode);
342        }
343        out.attribute(null, SLEEP_ATT_START_HR, Integer.toString(sleepStartHour));
344        out.attribute(null, SLEEP_ATT_START_MIN, Integer.toString(sleepStartMinute));
345        out.attribute(null, SLEEP_ATT_END_HR, Integer.toString(sleepEndHour));
346        out.attribute(null, SLEEP_ATT_END_MIN, Integer.toString(sleepEndMinute));
347        out.endTag(null, SLEEP_TAG);
348
349        if (conditionComponents != null && conditionIds != null
350                && conditionComponents.length == conditionIds.length) {
351            for (int i = 0; i < conditionComponents.length; i++) {
352                out.startTag(null, CONDITION_TAG);
353                out.attribute(null, CONDITION_ATT_COMPONENT,
354                        conditionComponents[i].flattenToString());
355                out.attribute(null, CONDITION_ATT_ID, conditionIds[i].toString());
356                out.endTag(null, CONDITION_TAG);
357            }
358        }
359        if (exitCondition != null && exitConditionComponent != null) {
360            out.startTag(null, EXIT_CONDITION_TAG);
361            out.attribute(null, EXIT_CONDITION_ATT_COMPONENT,
362                    exitConditionComponent.flattenToString());
363            writeConditionXml(exitCondition, out);
364            out.endTag(null, EXIT_CONDITION_TAG);
365        }
366        out.endTag(null, ZEN_TAG);
367    }
368
369    public static Condition readConditionXml(XmlPullParser parser) {
370        final Uri id = safeUri(parser, CONDITION_ATT_ID);
371        final String summary = parser.getAttributeValue(null, CONDITION_ATT_SUMMARY);
372        final String line1 = parser.getAttributeValue(null, CONDITION_ATT_LINE1);
373        final String line2 = parser.getAttributeValue(null, CONDITION_ATT_LINE2);
374        final int icon = safeInt(parser, CONDITION_ATT_ICON, -1);
375        final int state = safeInt(parser, CONDITION_ATT_STATE, -1);
376        final int flags = safeInt(parser, CONDITION_ATT_FLAGS, -1);
377        try {
378            return new Condition(id, summary, line1, line2, icon, state, flags);
379        } catch (IllegalArgumentException e) {
380            Slog.w(TAG, "Unable to read condition xml", e);
381            return null;
382        }
383    }
384
385    public static void writeConditionXml(Condition c, XmlSerializer out) throws IOException {
386        out.attribute(null, CONDITION_ATT_ID, c.id.toString());
387        out.attribute(null, CONDITION_ATT_SUMMARY, c.summary);
388        out.attribute(null, CONDITION_ATT_LINE1, c.line1);
389        out.attribute(null, CONDITION_ATT_LINE2, c.line2);
390        out.attribute(null, CONDITION_ATT_ICON, Integer.toString(c.icon));
391        out.attribute(null, CONDITION_ATT_STATE, Integer.toString(c.state));
392        out.attribute(null, CONDITION_ATT_FLAGS, Integer.toString(c.flags));
393    }
394
395    public static boolean isValidHour(int val) {
396        return val >= 0 && val < 24;
397    }
398
399    public static boolean isValidMinute(int val) {
400        return val >= 0 && val < 60;
401    }
402
403    private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) {
404        final String val = parser.getAttributeValue(null, att);
405        if (TextUtils.isEmpty(val)) return defValue;
406        return Boolean.valueOf(val);
407    }
408
409    private static int safeInt(XmlPullParser parser, String att, int defValue) {
410        final String val = parser.getAttributeValue(null, att);
411        return tryParseInt(val, defValue);
412    }
413
414    private static ComponentName safeComponentName(XmlPullParser parser, String att) {
415        final String val = parser.getAttributeValue(null, att);
416        if (TextUtils.isEmpty(val)) return null;
417        return ComponentName.unflattenFromString(val);
418    }
419
420    private static Uri safeUri(XmlPullParser parser, String att) {
421        final String val = parser.getAttributeValue(null, att);
422        if (TextUtils.isEmpty(val)) return null;
423        return Uri.parse(val);
424    }
425
426    @Override
427    public int describeContents() {
428        return 0;
429    }
430
431    public ZenModeConfig copy() {
432        final Parcel parcel = Parcel.obtain();
433        try {
434            writeToParcel(parcel, 0);
435            parcel.setDataPosition(0);
436            return new ZenModeConfig(parcel);
437        } finally {
438            parcel.recycle();
439        }
440    }
441
442    public static final Parcelable.Creator<ZenModeConfig> CREATOR
443            = new Parcelable.Creator<ZenModeConfig>() {
444        @Override
445        public ZenModeConfig createFromParcel(Parcel source) {
446            return new ZenModeConfig(source);
447        }
448
449        @Override
450        public ZenModeConfig[] newArray(int size) {
451            return new ZenModeConfig[size];
452        }
453    };
454
455    public DowntimeInfo toDowntimeInfo() {
456        final DowntimeInfo downtime = new DowntimeInfo();
457        downtime.startHour = sleepStartHour;
458        downtime.startMinute = sleepStartMinute;
459        downtime.endHour = sleepEndHour;
460        downtime.endMinute = sleepEndMinute;
461        return downtime;
462    }
463
464    public static Condition toTimeCondition(int minutesFromNow) {
465        final long now = System.currentTimeMillis();
466        final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS;
467        return toTimeCondition(now + millis, minutesFromNow);
468    }
469
470    public static Condition toTimeCondition(long time, int minutes) {
471        final int num = minutes < 60 ? minutes : Math.round(minutes / 60f);
472        final int resId = minutes < 60
473                ? com.android.internal.R.plurals.zen_mode_duration_minutes
474                : com.android.internal.R.plurals.zen_mode_duration_hours;
475        final String caption = Resources.getSystem().getQuantityString(resId, num, num);
476        final Uri id = toCountdownConditionId(time);
477        return new Condition(id, caption, "", "", 0, Condition.STATE_TRUE,
478                Condition.FLAG_RELEVANT_NOW);
479    }
480
481    // For built-in conditions
482    private static final String SYSTEM_AUTHORITY = "android";
483
484    // Built-in countdown conditions, e.g. condition://android/countdown/1399917958951
485    private static final String COUNTDOWN_PATH = "countdown";
486
487    public static Uri toCountdownConditionId(long time) {
488        return new Uri.Builder().scheme(Condition.SCHEME)
489                .authority(SYSTEM_AUTHORITY)
490                .appendPath(COUNTDOWN_PATH)
491                .appendPath(Long.toString(time))
492                .build();
493    }
494
495    public static long tryParseCountdownConditionId(Uri conditionId) {
496        if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)) return 0;
497        if (conditionId.getPathSegments().size() != 2
498                || !COUNTDOWN_PATH.equals(conditionId.getPathSegments().get(0))) return 0;
499        try {
500            return Long.parseLong(conditionId.getPathSegments().get(1));
501        } catch (RuntimeException e) {
502            Slog.w(TAG, "Error parsing countdown condition: " + conditionId, e);
503            return 0;
504        }
505    }
506
507    public static boolean isValidCountdownConditionId(Uri conditionId) {
508        return tryParseCountdownConditionId(conditionId) != 0;
509    }
510
511    // Built-in downtime conditions, e.g. condition://android/downtime?start=10.00&end=7.00
512    private static final String DOWNTIME_PATH = "downtime";
513
514    public static Uri toDowntimeConditionId(DowntimeInfo downtime) {
515        return new Uri.Builder().scheme(Condition.SCHEME)
516                .authority(SYSTEM_AUTHORITY)
517                .appendPath(DOWNTIME_PATH)
518                .appendQueryParameter("start", downtime.startHour + "." + downtime.startMinute)
519                .appendQueryParameter("end", downtime.endHour + "." + downtime.endMinute)
520                .build();
521    }
522
523    public static DowntimeInfo tryParseDowntimeConditionId(Uri conditionId) {
524        if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)
525                || conditionId.getPathSegments().size() != 1
526                || !DOWNTIME_PATH.equals(conditionId.getPathSegments().get(0))) {
527            return null;
528        }
529        final int[] start = tryParseHourAndMinute(conditionId.getQueryParameter("start"));
530        final int[] end = tryParseHourAndMinute(conditionId.getQueryParameter("end"));
531        if (start == null || end == null) return null;
532        final DowntimeInfo downtime = new DowntimeInfo();
533        downtime.startHour = start[0];
534        downtime.startMinute = start[1];
535        downtime.endHour = end[0];
536        downtime.endMinute = end[1];
537        return downtime;
538    }
539
540    private static int[] tryParseHourAndMinute(String value) {
541        if (TextUtils.isEmpty(value)) return null;
542        final int i = value.indexOf('.');
543        if (i < 1 || i >= value.length() - 1) return null;
544        final int hour = tryParseInt(value.substring(0, i), -1);
545        final int minute = tryParseInt(value.substring(i + 1), -1);
546        return isValidHour(hour) && isValidMinute(minute) ? new int[] { hour, minute } : null;
547    }
548
549    public static boolean isValidDowntimeConditionId(Uri conditionId) {
550        return tryParseDowntimeConditionId(conditionId) != null;
551    }
552
553    public static class DowntimeInfo {
554        public int startHour;   // 0-23
555        public int startMinute; // 0-59
556        public int endHour;
557        public int endMinute;
558
559        @Override
560        public int hashCode() {
561            return 0;
562        }
563
564        @Override
565        public boolean equals(Object o) {
566            if (!(o instanceof DowntimeInfo)) return false;
567            final DowntimeInfo other = (DowntimeInfo) o;
568            return startHour == other.startHour
569                    && startMinute == other.startMinute
570                    && endHour == other.endHour
571                    && endMinute == other.endMinute;
572        }
573    }
574}
575