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 | |
| 64 | add(new NetworkIdentity(type, subType, subscriberId, networkId, false)); |
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 | |
| 80 | private static void writeOptionalString(DataOutputStream out, String value) throws IOException { |
| 81 | if (value != null) { |
| 82 | out.writeByte(1); |
| 83 | out.writeUTF(value); |
| 84 | } else { |
| 85 | out.writeByte(0); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private static String readOptionalString(DataInputStream in) throws IOException { |
| 90 | if (in.readByte() != 0) { |
| 91 | return in.readUTF(); |
| 92 | } else { |
| 93 | return null; |
| 94 | } |
| 95 | } |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 96 | |
| 97 | @Override |
| 98 | public int compareTo(NetworkIdentitySet another) { |
| 99 | if (isEmpty()) return -1; |
| 100 | if (another.isEmpty()) return 1; |
| 101 | |
| 102 | final NetworkIdentity ident = iterator().next(); |
| 103 | final NetworkIdentity anotherIdent = another.iterator().next(); |
| 104 | return ident.compareTo(anotherIdent); |
| 105 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 106 | } |