Home | History | Annotate | Download | only in notification
      1 /**
      2  * Copyright (c) 2014, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License,  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 
     17 package android.service.notification;
     18 
     19 import android.app.ActivityManager;
     20 import android.app.NotificationManager.Policy;
     21 import android.content.ComponentName;
     22 import android.content.Context;
     23 import android.content.pm.ApplicationInfo;
     24 import android.content.pm.PackageManager;
     25 import android.content.res.Resources;
     26 import android.net.Uri;
     27 import android.os.Parcel;
     28 import android.os.Parcelable;
     29 import android.os.UserHandle;
     30 import android.provider.Settings.Global;
     31 import android.text.TextUtils;
     32 import android.text.format.DateFormat;
     33 import android.util.ArrayMap;
     34 import android.util.ArraySet;
     35 import android.util.Slog;
     36 
     37 import com.android.internal.R;
     38 
     39 import org.xmlpull.v1.XmlPullParser;
     40 import org.xmlpull.v1.XmlPullParserException;
     41 import org.xmlpull.v1.XmlSerializer;
     42 
     43 import java.io.IOException;
     44 import java.util.ArrayList;
     45 import java.util.Arrays;
     46 import java.util.Calendar;
     47 import java.util.Date;
     48 import java.util.GregorianCalendar;
     49 import java.util.Locale;
     50 import java.util.Objects;
     51 import java.util.UUID;
     52 
     53 /**
     54  * Persisted configuration for zen mode.
     55  *
     56  * @hide
     57  */
     58 public class ZenModeConfig implements Parcelable {
     59     private static String TAG = "ZenModeConfig";
     60 
     61     public static final int SOURCE_ANYONE = 0;
     62     public static final int SOURCE_CONTACT = 1;
     63     public static final int SOURCE_STAR = 2;
     64     public static final int MAX_SOURCE = SOURCE_STAR;
     65     private static final int DEFAULT_SOURCE = SOURCE_CONTACT;
     66 
     67     public static final int[] ALL_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
     68             Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY };
     69     public static final int[] WEEKNIGHT_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
     70             Calendar.WEDNESDAY, Calendar.THURSDAY };
     71     public static final int[] WEEKEND_DAYS = { Calendar.FRIDAY, Calendar.SATURDAY };
     72 
     73     public static final int[] MINUTE_BUCKETS = generateMinuteBuckets();
     74     private static final int SECONDS_MS = 1000;
     75     private static final int MINUTES_MS = 60 * SECONDS_MS;
     76     private static final int DAY_MINUTES = 24 * 60;
     77     private static final int ZERO_VALUE_MS = 10 * SECONDS_MS;
     78 
     79     private static final boolean DEFAULT_ALLOW_CALLS = true;
     80     private static final boolean DEFAULT_ALLOW_MESSAGES = false;
     81     private static final boolean DEFAULT_ALLOW_REMINDERS = true;
     82     private static final boolean DEFAULT_ALLOW_EVENTS = true;
     83     private static final boolean DEFAULT_ALLOW_REPEAT_CALLERS = false;
     84     private static final boolean DEFAULT_ALLOW_SCREEN_OFF = true;
     85     private static final boolean DEFAULT_ALLOW_SCREEN_ON = true;
     86 
     87     private static final int XML_VERSION = 2;
     88     public static final String ZEN_TAG = "zen";
     89     private static final String ZEN_ATT_VERSION = "version";
     90     private static final String ZEN_ATT_USER = "user";
     91     private static final String ALLOW_TAG = "allow";
     92     private static final String ALLOW_ATT_CALLS = "calls";
     93     private static final String ALLOW_ATT_REPEAT_CALLERS = "repeatCallers";
     94     private static final String ALLOW_ATT_MESSAGES = "messages";
     95     private static final String ALLOW_ATT_FROM = "from";
     96     private static final String ALLOW_ATT_CALLS_FROM = "callsFrom";
     97     private static final String ALLOW_ATT_MESSAGES_FROM = "messagesFrom";
     98     private static final String ALLOW_ATT_REMINDERS = "reminders";
     99     private static final String ALLOW_ATT_EVENTS = "events";
    100     private static final String ALLOW_ATT_SCREEN_OFF = "visualScreenOff";
    101     private static final String ALLOW_ATT_SCREEN_ON = "visualScreenOn";
    102 
    103     private static final String CONDITION_TAG = "condition";
    104     private static final String CONDITION_ATT_COMPONENT = "component";
    105     private static final String CONDITION_ATT_ID = "id";
    106     private static final String CONDITION_ATT_SUMMARY = "summary";
    107     private static final String CONDITION_ATT_LINE1 = "line1";
    108     private static final String CONDITION_ATT_LINE2 = "line2";
    109     private static final String CONDITION_ATT_ICON = "icon";
    110     private static final String CONDITION_ATT_STATE = "state";
    111     private static final String CONDITION_ATT_FLAGS = "flags";
    112 
    113     private static final String MANUAL_TAG = "manual";
    114     private static final String AUTOMATIC_TAG = "automatic";
    115 
    116     private static final String RULE_ATT_ID = "ruleId";
    117     private static final String RULE_ATT_ENABLED = "enabled";
    118     private static final String RULE_ATT_SNOOZING = "snoozing";
    119     private static final String RULE_ATT_NAME = "name";
    120     private static final String RULE_ATT_COMPONENT = "component";
    121     private static final String RULE_ATT_ZEN = "zen";
    122     private static final String RULE_ATT_CONDITION_ID = "conditionId";
    123     private static final String RULE_ATT_CREATION_TIME = "creationTime";
    124     private static final String RULE_ATT_ENABLER = "enabler";
    125 
    126     public boolean allowCalls = DEFAULT_ALLOW_CALLS;
    127     public boolean allowRepeatCallers = DEFAULT_ALLOW_REPEAT_CALLERS;
    128     public boolean allowMessages = DEFAULT_ALLOW_MESSAGES;
    129     public boolean allowReminders = DEFAULT_ALLOW_REMINDERS;
    130     public boolean allowEvents = DEFAULT_ALLOW_EVENTS;
    131     public int allowCallsFrom = DEFAULT_SOURCE;
    132     public int allowMessagesFrom = DEFAULT_SOURCE;
    133     public int user = UserHandle.USER_SYSTEM;
    134     public boolean allowWhenScreenOff = DEFAULT_ALLOW_SCREEN_OFF;
    135     public boolean allowWhenScreenOn = DEFAULT_ALLOW_SCREEN_ON;
    136 
    137     public ZenRule manualRule;
    138     public ArrayMap<String, ZenRule> automaticRules = new ArrayMap<>();
    139 
    140     public ZenModeConfig() { }
    141 
    142     public ZenModeConfig(Parcel source) {
    143         allowCalls = source.readInt() == 1;
    144         allowRepeatCallers = source.readInt() == 1;
    145         allowMessages = source.readInt() == 1;
    146         allowReminders = source.readInt() == 1;
    147         allowEvents = source.readInt() == 1;
    148         allowCallsFrom = source.readInt();
    149         allowMessagesFrom = source.readInt();
    150         user = source.readInt();
    151         manualRule = source.readParcelable(null);
    152         final int len = source.readInt();
    153         if (len > 0) {
    154             final String[] ids = new String[len];
    155             final ZenRule[] rules = new ZenRule[len];
    156             source.readStringArray(ids);
    157             source.readTypedArray(rules, ZenRule.CREATOR);
    158             for (int i = 0; i < len; i++) {
    159                 automaticRules.put(ids[i], rules[i]);
    160             }
    161         }
    162         allowWhenScreenOff = source.readInt() == 1;
    163         allowWhenScreenOn = source.readInt() == 1;
    164     }
    165 
    166     @Override
    167     public void writeToParcel(Parcel dest, int flags) {
    168         dest.writeInt(allowCalls ? 1 : 0);
    169         dest.writeInt(allowRepeatCallers ? 1 : 0);
    170         dest.writeInt(allowMessages ? 1 : 0);
    171         dest.writeInt(allowReminders ? 1 : 0);
    172         dest.writeInt(allowEvents ? 1 : 0);
    173         dest.writeInt(allowCallsFrom);
    174         dest.writeInt(allowMessagesFrom);
    175         dest.writeInt(user);
    176         dest.writeParcelable(manualRule, 0);
    177         if (!automaticRules.isEmpty()) {
    178             final int len = automaticRules.size();
    179             final String[] ids = new String[len];
    180             final ZenRule[] rules = new ZenRule[len];
    181             for (int i = 0; i < len; i++) {
    182                 ids[i] = automaticRules.keyAt(i);
    183                 rules[i] = automaticRules.valueAt(i);
    184             }
    185             dest.writeInt(len);
    186             dest.writeStringArray(ids);
    187             dest.writeTypedArray(rules, 0);
    188         } else {
    189             dest.writeInt(0);
    190         }
    191         dest.writeInt(allowWhenScreenOff ? 1 : 0);
    192         dest.writeInt(allowWhenScreenOn ? 1 : 0);
    193     }
    194 
    195     @Override
    196     public String toString() {
    197         return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[')
    198                 .append("user=").append(user)
    199                 .append(",allowCalls=").append(allowCalls)
    200                 .append(",allowRepeatCallers=").append(allowRepeatCallers)
    201                 .append(",allowMessages=").append(allowMessages)
    202                 .append(",allowCallsFrom=").append(sourceToString(allowCallsFrom))
    203                 .append(",allowMessagesFrom=").append(sourceToString(allowMessagesFrom))
    204                 .append(",allowReminders=").append(allowReminders)
    205                 .append(",allowEvents=").append(allowEvents)
    206                 .append(",allowWhenScreenOff=").append(allowWhenScreenOff)
    207                 .append(",allowWhenScreenOn=").append(allowWhenScreenOn)
    208                 .append(",automaticRules=").append(automaticRules)
    209                 .append(",manualRule=").append(manualRule)
    210                 .append(']').toString();
    211     }
    212 
    213     private Diff diff(ZenModeConfig to) {
    214         final Diff d = new Diff();
    215         if (to == null) {
    216             return d.addLine("config", "delete");
    217         }
    218         if (user != to.user) {
    219             d.addLine("user", user, to.user);
    220         }
    221         if (allowCalls != to.allowCalls) {
    222             d.addLine("allowCalls", allowCalls, to.allowCalls);
    223         }
    224         if (allowRepeatCallers != to.allowRepeatCallers) {
    225             d.addLine("allowRepeatCallers", allowRepeatCallers, to.allowRepeatCallers);
    226         }
    227         if (allowMessages != to.allowMessages) {
    228             d.addLine("allowMessages", allowMessages, to.allowMessages);
    229         }
    230         if (allowCallsFrom != to.allowCallsFrom) {
    231             d.addLine("allowCallsFrom", allowCallsFrom, to.allowCallsFrom);
    232         }
    233         if (allowMessagesFrom != to.allowMessagesFrom) {
    234             d.addLine("allowMessagesFrom", allowMessagesFrom, to.allowMessagesFrom);
    235         }
    236         if (allowReminders != to.allowReminders) {
    237             d.addLine("allowReminders", allowReminders, to.allowReminders);
    238         }
    239         if (allowEvents != to.allowEvents) {
    240             d.addLine("allowEvents", allowEvents, to.allowEvents);
    241         }
    242         if (allowWhenScreenOff != to.allowWhenScreenOff) {
    243             d.addLine("allowWhenScreenOff", allowWhenScreenOff, to.allowWhenScreenOff);
    244         }
    245         if (allowWhenScreenOn != to.allowWhenScreenOn) {
    246             d.addLine("allowWhenScreenOn", allowWhenScreenOn, to.allowWhenScreenOn);
    247         }
    248         final ArraySet<String> allRules = new ArraySet<>();
    249         addKeys(allRules, automaticRules);
    250         addKeys(allRules, to.automaticRules);
    251         final int N = allRules.size();
    252         for (int i = 0; i < N; i++) {
    253             final String rule = allRules.valueAt(i);
    254             final ZenRule fromRule = automaticRules != null ? automaticRules.get(rule) : null;
    255             final ZenRule toRule = to.automaticRules != null ? to.automaticRules.get(rule) : null;
    256             ZenRule.appendDiff(d, "automaticRule[" + rule + "]", fromRule, toRule);
    257         }
    258         ZenRule.appendDiff(d, "manualRule", manualRule, to.manualRule);
    259         return d;
    260     }
    261 
    262     public static Diff diff(ZenModeConfig from, ZenModeConfig to) {
    263         if (from == null) {
    264             final Diff d = new Diff();
    265             if (to != null) {
    266                 d.addLine("config", "insert");
    267             }
    268             return d;
    269         }
    270         return from.diff(to);
    271     }
    272 
    273     private static <T> void addKeys(ArraySet<T> set, ArrayMap<T, ?> map) {
    274         if (map != null) {
    275             for (int i = 0; i < map.size(); i++) {
    276                 set.add(map.keyAt(i));
    277             }
    278         }
    279     }
    280 
    281     public boolean isValid() {
    282         if (!isValidManualRule(manualRule)) return false;
    283         final int N = automaticRules.size();
    284         for (int i = 0; i < N; i++) {
    285             if (!isValidAutomaticRule(automaticRules.valueAt(i))) return false;
    286         }
    287         return true;
    288     }
    289 
    290     private static boolean isValidManualRule(ZenRule rule) {
    291         return rule == null || Global.isValidZenMode(rule.zenMode) && sameCondition(rule);
    292     }
    293 
    294     private static boolean isValidAutomaticRule(ZenRule rule) {
    295         return rule != null && !TextUtils.isEmpty(rule.name) && Global.isValidZenMode(rule.zenMode)
    296                 && rule.conditionId != null && sameCondition(rule);
    297     }
    298 
    299     private static boolean sameCondition(ZenRule rule) {
    300         if (rule == null) return false;
    301         if (rule.conditionId == null) {
    302             return rule.condition == null;
    303         } else {
    304             return rule.condition == null || rule.conditionId.equals(rule.condition.id);
    305         }
    306     }
    307 
    308     private static int[] generateMinuteBuckets() {
    309         final int maxHrs = 12;
    310         final int[] buckets = new int[maxHrs + 3];
    311         buckets[0] = 15;
    312         buckets[1] = 30;
    313         buckets[2] = 45;
    314         for (int i = 1; i <= maxHrs; i++) {
    315             buckets[2 + i] = 60 * i;
    316         }
    317         return buckets;
    318     }
    319 
    320     public static String sourceToString(int source) {
    321         switch (source) {
    322             case SOURCE_ANYONE:
    323                 return "anyone";
    324             case SOURCE_CONTACT:
    325                 return "contacts";
    326             case SOURCE_STAR:
    327                 return "stars";
    328             default:
    329                 return "UNKNOWN";
    330         }
    331     }
    332 
    333     @Override
    334     public boolean equals(Object o) {
    335         if (!(o instanceof ZenModeConfig)) return false;
    336         if (o == this) return true;
    337         final ZenModeConfig other = (ZenModeConfig) o;
    338         return other.allowCalls == allowCalls
    339                 && other.allowRepeatCallers == allowRepeatCallers
    340                 && other.allowMessages == allowMessages
    341                 && other.allowCallsFrom == allowCallsFrom
    342                 && other.allowMessagesFrom == allowMessagesFrom
    343                 && other.allowReminders == allowReminders
    344                 && other.allowEvents == allowEvents
    345                 && other.allowWhenScreenOff == allowWhenScreenOff
    346                 && other.allowWhenScreenOn == allowWhenScreenOn
    347                 && other.user == user
    348                 && Objects.equals(other.automaticRules, automaticRules)
    349                 && Objects.equals(other.manualRule, manualRule);
    350     }
    351 
    352     @Override
    353     public int hashCode() {
    354         return Objects.hash(allowCalls, allowRepeatCallers, allowMessages, allowCallsFrom,
    355                 allowMessagesFrom, allowReminders, allowEvents, allowWhenScreenOff,
    356                 allowWhenScreenOn,
    357                 user, automaticRules, manualRule);
    358     }
    359 
    360     private static String toDayList(int[] days) {
    361         if (days == null || days.length == 0) return "";
    362         final StringBuilder sb = new StringBuilder();
    363         for (int i = 0; i < days.length; i++) {
    364             if (i > 0) sb.append('.');
    365             sb.append(days[i]);
    366         }
    367         return sb.toString();
    368     }
    369 
    370     private static int[] tryParseDayList(String dayList, String sep) {
    371         if (dayList == null) return null;
    372         final String[] tokens = dayList.split(sep);
    373         if (tokens.length == 0) return null;
    374         final int[] rt = new int[tokens.length];
    375         for (int i = 0; i < tokens.length; i++) {
    376             final int day = tryParseInt(tokens[i], -1);
    377             if (day == -1) return null;
    378             rt[i] = day;
    379         }
    380         return rt;
    381     }
    382 
    383     private static int tryParseInt(String value, int defValue) {
    384         if (TextUtils.isEmpty(value)) return defValue;
    385         try {
    386             return Integer.parseInt(value);
    387         } catch (NumberFormatException e) {
    388             return defValue;
    389         }
    390     }
    391 
    392     private static long tryParseLong(String value, long defValue) {
    393         if (TextUtils.isEmpty(value)) return defValue;
    394         try {
    395             return Long.parseLong(value);
    396         } catch (NumberFormatException e) {
    397             return defValue;
    398         }
    399     }
    400 
    401     public static ZenModeConfig readXml(XmlPullParser parser)
    402             throws XmlPullParserException, IOException {
    403         int type = parser.getEventType();
    404         if (type != XmlPullParser.START_TAG) return null;
    405         String tag = parser.getName();
    406         if (!ZEN_TAG.equals(tag)) return null;
    407         final ZenModeConfig rt = new ZenModeConfig();
    408         rt.user = safeInt(parser, ZEN_ATT_USER, rt.user);
    409         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
    410             tag = parser.getName();
    411             if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) {
    412                 return rt;
    413             }
    414             if (type == XmlPullParser.START_TAG) {
    415                 if (ALLOW_TAG.equals(tag)) {
    416                     rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false);
    417                     rt.allowRepeatCallers = safeBoolean(parser, ALLOW_ATT_REPEAT_CALLERS,
    418                             DEFAULT_ALLOW_REPEAT_CALLERS);
    419                     rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false);
    420                     rt.allowReminders = safeBoolean(parser, ALLOW_ATT_REMINDERS,
    421                             DEFAULT_ALLOW_REMINDERS);
    422                     rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS, DEFAULT_ALLOW_EVENTS);
    423                     final int from = safeInt(parser, ALLOW_ATT_FROM, -1);
    424                     final int callsFrom = safeInt(parser, ALLOW_ATT_CALLS_FROM, -1);
    425                     final int messagesFrom = safeInt(parser, ALLOW_ATT_MESSAGES_FROM, -1);
    426                     if (isValidSource(callsFrom) && isValidSource(messagesFrom)) {
    427                         rt.allowCallsFrom = callsFrom;
    428                         rt.allowMessagesFrom = messagesFrom;
    429                     } else if (isValidSource(from)) {
    430                         Slog.i(TAG, "Migrating existing shared 'from': " + sourceToString(from));
    431                         rt.allowCallsFrom = from;
    432                         rt.allowMessagesFrom = from;
    433                     } else {
    434                         rt.allowCallsFrom = DEFAULT_SOURCE;
    435                         rt.allowMessagesFrom = DEFAULT_SOURCE;
    436                     }
    437                     rt.allowWhenScreenOff =
    438                             safeBoolean(parser, ALLOW_ATT_SCREEN_OFF, DEFAULT_ALLOW_SCREEN_OFF);
    439                     rt.allowWhenScreenOn =
    440                             safeBoolean(parser, ALLOW_ATT_SCREEN_ON, DEFAULT_ALLOW_SCREEN_ON);
    441                 } else if (MANUAL_TAG.equals(tag)) {
    442                     rt.manualRule = readRuleXml(parser);
    443                 } else if (AUTOMATIC_TAG.equals(tag)) {
    444                     final String id = parser.getAttributeValue(null, RULE_ATT_ID);
    445                     final ZenRule automaticRule = readRuleXml(parser);
    446                     if (id != null && automaticRule != null) {
    447                         automaticRule.id = id;
    448                         rt.automaticRules.put(id, automaticRule);
    449                     }
    450                 }
    451             }
    452         }
    453         throw new IllegalStateException("Failed to reach END_DOCUMENT");
    454     }
    455 
    456     public void writeXml(XmlSerializer out) throws IOException {
    457         out.startTag(null, ZEN_TAG);
    458         out.attribute(null, ZEN_ATT_VERSION, Integer.toString(XML_VERSION));
    459         out.attribute(null, ZEN_ATT_USER, Integer.toString(user));
    460 
    461         out.startTag(null, ALLOW_TAG);
    462         out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls));
    463         out.attribute(null, ALLOW_ATT_REPEAT_CALLERS, Boolean.toString(allowRepeatCallers));
    464         out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages));
    465         out.attribute(null, ALLOW_ATT_REMINDERS, Boolean.toString(allowReminders));
    466         out.attribute(null, ALLOW_ATT_EVENTS, Boolean.toString(allowEvents));
    467         out.attribute(null, ALLOW_ATT_CALLS_FROM, Integer.toString(allowCallsFrom));
    468         out.attribute(null, ALLOW_ATT_MESSAGES_FROM, Integer.toString(allowMessagesFrom));
    469         out.attribute(null, ALLOW_ATT_SCREEN_OFF, Boolean.toString(allowWhenScreenOff));
    470         out.attribute(null, ALLOW_ATT_SCREEN_ON, Boolean.toString(allowWhenScreenOn));
    471         out.endTag(null, ALLOW_TAG);
    472 
    473         if (manualRule != null) {
    474             out.startTag(null, MANUAL_TAG);
    475             writeRuleXml(manualRule, out);
    476             out.endTag(null, MANUAL_TAG);
    477         }
    478         final int N = automaticRules.size();
    479         for (int i = 0; i < N; i++) {
    480             final String id = automaticRules.keyAt(i);
    481             final ZenRule automaticRule = automaticRules.valueAt(i);
    482             out.startTag(null, AUTOMATIC_TAG);
    483             out.attribute(null, RULE_ATT_ID, id);
    484             writeRuleXml(automaticRule, out);
    485             out.endTag(null, AUTOMATIC_TAG);
    486         }
    487         out.endTag(null, ZEN_TAG);
    488     }
    489 
    490     public static ZenRule readRuleXml(XmlPullParser parser) {
    491         final ZenRule rt = new ZenRule();
    492         rt.enabled = safeBoolean(parser, RULE_ATT_ENABLED, true);
    493         rt.snoozing = safeBoolean(parser, RULE_ATT_SNOOZING, false);
    494         rt.name = parser.getAttributeValue(null, RULE_ATT_NAME);
    495         final String zen = parser.getAttributeValue(null, RULE_ATT_ZEN);
    496         rt.zenMode = tryParseZenMode(zen, -1);
    497         if (rt.zenMode == -1) {
    498             Slog.w(TAG, "Bad zen mode in rule xml:" + zen);
    499             return null;
    500         }
    501         rt.conditionId = safeUri(parser, RULE_ATT_CONDITION_ID);
    502         rt.component = safeComponentName(parser, RULE_ATT_COMPONENT);
    503         rt.creationTime = safeLong(parser, RULE_ATT_CREATION_TIME, 0);
    504         rt.enabler = parser.getAttributeValue(null, RULE_ATT_ENABLER);
    505         rt.condition = readConditionXml(parser);
    506         return rt;
    507     }
    508 
    509     public static void writeRuleXml(ZenRule rule, XmlSerializer out) throws IOException {
    510         out.attribute(null, RULE_ATT_ENABLED, Boolean.toString(rule.enabled));
    511         out.attribute(null, RULE_ATT_SNOOZING, Boolean.toString(rule.snoozing));
    512         if (rule.name != null) {
    513             out.attribute(null, RULE_ATT_NAME, rule.name);
    514         }
    515         out.attribute(null, RULE_ATT_ZEN, Integer.toString(rule.zenMode));
    516         if (rule.component != null) {
    517             out.attribute(null, RULE_ATT_COMPONENT, rule.component.flattenToString());
    518         }
    519         if (rule.conditionId != null) {
    520             out.attribute(null, RULE_ATT_CONDITION_ID, rule.conditionId.toString());
    521         }
    522         out.attribute(null, RULE_ATT_CREATION_TIME, Long.toString(rule.creationTime));
    523         if (rule.enabler != null) {
    524             out.attribute(null, RULE_ATT_ENABLER, rule.enabler);
    525         }
    526         if (rule.condition != null) {
    527             writeConditionXml(rule.condition, out);
    528         }
    529     }
    530 
    531     public static Condition readConditionXml(XmlPullParser parser) {
    532         final Uri id = safeUri(parser, CONDITION_ATT_ID);
    533         if (id == null) return null;
    534         final String summary = parser.getAttributeValue(null, CONDITION_ATT_SUMMARY);
    535         final String line1 = parser.getAttributeValue(null, CONDITION_ATT_LINE1);
    536         final String line2 = parser.getAttributeValue(null, CONDITION_ATT_LINE2);
    537         final int icon = safeInt(parser, CONDITION_ATT_ICON, -1);
    538         final int state = safeInt(parser, CONDITION_ATT_STATE, -1);
    539         final int flags = safeInt(parser, CONDITION_ATT_FLAGS, -1);
    540         try {
    541             return new Condition(id, summary, line1, line2, icon, state, flags);
    542         } catch (IllegalArgumentException e) {
    543             Slog.w(TAG, "Unable to read condition xml", e);
    544             return null;
    545         }
    546     }
    547 
    548     public static void writeConditionXml(Condition c, XmlSerializer out) throws IOException {
    549         out.attribute(null, CONDITION_ATT_ID, c.id.toString());
    550         out.attribute(null, CONDITION_ATT_SUMMARY, c.summary);
    551         out.attribute(null, CONDITION_ATT_LINE1, c.line1);
    552         out.attribute(null, CONDITION_ATT_LINE2, c.line2);
    553         out.attribute(null, CONDITION_ATT_ICON, Integer.toString(c.icon));
    554         out.attribute(null, CONDITION_ATT_STATE, Integer.toString(c.state));
    555         out.attribute(null, CONDITION_ATT_FLAGS, Integer.toString(c.flags));
    556     }
    557 
    558     public static boolean isValidHour(int val) {
    559         return val >= 0 && val < 24;
    560     }
    561 
    562     public static boolean isValidMinute(int val) {
    563         return val >= 0 && val < 60;
    564     }
    565 
    566     private static boolean isValidSource(int source) {
    567         return source >= SOURCE_ANYONE && source <= MAX_SOURCE;
    568     }
    569 
    570     private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) {
    571         final String val = parser.getAttributeValue(null, att);
    572         return safeBoolean(val, defValue);
    573     }
    574 
    575     private static boolean safeBoolean(String val, boolean defValue) {
    576         if (TextUtils.isEmpty(val)) return defValue;
    577         return Boolean.parseBoolean(val);
    578     }
    579 
    580     private static int safeInt(XmlPullParser parser, String att, int defValue) {
    581         final String val = parser.getAttributeValue(null, att);
    582         return tryParseInt(val, defValue);
    583     }
    584 
    585     private static ComponentName safeComponentName(XmlPullParser parser, String att) {
    586         final String val = parser.getAttributeValue(null, att);
    587         if (TextUtils.isEmpty(val)) return null;
    588         return ComponentName.unflattenFromString(val);
    589     }
    590 
    591     private static Uri safeUri(XmlPullParser parser, String att) {
    592         final String val = parser.getAttributeValue(null, att);
    593         if (TextUtils.isEmpty(val)) return null;
    594         return Uri.parse(val);
    595     }
    596 
    597     private static long safeLong(XmlPullParser parser, String att, long defValue) {
    598         final String val = parser.getAttributeValue(null, att);
    599         return tryParseLong(val, defValue);
    600     }
    601 
    602     @Override
    603     public int describeContents() {
    604         return 0;
    605     }
    606 
    607     public ZenModeConfig copy() {
    608         final Parcel parcel = Parcel.obtain();
    609         try {
    610             writeToParcel(parcel, 0);
    611             parcel.setDataPosition(0);
    612             return new ZenModeConfig(parcel);
    613         } finally {
    614             parcel.recycle();
    615         }
    616     }
    617 
    618     public static final Parcelable.Creator<ZenModeConfig> CREATOR
    619             = new Parcelable.Creator<ZenModeConfig>() {
    620         @Override
    621         public ZenModeConfig createFromParcel(Parcel source) {
    622             return new ZenModeConfig(source);
    623         }
    624 
    625         @Override
    626         public ZenModeConfig[] newArray(int size) {
    627             return new ZenModeConfig[size];
    628         }
    629     };
    630 
    631     public Policy toNotificationPolicy() {
    632         int priorityCategories = 0;
    633         int priorityCallSenders = Policy.PRIORITY_SENDERS_CONTACTS;
    634         int priorityMessageSenders = Policy.PRIORITY_SENDERS_CONTACTS;
    635         if (allowCalls) {
    636             priorityCategories |= Policy.PRIORITY_CATEGORY_CALLS;
    637         }
    638         if (allowMessages) {
    639             priorityCategories |= Policy.PRIORITY_CATEGORY_MESSAGES;
    640         }
    641         if (allowEvents) {
    642             priorityCategories |= Policy.PRIORITY_CATEGORY_EVENTS;
    643         }
    644         if (allowReminders) {
    645             priorityCategories |= Policy.PRIORITY_CATEGORY_REMINDERS;
    646         }
    647         if (allowRepeatCallers) {
    648             priorityCategories |= Policy.PRIORITY_CATEGORY_REPEAT_CALLERS;
    649         }
    650         int suppressedVisualEffects = 0;
    651         if (!allowWhenScreenOff) {
    652             suppressedVisualEffects |= Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
    653         }
    654         if (!allowWhenScreenOn) {
    655             suppressedVisualEffects |= Policy.SUPPRESSED_EFFECT_SCREEN_ON;
    656         }
    657         priorityCallSenders = sourceToPrioritySenders(allowCallsFrom, priorityCallSenders);
    658         priorityMessageSenders = sourceToPrioritySenders(allowMessagesFrom, priorityMessageSenders);
    659         return new Policy(priorityCategories, priorityCallSenders, priorityMessageSenders,
    660                 suppressedVisualEffects);
    661     }
    662 
    663     private static int sourceToPrioritySenders(int source, int def) {
    664         switch (source) {
    665             case SOURCE_ANYONE: return Policy.PRIORITY_SENDERS_ANY;
    666             case SOURCE_CONTACT: return Policy.PRIORITY_SENDERS_CONTACTS;
    667             case SOURCE_STAR: return Policy.PRIORITY_SENDERS_STARRED;
    668             default: return def;
    669         }
    670     }
    671 
    672     private static int prioritySendersToSource(int prioritySenders, int def) {
    673         switch (prioritySenders) {
    674             case Policy.PRIORITY_SENDERS_CONTACTS: return SOURCE_CONTACT;
    675             case Policy.PRIORITY_SENDERS_STARRED: return SOURCE_STAR;
    676             case Policy.PRIORITY_SENDERS_ANY: return SOURCE_ANYONE;
    677             default: return def;
    678         }
    679     }
    680 
    681     public void applyNotificationPolicy(Policy policy) {
    682         if (policy == null) return;
    683         allowCalls = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_CALLS) != 0;
    684         allowMessages = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_MESSAGES) != 0;
    685         allowEvents = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_EVENTS) != 0;
    686         allowReminders = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REMINDERS) != 0;
    687         allowRepeatCallers = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)
    688                 != 0;
    689         allowCallsFrom = prioritySendersToSource(policy.priorityCallSenders, allowCallsFrom);
    690         allowMessagesFrom = prioritySendersToSource(policy.priorityMessageSenders,
    691                 allowMessagesFrom);
    692         if (policy.suppressedVisualEffects != Policy.SUPPRESSED_EFFECTS_UNSET) {
    693             allowWhenScreenOff =
    694                     (policy.suppressedVisualEffects & Policy.SUPPRESSED_EFFECT_SCREEN_OFF) == 0;
    695             allowWhenScreenOn =
    696                     (policy.suppressedVisualEffects & Policy.SUPPRESSED_EFFECT_SCREEN_ON) == 0;
    697         }
    698     }
    699 
    700     public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle) {
    701         return toTimeCondition(context, minutesFromNow, userHandle, false /*shortVersion*/);
    702     }
    703 
    704     public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle,
    705             boolean shortVersion) {
    706         final long now = System.currentTimeMillis();
    707         final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS;
    708         return toTimeCondition(context, now + millis, minutesFromNow, userHandle, shortVersion);
    709     }
    710 
    711     public static Condition toTimeCondition(Context context, long time, int minutes,
    712             int userHandle, boolean shortVersion) {
    713         final int num;
    714         String summary, line1, line2;
    715         final CharSequence formattedTime =
    716                 getFormattedTime(context, time, isToday(time), userHandle);
    717         final Resources res = context.getResources();
    718         if (minutes < 60) {
    719             // display as minutes
    720             num = minutes;
    721             int summaryResId = shortVersion ? R.plurals.zen_mode_duration_minutes_summary_short
    722                     : R.plurals.zen_mode_duration_minutes_summary;
    723             summary = res.getQuantityString(summaryResId, num, num, formattedTime);
    724             int line1ResId = shortVersion ? R.plurals.zen_mode_duration_minutes_short
    725                     : R.plurals.zen_mode_duration_minutes;
    726             line1 = res.getQuantityString(line1ResId, num, num, formattedTime);
    727             line2 = res.getString(R.string.zen_mode_until, formattedTime);
    728         } else if (minutes < DAY_MINUTES) {
    729             // display as hours
    730             num =  Math.round(minutes / 60f);
    731             int summaryResId = shortVersion ? R.plurals.zen_mode_duration_hours_summary_short
    732                     : R.plurals.zen_mode_duration_hours_summary;
    733             summary = res.getQuantityString(summaryResId, num, num, formattedTime);
    734             int line1ResId = shortVersion ? R.plurals.zen_mode_duration_hours_short
    735                     : R.plurals.zen_mode_duration_hours;
    736             line1 = res.getQuantityString(line1ResId, num, num, formattedTime);
    737             line2 = res.getString(R.string.zen_mode_until, formattedTime);
    738         } else {
    739             // display as day/time
    740             summary = line1 = line2 = res.getString(R.string.zen_mode_until, formattedTime);
    741         }
    742         final Uri id = toCountdownConditionId(time, false);
    743         return new Condition(id, summary, line1, line2, 0, Condition.STATE_TRUE,
    744                 Condition.FLAG_RELEVANT_NOW);
    745     }
    746 
    747     /**
    748      * Converts countdown to alarm parameters into a condition with user facing summary
    749      */
    750     public static Condition toNextAlarmCondition(Context context, long alarm,
    751             int userHandle) {
    752         boolean isSameDay = isToday(alarm);
    753         final CharSequence formattedTime = getFormattedTime(context, alarm, isSameDay, userHandle);
    754         final Resources res = context.getResources();
    755         final String line1 = res.getString(R.string.zen_mode_until, formattedTime);
    756         final Uri id = toCountdownConditionId(alarm, true);
    757         return new Condition(id, "", line1, "", 0, Condition.STATE_TRUE,
    758                 Condition.FLAG_RELEVANT_NOW);
    759     }
    760 
    761     private static CharSequence getFormattedTime(Context context, long time, boolean isSameDay,
    762             int userHandle) {
    763         String skeleton = (!isSameDay ? "EEE " : "")
    764                 + (DateFormat.is24HourFormat(context, userHandle) ? "Hm" : "hma");
    765         final String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
    766         return DateFormat.format(pattern, time);
    767     }
    768 
    769     private static boolean isToday(long time) {
    770         GregorianCalendar now = new GregorianCalendar();
    771         GregorianCalendar endTime = new GregorianCalendar();
    772         endTime.setTimeInMillis(time);
    773         if (now.get(Calendar.YEAR) == endTime.get(Calendar.YEAR)
    774                 && now.get(Calendar.MONTH) == endTime.get(Calendar.MONTH)
    775                 && now.get(Calendar.DATE) == endTime.get(Calendar.DATE)) {
    776             return true;
    777         }
    778         return false;
    779     }
    780 
    781     // ==== Built-in system conditions ====
    782 
    783     public static final String SYSTEM_AUTHORITY = "android";
    784 
    785     // ==== Built-in system condition: countdown ====
    786 
    787     public static final String COUNTDOWN_PATH = "countdown";
    788 
    789     public static final String IS_ALARM_PATH = "alarm";
    790 
    791     /**
    792      * Converts countdown condition parameters into a condition id.
    793      */
    794     public static Uri toCountdownConditionId(long time, boolean alarm) {
    795         return new Uri.Builder().scheme(Condition.SCHEME)
    796                 .authority(SYSTEM_AUTHORITY)
    797                 .appendPath(COUNTDOWN_PATH)
    798                 .appendPath(Long.toString(time))
    799                 .appendPath(IS_ALARM_PATH)
    800                 .appendPath(Boolean.toString(alarm))
    801                 .build();
    802     }
    803 
    804     public static long tryParseCountdownConditionId(Uri conditionId) {
    805         if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)) return 0;
    806         if (conditionId.getPathSegments().size() < 2
    807                 || !COUNTDOWN_PATH.equals(conditionId.getPathSegments().get(0))) return 0;
    808         try {
    809             return Long.parseLong(conditionId.getPathSegments().get(1));
    810         } catch (RuntimeException e) {
    811             Slog.w(TAG, "Error parsing countdown condition: " + conditionId, e);
    812             return 0;
    813         }
    814     }
    815 
    816     /**
    817      * Returns whether this condition is a countdown condition.
    818      */
    819     public static boolean isValidCountdownConditionId(Uri conditionId) {
    820         return tryParseCountdownConditionId(conditionId) != 0;
    821     }
    822 
    823     /**
    824      * Returns whether this condition is a countdown to an alarm.
    825      */
    826     public static boolean isValidCountdownToAlarmConditionId(Uri conditionId) {
    827         if (tryParseCountdownConditionId(conditionId) != 0) {
    828             if (conditionId.getPathSegments().size() < 4
    829                     || !IS_ALARM_PATH.equals(conditionId.getPathSegments().get(2))) {
    830                 return false;
    831             }
    832             try {
    833                 return Boolean.parseBoolean(conditionId.getPathSegments().get(3));
    834             } catch (RuntimeException e) {
    835                 Slog.w(TAG, "Error parsing countdown alarm condition: " + conditionId, e);
    836                 return false;
    837             }
    838         }
    839         return false;
    840     }
    841 
    842     // ==== Built-in system condition: schedule ====
    843 
    844     public static final String SCHEDULE_PATH = "schedule";
    845 
    846     public static Uri toScheduleConditionId(ScheduleInfo schedule) {
    847         return new Uri.Builder().scheme(Condition.SCHEME)
    848                 .authority(SYSTEM_AUTHORITY)
    849                 .appendPath(SCHEDULE_PATH)
    850                 .appendQueryParameter("days", toDayList(schedule.days))
    851                 .appendQueryParameter("start", schedule.startHour + "." + schedule.startMinute)
    852                 .appendQueryParameter("end", schedule.endHour + "." + schedule.endMinute)
    853                 .appendQueryParameter("exitAtAlarm", String.valueOf(schedule.exitAtAlarm))
    854                 .build();
    855     }
    856 
    857     public static boolean isValidScheduleConditionId(Uri conditionId) {
    858         return tryParseScheduleConditionId(conditionId) != null;
    859     }
    860 
    861     public static ScheduleInfo tryParseScheduleConditionId(Uri conditionId) {
    862         final boolean isSchedule =  conditionId != null
    863                 && conditionId.getScheme().equals(Condition.SCHEME)
    864                 && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY)
    865                 && conditionId.getPathSegments().size() == 1
    866                 && conditionId.getPathSegments().get(0).equals(ZenModeConfig.SCHEDULE_PATH);
    867         if (!isSchedule) return null;
    868         final int[] start = tryParseHourAndMinute(conditionId.getQueryParameter("start"));
    869         final int[] end = tryParseHourAndMinute(conditionId.getQueryParameter("end"));
    870         if (start == null || end == null) return null;
    871         final ScheduleInfo rt = new ScheduleInfo();
    872         rt.days = tryParseDayList(conditionId.getQueryParameter("days"), "\\.");
    873         rt.startHour = start[0];
    874         rt.startMinute = start[1];
    875         rt.endHour = end[0];
    876         rt.endMinute = end[1];
    877         rt.exitAtAlarm = safeBoolean(conditionId.getQueryParameter("exitAtAlarm"), false);
    878         return rt;
    879     }
    880 
    881     public static ComponentName getScheduleConditionProvider() {
    882         return new ComponentName(SYSTEM_AUTHORITY, "ScheduleConditionProvider");
    883     }
    884 
    885     public static class ScheduleInfo {
    886         public int[] days;
    887         public int startHour;
    888         public int startMinute;
    889         public int endHour;
    890         public int endMinute;
    891         public boolean exitAtAlarm;
    892         public long nextAlarm;
    893 
    894         @Override
    895         public int hashCode() {
    896             return 0;
    897         }
    898 
    899         @Override
    900         public boolean equals(Object o) {
    901             if (!(o instanceof ScheduleInfo)) return false;
    902             final ScheduleInfo other = (ScheduleInfo) o;
    903             return toDayList(days).equals(toDayList(other.days))
    904                     && startHour == other.startHour
    905                     && startMinute == other.startMinute
    906                     && endHour == other.endHour
    907                     && endMinute == other.endMinute
    908                     && exitAtAlarm == other.exitAtAlarm;
    909         }
    910 
    911         public ScheduleInfo copy() {
    912             final ScheduleInfo rt = new ScheduleInfo();
    913             if (days != null) {
    914                 rt.days = new int[days.length];
    915                 System.arraycopy(days, 0, rt.days, 0, days.length);
    916             }
    917             rt.startHour = startHour;
    918             rt.startMinute = startMinute;
    919             rt.endHour = endHour;
    920             rt.endMinute = endMinute;
    921             rt.exitAtAlarm = exitAtAlarm;
    922             rt.nextAlarm = nextAlarm;
    923             return rt;
    924         }
    925 
    926         @Override
    927         public String toString() {
    928             return "ScheduleInfo{" +
    929                     "days=" + Arrays.toString(days) +
    930                     ", startHour=" + startHour +
    931                     ", startMinute=" + startMinute +
    932                     ", endHour=" + endHour +
    933                     ", endMinute=" + endMinute +
    934                     ", exitAtAlarm=" + exitAtAlarm +
    935                     ", nextAlarm=" + ts(nextAlarm) +
    936                     '}';
    937         }
    938 
    939         protected static String ts(long time) {
    940             return new Date(time) + " (" + time + ")";
    941         }
    942     }
    943 
    944     // ==== Built-in system condition: event ====
    945 
    946     public static final String EVENT_PATH = "event";
    947 
    948     public static Uri toEventConditionId(EventInfo event) {
    949         return new Uri.Builder().scheme(Condition.SCHEME)
    950                 .authority(SYSTEM_AUTHORITY)
    951                 .appendPath(EVENT_PATH)
    952                 .appendQueryParameter("userId", Long.toString(event.userId))
    953                 .appendQueryParameter("calendar", event.calendar != null ? event.calendar : "")
    954                 .appendQueryParameter("reply", Integer.toString(event.reply))
    955                 .build();
    956     }
    957 
    958     public static boolean isValidEventConditionId(Uri conditionId) {
    959         return tryParseEventConditionId(conditionId) != null;
    960     }
    961 
    962     public static EventInfo tryParseEventConditionId(Uri conditionId) {
    963         final boolean isEvent = conditionId != null
    964                 && conditionId.getScheme().equals(Condition.SCHEME)
    965                 && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY)
    966                 && conditionId.getPathSegments().size() == 1
    967                 && conditionId.getPathSegments().get(0).equals(EVENT_PATH);
    968         if (!isEvent) return null;
    969         final EventInfo rt = new EventInfo();
    970         rt.userId = tryParseInt(conditionId.getQueryParameter("userId"), UserHandle.USER_NULL);
    971         rt.calendar = conditionId.getQueryParameter("calendar");
    972         if (TextUtils.isEmpty(rt.calendar) || tryParseLong(rt.calendar, -1L) != -1L) {
    973             rt.calendar = null;
    974         }
    975         rt.reply = tryParseInt(conditionId.getQueryParameter("reply"), 0);
    976         return rt;
    977     }
    978 
    979     public static ComponentName getEventConditionProvider() {
    980         return new ComponentName(SYSTEM_AUTHORITY, "EventConditionProvider");
    981     }
    982 
    983     public static class EventInfo {
    984         public static final int REPLY_ANY_EXCEPT_NO = 0;
    985         public static final int REPLY_YES_OR_MAYBE = 1;
    986         public static final int REPLY_YES = 2;
    987 
    988         public int userId = UserHandle.USER_NULL;  // USER_NULL = unspecified - use current user
    989         public String calendar;  // CalendarContract.Calendars.OWNER_ACCOUNT, or null for any
    990         public int reply;
    991 
    992         @Override
    993         public int hashCode() {
    994             return 0;
    995         }
    996 
    997         @Override
    998         public boolean equals(Object o) {
    999             if (!(o instanceof EventInfo)) return false;
   1000             final EventInfo other = (EventInfo) o;
   1001             return userId == other.userId
   1002                     && Objects.equals(calendar, other.calendar)
   1003                     && reply == other.reply;
   1004         }
   1005 
   1006         public EventInfo copy() {
   1007             final EventInfo rt = new EventInfo();
   1008             rt.userId = userId;
   1009             rt.calendar = calendar;
   1010             rt.reply = reply;
   1011             return rt;
   1012         }
   1013 
   1014         public static int resolveUserId(int userId) {
   1015             return userId == UserHandle.USER_NULL ? ActivityManager.getCurrentUser() : userId;
   1016         }
   1017     }
   1018 
   1019     // ==== End built-in system conditions ====
   1020 
   1021     private static int[] tryParseHourAndMinute(String value) {
   1022         if (TextUtils.isEmpty(value)) return null;
   1023         final int i = value.indexOf('.');
   1024         if (i < 1 || i >= value.length() - 1) return null;
   1025         final int hour = tryParseInt(value.substring(0, i), -1);
   1026         final int minute = tryParseInt(value.substring(i + 1), -1);
   1027         return isValidHour(hour) && isValidMinute(minute) ? new int[] { hour, minute } : null;
   1028     }
   1029 
   1030     private static int tryParseZenMode(String value, int defValue) {
   1031         final int rt = tryParseInt(value, defValue);
   1032         return Global.isValidZenMode(rt) ? rt : defValue;
   1033     }
   1034 
   1035     public static String newRuleId() {
   1036         return UUID.randomUUID().toString().replace("-", "");
   1037     }
   1038 
   1039     private static String getOwnerCaption(Context context, String owner) {
   1040         final PackageManager pm = context.getPackageManager();
   1041         try {
   1042             final ApplicationInfo info = pm.getApplicationInfo(owner, 0);
   1043             if (info != null) {
   1044                 final CharSequence seq = info.loadLabel(pm);
   1045                 if (seq != null) {
   1046                     final String str = seq.toString().trim();
   1047                     if (str.length() > 0) {
   1048                         return str;
   1049                     }
   1050                 }
   1051             }
   1052         } catch (Throwable e) {
   1053             Slog.w(TAG, "Error loading owner caption", e);
   1054         }
   1055         return "";
   1056     }
   1057 
   1058     public static String getConditionSummary(Context context, ZenModeConfig config,
   1059             int userHandle, boolean shortVersion) {
   1060         return getConditionLine(context, config, userHandle, false /*useLine1*/, shortVersion);
   1061     }
   1062 
   1063     private static String getConditionLine(Context context, ZenModeConfig config,
   1064             int userHandle, boolean useLine1, boolean shortVersion) {
   1065         if (config == null) return "";
   1066         String summary = "";
   1067         if (config.manualRule != null) {
   1068             final Uri id = config.manualRule.conditionId;
   1069             if (config.manualRule.enabler != null) {
   1070                 summary = getOwnerCaption(context, config.manualRule.enabler);
   1071             } else {
   1072                 if (id == null) {
   1073                     summary = context.getString(com.android.internal.R.string.zen_mode_forever);
   1074                 } else {
   1075                     final long time = tryParseCountdownConditionId(id);
   1076                     Condition c = config.manualRule.condition;
   1077                     if (time > 0) {
   1078                         final long now = System.currentTimeMillis();
   1079                         final long span = time - now;
   1080                         c = toTimeCondition(context, time, Math.round(span / (float) MINUTES_MS),
   1081                                 userHandle, shortVersion);
   1082                     }
   1083                     final String rt = c == null ? "" : useLine1 ? c.line1 : c.summary;
   1084                     summary = TextUtils.isEmpty(rt) ? "" : rt;
   1085                 }
   1086             }
   1087         }
   1088         for (ZenRule automaticRule : config.automaticRules.values()) {
   1089             if (automaticRule.isAutomaticActive()) {
   1090                 if (summary.isEmpty()) {
   1091                     summary = automaticRule.name;
   1092                 } else {
   1093                     summary = context.getResources()
   1094                             .getString(R.string.zen_mode_rule_name_combination, summary,
   1095                                     automaticRule.name);
   1096                 }
   1097 
   1098             }
   1099         }
   1100         return summary;
   1101     }
   1102 
   1103     public static class ZenRule implements Parcelable {
   1104         public boolean enabled;
   1105         public boolean snoozing;         // user manually disabled this instance
   1106         public String name;              // required for automatic
   1107         public int zenMode;
   1108         public Uri conditionId;          // required for automatic
   1109         public Condition condition;      // optional
   1110         public ComponentName component;  // optional
   1111         public String id;                // required for automatic (unique)
   1112         public long creationTime;        // required for automatic
   1113         public String enabler;          // package name, only used for manual rules.
   1114 
   1115         public ZenRule() { }
   1116 
   1117         public ZenRule(Parcel source) {
   1118             enabled = source.readInt() == 1;
   1119             snoozing = source.readInt() == 1;
   1120             if (source.readInt() == 1) {
   1121                 name = source.readString();
   1122             }
   1123             zenMode = source.readInt();
   1124             conditionId = source.readParcelable(null);
   1125             condition = source.readParcelable(null);
   1126             component = source.readParcelable(null);
   1127             if (source.readInt() == 1) {
   1128                 id = source.readString();
   1129             }
   1130             creationTime = source.readLong();
   1131             if (source.readInt() == 1) {
   1132                 enabler = source.readString();
   1133             }
   1134         }
   1135 
   1136         @Override
   1137         public int describeContents() {
   1138             return 0;
   1139         }
   1140 
   1141         @Override
   1142         public void writeToParcel(Parcel dest, int flags) {
   1143             dest.writeInt(enabled ? 1 : 0);
   1144             dest.writeInt(snoozing ? 1 : 0);
   1145             if (name != null) {
   1146                 dest.writeInt(1);
   1147                 dest.writeString(name);
   1148             } else {
   1149                 dest.writeInt(0);
   1150             }
   1151             dest.writeInt(zenMode);
   1152             dest.writeParcelable(conditionId, 0);
   1153             dest.writeParcelable(condition, 0);
   1154             dest.writeParcelable(component, 0);
   1155             if (id != null) {
   1156                 dest.writeInt(1);
   1157                 dest.writeString(id);
   1158             } else {
   1159                 dest.writeInt(0);
   1160             }
   1161             dest.writeLong(creationTime);
   1162             if (enabler != null) {
   1163                 dest.writeInt(1);
   1164                 dest.writeString(enabler);
   1165             } else {
   1166                 dest.writeInt(0);
   1167             }
   1168         }
   1169 
   1170         @Override
   1171         public String toString() {
   1172             return new StringBuilder(ZenRule.class.getSimpleName()).append('[')
   1173                     .append("enabled=").append(enabled)
   1174                     .append(",snoozing=").append(snoozing)
   1175                     .append(",name=").append(name)
   1176                     .append(",zenMode=").append(Global.zenModeToString(zenMode))
   1177                     .append(",conditionId=").append(conditionId)
   1178                     .append(",condition=").append(condition)
   1179                     .append(",component=").append(component)
   1180                     .append(",id=").append(id)
   1181                     .append(",creationTime=").append(creationTime)
   1182                     .append(",enabler=").append(enabler)
   1183                     .append(']').toString();
   1184         }
   1185 
   1186         private static void appendDiff(Diff d, String item, ZenRule from, ZenRule to) {
   1187             if (d == null) return;
   1188             if (from == null) {
   1189                 if (to != null) {
   1190                     d.addLine(item, "insert");
   1191                 }
   1192                 return;
   1193             }
   1194             from.appendDiff(d, item, to);
   1195         }
   1196 
   1197         private void appendDiff(Diff d, String item, ZenRule to) {
   1198             if (to == null) {
   1199                 d.addLine(item, "delete");
   1200                 return;
   1201             }
   1202             if (enabled != to.enabled) {
   1203                 d.addLine(item, "enabled", enabled, to.enabled);
   1204             }
   1205             if (snoozing != to.snoozing) {
   1206                 d.addLine(item, "snoozing", snoozing, to.snoozing);
   1207             }
   1208             if (!Objects.equals(name, to.name)) {
   1209                 d.addLine(item, "name", name, to.name);
   1210             }
   1211             if (zenMode != to.zenMode) {
   1212                 d.addLine(item, "zenMode", zenMode, to.zenMode);
   1213             }
   1214             if (!Objects.equals(conditionId, to.conditionId)) {
   1215                 d.addLine(item, "conditionId", conditionId, to.conditionId);
   1216             }
   1217             if (!Objects.equals(condition, to.condition)) {
   1218                 d.addLine(item, "condition", condition, to.condition);
   1219             }
   1220             if (!Objects.equals(component, to.component)) {
   1221                 d.addLine(item, "component", component, to.component);
   1222             }
   1223             if (!Objects.equals(id, to.id)) {
   1224                 d.addLine(item, "id", id, to.id);
   1225             }
   1226             if (creationTime != to.creationTime) {
   1227                 d.addLine(item, "creationTime", creationTime, to.creationTime);
   1228             }
   1229             if (enabler != to.enabler) {
   1230                 d.addLine(item, "enabler", enabler, to.enabler);
   1231             }
   1232         }
   1233 
   1234         @Override
   1235         public boolean equals(Object o) {
   1236             if (!(o instanceof ZenRule)) return false;
   1237             if (o == this) return true;
   1238             final ZenRule other = (ZenRule) o;
   1239             return other.enabled == enabled
   1240                     && other.snoozing == snoozing
   1241                     && Objects.equals(other.name, name)
   1242                     && other.zenMode == zenMode
   1243                     && Objects.equals(other.conditionId, conditionId)
   1244                     && Objects.equals(other.condition, condition)
   1245                     && Objects.equals(other.component, component)
   1246                     && Objects.equals(other.id, id)
   1247                     && other.creationTime == creationTime
   1248                     && Objects.equals(other.enabler, enabler);
   1249         }
   1250 
   1251         @Override
   1252         public int hashCode() {
   1253             return Objects.hash(enabled, snoozing, name, zenMode, conditionId, condition,
   1254                     component, id, creationTime, enabler);
   1255         }
   1256 
   1257         public boolean isAutomaticActive() {
   1258             return enabled && !snoozing && component != null && isTrueOrUnknown();
   1259         }
   1260 
   1261         public boolean isTrueOrUnknown() {
   1262             return condition != null && (condition.state == Condition.STATE_TRUE
   1263                     || condition.state == Condition.STATE_UNKNOWN);
   1264         }
   1265 
   1266         public static final Parcelable.Creator<ZenRule> CREATOR
   1267                 = new Parcelable.Creator<ZenRule>() {
   1268             @Override
   1269             public ZenRule createFromParcel(Parcel source) {
   1270                 return new ZenRule(source);
   1271             }
   1272             @Override
   1273             public ZenRule[] newArray(int size) {
   1274                 return new ZenRule[size];
   1275             }
   1276         };
   1277     }
   1278 
   1279     public static class Diff {
   1280         private final ArrayList<String> lines = new ArrayList<>();
   1281 
   1282         @Override
   1283         public String toString() {
   1284             final StringBuilder sb = new StringBuilder("Diff[");
   1285             final int N = lines.size();
   1286             for (int i = 0; i < N; i++) {
   1287                 if (i > 0) {
   1288                     sb.append(',');
   1289                 }
   1290                 sb.append(lines.get(i));
   1291             }
   1292             return sb.append(']').toString();
   1293         }
   1294 
   1295         private Diff addLine(String item, String action) {
   1296             lines.add(item + ":" + action);
   1297             return this;
   1298         }
   1299 
   1300         public Diff addLine(String item, String subitem, Object from, Object to) {
   1301             return addLine(item + "." + subitem, from, to);
   1302         }
   1303 
   1304         public Diff addLine(String item, Object from, Object to) {
   1305             return addLine(item, from + "->" + to);
   1306         }
   1307     }
   1308 
   1309 }
   1310