blob: febc730480e451b421c990e6acf341023444cfd2 [file] [log] [blame]
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -07001/*
2 * Copyright (C) 2011 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
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070019import static android.net.ConnectivityManager.TYPE_WIFI;
20import static android.net.ConnectivityManager.getNetworkTypeName;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070021import static android.net.ConnectivityManager.isNetworkTypeMobile;
22
23import android.content.Context;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070024import android.net.wifi.WifiInfo;
25import android.net.wifi.WifiManager;
Jeff Sharkey02e21d62011-07-17 15:53:33 -070026import android.os.Build;
Makoto Onukida65a522017-01-13 10:23:30 -080027import android.service.NetworkIdentityProto;
Jeff Sharkey32566012014-12-02 18:30:14 -080028import android.util.Slog;
Makoto Onukida65a522017-01-13 10:23:30 -080029import android.util.proto.ProtoOutputStream;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070030
Kenny Roote6585b32013-12-13 12:00:26 -080031import java.util.Objects;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070032
33/**
34 * Network definition that includes strong identity. Analogous to combining
35 * {@link NetworkInfo} and an IMSI.
36 *
37 * @hide
38 */
Jeff Sharkey55a442e2014-11-18 18:22:21 -080039public class NetworkIdentity implements Comparable<NetworkIdentity> {
Jeff Sharkey32566012014-12-02 18:30:14 -080040 private static final String TAG = "NetworkIdentity";
41
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070042 /**
43 * When enabled, combine all {@link #mSubType} together under
44 * {@link #SUBTYPE_COMBINED}.
Jeff Sharkey69736342014-12-08 14:50:12 -080045 *
46 * @deprecated we no longer offer to collect statistics on a per-subtype
47 * basis; this is always disabled.
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070048 */
Jeff Sharkey69736342014-12-08 14:50:12 -080049 @Deprecated
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070050 public static final boolean COMBINE_SUBTYPE_ENABLED = true;
51
52 public static final int SUBTYPE_COMBINED = -1;
53
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070054 final int mType;
55 final int mSubType;
56 final String mSubscriberId;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070057 final String mNetworkId;
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070058 final boolean mRoaming;
Jack Yu66a6be32016-03-30 11:14:39 -070059 final boolean mMetered;
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +090060 final boolean mDefaultNetwork;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070061
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070062 public NetworkIdentity(
Jack Yu66a6be32016-03-30 11:14:39 -070063 int type, int subType, String subscriberId, String networkId, boolean roaming,
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +090064 boolean metered, boolean defaultNetwork) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070065 mType = type;
66 mSubType = COMBINE_SUBTYPE_ENABLED ? SUBTYPE_COMBINED : subType;
67 mSubscriberId = subscriberId;
68 mNetworkId = networkId;
69 mRoaming = roaming;
Jack Yu66a6be32016-03-30 11:14:39 -070070 mMetered = metered;
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +090071 mDefaultNetwork = defaultNetwork;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070072 }
73
74 @Override
75 public int hashCode() {
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +090076 return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming, mMetered,
77 mDefaultNetwork);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070078 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (obj instanceof NetworkIdentity) {
83 final NetworkIdentity ident = (NetworkIdentity) obj;
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070084 return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming
Kenny Roote6585b32013-12-13 12:00:26 -080085 && Objects.equals(mSubscriberId, ident.mSubscriberId)
Jack Yu66a6be32016-03-30 11:14:39 -070086 && Objects.equals(mNetworkId, ident.mNetworkId)
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +090087 && mMetered == ident.mMetered
88 && mDefaultNetwork == ident.mDefaultNetwork;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070089 }
90 return false;
91 }
92
93 @Override
94 public String toString() {
Jeff Sharkey55a442e2014-11-18 18:22:21 -080095 final StringBuilder builder = new StringBuilder("{");
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070096 builder.append("type=").append(getNetworkTypeName(mType));
97 builder.append(", subType=");
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070098 if (COMBINE_SUBTYPE_ENABLED) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070099 builder.append("COMBINED");
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700100 } else {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700101 builder.append(mSubType);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700102 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700103 if (mSubscriberId != null) {
104 builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId));
105 }
106 if (mNetworkId != null) {
107 builder.append(", networkId=").append(mNetworkId);
108 }
109 if (mRoaming) {
110 builder.append(", ROAMING");
111 }
Jack Yu66a6be32016-03-30 11:14:39 -0700112 builder.append(", metered=").append(mMetered);
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +0900113 builder.append(", defaultNetwork=").append(mDefaultNetwork);
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800114 return builder.append("}").toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700115 }
116
Makoto Onukida65a522017-01-13 10:23:30 -0800117 public void writeToProto(ProtoOutputStream proto, long tag) {
118 final long start = proto.start(tag);
119
120 proto.write(NetworkIdentityProto.TYPE, mType);
121
122 // Not dumping mSubType, subtypes are no longer supported.
123
124 if (mSubscriberId != null) {
125 proto.write(NetworkIdentityProto.SUBSCRIBER_ID, scrubSubscriberId(mSubscriberId));
126 }
127 proto.write(NetworkIdentityProto.NETWORK_ID, mNetworkId);
128 proto.write(NetworkIdentityProto.ROAMING, mRoaming);
129 proto.write(NetworkIdentityProto.METERED, mMetered);
Lorenzo Colitti9781f7852018-01-20 02:02:56 +0900130 proto.write(NetworkIdentityProto.DEFAULT_NETWORK, mDefaultNetwork);
Makoto Onukida65a522017-01-13 10:23:30 -0800131
132 proto.end(start);
133 }
134
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700135 public int getType() {
136 return mType;
137 }
138
139 public int getSubType() {
140 return mSubType;
141 }
142
143 public String getSubscriberId() {
144 return mSubscriberId;
145 }
146
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700147 public String getNetworkId() {
148 return mNetworkId;
149 }
150
Jeff Sharkey5dc0c262011-06-19 22:21:05 -0700151 public boolean getRoaming() {
152 return mRoaming;
153 }
154
Jack Yu66a6be32016-03-30 11:14:39 -0700155 public boolean getMetered() {
156 return mMetered;
157 }
158
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +0900159 public boolean getDefaultNetwork() {
160 return mDefaultNetwork;
161 }
162
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700163 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700164 * Scrub given IMSI on production builds.
165 */
166 public static String scrubSubscriberId(String subscriberId) {
Jeff Sharkey5ab02432017-06-27 11:01:36 -0600167 if (Build.IS_ENG) {
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700168 return subscriberId;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700169 } else if (subscriberId != null) {
170 // TODO: parse this as MCC+MNC instead of hard-coding
171 return subscriberId.substring(0, Math.min(6, subscriberId.length())) + "...";
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700172 } else {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700173 return "null";
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700174 }
175 }
176
177 /**
Jeff Sharkey32566012014-12-02 18:30:14 -0800178 * Scrub given IMSI on production builds.
179 */
180 public static String[] scrubSubscriberId(String[] subscriberId) {
181 if (subscriberId == null) return null;
182 final String[] res = new String[subscriberId.length];
183 for (int i = 0; i < res.length; i++) {
184 res[i] = NetworkIdentity.scrubSubscriberId(subscriberId[i]);
185 }
186 return res;
187 }
188
189 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700190 * Build a {@link NetworkIdentity} from the given {@link NetworkState},
191 * assuming that any mobile networks are using the current IMSI.
192 */
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +0900193 public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state,
194 boolean defaultNetwork) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700195 final int type = state.networkInfo.getType();
196 final int subType = state.networkInfo.getSubtype();
197
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700198 String subscriberId = null;
199 String networkId = null;
Jeff Sharkey72f9c422017-10-27 17:22:59 -0600200 boolean roaming = !state.networkCapabilities.hasCapability(
201 NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
Stephen Chen25147872016-10-21 12:44:26 -0700202 boolean metered = !state.networkCapabilities.hasCapability(
203 NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700204
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700205 if (isNetworkTypeMobile(type)) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800206 if (state.subscriberId == null) {
Jack Yubfd1d6e2016-08-23 16:09:36 -0700207 if (state.networkInfo.getState() != NetworkInfo.State.DISCONNECTED &&
208 state.networkInfo.getState() != NetworkInfo.State.UNKNOWN) {
209 Slog.w(TAG, "Active mobile network without subscriber! ni = "
210 + state.networkInfo);
211 }
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700212 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700213
Jeff Sharkey32566012014-12-02 18:30:14 -0800214 subscriberId = state.subscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800215
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700216 } else if (type == TYPE_WIFI) {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700217 if (state.networkId != null) {
218 networkId = state.networkId;
219 } else {
220 final WifiManager wifi = (WifiManager) context.getSystemService(
221 Context.WIFI_SERVICE);
222 final WifiInfo info = wifi.getConnectionInfo();
223 networkId = info != null ? info.getSSID() : null;
224 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700225 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700226
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +0900227 return new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered,
228 defaultNetwork);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700229 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800230
231 @Override
232 public int compareTo(NetworkIdentity another) {
233 int res = Integer.compare(mType, another.mType);
234 if (res == 0) {
235 res = Integer.compare(mSubType, another.mSubType);
236 }
237 if (res == 0 && mSubscriberId != null && another.mSubscriberId != null) {
238 res = mSubscriberId.compareTo(another.mSubscriberId);
239 }
240 if (res == 0 && mNetworkId != null && another.mNetworkId != null) {
241 res = mNetworkId.compareTo(another.mNetworkId);
242 }
243 if (res == 0) {
244 res = Boolean.compare(mRoaming, another.mRoaming);
245 }
Jack Yu66a6be32016-03-30 11:14:39 -0700246 if (res == 0) {
247 res = Boolean.compare(mMetered, another.mMetered);
248 }
Lorenzo Colittid3e4a1e2018-01-19 01:12:04 +0900249 if (res == 0) {
250 res = Boolean.compare(mDefaultNetwork, another.mDefaultNetwork);
251 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800252 return res;
253 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700254}