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 com.android.server.net; |
| 18 | |
| 19 | import android.net.NetworkIdentity; |
| 20 | |
| 21 | import java.io.DataInputStream; |
| 22 | import java.io.DataOutputStream; |
| 23 | import java.io.IOException; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 24 | import 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 Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 32 | public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements |
| 33 | Comparable<NetworkIdentitySet> { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 34 | private static final int VERSION_INIT = 1; |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 35 | private static final int VERSION_ADD_ROAMING = 2; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 36 | private static final int VERSION_ADD_NETWORK_ID = 3; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 37 | |
| 38 | public NetworkIdentitySet() { |
| 39 | } |
| 40 | |
| 41 | public NetworkIdentitySet(DataInputStream in) throws IOException { |
| 42 | final int version = in.readInt(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 43 | 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 Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 47 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 48 | 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 56 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 57 | final boolean roaming; |
| 58 | if (version >= VERSION_ADD_ROAMING) { |
| 59 | roaming = in.readBoolean(); |
| 60 | } else { |
| 61 | roaming = false; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 62 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 63 | |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 64 | add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming)); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | public void writeToStream(DataOutputStream out) throws IOException { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 69 | out.writeInt(VERSION_ADD_NETWORK_ID); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 70 | out.writeInt(size()); |
| 71 | for (NetworkIdentity ident : this) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 72 | out.writeInt(ident.getType()); |
| 73 | out.writeInt(ident.getSubType()); |
| 74 | writeOptionalString(out, ident.getSubscriberId()); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 75 | writeOptionalString(out, ident.getNetworkId()); |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 76 | out.writeBoolean(ident.getRoaming()); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 80 | /** @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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 93 | 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 Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 109 | |
| 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 Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 119 | } |