NetworkIdentity.java revision 1b5a2a96f793211bfbd39aa29cc41031dfa23950
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
17package android.net;
18
19import static android.net.ConnectivityManager.isNetworkTypeMobile;
20
21import android.content.Context;
22import android.telephony.TelephonyManager;
23
24import com.android.internal.util.Objects;
25
26/**
27 * Network definition that includes strong identity. Analogous to combining
28 * {@link NetworkInfo} and an IMSI.
29 *
30 * @hide
31 */
32public class NetworkIdentity {
33    final int mType;
34    final int mSubType;
35    final String mSubscriberId;
36
37    public NetworkIdentity(int type, int subType, String subscriberId) {
38        this.mType = type;
39        this.mSubType = subType;
40        this.mSubscriberId = subscriberId;
41    }
42
43    @Override
44    public int hashCode() {
45        return Objects.hashCode(mType, mSubType, mSubscriberId);
46    }
47
48    @Override
49    public boolean equals(Object obj) {
50        if (obj instanceof NetworkIdentity) {
51            final NetworkIdentity ident = (NetworkIdentity) obj;
52            return mType == ident.mType && mSubType == ident.mSubType
53                    && Objects.equal(mSubscriberId, ident.mSubscriberId);
54        }
55        return false;
56    }
57
58    @Override
59    public String toString() {
60        final String typeName = ConnectivityManager.getNetworkTypeName(mType);
61        final String subTypeName;
62        if (ConnectivityManager.isNetworkTypeMobile(mType)) {
63            subTypeName = TelephonyManager.getNetworkTypeName(mSubType);
64        } else {
65            subTypeName = Integer.toString(mSubType);
66        }
67
68        final String scrubSubscriberId = mSubscriberId != null ? "valid" : "null";
69        return "[type=" + typeName + ", subType=" + subTypeName + ", subscriberId="
70                + scrubSubscriberId + "]";
71    }
72
73    public int getType() {
74        return mType;
75    }
76
77    public int getSubType() {
78        return mSubType;
79    }
80
81    public String getSubscriberId() {
82        return mSubscriberId;
83    }
84
85    /**
86     * Build a {@link NetworkIdentity} from the given {@link NetworkState},
87     * assuming that any mobile networks are using the current IMSI.
88     */
89    public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state) {
90        final int type = state.networkInfo.getType();
91        final int subType = state.networkInfo.getSubtype();
92
93        // TODO: consider moving subscriberId over to LinkCapabilities, so it
94        // comes from an authoritative source.
95
96        final String subscriberId;
97        if (isNetworkTypeMobile(type)) {
98            final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
99                    Context.TELEPHONY_SERVICE);
100            subscriberId = telephony.getSubscriberId();
101        } else {
102            subscriberId = null;
103        }
104        return new NetworkIdentity(type, subType, subscriberId);
105    }
106
107}
108