blob: 726f770597077563c5ee24cd87d11b9aefee8e3a [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
100 public boolean satisfiedBy(NetworkSpecifier other) {
101 // Any generic requests should be satisfied by a specific telephony network.
102 // For simplicity, we treat null same as MatchAllNetworkSpecifier
103 return equals(other) || other == null || other instanceof MatchAllNetworkSpecifier;
104 }
105
106
107 /**
108 * Builder to create {@link TelephonyNetworkSpecifier} object.
109 */
110 public static final class Builder {
111 // Integer.MIN_VALUE which is not a valid subId, services as the sentinel to check if
112 // subId was set
113 private static final int SENTINEL_SUB_ID = Integer.MIN_VALUE;
114
115 private int mSubId;
116
117 public Builder() {
118 mSubId = SENTINEL_SUB_ID;
119 }
120
121 /**
122 * Set the subscription id.
123 *
124 * @param subId The subscription Id.
125 * @return Instance of {@link Builder} to enable the chaining of the builder method.
126 */
127 public @NonNull Builder setSubscriptionId(int subId) {
128 mSubId = subId;
129 return this;
130 }
131
132 /**
133 * Create a NetworkSpecifier for the cellular network request.
134 *
135 * @return TelephonyNetworkSpecifier object.
136 * @throws IllegalArgumentException when subscription Id is not provided through
137 * {@link #setSubscriptionId(int)}.
138 */
139 public @NonNull TelephonyNetworkSpecifier build() {
140 if (mSubId == SENTINEL_SUB_ID) {
141 throw new IllegalArgumentException("Subscription Id is not provided.");
142 }
143 return new TelephonyNetworkSpecifier(mSubId);
144 }
145 }
146}