Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 20 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.isNetworkTypeMobile; |
| 22 | |
| 23 | import android.content.Context; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 24 | import android.net.wifi.WifiInfo; |
| 25 | import android.net.wifi.WifiManager; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 26 | import android.os.Build; |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 27 | import android.service.NetworkIdentityProto; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 28 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 29 | import android.util.Slog; |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 30 | import android.util.proto.ProtoOutputStream; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 31 | |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 32 | import java.util.Objects; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Network definition that includes strong identity. Analogous to combining |
| 36 | * {@link NetworkInfo} and an IMSI. |
| 37 | * |
| 38 | * @hide |
| 39 | */ |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 40 | public class NetworkIdentity implements Comparable<NetworkIdentity> { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 41 | private static final String TAG = "NetworkIdentity"; |
| 42 | |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 43 | /** |
| 44 | * When enabled, combine all {@link #mSubType} together under |
| 45 | * {@link #SUBTYPE_COMBINED}. |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 46 | * |
| 47 | * @deprecated we no longer offer to collect statistics on a per-subtype |
| 48 | * basis; this is always disabled. |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 49 | */ |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 50 | @Deprecated |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 51 | public static final boolean COMBINE_SUBTYPE_ENABLED = true; |
| 52 | |
| 53 | public static final int SUBTYPE_COMBINED = -1; |
| 54 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 55 | final int mType; |
| 56 | final int mSubType; |
| 57 | final String mSubscriberId; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 58 | final String mNetworkId; |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 59 | final boolean mRoaming; |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 60 | final boolean mMetered; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 61 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 62 | public NetworkIdentity( |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 63 | int type, int subType, String subscriberId, String networkId, boolean roaming, |
| 64 | boolean metered) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 65 | mType = type; |
| 66 | mSubType = COMBINE_SUBTYPE_ENABLED ? SUBTYPE_COMBINED : subType; |
| 67 | mSubscriberId = subscriberId; |
| 68 | mNetworkId = networkId; |
| 69 | mRoaming = roaming; |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 70 | mMetered = metered; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int hashCode() { |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 75 | return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming, mMetered); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean equals(Object obj) { |
| 80 | if (obj instanceof NetworkIdentity) { |
| 81 | final NetworkIdentity ident = (NetworkIdentity) obj; |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 82 | return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 83 | && Objects.equals(mSubscriberId, ident.mSubscriberId) |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 84 | && Objects.equals(mNetworkId, ident.mNetworkId) |
| 85 | && mMetered == ident.mMetered; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public String toString() { |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 92 | final StringBuilder builder = new StringBuilder("{"); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 93 | builder.append("type=").append(getNetworkTypeName(mType)); |
| 94 | builder.append(", subType="); |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 95 | if (COMBINE_SUBTYPE_ENABLED) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 96 | builder.append("COMBINED"); |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 97 | } else if (ConnectivityManager.isNetworkTypeMobile(mType)) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 98 | builder.append(TelephonyManager.getNetworkTypeName(mSubType)); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 99 | } else { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 100 | builder.append(mSubType); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 101 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 102 | 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 Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 111 | builder.append(", metered=").append(mMetered); |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 112 | return builder.append("}").toString(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 115 | 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 132 | 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 Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 144 | public String getNetworkId() { |
| 145 | return mNetworkId; |
| 146 | } |
| 147 | |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 148 | public boolean getRoaming() { |
| 149 | return mRoaming; |
| 150 | } |
| 151 | |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 152 | public boolean getMetered() { |
| 153 | return mMetered; |
| 154 | } |
| 155 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 156 | /** |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 157 | * Scrub given IMSI on production builds. |
| 158 | */ |
| 159 | public static String scrubSubscriberId(String subscriberId) { |
| 160 | if ("eng".equals(Build.TYPE)) { |
| 161 | return subscriberId; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 162 | } 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 Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 165 | } else { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 166 | return "null"; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 171 | * 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 183 | * 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 Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 190 | String subscriberId = null; |
| 191 | String networkId = null; |
| 192 | boolean roaming = false; |
Stephen Chen | 2514787 | 2016-10-21 12:44:26 -0700 | [diff] [blame] | 193 | boolean metered = !state.networkCapabilities.hasCapability( |
| 194 | NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 195 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 196 | if (isNetworkTypeMobile(type)) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 197 | if (state.subscriberId == null) { |
Jack Yu | bfd1d6e | 2016-08-23 16:09:36 -0700 | [diff] [blame] | 198 | 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 Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 203 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 204 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 205 | subscriberId = state.subscriberId; |
| 206 | roaming = state.networkInfo.isRoaming(); |
| 207 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 208 | } else if (type == TYPE_WIFI) { |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 209 | 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 217 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 218 | |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 219 | return new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 220 | } |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 221 | |
| 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 Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 237 | if (res == 0) { |
| 238 | res = Boolean.compare(mMetered, another.mMetered); |
| 239 | } |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 240 | return res; |
| 241 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 242 | } |