blob: 68dc715b08afaf527aa56801ce4e6fefa069e0d4 [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
26/**
27 * Identity of a {@code iface}, defined by the set of {@link NetworkIdentity}
28 * active on that interface.
29 *
30 * @hide
31 */
Jeff Sharkey55a442e2014-11-18 18:22:21 -080032public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
33 Comparable<NetworkIdentitySet> {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070034 private static final int VERSION_INIT = 1;
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070035 private static final int VERSION_ADD_ROAMING = 2;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070036 private static final int VERSION_ADD_NETWORK_ID = 3;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070037
38 public NetworkIdentitySet() {
39 }
40
41 public NetworkIdentitySet(DataInputStream in) throws IOException {
42 final int version = in.readInt();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070043 final int size = in.readInt();
44 for (int i = 0; i < size; i++) {
45 if (version <= VERSION_INIT) {
46 final int ignored = in.readInt();
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070047 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070048 final int type = in.readInt();
49 final int subType = in.readInt();
50 final String subscriberId = readOptionalString(in);
51 final String networkId;
52 if (version >= VERSION_ADD_NETWORK_ID) {
53 networkId = readOptionalString(in);
54 } else {
55 networkId = null;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070056 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070057 final boolean roaming;
58 if (version >= VERSION_ADD_ROAMING) {
59 roaming = in.readBoolean();
60 } else {
61 roaming = false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070062 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070063
Jeff Davidsona6a78072016-01-11 16:02:17 -080064 add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming));
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070065 }
66 }
67
68 public void writeToStream(DataOutputStream out) throws IOException {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070069 out.writeInt(VERSION_ADD_NETWORK_ID);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070070 out.writeInt(size());
71 for (NetworkIdentity ident : this) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070072 out.writeInt(ident.getType());
73 out.writeInt(ident.getSubType());
74 writeOptionalString(out, ident.getSubscriberId());
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070075 writeOptionalString(out, ident.getNetworkId());
Jeff Sharkey5dc0c262011-06-19 22:21:05 -070076 out.writeBoolean(ident.getRoaming());
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070077 }
78 }
79
Jeff Davidsona6a78072016-01-11 16:02:17 -080080 /** @return whether any {@link NetworkIdentity} in this set is considered roaming. */
81 public boolean isAnyMemberRoaming() {
82 if (isEmpty()) {
83 return false;
84 }
85 for (NetworkIdentity ident : this) {
86 if (ident.getRoaming()) {
87 return true;
88 }
89 }
90 return false;
91 }
92
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070093 private static void writeOptionalString(DataOutputStream out, String value) throws IOException {
94 if (value != null) {
95 out.writeByte(1);
96 out.writeUTF(value);
97 } else {
98 out.writeByte(0);
99 }
100 }
101
102 private static String readOptionalString(DataInputStream in) throws IOException {
103 if (in.readByte() != 0) {
104 return in.readUTF();
105 } else {
106 return null;
107 }
108 }
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800109
110 @Override
111 public int compareTo(NetworkIdentitySet another) {
112 if (isEmpty()) return -1;
113 if (another.isEmpty()) return 1;
114
115 final NetworkIdentity ident = iterator().next();
116 final NetworkIdentity anotherIdent = another.iterator().next();
117 return ident.compareTo(anotherIdent);
118 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700119}