1 /* 2 * Copyright (C) 2022 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; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.net.Uri; 22 23 import android.telephony.ims.RcsContactPresenceTuple; 24 import android.telephony.ims.RcsContactUceCapability; 25 import android.telephony.ims.RcsContactUceCapability.PresenceBuilder; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * A wrapper class that uses the parsed information to construct {@link RcsContactUceCapability} 32 * instances. 33 */ 34 35 public class RcsContactUceCapabilityWrapper { 36 private final Uri mContactUri; 37 private final int mSourceType; 38 private final int mRequestResult; 39 private boolean mIsMalformed; 40 private final List<RcsContactPresenceTuple> mPresenceTuples = new ArrayList<>(); 41 private Uri mEntityUri; 42 43 /** 44 * Create the wrapper, which can be used to set UCE capabilities as well as custom 45 * capability extensions. 46 * @param contact The contact URI that the capabilities are attached to. 47 * @param sourceType The type where the capabilities of this contact were retrieved from. 48 * @param requestResult the request result 49 */ RcsContactUceCapabilityWrapper(@onNull Uri contact, int sourceType, int requestResult)50 public RcsContactUceCapabilityWrapper(@NonNull Uri contact, int sourceType, int requestResult) { 51 mContactUri = contact; 52 mSourceType = sourceType; 53 mRequestResult = requestResult; 54 mIsMalformed = false; 55 } 56 57 /** 58 * Add the {@link RcsContactPresenceTuple} into the presence tuple list. 59 * @param tuple The {@link RcsContactPresenceTuple} to be added into. 60 */ addCapabilityTuple(@onNull RcsContactPresenceTuple tuple)61 public void addCapabilityTuple(@NonNull RcsContactPresenceTuple tuple) { 62 mPresenceTuples.add(tuple); 63 } 64 65 /** 66 * This flag is set if at least one tuple could not be parsed due to malformed contents. 67 */ setMalformedContents()68 public void setMalformedContents() { 69 mIsMalformed = true; 70 } 71 72 /** 73 * Set the entity URI related to the contact whose capabilities were requested. 74 * @param entityUri the 'pres' URL of the PRESENTITY publishing presence document. 75 */ setEntityUri(@onNull Uri entityUri)76 public void setEntityUri(@NonNull Uri entityUri) { 77 mEntityUri = entityUri; 78 } 79 80 /** 81 * Whether the XML is malformed. 82 * @return {@code true} if all of the presence tuple information associated with 83 * the entity URI ({@link #getEntityUri}) is malformed and there is no tuple info 84 * available. If one or more of the tuples are still well-formed after parsing the 85 * XML, this method will return {@code false}. 86 */ isMalformed()87 public boolean isMalformed() { 88 if (mIsMalformed == false) { 89 return false; 90 } 91 if (mPresenceTuples.isEmpty()) { 92 return true; 93 } 94 return false; 95 } 96 97 /** 98 * Retrieve the entity URI of the contact whose presence information is being requested for. 99 * @return the URI representing the 'pres' URL of the PRESENTITY publishing presence document 100 * or {@code null} if the entity uri does not exist in the presence document. 101 */ getEntityUri()102 public @Nullable Uri getEntityUri() { 103 return mEntityUri; 104 } 105 106 /** 107 * @return a new RcsContactUceCapability instance from the contents of this wrapper. 108 */ toRcsContactUceCapability()109 public @NonNull RcsContactUceCapability toRcsContactUceCapability() { 110 111 PresenceBuilder presenceBuilder = new PresenceBuilder(mContactUri, 112 mSourceType, mRequestResult); 113 114 // Add all the capability tuples of this contact 115 presenceBuilder.addCapabilityTuples(mPresenceTuples); 116 presenceBuilder.setEntityUri(mEntityUri); 117 return presenceBuilder.build(); 118 } 119 } 120