blob: a9de23ef1b22c10bffdfcd191bbe97026c0f369b [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;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070058
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070059 public NetworkIdentity(
60 int type, int subType, String subscriberId, String networkId, boolean roaming) {
61 mType = type;
62 mSubType = COMBINE_SUBTYPE_ENABLED ? SUBTYPE_COMBINED : subType;
63 mSubscriberId = subscriberId;
64 mNetworkId = networkId;
65 mRoaming = roaming;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070066 }
67
68 @Override
69 public int hashCode() {
Kenny Roote6585b32013-12-13 12:00:26 -080070 return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070071 }
72
73 @Override
74 public boolean equals(Object obj) {
75 if (obj instanceof NetworkIdentity) {
76 final NetworkIdentity ident = (NetworkIdentity) obj;
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070077 return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming
Kenny Roote6585b32013-12-13 12:00:26 -080078 && Objects.equals(mSubscriberId, ident.mSubscriberId)
79 && Objects.equals(mNetworkId, ident.mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070080 }
81 return false;
82 }
83
84 @Override
85 public String toString() {
Jeff Sharkey55a442e2014-11-18 18:22:21 -080086 final StringBuilder builder = new StringBuilder("{");
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070087 builder.append("type=").append(getNetworkTypeName(mType));
88 builder.append(", subType=");
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070089 if (COMBINE_SUBTYPE_ENABLED) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070090 builder.append("COMBINED");
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070091 } else if (ConnectivityManager.isNetworkTypeMobile(mType)) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070092 builder.append(TelephonyManager.getNetworkTypeName(mSubType));
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070093 } else {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070094 builder.append(mSubType);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070095 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070096 if (mSubscriberId != null) {
97 builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId));
98 }
99 if (mNetworkId != null) {
100 builder.append(", networkId=").append(mNetworkId);
101 }
102 if (mRoaming) {
103 builder.append(", ROAMING");
104 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800105 return builder.append("}").toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700106 }
107
108 public int getType() {
109 return mType;
110 }
111
112 public int getSubType() {
113 return mSubType;
114 }
115
116 public String getSubscriberId() {
117 return mSubscriberId;
118 }
119
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700120 public String getNetworkId() {
121 return mNetworkId;
122 }
123
Jeff Sharkey5dc0c262011-06-19 22:21:05 -0700124 public boolean getRoaming() {
125 return mRoaming;
126 }
127
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700128 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700129 * Scrub given IMSI on production builds.
130 */
131 public static String scrubSubscriberId(String subscriberId) {
132 if ("eng".equals(Build.TYPE)) {
133 return subscriberId;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700134 } else if (subscriberId != null) {
135 // TODO: parse this as MCC+MNC instead of hard-coding
136 return subscriberId.substring(0, Math.min(6, subscriberId.length())) + "...";
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700137 } else {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700138 return "null";
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700139 }
140 }
141
142 /**
Jeff Sharkey32566012014-12-02 18:30:14 -0800143 * Scrub given IMSI on production builds.
144 */
145 public static String[] scrubSubscriberId(String[] subscriberId) {
146 if (subscriberId == null) return null;
147 final String[] res = new String[subscriberId.length];
148 for (int i = 0; i < res.length; i++) {
149 res[i] = NetworkIdentity.scrubSubscriberId(subscriberId[i]);
150 }
151 return res;
152 }
153
154 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700155 * Build a {@link NetworkIdentity} from the given {@link NetworkState},
156 * assuming that any mobile networks are using the current IMSI.
157 */
158 public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state) {
159 final int type = state.networkInfo.getType();
160 final int subType = state.networkInfo.getSubtype();
161
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700162 String subscriberId = null;
163 String networkId = null;
164 boolean roaming = false;
165
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700166 if (isNetworkTypeMobile(type)) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800167 if (state.subscriberId == null) {
168 Slog.w(TAG, "Active mobile network without subscriber!");
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700169 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700170
Jeff Sharkey32566012014-12-02 18:30:14 -0800171 subscriberId = state.subscriberId;
172 roaming = state.networkInfo.isRoaming();
173
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700174 } else if (type == TYPE_WIFI) {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700175 if (state.networkId != null) {
176 networkId = state.networkId;
177 } else {
178 final WifiManager wifi = (WifiManager) context.getSystemService(
179 Context.WIFI_SERVICE);
180 final WifiInfo info = wifi.getConnectionInfo();
181 networkId = info != null ? info.getSSID() : null;
182 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700183 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700184
185 return new NetworkIdentity(type, subType, subscriberId, networkId, roaming);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700186 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800187
188 @Override
189 public int compareTo(NetworkIdentity another) {
190 int res = Integer.compare(mType, another.mType);
191 if (res == 0) {
192 res = Integer.compare(mSubType, another.mSubType);
193 }
194 if (res == 0 && mSubscriberId != null && another.mSubscriberId != null) {
195 res = mSubscriberId.compareTo(another.mSubscriberId);
196 }
197 if (res == 0 && mNetworkId != null && another.mNetworkId != null) {
198 res = mNetworkId.compareTo(another.mNetworkId);
199 }
200 if (res == 0) {
201 res = Boolean.compare(mRoaming, another.mRoaming);
202 }
203 return res;
204 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700205}