blob: c704ef0c9ca7c150948b8e785a644389b4007138 [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;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070027import android.telephony.TelephonyManager;
Jeff Sharkey32566012014-12-02 18:30:14 -080028import android.util.Slog;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070029
Kenny Roote6585b32013-12-13 12:00:26 -080030import java.util.Objects;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070031
32/**
33 * Network definition that includes strong identity. Analogous to combining
34 * {@link NetworkInfo} and an IMSI.
35 *
36 * @hide
37 */
Jeff Sharkey55a442e2014-11-18 18:22:21 -080038public class NetworkIdentity implements Comparable<NetworkIdentity> {
Jeff Sharkey32566012014-12-02 18:30:14 -080039 private static final String TAG = "NetworkIdentity";
40
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070041 /**
42 * When enabled, combine all {@link #mSubType} together under
43 * {@link #SUBTYPE_COMBINED}.
Jeff Sharkey69736342014-12-08 14:50:12 -080044 *
45 * @deprecated we no longer offer to collect statistics on a per-subtype
46 * basis; this is always disabled.
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070047 */
Jeff Sharkey69736342014-12-08 14:50:12 -080048 @Deprecated
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070049 public static final boolean COMBINE_SUBTYPE_ENABLED = true;
50
51 public static final int SUBTYPE_COMBINED = -1;
52
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070053 final int mType;
54 final int mSubType;
55 final String mSubscriberId;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070056 final String mNetworkId;
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070057 final boolean mRoaming;
Jack Yu66a6be32016-03-30 11:14:39 -070058 final boolean mMetered;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070059
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070060 public NetworkIdentity(
Jack Yu66a6be32016-03-30 11:14:39 -070061 int type, int subType, String subscriberId, String networkId, boolean roaming,
62 boolean metered) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070063 mType = type;
64 mSubType = COMBINE_SUBTYPE_ENABLED ? SUBTYPE_COMBINED : subType;
65 mSubscriberId = subscriberId;
66 mNetworkId = networkId;
67 mRoaming = roaming;
Jack Yu66a6be32016-03-30 11:14:39 -070068 mMetered = metered;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070069 }
70
71 @Override
72 public int hashCode() {
Jack Yu66a6be32016-03-30 11:14:39 -070073 return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming, mMetered);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070074 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (obj instanceof NetworkIdentity) {
79 final NetworkIdentity ident = (NetworkIdentity) obj;
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070080 return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming
Kenny Roote6585b32013-12-13 12:00:26 -080081 && Objects.equals(mSubscriberId, ident.mSubscriberId)
Jack Yu66a6be32016-03-30 11:14:39 -070082 && Objects.equals(mNetworkId, ident.mNetworkId)
83 && mMetered == ident.mMetered;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070084 }
85 return false;
86 }
87
88 @Override
89 public String toString() {
Jeff Sharkey55a442e2014-11-18 18:22:21 -080090 final StringBuilder builder = new StringBuilder("{");
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070091 builder.append("type=").append(getNetworkTypeName(mType));
92 builder.append(", subType=");
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070093 if (COMBINE_SUBTYPE_ENABLED) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070094 builder.append("COMBINED");
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070095 } else if (ConnectivityManager.isNetworkTypeMobile(mType)) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070096 builder.append(TelephonyManager.getNetworkTypeName(mSubType));
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070097 } else {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070098 builder.append(mSubType);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070099 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700100 if (mSubscriberId != null) {
101 builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId));
102 }
103 if (mNetworkId != null) {
104 builder.append(", networkId=").append(mNetworkId);
105 }
106 if (mRoaming) {
107 builder.append(", ROAMING");
108 }
Jack Yu66a6be32016-03-30 11:14:39 -0700109 builder.append(", metered=").append(mMetered);
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800110 return builder.append("}").toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700111 }
112
113 public int getType() {
114 return mType;
115 }
116
117 public int getSubType() {
118 return mSubType;
119 }
120
121 public String getSubscriberId() {
122 return mSubscriberId;
123 }
124
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700125 public String getNetworkId() {
126 return mNetworkId;
127 }
128
Jeff Sharkey5dc0c262011-06-19 22:21:05 -0700129 public boolean getRoaming() {
130 return mRoaming;
131 }
132
Jack Yu66a6be32016-03-30 11:14:39 -0700133 public boolean getMetered() {
134 return mMetered;
135 }
136
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700137 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700138 * Scrub given IMSI on production builds.
139 */
140 public static String scrubSubscriberId(String subscriberId) {
141 if ("eng".equals(Build.TYPE)) {
142 return subscriberId;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700143 } else if (subscriberId != null) {
144 // TODO: parse this as MCC+MNC instead of hard-coding
145 return subscriberId.substring(0, Math.min(6, subscriberId.length())) + "...";
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700146 } else {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700147 return "null";
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700148 }
149 }
150
151 /**
Jeff Sharkey32566012014-12-02 18:30:14 -0800152 * Scrub given IMSI on production builds.
153 */
154 public static String[] scrubSubscriberId(String[] subscriberId) {
155 if (subscriberId == null) return null;
156 final String[] res = new String[subscriberId.length];
157 for (int i = 0; i < res.length; i++) {
158 res[i] = NetworkIdentity.scrubSubscriberId(subscriberId[i]);
159 }
160 return res;
161 }
162
163 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700164 * Build a {@link NetworkIdentity} from the given {@link NetworkState},
165 * assuming that any mobile networks are using the current IMSI.
166 */
167 public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state) {
168 final int type = state.networkInfo.getType();
169 final int subType = state.networkInfo.getSubtype();
170
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700171 String subscriberId = null;
172 String networkId = null;
173 boolean roaming = false;
Stephen Chenc926b732016-10-21 12:44:26 -0700174 boolean metered = !state.networkCapabilities.hasCapability(
175 NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700176
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700177 if (isNetworkTypeMobile(type)) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800178 if (state.subscriberId == null) {
Jack Yubfd1d6e2016-08-23 16:09:36 -0700179 if (state.networkInfo.getState() != NetworkInfo.State.DISCONNECTED &&
180 state.networkInfo.getState() != NetworkInfo.State.UNKNOWN) {
181 Slog.w(TAG, "Active mobile network without subscriber! ni = "
182 + state.networkInfo);
183 }
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700184 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700185
Jeff Sharkey32566012014-12-02 18:30:14 -0800186 subscriberId = state.subscriberId;
187 roaming = state.networkInfo.isRoaming();
188
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700189 } else if (type == TYPE_WIFI) {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700190 if (state.networkId != null) {
191 networkId = state.networkId;
192 } else {
193 final WifiManager wifi = (WifiManager) context.getSystemService(
194 Context.WIFI_SERVICE);
195 final WifiInfo info = wifi.getConnectionInfo();
196 networkId = info != null ? info.getSSID() : null;
197 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700198 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700199
Jack Yu66a6be32016-03-30 11:14:39 -0700200 return new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700201 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800202
203 @Override
204 public int compareTo(NetworkIdentity another) {
205 int res = Integer.compare(mType, another.mType);
206 if (res == 0) {
207 res = Integer.compare(mSubType, another.mSubType);
208 }
209 if (res == 0 && mSubscriberId != null && another.mSubscriberId != null) {
210 res = mSubscriberId.compareTo(another.mSubscriberId);
211 }
212 if (res == 0 && mNetworkId != null && another.mNetworkId != null) {
213 res = mNetworkId.compareTo(another.mNetworkId);
214 }
215 if (res == 0) {
216 res = Boolean.compare(mRoaming, another.mRoaming);
217 }
Jack Yu66a6be32016-03-30 11:14:39 -0700218 if (res == 0) {
219 res = Boolean.compare(mMetered, another.mMetered);
220 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800221 return res;
222 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700223}