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; 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 */ 32 public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements 33 Comparable<NetworkIdentitySet> { 34 private static final int VERSION_INIT = 1; 35 private static final int VERSION_ADD_ROAMING = 2; 36 private static final int VERSION_ADD_NETWORK_ID = 3; 37 38 public NetworkIdentitySet() { 39 } 40 41 public NetworkIdentitySet(DataInputStream in) throws IOException { 42 final int version = in.readInt(); 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(); 47 } 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; 56 } 57 final boolean roaming; 58 if (version >= VERSION_ADD_ROAMING) { 59 roaming = in.readBoolean(); 60 } else { 61 roaming = false; 62 } 63 64 add(new NetworkIdentity(type, subType, subscriberId, networkId, false)); 65 } 66 } 67 68 public void writeToStream(DataOutputStream out) throws IOException { 69 out.writeInt(VERSION_ADD_NETWORK_ID); 70 out.writeInt(size()); 71 for (NetworkIdentity ident : this) { 72 out.writeInt(ident.getType()); 73 out.writeInt(ident.getSubType()); 74 writeOptionalString(out, ident.getSubscriberId()); 75 writeOptionalString(out, ident.getNetworkId()); 76 out.writeBoolean(ident.getRoaming()); 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 } 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 } 106 } 107