blob: 33c71d5b312a7e2786bd1bf7eb6b9a2ca8babc51 [file] [log] [blame]
Rambo Wang818cf712019-12-12 18:16:10 -08001/*
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
17package android.net;
18
19import android.annotation.NonNull;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23/**
24 * NetworkSpecifier object for cellular network request. Apps should use the
25 * {@link TelephonyNetworkSpecifier.Builder} class to create an instance.
26 */
27public final class TelephonyNetworkSpecifier extends NetworkSpecifier implements Parcelable {
28
29 private final int mSubId;
30
31 /**
32 * Return the subscription Id of current TelephonyNetworkSpecifier object.
33 *
34 * @return The subscription id.
35 */
36 public int getSubscriptionId() {
37 return mSubId;
38 }
39
40 /**
41 * @hide
42 */
43 public TelephonyNetworkSpecifier(int subId) {
44 this.mSubId = subId;
45 }
46
47 public static final @NonNull Creator<TelephonyNetworkSpecifier> CREATOR =
48 new Creator<TelephonyNetworkSpecifier>() {
49 @Override
50 public TelephonyNetworkSpecifier createFromParcel(Parcel in) {
51 int subId = in.readInt();
52 return new TelephonyNetworkSpecifier(subId);
53 }
54
55 @Override
56 public TelephonyNetworkSpecifier[] newArray(int size) {
57 return new TelephonyNetworkSpecifier[size];
58 }
59 };
60
61 @Override
62 public int describeContents() {
63 return 0;
64 }
65
66 @Override
67 public void writeToParcel(@NonNull Parcel dest, int flags) {
68 dest.writeInt(mSubId);
69 }
70
71 @Override
72 public int hashCode() {
73 return mSubId;
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (this == obj) {
79 return true;
80 }
81 if (!(obj instanceof TelephonyNetworkSpecifier)) {
82 return false;
83 }
84
85 TelephonyNetworkSpecifier lhs = (TelephonyNetworkSpecifier) obj;
86 return mSubId == lhs.mSubId;
87 }
88
89 @Override
90 public String toString() {
91 return new StringBuilder()
92 .append("TelephonyNetworkSpecifier [")
93 .append("mSubId = ").append(mSubId)
94 .append("]")
95 .toString();
96 }
97
98 /** @hide */
99 @Override
Chalard Jean2da4f9f2020-03-27 17:57:34 +0900100 public boolean canBeSatisfiedBy(NetworkSpecifier other) {
Rambo Wang0417f692020-04-22 15:24:51 -0700101 // Although the only caller, NetworkCapabilities, already handled the case of
102 // MatchAllNetworkSpecifier, we do it again here in case the API will be used by others.
103 // TODO(b/154959809): consider implementing bi-directional specifier instead.
104 return equals(other) || other instanceof MatchAllNetworkSpecifier;
Rambo Wang818cf712019-12-12 18:16:10 -0800105 }
106
107
108 /**
109 * Builder to create {@link TelephonyNetworkSpecifier} object.
110 */
111 public static final class Builder {
112 // Integer.MIN_VALUE which is not a valid subId, services as the sentinel to check if
113 // subId was set
114 private static final int SENTINEL_SUB_ID = Integer.MIN_VALUE;
115
116 private int mSubId;
117
118 public Builder() {
119 mSubId = SENTINEL_SUB_ID;
120 }
121
122 /**
123 * Set the subscription id.
124 *
125 * @param subId The subscription Id.
126 * @return Instance of {@link Builder} to enable the chaining of the builder method.
127 */
128 public @NonNull Builder setSubscriptionId(int subId) {
129 mSubId = subId;
130 return this;
131 }
132
133 /**
134 * Create a NetworkSpecifier for the cellular network request.
135 *
136 * @return TelephonyNetworkSpecifier object.
137 * @throws IllegalArgumentException when subscription Id is not provided through
138 * {@link #setSubscriptionId(int)}.
139 */
140 public @NonNull TelephonyNetworkSpecifier build() {
141 if (mSubId == SENTINEL_SUB_ID) {
142 throw new IllegalArgumentException("Subscription Id is not provided.");
143 }
144 return new TelephonyNetworkSpecifier(mSubId);
145 }
146 }
147}