package android.app;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.proto.ProtoOutputStream;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public final class NotificationChannelGroup implements Parcelable {
private static final int MAX_TEXT_LENGTH = 1000;
private static final String TAG_GROUP = "channelGroup";
private static final String ATT_NAME = "name";
private static final String ATT_DESC = "desc";
private static final String ATT_ID = "id";
private static final String ATT_BLOCKED = "blocked";
private final String mId;
private CharSequence mName;
private String mDescription;
private boolean mBlocked;
private List<NotificationChannel> mChannels = new ArrayList<>();
public NotificationChannelGroup(String id, CharSequence name) {
this.mId = getTrimmedString(id);
this.mName = name != null ? getTrimmedString(name.toString()) : null;
}
protected NotificationChannelGroup(Parcel in) {
if (in.readByte() != 0) {
mId = in.readString();
} else {
mId = null;
}
mName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
if (in.readByte() != 0) {
mDescription = in.readString();
} else {
mDescription = null;
}
in.readParcelableList(mChannels, NotificationChannel.class.getClassLoader());
mBlocked = in.readBoolean();
}
private String getTrimmedString(String input) {
if (input != null && input.length() > MAX_TEXT_LENGTH) {
return input.substring(0, MAX_TEXT_LENGTH);
}
return input;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
if (mId != null) {
dest.writeByte((byte) 1);
dest.writeString(mId);
} else {
dest.writeByte((byte) 0);
}
TextUtils.writeToParcel(mName, dest, flags);
if (mDescription != null) {
dest.writeByte((byte) 1);
dest.writeString(mDescription);
} else {
dest.writeByte((byte) 0);
}
dest.writeParcelableList(mChannels, flags);
dest.writeBoolean(mBlocked);
}
public String getId() {
return mId;
}
public CharSequence getName() {
return mName;
}
public String getDescription() {
return mDescription;
}
public List<NotificationChannel> getChannels() {
return mChannels;
}
public boolean isBlocked() {
return mBlocked;
}
public void setDescription(String description) {
mDescription = getTrimmedString(description);
}
@TestApi
public void setBlocked(boolean blocked) {
mBlocked = blocked;
}
public void addChannel(NotificationChannel channel) {
mChannels.add(channel);
}
public void setChannels(List<NotificationChannel> channels) {
mChannels = channels;
}
public void populateFromXml(XmlPullParser parser) {
setDescription(parser.getAttributeValue(null, ATT_DESC));
setBlocked(safeBool(parser, ATT_BLOCKED, false));
}
private static boolean safeBool(XmlPullParser parser, String att, boolean defValue) {
final String value = parser.getAttributeValue(null, att);
if (TextUtils.isEmpty(value)) return defValue;
return Boolean.parseBoolean(value);
}
public void writeXml(XmlSerializer out) throws IOException {
out.startTag(null, TAG_GROUP);
out.attribute(null, ATT_ID, getId());
if (getName() != null) {
out.attribute(null, ATT_NAME, getName().toString());
}
if (getDescription() != null) {
out.attribute(null, ATT_DESC, getDescription().toString());
}
out.attribute(null, ATT_BLOCKED, Boolean.toString(isBlocked()));
out.endTag(null, TAG_GROUP);
}
@SystemApi
public JSONObject toJson() throws JSONException {
JSONObject record = new JSONObject();
record.put(ATT_ID, getId());
record.put(ATT_NAME, getName());
record.put(ATT_DESC, getDescription());
record.put(ATT_BLOCKED, isBlocked());
return record;
}
public static final Creator<NotificationChannelGroup> CREATOR =
new Creator<NotificationChannelGroup>() {
@Override
public NotificationChannelGroup createFromParcel(Parcel in) {
return new NotificationChannelGroup(in);
}
@Override
public NotificationChannelGroup[] newArray(int size) {
return new NotificationChannelGroup[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NotificationChannelGroup that = (NotificationChannelGroup) o;
if (isBlocked() != that.isBlocked()) return false;
if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false;
if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null) {
return false;
}
if (getDescription() != null ? !getDescription().equals(that.getDescription())
: that.getDescription() != null) {
return false;
}
return getChannels() != null ? getChannels().equals(that.getChannels())
: that.getChannels() == null;
}
@Override
public int hashCode() {
int result = getId() != null ? getId().hashCode() : 0;
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
result = 31 * result + (getDescription() != null ? getDescription().hashCode() : 0);
result = 31 * result + (isBlocked() ? 1 : 0);
result = 31 * result + (getChannels() != null ? getChannels().hashCode() : 0);
return result;
}
@Override
public NotificationChannelGroup clone() {
NotificationChannelGroup cloned = new NotificationChannelGroup(getId(), getName());
cloned.setDescription(getDescription());
cloned.setBlocked(isBlocked());
cloned.setChannels(getChannels());
return cloned;
}
@Override
public String toString() {
return "NotificationChannelGroup{"
+ "mId='" + mId + '\''
+ ", mName=" + mName
+ ", mDescription=" + (!TextUtils.isEmpty(mDescription) ? "hasDescription " : "")
+ ", mBlocked=" + mBlocked
+ ", mChannels=" + mChannels
+ '}';
}
public void writeToProto(ProtoOutputStream proto, long fieldId) {
final long token = proto.start(fieldId);
proto.write(NotificationChannelGroupProto.ID, mId);
proto.write(NotificationChannelGroupProto.NAME, mName.toString());
proto.write(NotificationChannelGroupProto.DESCRIPTION, mDescription);
proto.write(NotificationChannelGroupProto.IS_BLOCKED, mBlocked);
for (NotificationChannel channel : mChannels) {
channel.writeToProto(proto, NotificationChannelGroupProto.CHANNELS);
}
proto.end(token);
}
}