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 android.text.TextUtils;
20 
21 import com.android.ims.rcs.uce.presence.pidfparser.ElementBase;
22 import com.android.internal.annotations.VisibleForTesting;
23 
24 import org.xmlpull.v1.XmlPullParser;
25 import org.xmlpull.v1.XmlPullParserException;
26 import org.xmlpull.v1.XmlSerializer;
27 
28 import java.io.IOException;
29 
30 /**
31  * The "contact" element of the pidf.
32  */
33 public class Contact extends ElementBase {
34     /** The name of this element */
35     public static final String ELEMENT_NAME = "contact";
36 
37     private Double mPriority;
38     private String mContact;
39 
Contact()40     public Contact() {
41     }
42 
43     @Override
initNamespace()44     protected String initNamespace() {
45         return PidfConstant.NAMESPACE;
46     }
47 
48     @Override
initElementName()49     protected String initElementName() {
50         return ELEMENT_NAME;
51     }
52 
setPriority(Double priority)53     public void setPriority(Double priority) {
54         mPriority = priority;
55     }
56 
57     @VisibleForTesting
getPriority()58     public Double getPriority() {
59         return mPriority;
60     }
61 
setContact(String contact)62     public void setContact(String contact) {
63         mContact = contact;
64     }
65 
getContact()66     public String getContact() {
67         return mContact;
68     }
69 
70     @Override
serialize(XmlSerializer serializer)71     public void serialize(XmlSerializer serializer) throws IOException {
72         if (mContact == null) {
73             return;
74         }
75         String noNamespace = XmlPullParser.NO_NAMESPACE;
76         String namespace = getNamespace();
77         String elementName = getElementName();
78         serializer.startTag(namespace, elementName);
79         if (mPriority != null) {
80             serializer.attribute(noNamespace, "priority", String.valueOf(mPriority));
81         }
82         serializer.text(mContact);
83         serializer.endTag(namespace, elementName);
84     }
85 
86     @Override
parse(XmlPullParser parser)87     public void parse(XmlPullParser parser) throws IOException, XmlPullParserException {
88         String namespace = parser.getNamespace();
89         String name = parser.getName();
90 
91         if (!verifyParsingElement(namespace, name)) {
92             throw new XmlPullParserException("Incorrect element: " + namespace + ", " + name);
93         }
94 
95         String priority = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "priority");
96         if (!TextUtils.isEmpty(priority)) {
97             mPriority = Double.parseDouble(priority);
98         }
99 
100         // Move to the next event to get the value.
101         int eventType = parser.next();
102 
103         // Get the value if the event type is text.
104         if (eventType == XmlPullParser.TEXT) {
105             String contact = parser.getText();
106             if (!TextUtils.isEmpty(contact)) {
107                 mContact = contact;
108             }
109         }
110 
111         // Move to the end tag.
112         moveToElementEndTag(parser, eventType);
113     }
114 }
115