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; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 58 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 59 | 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public int hashCode() { |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 70 | return Objects.hash(mType, mSubType, mSubscriberId, mNetworkId, mRoaming); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean equals(Object obj) { |
| 75 | if (obj instanceof NetworkIdentity) { |
| 76 | final NetworkIdentity ident = (NetworkIdentity) obj; |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 77 | return mType == ident.mType && mSubType == ident.mSubType && mRoaming == ident.mRoaming |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 78 | && Objects.equals(mSubscriberId, ident.mSubscriberId) |
| 79 | && Objects.equals(mNetworkId, ident.mNetworkId); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String toString() { |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 86 | final StringBuilder builder = new StringBuilder("{"); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 87 | builder.append("type=").append(getNetworkTypeName(mType)); |
| 88 | builder.append(", subType="); |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 89 | if (COMBINE_SUBTYPE_ENABLED) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 90 | builder.append("COMBINED"); |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 91 | } else if (ConnectivityManager.isNetworkTypeMobile(mType)) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 92 | builder.append(TelephonyManager.getNetworkTypeName(mSubType)); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 93 | } else { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 94 | builder.append(mSubType); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 95 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 96 | 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 Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 105 | return builder.append("}").toString(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 106 | } |
| 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 Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 120 | public String getNetworkId() { |
| 121 | return mNetworkId; |
| 122 | } |
| 123 | |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 124 | public boolean getRoaming() { |
| 125 | return mRoaming; |
| 126 | } |
| 127 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 128 | /** |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 129 | * Scrub given IMSI on production builds. |
| 130 | */ |
| 131 | public static String scrubSubscriberId(String subscriberId) { |
| 132 | if ("eng".equals(Build.TYPE)) { |
| 133 | return subscriberId; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 134 | } 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 Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 137 | } else { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 138 | return "null"; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 143 | * 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 155 | * 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 Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 162 | String subscriberId = null; |
| 163 | String networkId = null; |
| 164 | boolean roaming = false; |
| 165 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 166 | if (isNetworkTypeMobile(type)) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 167 | if (state.subscriberId == null) { |
| 168 | Slog.w(TAG, "Active mobile network without subscriber!"); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 169 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 170 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 171 | subscriberId = state.subscriberId; |
| 172 | roaming = state.networkInfo.isRoaming(); |
| 173 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 174 | } else if (type == TYPE_WIFI) { |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 175 | 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 183 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 184 | |
| 185 | return new NetworkIdentity(type, subType, subscriberId, networkId, roaming); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 186 | } |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 187 | |
| 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 205 | } |