• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.data;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.util.Arrays;
25 import java.util.Objects;
26 
27 /**
28  * A traffic descriptor, as defined in 3GPP TS 24.526 Section 5.2. It is used for UE Route Selection
29  * Policy(URSP) traffic matching as described in 3GPP TS 24.526 Section 4.2.2. It includes an
30  * optional Data Network Name(DNN), which, if present, must be used for traffic matching; it does
31  * not specify the end point to be used for the data call.
32  */
33 public final class TrafficDescriptor implements Parcelable {
34     private final String mDnn;
35     private final byte[] mOsAppId;
36 
TrafficDescriptor(@onNull Parcel in)37     private TrafficDescriptor(@NonNull Parcel in) {
38         mDnn = in.readString();
39         mOsAppId = in.createByteArray();
40     }
41 
42     /**
43      * Create a traffic descriptor, as defined in 3GPP TS 24.526 Section 5.2
44      * @param dnn optional DNN, which must be used for traffic matching, if present
45      * @param osAppId OsId + osAppId of the traffic descriptor
46      *
47      * @hide
48      */
TrafficDescriptor(String dnn, byte[] osAppId)49     public TrafficDescriptor(String dnn, byte[] osAppId) {
50         mDnn = dnn;
51         mOsAppId = osAppId;
52     }
53 
54     /**
55      * DNN stands for Data Network Name and represents an APN as defined in 3GPP TS 23.003.
56      * @return the DNN of this traffic descriptor if one is included by the network, null
57      * otherwise.
58      */
getDataNetworkName()59     public @Nullable String getDataNetworkName() {
60         return mDnn;
61     }
62 
63     /**
64      * OsAppId is the app id as defined in 3GPP TS 24.526 Section 5.2, and it identifies a traffic
65      * category. It includes the OS Id component of the field as defined in the specs.
66      * @return the OS App ID of this traffic descriptor if one is included by the network, null
67      * otherwise.
68      */
getOsAppId()69     public @Nullable byte[] getOsAppId() {
70         return mOsAppId;
71     }
72 
73     @Override
describeContents()74     public int describeContents() {
75         return 0;
76     }
77 
78     @NonNull @Override
toString()79     public String toString() {
80         return "TrafficDescriptor={mDnn=" + mDnn + ", mOsAppId=" + mOsAppId + "}";
81     }
82 
83     @Override
writeToParcel(@onNull Parcel dest, int flags)84     public void writeToParcel(@NonNull Parcel dest, int flags) {
85         dest.writeString(mDnn);
86         dest.writeByteArray(mOsAppId);
87     }
88 
89     public static final @NonNull Parcelable.Creator<TrafficDescriptor> CREATOR =
90             new Parcelable.Creator<TrafficDescriptor>() {
91                 @Override
92                 public @NonNull TrafficDescriptor createFromParcel(@NonNull Parcel source) {
93                     return new TrafficDescriptor(source);
94                 }
95 
96                 @Override
97                 public @NonNull TrafficDescriptor[] newArray(int size) {
98                     return new TrafficDescriptor[size];
99                 }
100             };
101 
102     @Override
equals(@ullable Object o)103     public boolean equals(@Nullable Object o) {
104         if (this == o) return true;
105         if (o == null || getClass() != o.getClass()) return false;
106         TrafficDescriptor that = (TrafficDescriptor) o;
107         return Objects.equals(mDnn, that.mDnn) && Arrays.equals(mOsAppId, that.mOsAppId);
108     }
109 
110     @Override
hashCode()111     public int hashCode() {
112         return Objects.hash(mDnn, mOsAppId);
113     }
114 
115     /**
116      * Provides a convenient way to set the fields of a {@link TrafficDescriptor} when creating a
117      * new instance.
118      *
119      * <p>The example below shows how you might create a new {@code TrafficDescriptor}:
120      *
121      * <pre><code>
122      *
123      * TrafficDescriptor response = new TrafficDescriptor.Builder()
124      *     .setDnn("")
125      *     .build();
126      * </code></pre>
127      *
128      */
129     public static final class Builder {
130         private String mDnn = null;
131         private byte[] mOsAppId = null;
132 
133         /**
134          * Default constructor for Builder.
135          */
Builder()136         public Builder() {
137         }
138 
139         /**
140          * Set the Data Network Name(DNN).
141          *
142          * @return The same instance of the builder.
143          */
144         @NonNull
setDataNetworkName(@onNull String dnn)145         public Builder setDataNetworkName(@NonNull String dnn) {
146             this.mDnn = dnn;
147             return this;
148         }
149 
150         /**
151          * Set the OS App ID (including OS Id as defind in the specs).
152          *
153          * @return The same instance of the builder.
154          */
155         @NonNull
setOsAppId(@onNull byte[] osAppId)156         public Builder setOsAppId(@NonNull byte[] osAppId) {
157             this.mOsAppId = osAppId;
158             return this;
159         }
160 
161         /**
162          * Build the {@link TrafficDescriptor}.
163          *
164          * @throws IllegalArgumentException if DNN and OS App ID are null.
165          *
166          * @return the {@link TrafficDescriptor} object.
167          */
168         @NonNull
build()169         public TrafficDescriptor build() {
170             if (this.mDnn == null && this.mOsAppId == null) {
171                 throw new IllegalArgumentException("DNN and OS App ID are null");
172             }
173             return new TrafficDescriptor(this.mDnn, this.mOsAppId);
174         }
175     }
176 }
177