blob: c48f43058fca72e30225ab75995f99e993b73a89 [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 com.android.server.net;
18
19import android.net.NetworkIdentity;
20
21import java.io.DataInputStream;
22import java.io.DataOutputStream;
23import java.io.IOException;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070024import java.util.HashSet;
25
Jack Yu66a6be32016-03-30 11:14:39 -070026import static android.net.ConnectivityManager.TYPE_MOBILE;
27
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070028/**
29 * Identity of a {@code iface}, defined by the set of {@link NetworkIdentity}
30 * active on that interface.
31 *
32 * @hide
33 */
Jeff Sharkey55a442e2014-11-18 18:22:21 -080034public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
35 Comparable<NetworkIdentitySet> {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070036 private static final int VERSION_INIT = 1;
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070037 private static final int VERSION_ADD_ROAMING = 2;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070038 private static final int VERSION_ADD_NETWORK_ID = 3;
Jack Yu66a6be32016-03-30 11:14:39 -070039 private static final int VERSION_ADD_METERED = 4;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070040
41 public NetworkIdentitySet() {
42 }
43
44 public NetworkIdentitySet(DataInputStream in) throws IOException {
45 final int version = in.readInt();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070046 final int size = in.readInt();
47 for (int i = 0; i < size; i++) {
48 if (version <= VERSION_INIT) {
49 final int ignored = in.readInt();
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070050 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070051 final int type = in.readInt();
52 final int subType = in.readInt();
53 final String subscriberId = readOptionalString(in);
54 final String networkId;
55 if (version >= VERSION_ADD_NETWORK_ID) {
56 networkId = readOptionalString(in);
57 } else {
58 networkId = null;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070059 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070060 final boolean roaming;
61 if (version >= VERSION_ADD_ROAMING) {
62 roaming = in.readBoolean();
63 } else {
64 roaming = false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070065 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070066
Jack Yu66a6be32016-03-30 11:14:39 -070067 final boolean metered;
68 if (version >= VERSION_ADD_METERED) {
69 metered = in.readBoolean();
70 } else {
71 // If this is the old data and the type is mobile, treat it as metered. (Note that
72 // if this is a mobile network, TYPE_MOBILE is the only possible type that could be
73 // used.)
74 metered = (type == TYPE_MOBILE);
75 }
76
77 add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered));
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070078 }
79 }
80
81 public void writeToStream(DataOutputStream out) throws IOException {
Jack Yu66a6be32016-03-30 11:14:39 -070082 out.writeInt(VERSION_ADD_METERED);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070083 out.writeInt(size());
84 for (NetworkIdentity ident : this) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070085 out.writeInt(ident.getType());
86 out.writeInt(ident.getSubType());
87 writeOptionalString(out, ident.getSubscriberId());
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070088 writeOptionalString(out, ident.getNetworkId());
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070089 out.writeBoolean(ident.getRoaming());
Jack Yu66a6be32016-03-30 11:14:39 -070090 out.writeBoolean(ident.getMetered());
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070091 }
92 }
93
Stephen Chen25147872016-10-21 12:44:26 -070094 /** @return whether any {@link NetworkIdentity} in this set is considered metered. */
95 public boolean isAnyMemberMetered() {
96 if (isEmpty()) {
97 return false;
98 }
99 for (NetworkIdentity ident : this) {
100 if (ident.getMetered()) {
101 return true;
102 }
103 }
104 return false;
105 }
106
Jeff Davidsona6a78072016-01-11 16:02:17 -0800107 /** @return whether any {@link NetworkIdentity} in this set is considered roaming. */
108 public boolean isAnyMemberRoaming() {
109 if (isEmpty()) {
110 return false;
111 }
112 for (NetworkIdentity ident : this) {
113 if (ident.getRoaming()) {
114 return true;
115 }
116 }
117 return false;
118 }
119
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700120 private static void writeOptionalString(DataOutputStream out, String value) throws IOException {
121 if (value != null) {
122 out.writeByte(1);
123 out.writeUTF(value);
124 } else {
125 out.writeByte(0);
126 }
127 }
128
129 private static String readOptionalString(DataInputStream in) throws IOException {
130 if (in.readByte() != 0) {
131 return in.readUTF();
132 } else {
133 return null;
134 }
135 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800136
137 @Override
138 public int compareTo(NetworkIdentitySet another) {
139 if (isEmpty()) return -1;
140 if (another.isEmpty()) return 1;
141
142 final NetworkIdentity ident = iterator().next();
143 final NetworkIdentity anotherIdent = another.iterator().next();
144 return ident.compareTo(anotherIdent);
145 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700146}