1 /*
2  * Copyright (C) 2018 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 android.telephony.ims.stub;
18 
19 import android.annotation.SystemApi;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.telephony.ims.feature.ImsFeature;
23 import android.util.ArraySet;
24 import android.util.Pair;
25 
26 import java.util.Set;
27 
28 /**
29  * Container class for IMS Feature configuration. This class contains the features that the
30  * ImsService supports, which are defined in {@link ImsFeature} as
31  * {@link ImsFeature#FEATURE_EMERGENCY_MMTEL}, {@link ImsFeature#FEATURE_MMTEL}, and
32  * {@link ImsFeature#FEATURE_RCS}.
33  *
34  * @hide
35  */
36 @SystemApi
37 public final class ImsFeatureConfiguration implements Parcelable {
38 
39     public static final class FeatureSlotPair {
40         /**
41          * SIM slot that this feature is associated with.
42          */
43         public final int slotId;
44         /**
45          * The feature that this slotId supports. Supported values are
46          * {@link ImsFeature#FEATURE_EMERGENCY_MMTEL}, {@link ImsFeature#FEATURE_MMTEL}, and
47          * {@link ImsFeature#FEATURE_RCS}.
48          */
49         public final @ImsFeature.FeatureType int featureType;
50 
51         /**
52          * A mapping from slotId to IMS Feature type.
53          * @param slotId the SIM slot ID associated with this feature.
54          * @param featureType The feature that this slotId supports. Supported values are
55          * {@link ImsFeature#FEATURE_EMERGENCY_MMTEL}, {@link ImsFeature#FEATURE_MMTEL}, and
56          * {@link ImsFeature#FEATURE_RCS}.
57          */
FeatureSlotPair(int slotId, @ImsFeature.FeatureType int featureType)58         public FeatureSlotPair(int slotId, @ImsFeature.FeatureType int featureType) {
59             this.slotId = slotId;
60             this.featureType = featureType;
61         }
62 
63         @Override
equals(Object o)64         public boolean equals(Object o) {
65             if (this == o) return true;
66             if (o == null || getClass() != o.getClass()) return false;
67 
68             FeatureSlotPair that = (FeatureSlotPair) o;
69 
70             if (slotId != that.slotId) return false;
71             return featureType == that.featureType;
72         }
73 
74         @Override
hashCode()75         public int hashCode() {
76             int result = slotId;
77             result = 31 * result + featureType;
78             return result;
79         }
80 
81         @Override
toString()82         public String toString() {
83             return "{s=" + slotId + ", f=" + featureType + "}";
84         }
85     }
86 
87     /**
88      * Features that this ImsService supports.
89      */
90     private final Set<FeatureSlotPair> mFeatures;
91 
92     /**
93      * Builder for {@link ImsFeatureConfiguration}.
94      */
95     public static class Builder {
96             ImsFeatureConfiguration mConfig;
Builder()97         public Builder() {
98             mConfig = new ImsFeatureConfiguration();
99         }
100 
101         /**
102          * Adds an IMS feature associated with a SIM slot ID.
103          * @param slotId The slot ID associated with the IMS feature.
104          * @param featureType The feature that the slot ID supports. Supported values are
105          * {@link ImsFeature#FEATURE_EMERGENCY_MMTEL}, {@link ImsFeature#FEATURE_MMTEL}, and
106          * {@link ImsFeature#FEATURE_RCS}.
107          * @return a {@link Builder} to continue constructing the ImsFeatureConfiguration.
108          */
addFeature(int slotId, @ImsFeature.FeatureType int featureType)109         public Builder addFeature(int slotId, @ImsFeature.FeatureType int featureType) {
110             mConfig.addFeature(slotId, featureType);
111             return this;
112         }
113 
build()114         public ImsFeatureConfiguration build() {
115             return mConfig;
116         }
117     }
118 
119     /**
120      * Creates with all registration features empty.
121      * @hide
122      */
ImsFeatureConfiguration()123     public ImsFeatureConfiguration() {
124         mFeatures = new ArraySet<>();
125     }
126 
127     /**
128      * Configuration of the ImsService, which describes which features the ImsService supports
129      * (for registration).
130      * @param features a set of {@link FeatureSlotPair}s that describe which features this
131      *         ImsService supports.
132      * @hide
133      */
ImsFeatureConfiguration(Set<FeatureSlotPair> features)134     public ImsFeatureConfiguration(Set<FeatureSlotPair> features) {
135         mFeatures = new ArraySet<>();
136 
137         if (features != null) {
138             mFeatures.addAll(features);
139         }
140     }
141 
142     /**
143      * @return a set of supported slot ID to feature type pairs contained within a
144      * {@link FeatureSlotPair}.
145      */
getServiceFeatures()146     public Set<FeatureSlotPair> getServiceFeatures() {
147         return new ArraySet<>(mFeatures);
148     }
149 
150     /**
151      * @hide
152      */
addFeature(int slotId, int feature)153     void addFeature(int slotId, int feature) {
154         mFeatures.add(new FeatureSlotPair(slotId, feature));
155     }
156 
157     /** @hide */
ImsFeatureConfiguration(Parcel in)158     protected ImsFeatureConfiguration(Parcel in) {
159         int featurePairLength = in.readInt();
160         // length
161         mFeatures = new ArraySet<>(featurePairLength);
162         for (int i = 0; i < featurePairLength; i++) {
163             // pair of reads for each entry (slotId->featureType)
164             mFeatures.add(new FeatureSlotPair(in.readInt(), in.readInt()));
165         }
166     }
167 
168     public static final @android.annotation.NonNull Creator<ImsFeatureConfiguration> CREATOR
169             = new Creator<ImsFeatureConfiguration>() {
170         @Override
171         public ImsFeatureConfiguration createFromParcel(Parcel in) {
172             return new ImsFeatureConfiguration(in);
173         }
174 
175         @Override
176         public ImsFeatureConfiguration[] newArray(int size) {
177             return new ImsFeatureConfiguration[size];
178         }
179     };
180 
181     @Override
describeContents()182     public int describeContents() {
183         return 0;
184     }
185 
186     @Override
writeToParcel(Parcel dest, int flags)187     public void writeToParcel(Parcel dest, int flags) {
188         FeatureSlotPair[] featureSlotPairs = new FeatureSlotPair[mFeatures.size()];
189         mFeatures.toArray(featureSlotPairs);
190         // length of list
191         dest.writeInt(featureSlotPairs.length);
192         // then pairs of integers for each entry (slotId->featureType).
193         for (FeatureSlotPair featureSlotPair : featureSlotPairs) {
194             dest.writeInt(featureSlotPair.slotId);
195             dest.writeInt(featureSlotPair.featureType);
196         }
197     }
198 
199     /**
200      * @hide
201      */
202     @Override
equals(Object o)203     public boolean equals(Object o) {
204         if (this == o) return true;
205         if (!(o instanceof ImsFeatureConfiguration)) return false;
206 
207         ImsFeatureConfiguration
208                 that = (ImsFeatureConfiguration) o;
209 
210         return mFeatures.equals(that.mFeatures);
211     }
212 
213     /**
214      * @hide
215      */
216     @Override
hashCode()217     public int hashCode() {
218         return mFeatures.hashCode();
219     }
220 }
221