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 import android.service.NetworkIdentitySetProto; 21 import android.util.proto.ProtoOutputStream; 22 23 import java.io.DataInputStream; 24 import java.io.DataOutputStream; 25 import java.io.IOException; 26 import java.util.HashSet; 27 28 import static android.net.ConnectivityManager.TYPE_MOBILE; 29 30 /** 31 * Identity of a {@code iface}, defined by the set of {@link NetworkIdentity} 32 * active on that interface. 33 * 34 * @hide 35 */ 36 public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements 37 Comparable<NetworkIdentitySet> { 38 private static final int VERSION_INIT = 1; 39 private static final int VERSION_ADD_ROAMING = 2; 40 private static final int VERSION_ADD_NETWORK_ID = 3; 41 private static final int VERSION_ADD_METERED = 4; 42 43 public NetworkIdentitySet() { 44 } 45 46 public NetworkIdentitySet(DataInputStream in) throws IOException { 47 final int version = in.readInt(); 48 final int size = in.readInt(); 49 for (int i = 0; i < size; i++) { 50 if (version <= VERSION_INIT) { 51 final int ignored = in.readInt(); 52 } 53 final int type = in.readInt(); 54 final int subType = in.readInt(); 55 final String subscriberId = readOptionalString(in); 56 final String networkId; 57 if (version >= VERSION_ADD_NETWORK_ID) { 58 networkId = readOptionalString(in); 59 } else { 60 networkId = null; 61 } 62 final boolean roaming; 63 if (version >= VERSION_ADD_ROAMING) { 64 roaming = in.readBoolean(); 65 } else { 66 roaming = false; 67 } 68 69 final boolean metered; 70 if (version >= VERSION_ADD_METERED) { 71 metered = in.readBoolean(); 72 } else { 73 // If this is the old data and the type is mobile, treat it as metered. (Note that 74 // if this is a mobile network, TYPE_MOBILE is the only possible type that could be 75 // used.) 76 metered = (type == TYPE_MOBILE); 77 } 78 79 add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered)); 80 } 81 } 82 83 public void writeToStream(DataOutputStream out) throws IOException { 84 out.writeInt(VERSION_ADD_METERED); 85 out.writeInt(size()); 86 for (NetworkIdentity ident : this) { 87 out.writeInt(ident.getType()); 88 out.writeInt(ident.getSubType()); 89 writeOptionalString(out, ident.getSubscriberId()); 90 writeOptionalString(out, ident.getNetworkId()); 91 out.writeBoolean(ident.getRoaming()); 92 out.writeBoolean(ident.getMetered()); 93 } 94 } 95 96 /** @return whether any {@link NetworkIdentity} in this set is considered metered. */ 97 public boolean isAnyMemberMetered() { 98 if (isEmpty()) { 99 return false; 100 } 101 for (NetworkIdentity ident : this) { 102 if (ident.getMetered()) { 103 return true; 104 } 105 } 106 return false; 107 } 108 109 /** @return whether any {@link NetworkIdentity} in this set is considered roaming. */ 110 public boolean isAnyMemberRoaming() { 111 if (isEmpty()) { 112 return false; 113 } 114 for (NetworkIdentity ident : this) { 115 if (ident.getRoaming()) { 116 return true; 117 } 118 } 119 return false; 120 } 121 122 private static void writeOptionalString(DataOutputStream out, String value) throws IOException { 123 if (value != null) { 124 out.writeByte(1); 125 out.writeUTF(value); 126 } else { 127 out.writeByte(0); 128 } 129 } 130 131 private static String readOptionalString(DataInputStream in) throws IOException { 132 if (in.readByte() != 0) { 133 return in.readUTF(); 134 } else { 135 return null; 136 } 137 } 138 139 @Override 140 public int compareTo(NetworkIdentitySet another) { 141 if (isEmpty()) return -1; 142 if (another.isEmpty()) return 1; 143 144 final NetworkIdentity ident = iterator().next(); 145 final NetworkIdentity anotherIdent = another.iterator().next(); 146 return ident.compareTo(anotherIdent); 147 } 148 149 public void writeToProto(ProtoOutputStream proto, long tag) { 150 final long start = proto.start(tag); 151 152 for (NetworkIdentity ident : this) { 153 ident.writeToProto(proto, NetworkIdentitySetProto.IDENTITIES); 154 } 155 156 proto.end(start); 157 } 158 } 159