1 /* 2 * Copyright (C) 2020 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 17 package com.android.ims.rcs.uce.presence.pidfparser.pidf; 18 19 import com.android.ims.rcs.uce.presence.pidfparser.ElementBase; 20 import com.android.ims.rcs.uce.presence.pidfparser.capabilities.ServiceCaps; 21 import com.android.ims.rcs.uce.presence.pidfparser.omapres.ServiceDescription; 22 23 import org.xmlpull.v1.XmlPullParser; 24 import org.xmlpull.v1.XmlPullParserException; 25 import org.xmlpull.v1.XmlSerializer; 26 27 import java.io.IOException; 28 import java.util.ArrayList; 29 import java.util.Collections; 30 import java.util.List; 31 32 /** 33 * The "tuple" element of the pidf. 34 */ 35 public class Tuple extends ElementBase { 36 /** 37 * The tuple element consists the following elements: 38 * 1: one "status" element 39 * 2: any number of optional extension elements 40 * 3: an optional "contact" element 41 * 4: any number of optional "note" elements 42 * 5: an optional "timestamp" element 43 */ 44 45 /** The name of this element */ 46 public static final String ELEMENT_NAME = "tuple"; 47 48 private static final String ATTRIBUTE_NAME_TUPLE_ID = "id"; 49 50 private static long sTupleId = 0; 51 52 private static final Object LOCK = new Object(); 53 54 private String mId; 55 private Status mStatus; 56 private ServiceDescription mServiceDescription; 57 private ServiceCaps mServiceCaps; 58 private Contact mContact; 59 private List<Note> mNoteList = new ArrayList<>(); 60 private Timestamp mTimestamp; 61 Tuple()62 public Tuple() { 63 mId = getTupleId(); 64 } 65 66 @Override initNamespace()67 protected String initNamespace() { 68 return PidfConstant.NAMESPACE; 69 } 70 71 @Override initElementName()72 protected String initElementName() { 73 return ELEMENT_NAME; 74 } 75 setStatus(Status status)76 public void setStatus(Status status) { 77 mStatus = status; 78 } 79 getStatus()80 public Status getStatus() { 81 return mStatus; 82 } 83 setServiceDescription(ServiceDescription servDescription)84 public void setServiceDescription(ServiceDescription servDescription) { 85 mServiceDescription = servDescription; 86 } 87 getServiceDescription()88 public ServiceDescription getServiceDescription() { 89 return mServiceDescription; 90 } 91 setServiceCaps(ServiceCaps serviceCaps)92 public void setServiceCaps(ServiceCaps serviceCaps) { 93 mServiceCaps = serviceCaps; 94 } 95 getServiceCaps()96 public ServiceCaps getServiceCaps() { 97 return mServiceCaps; 98 } 99 setContact(Contact contact)100 public void setContact(Contact contact) { 101 mContact = contact; 102 } 103 getContact()104 public Contact getContact() { 105 return mContact; 106 } 107 addNote(Note note)108 public void addNote(Note note) { 109 mNoteList.add(note); 110 } 111 getNoteList()112 public List<Note> getNoteList() { 113 return Collections.unmodifiableList(mNoteList); 114 } 115 setTimestamp(Timestamp timestamp)116 public void setTimestamp(Timestamp timestamp) { 117 mTimestamp = timestamp; 118 } 119 getTimestamp()120 public Timestamp getTimestamp() { 121 return mTimestamp; 122 } 123 124 @Override serialize(XmlSerializer serializer)125 public void serialize(XmlSerializer serializer) throws IOException { 126 String namespace = getNamespace(); 127 String elementName = getElementName(); 128 129 serializer.startTag(namespace, elementName); 130 // id attribute 131 serializer.attribute(XmlPullParser.NO_NAMESPACE, ATTRIBUTE_NAME_TUPLE_ID, mId); 132 133 // status element 134 mStatus.serialize(serializer); 135 136 // Service description 137 if (mServiceDescription != null) { 138 mServiceDescription.serialize(serializer); 139 } 140 141 // Service capabilities 142 if (mServiceCaps != null) { 143 mServiceCaps.serialize(serializer); 144 } 145 146 // contact element 147 if (mContact != null) { 148 mContact.serialize(serializer); 149 } 150 151 // note element 152 for (Note note: mNoteList) { 153 note.serialize(serializer); 154 } 155 156 // Timestamp 157 if (mTimestamp != null) { 158 mTimestamp.serialize(serializer); 159 } 160 serializer.endTag(namespace, elementName); 161 } 162 163 @Override parse(XmlPullParser parser)164 public void parse(XmlPullParser parser) throws IOException, XmlPullParserException { 165 String namespace = parser.getNamespace(); 166 String name = parser.getName(); 167 168 if (!verifyParsingElement(namespace, name)) { 169 throw new XmlPullParserException("Incorrect element: " + namespace + ", " + name); 170 } 171 172 // id attribute 173 mId = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, ATTRIBUTE_NAME_TUPLE_ID); 174 175 // Move to the next event. 176 int eventType = parser.next(); 177 178 while(!(eventType == XmlPullParser.END_TAG 179 && getNamespace().equals(parser.getNamespace()) 180 && getElementName().equals(parser.getName()))) { 181 182 if (eventType == XmlPullParser.START_TAG) { 183 String tagName = parser.getName(); 184 185 if (Status.ELEMENT_NAME.equals(tagName)) { 186 Status status = new Status(); 187 status.parse(parser); 188 mStatus = status; 189 } else if (ServiceDescription.ELEMENT_NAME.equals(tagName)) { 190 ServiceDescription serviceDescription = new ServiceDescription(); 191 serviceDescription.parse(parser); 192 mServiceDescription = serviceDescription; 193 } else if (ServiceCaps.ELEMENT_NAME.equals(tagName)) { 194 ServiceCaps serviceCaps = new ServiceCaps(); 195 serviceCaps.parse(parser); 196 mServiceCaps = serviceCaps; 197 } else if (Contact.ELEMENT_NAME.equals(tagName)) { 198 Contact contact = new Contact(); 199 contact.parse(parser); 200 mContact = contact; 201 } else if (Note.ELEMENT_NAME.equals(tagName)) { 202 Note note = new Note(); 203 note.parse(parser); 204 mNoteList.add(note); 205 } else if (Timestamp.ELEMENT_NAME.equals(tagName)) { 206 Timestamp timestamp = new Timestamp(); 207 timestamp.parse(parser); 208 mTimestamp = timestamp; 209 } 210 } 211 212 eventType = parser.next(); 213 214 // Leave directly if the event type is the end of the document. 215 if (eventType == XmlPullParser.END_DOCUMENT) { 216 return; 217 } 218 } 219 } 220 getTupleId()221 private String getTupleId() { 222 synchronized (LOCK) { 223 return "tid" + (sTupleId++); 224 } 225 } 226 } 227