ZenModeConfig.java revision 056c519df1dfb8fdc57daddfdf09bc0e1ffddac4
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.os.Parcel; 20import android.os.Parcelable; 21import android.text.TextUtils; 22 23import org.xmlpull.v1.XmlPullParser; 24import org.xmlpull.v1.XmlPullParserException; 25import org.xmlpull.v1.XmlSerializer; 26 27import java.io.IOException; 28import java.util.Objects; 29 30/** 31 * Persisted configuration for zen mode. 32 * 33 * @hide 34 */ 35public class ZenModeConfig implements Parcelable { 36 37 public static final String SLEEP_MODE_NIGHTS = "nights"; 38 public static final String SLEEP_MODE_WEEKNIGHTS = "weeknights"; 39 40 private static final int XML_VERSION = 1; 41 private static final String ZEN_TAG = "zen"; 42 private static final String ZEN_ATT_VERSION = "version"; 43 private static final String ALLOW_TAG = "allow"; 44 private static final String ALLOW_ATT_CALLS = "calls"; 45 private static final String ALLOW_ATT_MESSAGES = "messages"; 46 private static final String SLEEP_TAG = "sleep"; 47 private static final String SLEEP_ATT_MODE = "mode"; 48 49 private static final String SLEEP_ATT_START_HR = "startHour"; 50 private static final String SLEEP_ATT_START_MIN = "startMin"; 51 private static final String SLEEP_ATT_END_HR = "endHour"; 52 private static final String SLEEP_ATT_END_MIN = "endMin"; 53 54 public boolean allowCalls; 55 public boolean allowMessages; 56 57 public String sleepMode; 58 public int sleepStartHour; 59 public int sleepStartMinute; 60 public int sleepEndHour; 61 public int sleepEndMinute; 62 63 public ZenModeConfig() { } 64 65 public ZenModeConfig(Parcel source) { 66 allowCalls = source.readInt() == 1; 67 allowMessages = source.readInt() == 1; 68 if (source.readInt() == 1) { 69 sleepMode = source.readString(); 70 } 71 sleepStartHour = source.readInt(); 72 sleepStartMinute = source.readInt(); 73 sleepEndHour = source.readInt(); 74 sleepEndMinute = source.readInt(); 75 } 76 77 @Override 78 public void writeToParcel(Parcel dest, int flags) { 79 dest.writeInt(allowCalls ? 1 : 0); 80 dest.writeInt(allowMessages ? 1 : 0); 81 if (sleepMode != null) { 82 dest.writeInt(1); 83 dest.writeString(sleepMode); 84 } else { 85 dest.writeInt(0); 86 } 87 dest.writeInt(sleepStartHour); 88 dest.writeInt(sleepStartMinute); 89 dest.writeInt(sleepEndHour); 90 dest.writeInt(sleepEndMinute); 91 } 92 93 @Override 94 public String toString() { 95 return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[') 96 .append("allowCalls=").append(allowCalls) 97 .append(",allowMessages=").append(allowMessages) 98 .append(",sleepMode=").append(sleepMode) 99 .append(",sleepStart=").append(sleepStartHour).append('.').append(sleepStartMinute) 100 .append(",sleepEnd=").append(sleepEndHour).append('.').append(sleepEndMinute) 101 .append(']').toString(); 102 } 103 104 @Override 105 public boolean equals(Object o) { 106 if (!(o instanceof ZenModeConfig)) return false; 107 if (o == this) return true; 108 final ZenModeConfig other = (ZenModeConfig) o; 109 return other.allowCalls == allowCalls 110 && other.allowMessages == allowMessages 111 && Objects.equals(other.sleepMode, sleepMode) 112 && other.sleepStartHour == sleepStartHour 113 && other.sleepStartMinute == sleepStartMinute 114 && other.sleepEndHour == sleepEndHour 115 && other.sleepEndMinute == sleepEndMinute; 116 } 117 118 @Override 119 public int hashCode() { 120 return Objects.hash(allowCalls, allowMessages, sleepMode, sleepStartHour, 121 sleepStartMinute, sleepEndHour, sleepEndMinute); 122 } 123 124 public boolean isValid() { 125 return isValidHour(sleepStartHour) && isValidMinute(sleepStartMinute) 126 && isValidHour(sleepEndHour) && isValidMinute(sleepEndMinute) 127 && (sleepMode == null || sleepMode.equals(SLEEP_MODE_NIGHTS) 128 || sleepMode.equals(SLEEP_MODE_WEEKNIGHTS)); 129 } 130 131 public static ZenModeConfig readXml(XmlPullParser parser) 132 throws XmlPullParserException, IOException { 133 int type = parser.getEventType(); 134 if (type != XmlPullParser.START_TAG) return null; 135 String tag = parser.getName(); 136 if (!ZEN_TAG.equals(tag)) return null; 137 final ZenModeConfig rt = new ZenModeConfig(); 138 final int version = Integer.parseInt(parser.getAttributeValue(null, ZEN_ATT_VERSION)); 139 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { 140 tag = parser.getName(); 141 if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) return rt; 142 if (type == XmlPullParser.START_TAG) { 143 if (ALLOW_TAG.equals(tag)) { 144 rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false); 145 rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false); 146 } else if (SLEEP_TAG.equals(tag)) { 147 final String mode = parser.getAttributeValue(null, SLEEP_ATT_MODE); 148 rt.sleepMode = (SLEEP_MODE_NIGHTS.equals(mode) 149 || SLEEP_MODE_WEEKNIGHTS.equals(mode)) ? mode : null; 150 final int startHour = safeInt(parser, SLEEP_ATT_START_HR, 0); 151 final int startMinute = safeInt(parser, SLEEP_ATT_START_MIN, 0); 152 final int endHour = safeInt(parser, SLEEP_ATT_END_HR, 0); 153 final int endMinute = safeInt(parser, SLEEP_ATT_END_MIN, 0); 154 rt.sleepStartHour = isValidHour(startHour) ? startHour : 0; 155 rt.sleepStartMinute = isValidMinute(startMinute) ? startMinute : 0; 156 rt.sleepEndHour = isValidHour(endHour) ? endHour : 0; 157 rt.sleepEndMinute = isValidMinute(endMinute) ? endMinute : 0; 158 } 159 } 160 } 161 return rt; 162 } 163 164 public void writeXml(XmlSerializer out) throws IOException { 165 out.startTag(null, ZEN_TAG); 166 out.attribute(null, ZEN_ATT_VERSION, Integer.toString(XML_VERSION)); 167 168 out.startTag(null, ALLOW_TAG); 169 out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls)); 170 out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages)); 171 out.endTag(null, ALLOW_TAG); 172 173 out.startTag(null, SLEEP_TAG); 174 if (sleepMode != null) { 175 out.attribute(null, SLEEP_ATT_MODE, sleepMode); 176 } 177 out.attribute(null, SLEEP_ATT_START_HR, Integer.toString(sleepStartHour)); 178 out.attribute(null, SLEEP_ATT_START_MIN, Integer.toString(sleepStartMinute)); 179 out.attribute(null, SLEEP_ATT_END_HR, Integer.toString(sleepEndHour)); 180 out.attribute(null, SLEEP_ATT_END_MIN, Integer.toString(sleepEndMinute)); 181 out.endTag(null, SLEEP_TAG); 182 183 out.endTag(null, ZEN_TAG); 184 } 185 186 public static boolean isValidHour(int val) { 187 return val >= 0 && val < 24; 188 } 189 190 public static boolean isValidMinute(int val) { 191 return val >= 0 && val < 60; 192 } 193 194 private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) { 195 final String val = parser.getAttributeValue(null, att); 196 if (TextUtils.isEmpty(val)) return defValue; 197 return Boolean.valueOf(val); 198 } 199 200 private static int safeInt(XmlPullParser parser, String att, int defValue) { 201 final String val = parser.getAttributeValue(null, att); 202 if (TextUtils.isEmpty(val)) return defValue; 203 return Integer.valueOf(val); 204 } 205 206 @Override 207 public int describeContents() { 208 return 0; 209 } 210 211 public ZenModeConfig copy() { 212 final Parcel parcel = Parcel.obtain(); 213 try { 214 writeToParcel(parcel, 0); 215 parcel.setDataPosition(0); 216 return new ZenModeConfig(parcel); 217 } finally { 218 parcel.recycle(); 219 } 220 } 221 222 public static final Parcelable.Creator<ZenModeConfig> CREATOR 223 = new Parcelable.Creator<ZenModeConfig>() { 224 @Override 225 public ZenModeConfig createFromParcel(Parcel source) { 226 return new ZenModeConfig(source); 227 } 228 229 @Override 230 public ZenModeConfig[] newArray(int size) { 231 return new ZenModeConfig[size]; 232 } 233 }; 234} 235