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