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; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 27 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 28 | import android.util.Slog; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 29 | |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 30 | import java.util.Objects; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Network definition that includes strong identity. Analogous to combining |
| 34 | * {@link NetworkInfo} and an IMSI. |
| 35 | * |
| 36 | * @hide |
| 37 | */ |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 38 | public class NetworkIdentity implements Comparable<NetworkIdentity> { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 39 | private static final String TAG = "NetworkIdentity"; |
| 40 | |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 41 | /** |
| 42 | * When enabled, combine all {@link #mSubType} together under |
| 43 | * {@link #SUBTYPE_COMBINED}. |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 44 | * |
| 45 | * @deprecated we no longer offer to collect statistics on a per-subtype |
| 46 | * basis; this is always disabled. |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 47 | */ |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 48 | @Deprecated |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 49 | public static final boolean COMBINE_SUBTYPE_ENABLED = true; |
| 50 | |
| 51 | public static final int SUBTYPE_COMBINED = -1; |
| 52 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 53 | final int mType; |
| 54 | final int mSubType; |
| 55 | final String mSubscriberId; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 56 | final String mNetworkId; |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 57 | final boolean mRoaming; |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 58 | final boolean mMetered; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 59 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 60 | public NetworkIdentity( |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 61 | int type, int subType, String subscriberId, String networkId, boolean roaming, |
| 62 | boolean metered) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 63 | mType = type; |
| 64 | mSubType = COMBINE_SUBTYPE_ENABLED ? SUBTYPE_COMBINED : subType; |
| 65 | mSubscriberId = subscriberId; |
| 66 | mNetworkId = networkId; |
| 67 | mRoaming = roaming; |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 68 | mMetered = metered; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int hashCode() { |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 73 | return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming, mMetered); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean equals(Object obj) { |
| 78 | if (obj instanceof NetworkIdentity) { |
| 79 | final NetworkIdentity ident = (NetworkIdentity) obj; |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 80 | return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 81 | && Objects.equals(mSubscriberId, ident.mSubscriberId) |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 82 | && Objects.equals(mNetworkId, ident.mNetworkId) |
| 83 | && mMetered == ident.mMetered; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public String toString() { |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 90 | final StringBuilder builder = new StringBuilder("{"); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 91 | builder.append("type=").append(getNetworkTypeName(mType)); |
| 92 | builder.append(", subType="); |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 93 | if (COMBINE_SUBTYPE_ENABLED) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 94 | builder.append("COMBINED"); |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 95 | } else if (ConnectivityManager.isNetworkTypeMobile(mType)) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 96 | builder.append(TelephonyManager.getNetworkTypeName(mSubType)); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 97 | } else { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 98 | builder.append(mSubType); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 99 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 100 | 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 Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 109 | builder.append(", metered=").append(mMetered); |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 110 | return builder.append("}").toString(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 111 | } |
| 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 Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 125 | public String getNetworkId() { |
| 126 | return mNetworkId; |
| 127 | } |
| 128 | |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 129 | public boolean getRoaming() { |
| 130 | return mRoaming; |
| 131 | } |
| 132 | |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 133 | public boolean getMetered() { |
| 134 | return mMetered; |
| 135 | } |
| 136 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 137 | /** |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 138 | * Scrub given IMSI on production builds. |
| 139 | */ |
| 140 | public static String scrubSubscriberId(String subscriberId) { |
| 141 | if ("eng".equals(Build.TYPE)) { |
| 142 | return subscriberId; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 143 | } 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 Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 146 | } else { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 147 | return "null"; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 152 | * 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 164 | * 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 Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 171 | String subscriberId = null; |
| 172 | String networkId = null; |
| 173 | boolean roaming = false; |
Stephen Chen | c926b73 | 2016-10-21 12:44:26 -0700 | [diff] [blame] | 174 | boolean metered = !state.networkCapabilities.hasCapability( |
| 175 | NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 176 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 177 | if (isNetworkTypeMobile(type)) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 178 | if (state.subscriberId == null) { |
Jack Yu | bfd1d6e | 2016-08-23 16:09:36 -0700 | [diff] [blame] | 179 | 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 Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 184 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 185 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 186 | subscriberId = state.subscriberId; |
| 187 | roaming = state.networkInfo.isRoaming(); |
| 188 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 189 | } else if (type == TYPE_WIFI) { |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 190 | 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 198 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 199 | |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 200 | return new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 201 | } |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 202 | |
| 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 Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 218 | if (res == 0) { |
| 219 | res = Boolean.compare(mMetered, another.mMetered); |
| 220 | } |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 221 | return res; |
| 222 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 223 | } |