blob: aa6400bbf5258021c4a100b4d03b57e89eea87a2 [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
19import static android.net.ConnectivityManager.isNetworkTypeMobile;
20
21import android.content.Context;
Jeff Sharkey02e21d62011-07-17 15:53:33 -070022import android.os.Build;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070023import android.telephony.TelephonyManager;
24
25import com.android.internal.util.Objects;
26
27/**
28 * Network definition that includes strong identity. Analogous to combining
29 * {@link NetworkInfo} and an IMSI.
30 *
31 * @hide
32 */
33public class NetworkIdentity {
34 final int mType;
35 final int mSubType;
36 final String mSubscriberId;
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070037 final boolean mRoaming;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070038
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070039 public NetworkIdentity(int type, int subType, String subscriberId, boolean roaming) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070040 this.mType = type;
41 this.mSubType = subType;
42 this.mSubscriberId = subscriberId;
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070043 this.mRoaming = roaming;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070044 }
45
46 @Override
47 public int hashCode() {
48 return Objects.hashCode(mType, mSubType, mSubscriberId);
49 }
50
51 @Override
52 public boolean equals(Object obj) {
53 if (obj instanceof NetworkIdentity) {
54 final NetworkIdentity ident = (NetworkIdentity) obj;
55 return mType == ident.mType && mSubType == ident.mSubType
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070056 && Objects.equal(mSubscriberId, ident.mSubscriberId)
57 && mRoaming == ident.mRoaming;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070058 }
59 return false;
60 }
61
62 @Override
63 public String toString() {
64 final String typeName = ConnectivityManager.getNetworkTypeName(mType);
65 final String subTypeName;
66 if (ConnectivityManager.isNetworkTypeMobile(mType)) {
67 subTypeName = TelephonyManager.getNetworkTypeName(mSubType);
68 } else {
69 subTypeName = Integer.toString(mSubType);
70 }
71
Jeff Sharkey02e21d62011-07-17 15:53:33 -070072 final String scrubSubscriberId = scrubSubscriberId(mSubscriberId);
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070073 final String roaming = mRoaming ? ", ROAMING" : "";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070074 return "[type=" + typeName + ", subType=" + subTypeName + ", subscriberId="
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070075 + scrubSubscriberId + roaming + "]";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070076 }
77
78 public int getType() {
79 return mType;
80 }
81
82 public int getSubType() {
83 return mSubType;
84 }
85
86 public String getSubscriberId() {
87 return mSubscriberId;
88 }
89
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070090 public boolean getRoaming() {
91 return mRoaming;
92 }
93
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070094 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -070095 * Scrub given IMSI on production builds.
96 */
97 public static String scrubSubscriberId(String subscriberId) {
98 if ("eng".equals(Build.TYPE)) {
99 return subscriberId;
100 } else {
101 return subscriberId != null ? "valid" : "null";
102 }
103 }
104
105 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700106 * Build a {@link NetworkIdentity} from the given {@link NetworkState},
107 * assuming that any mobile networks are using the current IMSI.
108 */
109 public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state) {
110 final int type = state.networkInfo.getType();
111 final int subType = state.networkInfo.getSubtype();
112
113 // TODO: consider moving subscriberId over to LinkCapabilities, so it
114 // comes from an authoritative source.
115
116 final String subscriberId;
Jeff Sharkey5dc0c262011-06-19 22:21:05 -0700117 final boolean roaming;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700118 if (isNetworkTypeMobile(type)) {
Jeff Sharkey5dc0c262011-06-19 22:21:05 -0700119 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
120 Context.TELEPHONY_SERVICE);
121 roaming = telephony.isNetworkRoaming();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700122 if (state.subscriberId != null) {
123 subscriberId = state.subscriberId;
124 } else {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700125 subscriberId = telephony.getSubscriberId();
126 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700127 } else {
128 subscriberId = null;
Jeff Sharkey5dc0c262011-06-19 22:21:05 -0700129 roaming = false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700130 }
Jeff Sharkey5dc0c262011-06-19 22:21:05 -0700131 return new NetworkIdentity(type, subType, subscriberId, roaming);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700132 }
133
134}