Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -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 android.net; |
| 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | import android.os.SystemClock; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 22 | import android.util.SparseBooleanArray; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 23 | |
| 24 | import java.io.CharArrayWriter; |
| 25 | import java.io.PrintWriter; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 26 | import java.util.Arrays; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 27 | import java.util.HashSet; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 30 | * Collection of active network statistics. Can contain summary details across |
| 31 | * all interfaces, or details with per-UID granularity. Internally stores data |
| 32 | * as a large table, closely matching {@code /proc/} data format. This structure |
| 33 | * optimizes for rapid in-memory comparison, but consider using |
| 34 | * {@link NetworkStatsHistory} when persisting. |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 35 | * |
| 36 | * @hide |
| 37 | */ |
| 38 | public class NetworkStats implements Parcelable { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 39 | /** {@link #iface} value when interface details unavailable. */ |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 40 | public static final String IFACE_ALL = null; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 41 | /** {@link #uid} value when UID details unavailable. */ |
| 42 | public static final int UID_ALL = -1; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 43 | /** {@link #tag} value for without tag. */ |
| 44 | public static final int TAG_NONE = 0; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 45 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 46 | // TODO: move public fields to Entry accessors, then undeprecate |
| 47 | // TODO: refactor rx/tx to rxBytes/txBytes |
| 48 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 49 | /** |
| 50 | * {@link SystemClock#elapsedRealtime()} timestamp when this data was |
| 51 | * generated. |
| 52 | */ |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 53 | @Deprecated |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 54 | public final long elapsedRealtime; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 55 | @Deprecated |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 56 | public int size; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 57 | @Deprecated |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 58 | public String[] iface; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 59 | @Deprecated |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 60 | public int[] uid; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 61 | @Deprecated |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 62 | public int[] tag; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 63 | @Deprecated |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 64 | public long[] rx; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 65 | @Deprecated |
| 66 | public long[] rxPackets; |
| 67 | @Deprecated |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 68 | public long[] tx; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 69 | @Deprecated |
| 70 | public long[] txPackets; |
| 71 | |
| 72 | public static class Entry { |
| 73 | public String iface; |
| 74 | public int uid; |
| 75 | public int tag; |
| 76 | public long rxBytes; |
| 77 | public long rxPackets; |
| 78 | public long txBytes; |
| 79 | public long txPackets; |
| 80 | } |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 81 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 82 | public NetworkStats(long elapsedRealtime, int initialSize) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 83 | this.elapsedRealtime = elapsedRealtime; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 84 | this.size = 0; |
| 85 | this.iface = new String[initialSize]; |
| 86 | this.uid = new int[initialSize]; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 87 | this.tag = new int[initialSize]; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 88 | this.rx = new long[initialSize]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 89 | this.rxPackets = new long[initialSize]; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 90 | this.tx = new long[initialSize]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 91 | this.txPackets = new long[initialSize]; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | public NetworkStats(Parcel parcel) { |
| 95 | elapsedRealtime = parcel.readLong(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 96 | size = parcel.readInt(); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 97 | iface = parcel.createStringArray(); |
| 98 | uid = parcel.createIntArray(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 99 | tag = parcel.createIntArray(); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 100 | rx = parcel.createLongArray(); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 101 | rxPackets = parcel.createLongArray(); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 102 | tx = parcel.createLongArray(); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 103 | txPackets = parcel.createLongArray(); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Add new stats entry with given values. |
| 108 | */ |
| 109 | public NetworkStats addEntry(String iface, int uid, int tag, long rx, long tx) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 110 | final Entry entry = new Entry(); |
| 111 | entry.iface = iface; |
| 112 | entry.uid = uid; |
| 113 | entry.tag = tag; |
| 114 | entry.rxBytes = rx; |
| 115 | entry.txBytes = tx; |
| 116 | return addValues(entry); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Add new stats entry, copying from given {@link Entry}. The {@link Entry} |
| 121 | * object can be recycled across multiple calls. |
| 122 | */ |
| 123 | public NetworkStats addValues(Entry entry) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 124 | if (size >= this.iface.length) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 125 | final int newLength = Math.max(iface.length, 10) * 3 / 2; |
| 126 | iface = Arrays.copyOf(iface, newLength); |
| 127 | uid = Arrays.copyOf(uid, newLength); |
| 128 | tag = Arrays.copyOf(tag, newLength); |
| 129 | rx = Arrays.copyOf(rx, newLength); |
| 130 | rxPackets = Arrays.copyOf(rxPackets, newLength); |
| 131 | tx = Arrays.copyOf(tx, newLength); |
| 132 | txPackets = Arrays.copyOf(txPackets, newLength); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 135 | iface[size] = entry.iface; |
| 136 | uid[size] = entry.uid; |
| 137 | tag[size] = entry.tag; |
| 138 | rx[size] = entry.rxBytes; |
| 139 | rxPackets[size] = entry.rxPackets; |
| 140 | tx[size] = entry.txBytes; |
| 141 | txPackets[size] = entry.txPackets; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 142 | size++; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 143 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 144 | return this; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 147 | /** |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 148 | * Return specific stats entry. |
| 149 | */ |
| 150 | public Entry getValues(int i, Entry recycle) { |
| 151 | final Entry entry = recycle != null ? recycle : new Entry(); |
| 152 | entry.iface = iface[i]; |
| 153 | entry.uid = uid[i]; |
| 154 | entry.tag = tag[i]; |
| 155 | entry.rxBytes = rx[i]; |
| 156 | entry.rxPackets = rxPackets[i]; |
| 157 | entry.txBytes = tx[i]; |
| 158 | entry.txPackets = txPackets[i]; |
| 159 | return entry; |
| 160 | } |
| 161 | |
| 162 | public long getElapsedRealtime() { |
| 163 | return elapsedRealtime; |
| 164 | } |
| 165 | |
| 166 | public int size() { |
| 167 | return size; |
| 168 | } |
| 169 | |
| 170 | /** |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 171 | * Combine given values with an existing row, or create a new row if |
| 172 | * {@link #findIndex(String, int, int)} is unable to find match. Can also be |
| 173 | * used to subtract values from existing rows. |
| 174 | */ |
| 175 | public NetworkStats combineEntry(String iface, int uid, int tag, long rx, long tx) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 176 | // TODO: extent to accept rxPackets/txPackets |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 177 | final int i = findIndex(iface, uid, tag); |
| 178 | if (i == -1) { |
| 179 | // only create new entry when positive contribution |
| 180 | addEntry(iface, uid, tag, rx, tx); |
| 181 | } else { |
| 182 | this.rx[i] += rx; |
| 183 | this.tx[i] += tx; |
| 184 | } |
| 185 | return this; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 188 | /** |
| 189 | * Find first stats index that matches the requested parameters. |
| 190 | */ |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 191 | public int findIndex(String iface, int uid, int tag) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 192 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 193 | if (equal(iface, this.iface[i]) && uid == this.uid[i] && tag == this.tag[i]) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 194 | return i; |
| 195 | } |
| 196 | } |
| 197 | return -1; |
| 198 | } |
| 199 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 200 | /** |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 201 | * Return list of unique interfaces known by this data structure. |
| 202 | */ |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 203 | public String[] getUniqueIfaces() { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 204 | final HashSet<String> ifaces = new HashSet<String>(); |
| 205 | for (String iface : this.iface) { |
| 206 | if (iface != IFACE_ALL) { |
| 207 | ifaces.add(iface); |
| 208 | } |
| 209 | } |
| 210 | return ifaces.toArray(new String[ifaces.size()]); |
| 211 | } |
| 212 | |
| 213 | /** |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 214 | * Return list of unique UIDs known by this data structure. |
| 215 | */ |
| 216 | public int[] getUniqueUids() { |
| 217 | final SparseBooleanArray uids = new SparseBooleanArray(); |
| 218 | for (int uid : this.uid) { |
| 219 | uids.put(uid, true); |
| 220 | } |
| 221 | |
| 222 | final int size = uids.size(); |
| 223 | final int[] result = new int[size]; |
| 224 | for (int i = 0; i < size; i++) { |
| 225 | result[i] = uids.keyAt(i); |
| 226 | } |
| 227 | return result; |
| 228 | } |
| 229 | |
| 230 | /** |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 231 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 232 | * between two snapshots in time. Assumes that statistics rows collect over |
| 233 | * time, and that none of them have disappeared. |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 234 | * |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 235 | * @throws IllegalArgumentException when given {@link NetworkStats} is |
| 236 | * non-monotonic. |
| 237 | */ |
| 238 | public NetworkStats subtract(NetworkStats value) { |
| 239 | return subtract(value, true, false); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 244 | * between two snapshots in time. Assumes that statistics rows collect over |
| 245 | * time, and that none of them have disappeared. |
| 246 | * <p> |
| 247 | * Instead of throwing when counters are non-monotonic, this variant clamps |
| 248 | * results to never be negative. |
| 249 | */ |
| 250 | public NetworkStats subtractClamped(NetworkStats value) { |
| 251 | return subtract(value, false, true); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 256 | * between two snapshots in time. Assumes that statistics rows collect over |
| 257 | * time, and that none of them have disappeared. |
| 258 | * |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 259 | * @param enforceMonotonic Validate that incoming value is strictly |
| 260 | * monotonic compared to this object. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 261 | * @param clampNegative Instead of throwing like {@code enforceMonotonic}, |
| 262 | * clamp resulting counters at 0 to prevent negative values. |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 263 | */ |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 264 | private NetworkStats subtract( |
| 265 | NetworkStats value, boolean enforceMonotonic, boolean clampNegative) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 266 | final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime; |
| 267 | if (enforceMonotonic && deltaRealtime < 0) { |
| 268 | throw new IllegalArgumentException("found non-monotonic realtime"); |
| 269 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 270 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 271 | // result will have our rows, and elapsed time between snapshots |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 272 | final Entry entry = new Entry(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 273 | final NetworkStats result = new NetworkStats(deltaRealtime, size); |
| 274 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 275 | entry.iface = iface[i]; |
| 276 | entry.uid = uid[i]; |
| 277 | entry.tag = tag[i]; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 278 | |
| 279 | // find remote row that matches, and subtract |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 280 | final int j = value.findIndex(entry.iface, entry.uid, entry.tag); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 281 | if (j == -1) { |
| 282 | // newly appearing row, return entire value |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 283 | entry.rxBytes = rx[i]; |
| 284 | entry.rxPackets = rxPackets[i]; |
| 285 | entry.txBytes = tx[i]; |
| 286 | entry.txPackets = txPackets[i]; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 287 | } else { |
| 288 | // existing row, subtract remote value |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 289 | entry.rxBytes = rx[i] - value.rx[j]; |
| 290 | entry.rxPackets = rxPackets[i] - value.rxPackets[j]; |
| 291 | entry.txBytes = tx[i] - value.tx[j]; |
| 292 | entry.txPackets = txPackets[i] - value.txPackets[j]; |
| 293 | if (enforceMonotonic |
| 294 | && (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0 |
| 295 | || entry.txPackets < 0)) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 296 | throw new IllegalArgumentException("found non-monotonic values"); |
| 297 | } |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 298 | if (clampNegative) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 299 | entry.rxBytes = Math.max(0, entry.rxBytes); |
| 300 | entry.rxPackets = Math.max(0, entry.rxPackets); |
| 301 | entry.txBytes = Math.max(0, entry.txBytes); |
| 302 | entry.txPackets = Math.max(0, entry.txPackets); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 303 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 304 | } |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 305 | |
| 306 | result.addValues(entry); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 309 | return result; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 312 | private static boolean equal(Object a, Object b) { |
| 313 | return a == b || (a != null && a.equals(b)); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | public void dump(String prefix, PrintWriter pw) { |
| 317 | pw.print(prefix); |
| 318 | pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 319 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 320 | pw.print(prefix); |
| 321 | pw.print(" iface="); pw.print(iface[i]); |
| 322 | pw.print(" uid="); pw.print(uid[i]); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 323 | pw.print(" tag="); pw.print(tag[i]); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 324 | pw.print(" rxBytes="); pw.print(rx[i]); |
| 325 | pw.print(" rxPackets="); pw.print(rxPackets[i]); |
| 326 | pw.print(" txBytes="); pw.print(tx[i]); |
| 327 | pw.print(" txPackets="); pw.println(txPackets[i]); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public String toString() { |
| 333 | final CharArrayWriter writer = new CharArrayWriter(); |
| 334 | dump("", new PrintWriter(writer)); |
| 335 | return writer.toString(); |
| 336 | } |
| 337 | |
| 338 | /** {@inheritDoc} */ |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 339 | public int describeContents() { |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /** {@inheritDoc} */ |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 344 | public void writeToParcel(Parcel dest, int flags) { |
| 345 | dest.writeLong(elapsedRealtime); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 346 | dest.writeInt(size); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 347 | dest.writeStringArray(iface); |
| 348 | dest.writeIntArray(uid); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 349 | dest.writeIntArray(tag); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 350 | dest.writeLongArray(rx); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 351 | dest.writeLongArray(rxPackets); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 352 | dest.writeLongArray(tx); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame^] | 353 | dest.writeLongArray(txPackets); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() { |
| 357 | public NetworkStats createFromParcel(Parcel in) { |
| 358 | return new NetworkStats(in); |
| 359 | } |
| 360 | |
| 361 | public NetworkStats[] newArray(int size) { |
| 362 | return new NetworkStats[size]; |
| 363 | } |
| 364 | }; |
| 365 | } |