Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package android.app; |
| 17 | |
| 18 | import android.annotation.SystemApi; |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 19 | import android.annotation.TestApi; |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 21 | import android.content.Intent; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 24 | import android.text.TextUtils; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 25 | import android.util.proto.ProtoOutputStream; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 26 | |
| 27 | import org.json.JSONException; |
| 28 | import org.json.JSONObject; |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 29 | import org.xmlpull.v1.XmlPullParser; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 30 | import org.xmlpull.v1.XmlSerializer; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.util.ArrayList; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 34 | import java.util.List; |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 35 | import java.util.Objects; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * A grouping of related notification channels. e.g., channels that all belong to a single account. |
| 39 | */ |
| 40 | public final class NotificationChannelGroup implements Parcelable { |
| 41 | |
Julia Reynolds | 2c891c9 | 2017-03-17 14:23:47 -0400 | [diff] [blame] | 42 | /** |
| 43 | * The maximum length for text fields in a NotificationChannelGroup. Fields will be truncated at |
| 44 | * this limit. |
| 45 | */ |
| 46 | private static final int MAX_TEXT_LENGTH = 1000; |
| 47 | |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 48 | private static final String TAG_GROUP = "channelGroup"; |
| 49 | private static final String ATT_NAME = "name"; |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 50 | private static final String ATT_DESC = "desc"; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 51 | private static final String ATT_ID = "id"; |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 52 | private static final String ATT_BLOCKED = "blocked"; |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 53 | private static final String ATT_USER_LOCKED = "locked"; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 54 | |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 55 | /** |
| 56 | * @hide |
| 57 | */ |
| 58 | public static final int USER_LOCKED_BLOCKED_STATE = 0x00000001; |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * @see #getId() |
| 62 | */ |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 63 | @UnsupportedAppUsage |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 64 | private final String mId; |
| 65 | private CharSequence mName; |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 66 | private String mDescription; |
| 67 | private boolean mBlocked; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 68 | private List<NotificationChannel> mChannels = new ArrayList<>(); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 69 | // Bitwise representation of fields that have been changed by the user |
| 70 | private int mUserLockedFields; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 71 | |
| 72 | /** |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 73 | * Creates a notification channel group. |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 74 | * |
Julia Reynolds | 2c891c9 | 2017-03-17 14:23:47 -0400 | [diff] [blame] | 75 | * @param id The id of the group. Must be unique per package. the value may be truncated if |
| 76 | * it is too long. |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 77 | * @param name The user visible name of the group. You can rename this group when the system |
| 78 | * locale changes by listening for the {@link Intent#ACTION_LOCALE_CHANGED} |
Julia Reynolds | 2c891c9 | 2017-03-17 14:23:47 -0400 | [diff] [blame] | 79 | * broadcast. <p>The recommended maximum length is 40 characters; the value may be |
| 80 | * truncated if it is too long. |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 81 | */ |
| 82 | public NotificationChannelGroup(String id, CharSequence name) { |
Julia Reynolds | 2c891c9 | 2017-03-17 14:23:47 -0400 | [diff] [blame] | 83 | this.mId = getTrimmedString(id); |
| 84 | this.mName = name != null ? getTrimmedString(name.toString()) : null; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 87 | /** |
| 88 | * @hide |
| 89 | */ |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 90 | protected NotificationChannelGroup(Parcel in) { |
| 91 | if (in.readByte() != 0) { |
| 92 | mId = in.readString(); |
| 93 | } else { |
| 94 | mId = null; |
| 95 | } |
| 96 | mName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 97 | if (in.readByte() != 0) { |
| 98 | mDescription = in.readString(); |
| 99 | } else { |
| 100 | mDescription = null; |
| 101 | } |
Julia Reynolds | f02562a | 2017-01-26 13:33:56 -0500 | [diff] [blame] | 102 | in.readParcelableList(mChannels, NotificationChannel.class.getClassLoader()); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 103 | mBlocked = in.readBoolean(); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 104 | mUserLockedFields = in.readInt(); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Julia Reynolds | 2c891c9 | 2017-03-17 14:23:47 -0400 | [diff] [blame] | 107 | private String getTrimmedString(String input) { |
| 108 | if (input != null && input.length() > MAX_TEXT_LENGTH) { |
| 109 | return input.substring(0, MAX_TEXT_LENGTH); |
| 110 | } |
| 111 | return input; |
| 112 | } |
| 113 | |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 114 | @Override |
| 115 | public void writeToParcel(Parcel dest, int flags) { |
| 116 | if (mId != null) { |
| 117 | dest.writeByte((byte) 1); |
| 118 | dest.writeString(mId); |
| 119 | } else { |
| 120 | dest.writeByte((byte) 0); |
| 121 | } |
| 122 | TextUtils.writeToParcel(mName, dest, flags); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 123 | if (mDescription != null) { |
| 124 | dest.writeByte((byte) 1); |
| 125 | dest.writeString(mDescription); |
| 126 | } else { |
| 127 | dest.writeByte((byte) 0); |
| 128 | } |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 129 | dest.writeParcelableList(mChannels, flags); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 130 | dest.writeBoolean(mBlocked); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 131 | dest.writeInt(mUserLockedFields); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 135 | * Returns the id of this group. |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 136 | */ |
| 137 | public String getId() { |
| 138 | return mId; |
| 139 | } |
| 140 | |
| 141 | /** |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 142 | * Returns the user visible name of this group. |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 143 | */ |
| 144 | public CharSequence getName() { |
| 145 | return mName; |
| 146 | } |
| 147 | |
Julia Reynolds | 399d9bf | 2017-08-11 12:52:14 -0400 | [diff] [blame] | 148 | /** |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 149 | * Returns the user visible description of this group. |
| 150 | */ |
| 151 | public String getDescription() { |
| 152 | return mDescription; |
| 153 | } |
| 154 | |
| 155 | /** |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 156 | * Returns the list of channels that belong to this group |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 157 | */ |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 158 | public List<NotificationChannel> getChannels() { |
| 159 | return mChannels; |
| 160 | } |
| 161 | |
| 162 | /** |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 163 | * Returns whether or not notifications posted to {@link NotificationChannel channels} belonging |
Julia Reynolds | 8d2b053 | 2018-03-29 15:44:13 -0400 | [diff] [blame] | 164 | * to this group are blocked. This value is independent of |
| 165 | * {@link NotificationManager#areNotificationsEnabled()} and |
| 166 | * {@link NotificationChannel#getImportance()}. |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 167 | */ |
| 168 | public boolean isBlocked() { |
| 169 | return mBlocked; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Sets the user visible description of this group. |
| 174 | * |
| 175 | * <p>The recommended maximum length is 300 characters; the value may be truncated if it is too |
| 176 | * long. |
| 177 | */ |
| 178 | public void setDescription(String description) { |
| 179 | mDescription = getTrimmedString(description); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @hide |
| 184 | */ |
| 185 | @TestApi |
| 186 | public void setBlocked(boolean blocked) { |
| 187 | mBlocked = blocked; |
| 188 | } |
| 189 | |
| 190 | /** |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 191 | * @hide |
| 192 | */ |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 193 | public void addChannel(NotificationChannel channel) { |
| 194 | mChannels.add(channel); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @hide |
| 199 | */ |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 200 | public void setChannels(List<NotificationChannel> channels) { |
| 201 | mChannels = channels; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @hide |
| 206 | */ |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 207 | @TestApi |
| 208 | public void lockFields(int field) { |
| 209 | mUserLockedFields |= field; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @hide |
| 214 | */ |
| 215 | public void unlockFields(int field) { |
| 216 | mUserLockedFields &= ~field; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @hide |
| 221 | */ |
| 222 | @TestApi |
| 223 | public int getUserLockedFields() { |
| 224 | return mUserLockedFields; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @hide |
| 229 | */ |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 230 | public void populateFromXml(XmlPullParser parser) { |
| 231 | // Name, id, and importance are set in the constructor. |
| 232 | setDescription(parser.getAttributeValue(null, ATT_DESC)); |
| 233 | setBlocked(safeBool(parser, ATT_BLOCKED, false)); |
| 234 | } |
| 235 | |
| 236 | private static boolean safeBool(XmlPullParser parser, String att, boolean defValue) { |
| 237 | final String value = parser.getAttributeValue(null, att); |
| 238 | if (TextUtils.isEmpty(value)) return defValue; |
| 239 | return Boolean.parseBoolean(value); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * @hide |
| 244 | */ |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 245 | public void writeXml(XmlSerializer out) throws IOException { |
| 246 | out.startTag(null, TAG_GROUP); |
| 247 | |
| 248 | out.attribute(null, ATT_ID, getId()); |
Julia Reynolds | d528684 | 2017-03-02 15:37:10 -0500 | [diff] [blame] | 249 | if (getName() != null) { |
| 250 | out.attribute(null, ATT_NAME, getName().toString()); |
| 251 | } |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 252 | if (getDescription() != null) { |
| 253 | out.attribute(null, ATT_DESC, getDescription().toString()); |
| 254 | } |
| 255 | out.attribute(null, ATT_BLOCKED, Boolean.toString(isBlocked())); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 256 | out.attribute(null, ATT_USER_LOCKED, Integer.toString(mUserLockedFields)); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 257 | |
| 258 | out.endTag(null, TAG_GROUP); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * @hide |
| 263 | */ |
| 264 | @SystemApi |
| 265 | public JSONObject toJson() throws JSONException { |
| 266 | JSONObject record = new JSONObject(); |
| 267 | record.put(ATT_ID, getId()); |
| 268 | record.put(ATT_NAME, getName()); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 269 | record.put(ATT_DESC, getDescription()); |
| 270 | record.put(ATT_BLOCKED, isBlocked()); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 271 | record.put(ATT_USER_LOCKED, mUserLockedFields); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 272 | return record; |
| 273 | } |
| 274 | |
| 275 | public static final Creator<NotificationChannelGroup> CREATOR = |
| 276 | new Creator<NotificationChannelGroup>() { |
| 277 | @Override |
| 278 | public NotificationChannelGroup createFromParcel(Parcel in) { |
| 279 | return new NotificationChannelGroup(in); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public NotificationChannelGroup[] newArray(int size) { |
| 284 | return new NotificationChannelGroup[size]; |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | @Override |
| 289 | public int describeContents() { |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public boolean equals(Object o) { |
| 295 | if (this == o) return true; |
| 296 | if (o == null || getClass() != o.getClass()) return false; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 297 | NotificationChannelGroup that = (NotificationChannelGroup) o; |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 298 | return isBlocked() == that.isBlocked() && |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 299 | mUserLockedFields == that.mUserLockedFields && |
| 300 | Objects.equals(getId(), that.getId()) && |
| 301 | Objects.equals(getName(), that.getName()) && |
| 302 | Objects.equals(getDescription(), that.getDescription()) && |
| 303 | Objects.equals(getChannels(), that.getChannels()); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 307 | public int hashCode() { |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 308 | return Objects.hash(getId(), getName(), getDescription(), isBlocked(), getChannels(), |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 309 | mUserLockedFields); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 313 | public NotificationChannelGroup clone() { |
| 314 | NotificationChannelGroup cloned = new NotificationChannelGroup(getId(), getName()); |
| 315 | cloned.setDescription(getDescription()); |
| 316 | cloned.setBlocked(isBlocked()); |
| 317 | cloned.setChannels(getChannels()); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 318 | cloned.lockFields(mUserLockedFields); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 319 | return cloned; |
| 320 | } |
| 321 | |
| 322 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 323 | public String toString() { |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 324 | return "NotificationChannelGroup{" |
| 325 | + "mId='" + mId + '\'' |
| 326 | + ", mName=" + mName |
| 327 | + ", mDescription=" + (!TextUtils.isEmpty(mDescription) ? "hasDescription " : "") |
| 328 | + ", mBlocked=" + mBlocked |
| 329 | + ", mChannels=" + mChannels |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 330 | + ", mUserLockedFields=" + mUserLockedFields |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 331 | + '}'; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 332 | } |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 333 | |
| 334 | /** @hide */ |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 335 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 336 | final long token = proto.start(fieldId); |
| 337 | |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 338 | proto.write(NotificationChannelGroupProto.ID, mId); |
| 339 | proto.write(NotificationChannelGroupProto.NAME, mName.toString()); |
| 340 | proto.write(NotificationChannelGroupProto.DESCRIPTION, mDescription); |
| 341 | proto.write(NotificationChannelGroupProto.IS_BLOCKED, mBlocked); |
| 342 | for (NotificationChannel channel : mChannels) { |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 343 | channel.writeToProto(proto, NotificationChannelGroupProto.CHANNELS); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 344 | } |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 345 | proto.end(token); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 346 | } |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 347 | } |