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 | |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 26 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
| 27 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Identity of a {@code iface}, defined by the set of {@link NetworkIdentity} |
| 30 | * active on that interface. |
| 31 | * |
| 32 | * @hide |
| 33 | */ |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 34 | public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements |
| 35 | Comparable<NetworkIdentitySet> { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 36 | private static final int VERSION_INIT = 1; |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 37 | private static final int VERSION_ADD_ROAMING = 2; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 38 | private static final int VERSION_ADD_NETWORK_ID = 3; |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 39 | private static final int VERSION_ADD_METERED = 4; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 40 | |
| 41 | public NetworkIdentitySet() { |
| 42 | } |
| 43 | |
| 44 | public NetworkIdentitySet(DataInputStream in) throws IOException { |
| 45 | final int version = in.readInt(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 46 | final int size = in.readInt(); |
| 47 | for (int i = 0; i < size; i++) { |
| 48 | if (version <= VERSION_INIT) { |
| 49 | final int ignored = in.readInt(); |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 50 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 51 | final int type = in.readInt(); |
| 52 | final int subType = in.readInt(); |
| 53 | final String subscriberId = readOptionalString(in); |
| 54 | final String networkId; |
| 55 | if (version >= VERSION_ADD_NETWORK_ID) { |
| 56 | networkId = readOptionalString(in); |
| 57 | } else { |
| 58 | networkId = null; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 59 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 60 | final boolean roaming; |
| 61 | if (version >= VERSION_ADD_ROAMING) { |
| 62 | roaming = in.readBoolean(); |
| 63 | } else { |
| 64 | roaming = false; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 65 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 66 | |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 67 | final boolean metered; |
| 68 | if (version >= VERSION_ADD_METERED) { |
| 69 | metered = in.readBoolean(); |
| 70 | } else { |
| 71 | // If this is the old data and the type is mobile, treat it as metered. (Note that |
| 72 | // if this is a mobile network, TYPE_MOBILE is the only possible type that could be |
| 73 | // used.) |
| 74 | metered = (type == TYPE_MOBILE); |
| 75 | } |
| 76 | |
| 77 | add(new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered)); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | public void writeToStream(DataOutputStream out) throws IOException { |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 82 | out.writeInt(VERSION_ADD_METERED); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 83 | out.writeInt(size()); |
| 84 | for (NetworkIdentity ident : this) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 85 | out.writeInt(ident.getType()); |
| 86 | out.writeInt(ident.getSubType()); |
| 87 | writeOptionalString(out, ident.getSubscriberId()); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 88 | writeOptionalString(out, ident.getNetworkId()); |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 89 | out.writeBoolean(ident.getRoaming()); |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 90 | out.writeBoolean(ident.getMetered()); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Stephen Chen | 2514787 | 2016-10-21 12:44:26 -0700 | [diff] [blame] | 94 | /** @return whether any {@link NetworkIdentity} in this set is considered metered. */ |
| 95 | public boolean isAnyMemberMetered() { |
| 96 | if (isEmpty()) { |
| 97 | return false; |
| 98 | } |
| 99 | for (NetworkIdentity ident : this) { |
| 100 | if (ident.getMetered()) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 107 | /** @return whether any {@link NetworkIdentity} in this set is considered roaming. */ |
| 108 | public boolean isAnyMemberRoaming() { |
| 109 | if (isEmpty()) { |
| 110 | return false; |
| 111 | } |
| 112 | for (NetworkIdentity ident : this) { |
| 113 | if (ident.getRoaming()) { |
| 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 120 | private static void writeOptionalString(DataOutputStream out, String value) throws IOException { |
| 121 | if (value != null) { |
| 122 | out.writeByte(1); |
| 123 | out.writeUTF(value); |
| 124 | } else { |
| 125 | out.writeByte(0); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private static String readOptionalString(DataInputStream in) throws IOException { |
| 130 | if (in.readByte() != 0) { |
| 131 | return in.readUTF(); |
| 132 | } else { |
| 133 | return null; |
| 134 | } |
| 135 | } |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 136 | |
| 137 | @Override |
| 138 | public int compareTo(NetworkIdentitySet another) { |
| 139 | if (isEmpty()) return -1; |
| 140 | if (another.isEmpty()) return 1; |
| 141 | |
| 142 | final NetworkIdentity ident = iterator().next(); |
| 143 | final NetworkIdentity anotherIdent = another.iterator().next(); |
| 144 | return ident.compareTo(anotherIdent); |
| 145 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 146 | } |