blob: 0775bdaaf3803a32a6e6b023598c01213c19d123 [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 Sharkey1b5a2a92011-06-18 18:34:16 -070028import android.telephony.TelephonyManager;
Jeff Sharkey32566012014-12-02 18:30:14 -080029import android.util.Slog;
Makoto Onukida65a522017-01-13 10:23:30 -080030import android.util.proto.ProtoOutputStream;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070031
Kenny Roote6585b32013-12-13 12:00:26 -080032import java.util.Objects;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070033
34/**
35 * Network definition that includes strong identity. Analogous to combining
36 * {@link NetworkInfo} and an IMSI.
37 *
38 * @hide
39 */
Jeff Sharkey55a442e2014-11-18 18:22:21 -080040public class NetworkIdentity implements Comparable<NetworkIdentity> {
Jeff Sharkey32566012014-12-02 18:30:14 -080041 private static final String TAG = "NetworkIdentity";
42
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070043 /**
44 * When enabled, combine all {@link #mSubType} together under
45 * {@link #SUBTYPE_COMBINED}.
Jeff Sharkey69736342014-12-08 14:50:12 -080046 *
47 * @deprecated we no longer offer to collect statistics on a per-subtype
48 * basis; this is always disabled.
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070049 */
Jeff Sharkey69736342014-12-08 14:50:12 -080050 @Deprecated
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070051 public static final boolean COMBINE_SUBTYPE_ENABLED = true;
52
53 public static final int SUBTYPE_COMBINED = -1;
54
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070055 final int mType;
56 final int mSubType;
57 final String mSubscriberId;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070058 final String mNetworkId;
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070059 final boolean mRoaming;
Jack Yu66a6be32016-03-30 11:14:39 -070060 final boolean mMetered;
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,
64 boolean metered) {
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;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070071 }
72
73 @Override
74 public int hashCode() {
Jack Yu66a6be32016-03-30 11:14:39 -070075 return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming, mMetered);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070076 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (obj instanceof NetworkIdentity) {
81 final NetworkIdentity ident = (NetworkIdentity) obj;
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070082 return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming
Kenny Roote6585b32013-12-13 12:00:26 -080083 && Objects.equals(mSubscriberId, ident.mSubscriberId)
Jack Yu66a6be32016-03-30 11:14:39 -070084 && Objects.equals(mNetworkId, ident.mNetworkId)
85 && mMetered == ident.mMetered;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070086 }
87 return false;
88 }
89
90 @Override
91 public String toString() {
Jeff Sharkey55a442e2014-11-18 18:22:21 -080092 final StringBuilder builder = new StringBuilder("{");
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070093 builder.append("type=").append(getNetworkTypeName(mType));
94 builder.append(", subType=");
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070095 if (COMBINE_SUBTYPE_ENABLED) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070096 builder.append("COMBINED");
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070097 } else if (ConnectivityManager.isNetworkTypeMobile(mType)) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070098 builder.append(TelephonyManager.getNetworkTypeName(mSubType));
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070099 } else {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700100 builder.append(mSubType);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700101 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700102 if (mSubscriberId != null) {
103 builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId));
104 }
105 if (mNetworkId != null) {
106 builder.append(", networkId=").append(mNetworkId);
107 }
108 if (mRoaming) {
109 builder.append(", ROAMING");
110 }
Jack Yu66a6be32016-03-30 11:14:39 -0700111 builder.append(", metered=").append(mMetered);
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800112 return builder.append("}").toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700113 }
114
Makoto Onukida65a522017-01-13 10:23:30 -0800115 public void writeToProto(ProtoOutputStream proto, long tag) {
116 final long start = proto.start(tag);
117
118 proto.write(NetworkIdentityProto.TYPE, mType);
119
120 // Not dumping mSubType, subtypes are no longer supported.
121
122 if (mSubscriberId != null) {
123 proto.write(NetworkIdentityProto.SUBSCRIBER_ID, scrubSubscriberId(mSubscriberId));
124 }
125 proto.write(NetworkIdentityProto.NETWORK_ID, mNetworkId);
126 proto.write(NetworkIdentityProto.ROAMING, mRoaming);
127 proto.write(NetworkIdentityProto.METERED, mMetered);
128
129 proto.end(start);
130 }
131
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700132 public int getType() {
133 return mType;
134 }
135
136 public int getSubType() {
137 return mSubType;
138 }
139
140 public String getSubscriberId() {
141 return mSubscriberId;
142 }
143
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700144 public String getNetworkId() {
145 return mNetworkId;
146 }
147
Jeff Sharkey5dc0c262011-06-19 22:21:05 -0700148 public boolean getRoaming() {
149 return mRoaming;
150 }
151
Jack Yu66a6be32016-03-30 11:14:39 -0700152 public boolean getMetered() {
153 return mMetered;
154 }
155
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700156 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700157 * Scrub given IMSI on production builds.
158 */
159 public static String scrubSubscriberId(String subscriberId) {
160 if ("eng".equals(Build.TYPE)) {
161 return subscriberId;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700162 } else if (subscriberId != null) {
163 // TODO: parse this as MCC+MNC instead of hard-coding
164 return subscriberId.substring(0, Math.min(6, subscriberId.length())) + "...";
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700165 } else {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700166 return "null";
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700167 }
168 }
169
170 /**
Jeff Sharkey32566012014-12-02 18:30:14 -0800171 * Scrub given IMSI on production builds.
172 */
173 public static String[] scrubSubscriberId(String[] subscriberId) {
174 if (subscriberId == null) return null;
175 final String[] res = new String[subscriberId.length];
176 for (int i = 0; i < res.length; i++) {
177 res[i] = NetworkIdentity.scrubSubscriberId(subscriberId[i]);
178 }
179 return res;
180 }
181
182 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700183 * Build a {@link NetworkIdentity} from the given {@link NetworkState},
184 * assuming that any mobile networks are using the current IMSI.
185 */
186 public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state) {
187 final int type = state.networkInfo.getType();
188 final int subType = state.networkInfo.getSubtype();
189
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700190 String subscriberId = null;
191 String networkId = null;
192 boolean roaming = false;
Stephen Chen25147872016-10-21 12:44:26 -0700193 boolean metered = !state.networkCapabilities.hasCapability(
194 NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700195
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700196 if (isNetworkTypeMobile(type)) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800197 if (state.subscriberId == null) {
Jack Yubfd1d6e2016-08-23 16:09:36 -0700198 if (state.networkInfo.getState() != NetworkInfo.State.DISCONNECTED &&
199 state.networkInfo.getState() != NetworkInfo.State.UNKNOWN) {
200 Slog.w(TAG, "Active mobile network without subscriber! ni = "
201 + state.networkInfo);
202 }
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700203 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700204
Jeff Sharkey32566012014-12-02 18:30:14 -0800205 subscriberId = state.subscriberId;
206 roaming = state.networkInfo.isRoaming();
207
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700208 } else if (type == TYPE_WIFI) {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700209 if (state.networkId != null) {
210 networkId = state.networkId;
211 } else {
212 final WifiManager wifi = (WifiManager) context.getSystemService(
213 Context.WIFI_SERVICE);
214 final WifiInfo info = wifi.getConnectionInfo();
215 networkId = info != null ? info.getSSID() : null;
216 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700217 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700218
Jack Yu66a6be32016-03-30 11:14:39 -0700219 return new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700220 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800221
222 @Override
223 public int compareTo(NetworkIdentity another) {
224 int res = Integer.compare(mType, another.mType);
225 if (res == 0) {
226 res = Integer.compare(mSubType, another.mSubType);
227 }
228 if (res == 0 && mSubscriberId != null && another.mSubscriberId != null) {
229 res = mSubscriberId.compareTo(another.mSubscriberId);
230 }
231 if (res == 0 && mNetworkId != null && another.mNetworkId != null) {
232 res = mNetworkId.compareTo(another.mNetworkId);
233 }
234 if (res == 0) {
235 res = Boolean.compare(mRoaming, another.mRoaming);
236 }
Jack Yu66a6be32016-03-30 11:14:39 -0700237 if (res == 0) {
238 res = Boolean.compare(mMetered, another.mMetered);
239 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800240 return res;
241 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700242}