175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey/*
275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Copyright (C) 2011 The Android Open Source Project
375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *
475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * you may not use this file except in compliance with the License.
675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * You may obtain a copy of the License at
775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *
875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *
1075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
1175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
1275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * See the License for the specific language governing permissions and
1475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * limitations under the License.
1575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey */
1675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
1775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeypackage android.net;
1875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
19a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport static android.net.NetworkStats.IFACE_ALL;
20b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkeyimport static android.net.NetworkStats.SET_DEFAULT;
21a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport static android.net.NetworkStats.TAG_NONE;
22a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport static android.net.NetworkStats.UID_ALL;
2363d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.DataStreamUtils.readFullLongArray;
2463d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.DataStreamUtils.readVarLongArray;
2563d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.DataStreamUtils.writeVarLongArray;
2663d27a9233fed934340231f438493746084a681dJeff Sharkeyimport static android.net.NetworkStatsHistory.Entry.UNKNOWN;
27a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport static android.net.NetworkStatsHistory.ParcelUtils.readLongArray;
28a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyimport static android.net.NetworkStatsHistory.ParcelUtils.writeLongArray;
2963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport static com.android.internal.util.ArrayUtils.total;
30a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
3175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.os.Parcel;
3275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.os.Parcelable;
3369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkeyimport android.util.MathUtils;
3475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
3563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport com.android.internal.util.IndentingPrintWriter;
3663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey
3775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.io.CharArrayWriter;
3875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.io.DataInputStream;
3975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.io.DataOutputStream;
4075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.io.IOException;
4161ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkeyimport java.net.ProtocolException;
4275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.util.Arrays;
4361ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkeyimport java.util.Random;
4475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
4575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey/**
4675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Collection of historical network statistics, recorded into equally-sized
4775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * "buckets" in time. Internally it stores data in {@code long} series for more
4875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * efficient persistence.
4975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * <p>
5075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Each bucket is defined by a {@link #bucketStart} timestamp, and lasts for
5175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * {@link #bucketDuration}. Internally assumes that {@link #bucketStart} is
5275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * sorted at all times.
5375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *
5475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * @hide
5575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey */
5675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeypublic class NetworkStatsHistory implements Parcelable {
571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private static final int VERSION_INIT = 1;
5863d27a9233fed934340231f438493746084a681dJeff Sharkey    private static final int VERSION_ADD_PACKETS = 2;
59558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey    private static final int VERSION_ADD_ACTIVE = 3;
6075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
61558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey    public static final int FIELD_ACTIVE_TIME = 0x01;
62558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey    public static final int FIELD_RX_BYTES = 0x02;
63558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey    public static final int FIELD_RX_PACKETS = 0x04;
64558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey    public static final int FIELD_TX_BYTES = 0x08;
65558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey    public static final int FIELD_TX_PACKETS = 0x10;
66558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey    public static final int FIELD_OPERATIONS = 0x20;
67d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey
6863d27a9233fed934340231f438493746084a681dJeff Sharkey    public static final int FIELD_ALL = 0xFFFFFFFF;
6963d27a9233fed934340231f438493746084a681dJeff Sharkey
7063d27a9233fed934340231f438493746084a681dJeff Sharkey    private long bucketDuration;
71d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    private int bucketCount;
72d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    private long[] bucketStart;
73558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey    private long[] activeTime;
74d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    private long[] rxBytes;
75a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    private long[] rxPackets;
76d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    private long[] txBytes;
77a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    private long[] txPackets;
7863d27a9233fed934340231f438493746084a681dJeff Sharkey    private long[] operations;
7963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    private long totalBytes;
80d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey
81d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    public static class Entry {
8263d27a9233fed934340231f438493746084a681dJeff Sharkey        public static final long UNKNOWN = -1;
8363d27a9233fed934340231f438493746084a681dJeff Sharkey
84d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        public long bucketDuration;
85558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        public long bucketStart;
86558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        public long activeTime;
87d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        public long rxBytes;
88a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        public long rxPackets;
89d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        public long txBytes;
90a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        public long txPackets;
9163d27a9233fed934340231f438493746084a681dJeff Sharkey        public long operations;
92d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    }
9375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
94d2a458750e5a3d490af09cecb5c28370baf0a913Jeff Sharkey    public NetworkStatsHistory(long bucketDuration) {
9563d27a9233fed934340231f438493746084a681dJeff Sharkey        this(bucketDuration, 10, FIELD_ALL);
964a97122ebf4d92a3f94402041729d77905e6c0c0Jeff Sharkey    }
974a97122ebf4d92a3f94402041729d77905e6c0c0Jeff Sharkey
984a97122ebf4d92a3f94402041729d77905e6c0c0Jeff Sharkey    public NetworkStatsHistory(long bucketDuration, int initialSize) {
9963d27a9233fed934340231f438493746084a681dJeff Sharkey        this(bucketDuration, initialSize, FIELD_ALL);
10063d27a9233fed934340231f438493746084a681dJeff Sharkey    }
10163d27a9233fed934340231f438493746084a681dJeff Sharkey
10263d27a9233fed934340231f438493746084a681dJeff Sharkey    public NetworkStatsHistory(long bucketDuration, int initialSize, int fields) {
10375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        this.bucketDuration = bucketDuration;
1044a97122ebf4d92a3f94402041729d77905e6c0c0Jeff Sharkey        bucketStart = new long[initialSize];
105558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        if ((fields & FIELD_ACTIVE_TIME) != 0) activeTime = new long[initialSize];
10663d27a9233fed934340231f438493746084a681dJeff Sharkey        if ((fields & FIELD_RX_BYTES) != 0) rxBytes = new long[initialSize];
10763d27a9233fed934340231f438493746084a681dJeff Sharkey        if ((fields & FIELD_RX_PACKETS) != 0) rxPackets = new long[initialSize];
10863d27a9233fed934340231f438493746084a681dJeff Sharkey        if ((fields & FIELD_TX_BYTES) != 0) txBytes = new long[initialSize];
10963d27a9233fed934340231f438493746084a681dJeff Sharkey        if ((fields & FIELD_TX_PACKETS) != 0) txPackets = new long[initialSize];
11063d27a9233fed934340231f438493746084a681dJeff Sharkey        if ((fields & FIELD_OPERATIONS) != 0) operations = new long[initialSize];
1114a97122ebf4d92a3f94402041729d77905e6c0c0Jeff Sharkey        bucketCount = 0;
11263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        totalBytes = 0;
11363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    }
11463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey
11563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    public NetworkStatsHistory(NetworkStatsHistory existing, long bucketDuration) {
11663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        this(bucketDuration, existing.estimateResizeBuckets(bucketDuration));
11763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        recordEntireHistory(existing);
11875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
11975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
12075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public NetworkStatsHistory(Parcel in) {
12175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        bucketDuration = in.readLong();
12275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        bucketStart = readLongArray(in);
123558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        activeTime = readLongArray(in);
124a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        rxBytes = readLongArray(in);
125a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        rxPackets = readLongArray(in);
126a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        txBytes = readLongArray(in);
127a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        txPackets = readLongArray(in);
12863d27a9233fed934340231f438493746084a681dJeff Sharkey        operations = readLongArray(in);
12975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        bucketCount = bucketStart.length;
13063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        totalBytes = in.readLong();
13175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
13275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
133bfdd680ab44da173a4a39fcd6feccdebb9d1f855Jeff Sharkey    @Override
13475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void writeToParcel(Parcel out, int flags) {
13575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        out.writeLong(bucketDuration);
13675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        writeLongArray(out, bucketStart, bucketCount);
137558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        writeLongArray(out, activeTime, bucketCount);
138d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        writeLongArray(out, rxBytes, bucketCount);
139a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        writeLongArray(out, rxPackets, bucketCount);
140d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        writeLongArray(out, txBytes, bucketCount);
141a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        writeLongArray(out, txPackets, bucketCount);
14263d27a9233fed934340231f438493746084a681dJeff Sharkey        writeLongArray(out, operations, bucketCount);
14363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        out.writeLong(totalBytes);
14475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
14575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
14675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public NetworkStatsHistory(DataInputStream in) throws IOException {
14775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final int version = in.readInt();
14861ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey        switch (version) {
1491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case VERSION_INIT: {
15061ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey                bucketDuration = in.readLong();
15163d27a9233fed934340231f438493746084a681dJeff Sharkey                bucketStart = readFullLongArray(in);
15263d27a9233fed934340231f438493746084a681dJeff Sharkey                rxBytes = readFullLongArray(in);
153a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey                rxPackets = new long[bucketStart.length];
15463d27a9233fed934340231f438493746084a681dJeff Sharkey                txBytes = readFullLongArray(in);
155a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey                txPackets = new long[bucketStart.length];
15663d27a9233fed934340231f438493746084a681dJeff Sharkey                operations = new long[bucketStart.length];
15763d27a9233fed934340231f438493746084a681dJeff Sharkey                bucketCount = bucketStart.length;
15863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey                totalBytes = total(rxBytes) + total(txBytes);
15963d27a9233fed934340231f438493746084a681dJeff Sharkey                break;
16063d27a9233fed934340231f438493746084a681dJeff Sharkey            }
161558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey            case VERSION_ADD_PACKETS:
162558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey            case VERSION_ADD_ACTIVE: {
16363d27a9233fed934340231f438493746084a681dJeff Sharkey                bucketDuration = in.readLong();
16463d27a9233fed934340231f438493746084a681dJeff Sharkey                bucketStart = readVarLongArray(in);
165558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey                activeTime = (version >= VERSION_ADD_ACTIVE) ? readVarLongArray(in)
166558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey                        : new long[bucketStart.length];
16763d27a9233fed934340231f438493746084a681dJeff Sharkey                rxBytes = readVarLongArray(in);
16863d27a9233fed934340231f438493746084a681dJeff Sharkey                rxPackets = readVarLongArray(in);
16963d27a9233fed934340231f438493746084a681dJeff Sharkey                txBytes = readVarLongArray(in);
17063d27a9233fed934340231f438493746084a681dJeff Sharkey                txPackets = readVarLongArray(in);
17163d27a9233fed934340231f438493746084a681dJeff Sharkey                operations = readVarLongArray(in);
17261ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey                bucketCount = bucketStart.length;
17363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey                totalBytes = total(rxBytes) + total(txBytes);
17461ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey                break;
17561ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey            }
17661ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey            default: {
17761ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey                throw new ProtocolException("unexpected version: " + version);
17861ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey            }
17961ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey        }
180b0a579f83369ca2daa885222a35f1cd3c054ab11Jeff Sharkey
181b0a579f83369ca2daa885222a35f1cd3c054ab11Jeff Sharkey        if (bucketStart.length != bucketCount || rxBytes.length != bucketCount
182b0a579f83369ca2daa885222a35f1cd3c054ab11Jeff Sharkey                || rxPackets.length != bucketCount || txBytes.length != bucketCount
183b0a579f83369ca2daa885222a35f1cd3c054ab11Jeff Sharkey                || txPackets.length != bucketCount || operations.length != bucketCount) {
184b0a579f83369ca2daa885222a35f1cd3c054ab11Jeff Sharkey            throw new ProtocolException("Mismatched history lengths");
185b0a579f83369ca2daa885222a35f1cd3c054ab11Jeff Sharkey        }
18675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
18775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
18875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void writeToStream(DataOutputStream out) throws IOException {
189558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        out.writeInt(VERSION_ADD_ACTIVE);
19075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        out.writeLong(bucketDuration);
19163d27a9233fed934340231f438493746084a681dJeff Sharkey        writeVarLongArray(out, bucketStart, bucketCount);
192558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        writeVarLongArray(out, activeTime, bucketCount);
19363d27a9233fed934340231f438493746084a681dJeff Sharkey        writeVarLongArray(out, rxBytes, bucketCount);
19463d27a9233fed934340231f438493746084a681dJeff Sharkey        writeVarLongArray(out, rxPackets, bucketCount);
19563d27a9233fed934340231f438493746084a681dJeff Sharkey        writeVarLongArray(out, txBytes, bucketCount);
19663d27a9233fed934340231f438493746084a681dJeff Sharkey        writeVarLongArray(out, txPackets, bucketCount);
19763d27a9233fed934340231f438493746084a681dJeff Sharkey        writeVarLongArray(out, operations, bucketCount);
19875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
19975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
200bfdd680ab44da173a4a39fcd6feccdebb9d1f855Jeff Sharkey    @Override
20175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public int describeContents() {
20275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        return 0;
20375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
20475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
205d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    public int size() {
206d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        return bucketCount;
207d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    }
208d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey
209d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    public long getBucketDuration() {
210d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        return bucketDuration;
211d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    }
212d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey
213434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey    public long getStart() {
214434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        if (bucketCount > 0) {
215434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey            return bucketStart[0];
216434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        } else {
217434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey            return Long.MAX_VALUE;
218434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        }
219434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey    }
220434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey
221434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey    public long getEnd() {
222434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        if (bucketCount > 0) {
223434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey            return bucketStart[bucketCount - 1] + bucketDuration;
224434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        } else {
225434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey            return Long.MIN_VALUE;
226434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        }
227434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey    }
228434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey
229d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    /**
23063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey     * Return total bytes represented by this history.
23163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey     */
23263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    public long getTotalBytes() {
23363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        return totalBytes;
23463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    }
23563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey
23663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    /**
23769b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey     * Return index of bucket that contains or is immediately before the
23869b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey     * requested time.
23969b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey     */
24069b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey    public int getIndexBefore(long time) {
24169b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        int index = Arrays.binarySearch(bucketStart, 0, bucketCount, time);
24269b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        if (index < 0) {
24369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey            index = (~index) - 1;
24469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        } else {
24569b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey            index -= 1;
24669b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        }
24769b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        return MathUtils.constrain(index, 0, bucketCount - 1);
24869b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey    }
24969b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey
25069b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey    /**
25169b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey     * Return index of bucket that contains or is immediately after the
25269b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey     * requested time.
25369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey     */
25469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey    public int getIndexAfter(long time) {
25569b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        int index = Arrays.binarySearch(bucketStart, 0, bucketCount, time);
25669b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        if (index < 0) {
25769b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey            index = ~index;
25869b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        } else {
25969b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey            index += 1;
26069b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        }
26169b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        return MathUtils.constrain(index, 0, bucketCount - 1);
26269b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey    }
26369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey
26469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey    /**
265d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey     * Return specific stats entry.
266d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey     */
267d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    public Entry getValues(int i, Entry recycle) {
268d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        final Entry entry = recycle != null ? recycle : new Entry();
269d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        entry.bucketStart = bucketStart[i];
270d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        entry.bucketDuration = bucketDuration;
271558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        entry.activeTime = getLong(activeTime, i, UNKNOWN);
27263d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.rxBytes = getLong(rxBytes, i, UNKNOWN);
27363d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.rxPackets = getLong(rxPackets, i, UNKNOWN);
27463d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.txBytes = getLong(txBytes, i, UNKNOWN);
27563d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.txPackets = getLong(txPackets, i, UNKNOWN);
27663d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.operations = getLong(operations, i, UNKNOWN);
277d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey        return entry;
278d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey    }
279d37948f6ed1667d077e0e3a38808f42f981ddcc2Jeff Sharkey
28075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    /**
28175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey     * Record that data traffic occurred in the given time range. Will
28275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey     * distribute across internal buckets, creating new buckets as needed.
28375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey     */
284a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    @Deprecated
285a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    public void recordData(long start, long end, long rxBytes, long txBytes) {
286b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        recordData(start, end, new NetworkStats.Entry(
287b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey                IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, 0L, txBytes, 0L, 0L));
288a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    }
289a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
290a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    /**
291a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey     * Record that data traffic occurred in the given time range. Will
292a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey     * distribute across internal buckets, creating new buckets as needed.
293a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey     */
294a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    public void recordData(long start, long end, NetworkStats.Entry entry) {
29563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        long rxBytes = entry.rxBytes;
29663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        long rxPackets = entry.rxPackets;
29763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        long txBytes = entry.txBytes;
29863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        long txPackets = entry.txPackets;
29963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        long operations = entry.operations;
30063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey
30163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        if (entry.isNegative()) {
302a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            throw new IllegalArgumentException("tried recording negative data");
3031b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
30463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        if (entry.isEmpty()) {
305367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey            return;
306367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey        }
3071b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
30875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // create any buckets needed by this range
30975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        ensureBuckets(start, end);
31075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
31175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // distribute data usage into buckets
312a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        long duration = end - start;
31369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        final int startIndex = getIndexAfter(end);
31469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        for (int i = startIndex; i >= 0; i--) {
31575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final long curStart = bucketStart[i];
31675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final long curEnd = curStart + bucketDuration;
31775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
31875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            // bucket is older than record; we're finished
31975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            if (curEnd < start) break;
32075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            // bucket is newer than record; keep looking
32175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            if (curStart > end) continue;
32275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
32375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final long overlap = Math.min(curEnd, end) - Math.max(curStart, start);
324a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            if (overlap <= 0) continue;
325a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
326a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            // integer math each time is faster than floating point
32763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            final long fracRxBytes = rxBytes * overlap / duration;
32863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            final long fracRxPackets = rxPackets * overlap / duration;
32963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            final long fracTxBytes = txBytes * overlap / duration;
33063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            final long fracTxPackets = txPackets * overlap / duration;
33163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            final long fracOperations = operations * overlap / duration;
332a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
333558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey            addLong(activeTime, i, overlap);
33463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            addLong(this.rxBytes, i, fracRxBytes); rxBytes -= fracRxBytes;
33563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            addLong(this.rxPackets, i, fracRxPackets); rxPackets -= fracRxPackets;
33663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            addLong(this.txBytes, i, fracTxBytes); txBytes -= fracTxBytes;
33763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            addLong(this.txPackets, i, fracTxPackets); txPackets -= fracTxPackets;
33863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            addLong(this.operations, i, fracOperations); operations -= fracOperations;
339a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
340a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            duration -= overlap;
34175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
34263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey
34363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        totalBytes += entry.rxBytes + entry.txBytes;
34475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
34575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
34675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    /**
34719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey     * Record an entire {@link NetworkStatsHistory} into this history. Usually
34819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey     * for combining together stats for external reporting.
34919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey     */
35019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey    public void recordEntireHistory(NetworkStatsHistory input) {
35170c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey        recordHistory(input, Long.MIN_VALUE, Long.MAX_VALUE);
35270c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    }
35370c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey
35470c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    /**
35570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey     * Record given {@link NetworkStatsHistory} into this history, copying only
35670c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey     * buckets that atomically occur in the inclusive time range. Doesn't
35770c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey     * interpolate across partial buckets.
35870c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey     */
35970c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    public void recordHistory(NetworkStatsHistory input, long start, long end) {
360a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        final NetworkStats.Entry entry = new NetworkStats.Entry(
361b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey                IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
36219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey        for (int i = 0; i < input.bucketCount; i++) {
36370c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            final long bucketStart = input.bucketStart[i];
36470c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            final long bucketEnd = bucketStart + input.bucketDuration;
36570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey
36670c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            // skip when bucket is outside requested range
36770c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            if (bucketStart < start || bucketEnd > end) continue;
368a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
36963d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.rxBytes = getLong(input.rxBytes, i, 0L);
37063d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.rxPackets = getLong(input.rxPackets, i, 0L);
37163d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.txBytes = getLong(input.txBytes, i, 0L);
37263d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.txPackets = getLong(input.txPackets, i, 0L);
37363d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.operations = getLong(input.operations, i, 0L);
374a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
37570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            recordData(bucketStart, bucketEnd, entry);
37619862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey        }
37719862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey    }
37819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey
37919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey    /**
38075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey     * Ensure that buckets exist for given time range, creating as needed.
38175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey     */
38275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    private void ensureBuckets(long start, long end) {
38375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // normalize incoming range to bucket boundaries
38475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        start -= start % bucketDuration;
38575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        end += (bucketDuration - (end % bucketDuration)) % bucketDuration;
38675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
38775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        for (long now = start; now < end; now += bucketDuration) {
38875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            // try finding existing bucket
38975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final int index = Arrays.binarySearch(bucketStart, 0, bucketCount, now);
39075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            if (index < 0) {
39175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                // bucket missing, create and insert
39275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey                insertBucket(~index, now);
39375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            }
39475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
39575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
39675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
39775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    /**
39875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey     * Insert new bucket at requested index and starting time.
39975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey     */
40075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    private void insertBucket(int index, long start) {
40175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // create more buckets when needed
4024a97122ebf4d92a3f94402041729d77905e6c0c0Jeff Sharkey        if (bucketCount >= bucketStart.length) {
4034a97122ebf4d92a3f94402041729d77905e6c0c0Jeff Sharkey            final int newLength = Math.max(bucketStart.length, 10) * 3 / 2;
40475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            bucketStart = Arrays.copyOf(bucketStart, newLength);
405558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey            if (activeTime != null) activeTime = Arrays.copyOf(activeTime, newLength);
40663d27a9233fed934340231f438493746084a681dJeff Sharkey            if (rxBytes != null) rxBytes = Arrays.copyOf(rxBytes, newLength);
40763d27a9233fed934340231f438493746084a681dJeff Sharkey            if (rxPackets != null) rxPackets = Arrays.copyOf(rxPackets, newLength);
40863d27a9233fed934340231f438493746084a681dJeff Sharkey            if (txBytes != null) txBytes = Arrays.copyOf(txBytes, newLength);
40963d27a9233fed934340231f438493746084a681dJeff Sharkey            if (txPackets != null) txPackets = Arrays.copyOf(txPackets, newLength);
41063d27a9233fed934340231f438493746084a681dJeff Sharkey            if (operations != null) operations = Arrays.copyOf(operations, newLength);
41175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
41275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
41375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        // create gap when inserting bucket in middle
41475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        if (index < bucketCount) {
41575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final int dstPos = index + 1;
41675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final int length = bucketCount - index;
41775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
41875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            System.arraycopy(bucketStart, index, bucketStart, dstPos, length);
419558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey            if (activeTime != null) System.arraycopy(activeTime, index, activeTime, dstPos, length);
42063d27a9233fed934340231f438493746084a681dJeff Sharkey            if (rxBytes != null) System.arraycopy(rxBytes, index, rxBytes, dstPos, length);
42163d27a9233fed934340231f438493746084a681dJeff Sharkey            if (rxPackets != null) System.arraycopy(rxPackets, index, rxPackets, dstPos, length);
42263d27a9233fed934340231f438493746084a681dJeff Sharkey            if (txBytes != null) System.arraycopy(txBytes, index, txBytes, dstPos, length);
42363d27a9233fed934340231f438493746084a681dJeff Sharkey            if (txPackets != null) System.arraycopy(txPackets, index, txPackets, dstPos, length);
42463d27a9233fed934340231f438493746084a681dJeff Sharkey            if (operations != null) System.arraycopy(operations, index, operations, dstPos, length);
42575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
42675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
42775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        bucketStart[index] = start;
428558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        setLong(activeTime, index, 0L);
42963d27a9233fed934340231f438493746084a681dJeff Sharkey        setLong(rxBytes, index, 0L);
43063d27a9233fed934340231f438493746084a681dJeff Sharkey        setLong(rxPackets, index, 0L);
43163d27a9233fed934340231f438493746084a681dJeff Sharkey        setLong(txBytes, index, 0L);
43263d27a9233fed934340231f438493746084a681dJeff Sharkey        setLong(txPackets, index, 0L);
43363d27a9233fed934340231f438493746084a681dJeff Sharkey        setLong(operations, index, 0L);
43475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        bucketCount++;
43575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
43675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
43775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    /**
43875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey     * Remove buckets older than requested cutoff.
43975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey     */
44063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    @Deprecated
44175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public void removeBucketsBefore(long cutoff) {
44275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        int i;
44375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        for (i = 0; i < bucketCount; i++) {
44475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final long curStart = bucketStart[i];
44575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final long curEnd = curStart + bucketDuration;
44675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
44775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            // cutoff happens before or during this bucket; everything before
44875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            // this bucket should be removed.
44975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            if (curEnd > cutoff) break;
45075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
45175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
45275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        if (i > 0) {
45375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            final int length = bucketStart.length;
45475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            bucketStart = Arrays.copyOfRange(bucketStart, i, length);
455558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey            if (activeTime != null) activeTime = Arrays.copyOfRange(activeTime, i, length);
45663d27a9233fed934340231f438493746084a681dJeff Sharkey            if (rxBytes != null) rxBytes = Arrays.copyOfRange(rxBytes, i, length);
45763d27a9233fed934340231f438493746084a681dJeff Sharkey            if (rxPackets != null) rxPackets = Arrays.copyOfRange(rxPackets, i, length);
45863d27a9233fed934340231f438493746084a681dJeff Sharkey            if (txBytes != null) txBytes = Arrays.copyOfRange(txBytes, i, length);
45963d27a9233fed934340231f438493746084a681dJeff Sharkey            if (txPackets != null) txPackets = Arrays.copyOfRange(txPackets, i, length);
46063d27a9233fed934340231f438493746084a681dJeff Sharkey            if (operations != null) operations = Arrays.copyOfRange(operations, i, length);
46175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            bucketCount -= i;
46263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey
46363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            // TODO: subtract removed values from totalBytes
46475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
46575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
46675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
46761ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey    /**
46819862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey     * Return interpolated data usage across the requested range. Interpolates
46919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey     * across buckets, so values may be rounded slightly.
47019862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey     */
471434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey    public Entry getValues(long start, long end, Entry recycle) {
472434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        return getValues(start, end, Long.MAX_VALUE, recycle);
473434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey    }
474434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey
475434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey    /**
476434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey     * Return interpolated data usage across the requested range. Interpolates
477434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey     * across buckets, so values may be rounded slightly.
478434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey     */
479434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey    public Entry getValues(long start, long end, long now, Entry recycle) {
480434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        final Entry entry = recycle != null ? recycle : new Entry();
481434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        entry.bucketDuration = end - start;
482558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        entry.bucketStart = start;
483558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey        entry.activeTime = activeTime != null ? 0 : UNKNOWN;
48463d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.rxBytes = rxBytes != null ? 0 : UNKNOWN;
48563d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.rxPackets = rxPackets != null ? 0 : UNKNOWN;
48663d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.txBytes = txBytes != null ? 0 : UNKNOWN;
48763d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.txPackets = txPackets != null ? 0 : UNKNOWN;
48863d27a9233fed934340231f438493746084a681dJeff Sharkey        entry.operations = operations != null ? 0 : UNKNOWN;
48919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey
49069b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        final int startIndex = getIndexAfter(end);
49169b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey        for (int i = startIndex; i >= 0; i--) {
49219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey            final long curStart = bucketStart[i];
49319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey            final long curEnd = curStart + bucketDuration;
49419862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey
49534c73acf88c8190b8cd51d8b8f2b9c22aa7f7941Jeff Sharkey            // bucket is older than request; we're finished
49634c73acf88c8190b8cd51d8b8f2b9c22aa7f7941Jeff Sharkey            if (curEnd <= start) break;
49734c73acf88c8190b8cd51d8b8f2b9c22aa7f7941Jeff Sharkey            // bucket is newer than request; keep looking
49834c73acf88c8190b8cd51d8b8f2b9c22aa7f7941Jeff Sharkey            if (curStart >= end) continue;
49919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey
500434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey            // include full value for active buckets, otherwise only fractional
501434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey            final boolean activeBucket = curStart < now && curEnd > now;
50269b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey            final long overlap;
50369b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey            if (activeBucket) {
50469b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey                overlap = bucketDuration;
50569b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey            } else {
50669b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey                final long overlapEnd = curEnd < end ? curEnd : end;
50769b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey                final long overlapStart = curStart > start ? curStart : start;
50869b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey                overlap = overlapEnd - overlapStart;
50969b0f63af2e3babc2e9f048c4682032a0c17d9d0Jeff Sharkey            }
510a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            if (overlap <= 0) continue;
511a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
512a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            // integer math each time is faster than floating point
513558a23200697d306b75750cf4612cf0717e73537Jeff Sharkey            if (activeTime != null) entry.activeTime += activeTime[i] * overlap / bucketDuration;
51463d27a9233fed934340231f438493746084a681dJeff Sharkey            if (rxBytes != null) entry.rxBytes += rxBytes[i] * overlap / bucketDuration;
51563d27a9233fed934340231f438493746084a681dJeff Sharkey            if (rxPackets != null) entry.rxPackets += rxPackets[i] * overlap / bucketDuration;
51663d27a9233fed934340231f438493746084a681dJeff Sharkey            if (txBytes != null) entry.txBytes += txBytes[i] * overlap / bucketDuration;
51763d27a9233fed934340231f438493746084a681dJeff Sharkey            if (txPackets != null) entry.txPackets += txPackets[i] * overlap / bucketDuration;
51863d27a9233fed934340231f438493746084a681dJeff Sharkey            if (operations != null) entry.operations += operations[i] * overlap / bucketDuration;
51919862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey        }
520434962e44ea93b1c4d216c55f636a435bf54aa54Jeff Sharkey        return entry;
52119862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey    }
52219862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey
52319862bf5d058b6ab0c2979e7a5e0297dae6b170bJeff Sharkey    /**
52461ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey     * @deprecated only for temporary testing
52561ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey     */
52661ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey    @Deprecated
527293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey    public void generateRandom(long start, long end, long bytes) {
528293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey        final Random r = new Random();
529293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey
530293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey        final float fractionRx = r.nextFloat();
531293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey        final long rxBytes = (long) (bytes * fractionRx);
532293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey        final long txBytes = (long) (bytes * (1 - fractionRx));
533293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey
534293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey        final long rxPackets = rxBytes / 1024;
535293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey        final long txPackets = txBytes / 1024;
536293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey        final long operations = rxBytes / 2048;
537293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey
538293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey        generateRandom(start, end, rxBytes, rxPackets, txBytes, txPackets, operations, r);
539293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey    }
540293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey
541293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey    /**
542293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey     * @deprecated only for temporary testing
543293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey     */
544293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey    @Deprecated
54563d27a9233fed934340231f438493746084a681dJeff Sharkey    public void generateRandom(long start, long end, long rxBytes, long rxPackets, long txBytes,
546293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey            long txPackets, long operations, Random r) {
54761ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey        ensureBuckets(start, end);
54861ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey
549a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        final NetworkStats.Entry entry = new NetworkStats.Entry(
550b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey                IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
55104cd0e47dbc1e9769ac6f258c923d5b17fa57986Jeff Sharkey        while (rxBytes > 1024 || rxPackets > 128 || txBytes > 1024 || txPackets > 128
55204cd0e47dbc1e9769ac6f258c923d5b17fa57986Jeff Sharkey                || operations > 32) {
55361ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey            final long curStart = randomLong(r, start, end);
554293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey            final long curEnd = curStart + randomLong(r, 0, (end - curStart) / 2);
55563d27a9233fed934340231f438493746084a681dJeff Sharkey
55663d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.rxBytes = randomLong(r, 0, rxBytes);
55763d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.rxPackets = randomLong(r, 0, rxPackets);
55863d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.txBytes = randomLong(r, 0, txBytes);
55963d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.txPackets = randomLong(r, 0, txPackets);
56063d27a9233fed934340231f438493746084a681dJeff Sharkey            entry.operations = randomLong(r, 0, operations);
56163d27a9233fed934340231f438493746084a681dJeff Sharkey
56263d27a9233fed934340231f438493746084a681dJeff Sharkey            rxBytes -= entry.rxBytes;
56363d27a9233fed934340231f438493746084a681dJeff Sharkey            rxPackets -= entry.rxPackets;
56463d27a9233fed934340231f438493746084a681dJeff Sharkey            txBytes -= entry.txBytes;
56563d27a9233fed934340231f438493746084a681dJeff Sharkey            txPackets -= entry.txPackets;
56663d27a9233fed934340231f438493746084a681dJeff Sharkey            operations -= entry.operations;
567f0ceede8fff5df24e5c98701d81c2b71eb138aa9Jeff Sharkey
568f0ceede8fff5df24e5c98701d81c2b71eb138aa9Jeff Sharkey            recordData(curStart, curEnd, entry);
56961ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey        }
57061ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey    }
57161ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey
572293779f9c63cbae0dde564449f0270b595593b0dJeff Sharkey    public static long randomLong(Random r, long start, long end) {
57361ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey        return (long) (start + (r.nextFloat() * (end - start)));
57461ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey    }
57561ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey
57663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    public void dump(IndentingPrintWriter pw, boolean fullHistory) {
57761ee0bbb5b87fb5c4c3dc219868d52743def3d2bJeff Sharkey        pw.print("NetworkStatsHistory: bucketDuration="); pw.println(bucketDuration);
57863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        pw.increaseIndent();
579350083e36b9db6062e165954403ef921ff3dfdadJeff Sharkey
580350083e36b9db6062e165954403ef921ff3dfdadJeff Sharkey        final int start = fullHistory ? 0 : Math.max(0, bucketCount - 32);
581350083e36b9db6062e165954403ef921ff3dfdadJeff Sharkey        if (start > 0) {
58263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            pw.print("(omitting "); pw.print(start); pw.println(" buckets)");
583350083e36b9db6062e165954403ef921ff3dfdadJeff Sharkey        }
584350083e36b9db6062e165954403ef921ff3dfdadJeff Sharkey
585350083e36b9db6062e165954403ef921ff3dfdadJeff Sharkey        for (int i = start; i < bucketCount; i++) {
58663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey            pw.print("bucketStart="); pw.print(bucketStart[i]);
58747eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey            if (activeTime != null) { pw.print(" activeTime="); pw.print(activeTime[i]); }
58847eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey            if (rxBytes != null) { pw.print(" rxBytes="); pw.print(rxBytes[i]); }
58947eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey            if (rxPackets != null) { pw.print(" rxPackets="); pw.print(rxPackets[i]); }
59047eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey            if (txBytes != null) { pw.print(" txBytes="); pw.print(txBytes[i]); }
59147eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey            if (txPackets != null) { pw.print(" txPackets="); pw.print(txPackets[i]); }
59247eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey            if (operations != null) { pw.print(" operations="); pw.print(operations[i]); }
59363d27a9233fed934340231f438493746084a681dJeff Sharkey            pw.println();
59475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
59563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey
59663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        pw.decreaseIndent();
59775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
59875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
59975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    @Override
60075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public String toString() {
60175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        final CharArrayWriter writer = new CharArrayWriter();
60263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        dump(new IndentingPrintWriter(writer, "  "), false);
60375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        return writer.toString();
60475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
60575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
60675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    public static final Creator<NetworkStatsHistory> CREATOR = new Creator<NetworkStatsHistory>() {
607bfdd680ab44da173a4a39fcd6feccdebb9d1f855Jeff Sharkey        @Override
60875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        public NetworkStatsHistory createFromParcel(Parcel in) {
60975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            return new NetworkStatsHistory(in);
61075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
61175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
612bfdd680ab44da173a4a39fcd6feccdebb9d1f855Jeff Sharkey        @Override
61375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        public NetworkStatsHistory[] newArray(int size) {
61475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey            return new NetworkStatsHistory[size];
61575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
61675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    };
61775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
61863d27a9233fed934340231f438493746084a681dJeff Sharkey    private static long getLong(long[] array, int i, long value) {
61963d27a9233fed934340231f438493746084a681dJeff Sharkey        return array != null ? array[i] : value;
62063d27a9233fed934340231f438493746084a681dJeff Sharkey    }
62163d27a9233fed934340231f438493746084a681dJeff Sharkey
62263d27a9233fed934340231f438493746084a681dJeff Sharkey    private static void setLong(long[] array, int i, long value) {
62363d27a9233fed934340231f438493746084a681dJeff Sharkey        if (array != null) array[i] = value;
62463d27a9233fed934340231f438493746084a681dJeff Sharkey    }
62563d27a9233fed934340231f438493746084a681dJeff Sharkey
62663d27a9233fed934340231f438493746084a681dJeff Sharkey    private static void addLong(long[] array, int i, long value) {
62763d27a9233fed934340231f438493746084a681dJeff Sharkey        if (array != null) array[i] += value;
62863d27a9233fed934340231f438493746084a681dJeff Sharkey    }
62963d27a9233fed934340231f438493746084a681dJeff Sharkey
63063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    public int estimateResizeBuckets(long newBucketDuration) {
63163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey        return (int) (size() * getBucketDuration() / newBucketDuration);
63263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey    }
63363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey
634a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    /**
635a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey     * Utility methods for interacting with {@link DataInputStream} and
636a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey     * {@link DataOutputStream}, mostly dealing with writing partial arrays.
637a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey     */
638a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    public static class DataStreamUtils {
63963d27a9233fed934340231f438493746084a681dJeff Sharkey        @Deprecated
64063d27a9233fed934340231f438493746084a681dJeff Sharkey        public static long[] readFullLongArray(DataInputStream in) throws IOException {
641a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            final int size = in.readInt();
642a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            final long[] values = new long[size];
643a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            for (int i = 0; i < values.length; i++) {
644a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey                values[i] = in.readLong();
645a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            }
646a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            return values;
647a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        }
648a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
64963d27a9233fed934340231f438493746084a681dJeff Sharkey        /**
65063d27a9233fed934340231f438493746084a681dJeff Sharkey         * Read variable-length {@link Long} using protobuf-style approach.
65163d27a9233fed934340231f438493746084a681dJeff Sharkey         */
65263d27a9233fed934340231f438493746084a681dJeff Sharkey        public static long readVarLong(DataInputStream in) throws IOException {
65363d27a9233fed934340231f438493746084a681dJeff Sharkey            int shift = 0;
65463d27a9233fed934340231f438493746084a681dJeff Sharkey            long result = 0;
65563d27a9233fed934340231f438493746084a681dJeff Sharkey            while (shift < 64) {
65663d27a9233fed934340231f438493746084a681dJeff Sharkey                byte b = in.readByte();
65763d27a9233fed934340231f438493746084a681dJeff Sharkey                result |= (long) (b & 0x7F) << shift;
65863d27a9233fed934340231f438493746084a681dJeff Sharkey                if ((b & 0x80) == 0)
65963d27a9233fed934340231f438493746084a681dJeff Sharkey                    return result;
66063d27a9233fed934340231f438493746084a681dJeff Sharkey                shift += 7;
66163d27a9233fed934340231f438493746084a681dJeff Sharkey            }
66263d27a9233fed934340231f438493746084a681dJeff Sharkey            throw new ProtocolException("malformed long");
66363d27a9233fed934340231f438493746084a681dJeff Sharkey        }
66463d27a9233fed934340231f438493746084a681dJeff Sharkey
66563d27a9233fed934340231f438493746084a681dJeff Sharkey        /**
66663d27a9233fed934340231f438493746084a681dJeff Sharkey         * Write variable-length {@link Long} using protobuf-style approach.
66763d27a9233fed934340231f438493746084a681dJeff Sharkey         */
66863d27a9233fed934340231f438493746084a681dJeff Sharkey        public static void writeVarLong(DataOutputStream out, long value) throws IOException {
66963d27a9233fed934340231f438493746084a681dJeff Sharkey            while (true) {
67063d27a9233fed934340231f438493746084a681dJeff Sharkey                if ((value & ~0x7FL) == 0) {
67163d27a9233fed934340231f438493746084a681dJeff Sharkey                    out.writeByte((int) value);
67263d27a9233fed934340231f438493746084a681dJeff Sharkey                    return;
67363d27a9233fed934340231f438493746084a681dJeff Sharkey                } else {
67463d27a9233fed934340231f438493746084a681dJeff Sharkey                    out.writeByte(((int) value & 0x7F) | 0x80);
67563d27a9233fed934340231f438493746084a681dJeff Sharkey                    value >>>= 7;
67663d27a9233fed934340231f438493746084a681dJeff Sharkey                }
67763d27a9233fed934340231f438493746084a681dJeff Sharkey            }
67863d27a9233fed934340231f438493746084a681dJeff Sharkey        }
67963d27a9233fed934340231f438493746084a681dJeff Sharkey
68063d27a9233fed934340231f438493746084a681dJeff Sharkey        public static long[] readVarLongArray(DataInputStream in) throws IOException {
68163d27a9233fed934340231f438493746084a681dJeff Sharkey            final int size = in.readInt();
68263d27a9233fed934340231f438493746084a681dJeff Sharkey            if (size == -1) return null;
68363d27a9233fed934340231f438493746084a681dJeff Sharkey            final long[] values = new long[size];
68463d27a9233fed934340231f438493746084a681dJeff Sharkey            for (int i = 0; i < values.length; i++) {
68563d27a9233fed934340231f438493746084a681dJeff Sharkey                values[i] = readVarLong(in);
68663d27a9233fed934340231f438493746084a681dJeff Sharkey            }
68763d27a9233fed934340231f438493746084a681dJeff Sharkey            return values;
68863d27a9233fed934340231f438493746084a681dJeff Sharkey        }
68963d27a9233fed934340231f438493746084a681dJeff Sharkey
69063d27a9233fed934340231f438493746084a681dJeff Sharkey        public static void writeVarLongArray(DataOutputStream out, long[] values, int size)
691a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey                throws IOException {
69263d27a9233fed934340231f438493746084a681dJeff Sharkey            if (values == null) {
69363d27a9233fed934340231f438493746084a681dJeff Sharkey                out.writeInt(-1);
69463d27a9233fed934340231f438493746084a681dJeff Sharkey                return;
69563d27a9233fed934340231f438493746084a681dJeff Sharkey            }
696a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            if (size > values.length) {
697a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey                throw new IllegalArgumentException("size larger than length");
698a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            }
699a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            out.writeInt(size);
700a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            for (int i = 0; i < size; i++) {
70163d27a9233fed934340231f438493746084a681dJeff Sharkey                writeVarLong(out, values[i]);
702a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            }
70375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
70475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
70575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
706a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    /**
707a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey     * Utility methods for interacting with {@link Parcel} structures, mostly
708a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey     * dealing with writing partial arrays.
709a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey     */
710a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey    public static class ParcelUtils {
711a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        public static long[] readLongArray(Parcel in) {
712a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            final int size = in.readInt();
71363d27a9233fed934340231f438493746084a681dJeff Sharkey            if (size == -1) return null;
714a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            final long[] values = new long[size];
715a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            for (int i = 0; i < values.length; i++) {
716a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey                values[i] = in.readLong();
717a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            }
718a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            return values;
71975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
72075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
721a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        public static void writeLongArray(Parcel out, long[] values, int size) {
72263d27a9233fed934340231f438493746084a681dJeff Sharkey            if (values == null) {
72363d27a9233fed934340231f438493746084a681dJeff Sharkey                out.writeInt(-1);
72463d27a9233fed934340231f438493746084a681dJeff Sharkey                return;
725a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            }
726a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            if (size > values.length) {
727a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey                throw new IllegalArgumentException("size larger than length");
728a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            }
729a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            out.writeInt(size);
730a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            for (int i = 0; i < size; i++) {
73163d27a9233fed934340231f438493746084a681dJeff Sharkey                out.writeLong(values[i]);
732a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            }
73375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey        }
73475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey    }
73575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
73675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey}
737