Merge "[DU09-0]Adding the NetworkStatsCollection Builder" am: b6ac084831 am: a6c7dd6df0 am: 41e14bee2e

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1956982

Change-Id: I8c097ec727d821b5affd954fefc005cd69f514a3
This commit is contained in:
Junyu Lai
2022-01-25 17:23:28 +00:00
committed by Automerger Merge Worker

View File

@@ -16,6 +16,7 @@
package android.net;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
import static android.net.NetworkStats.IFACE_ALL;
@@ -34,6 +35,8 @@ import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRation
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.net.NetworkStatsHistory.Entry;
import android.os.Binder;
import android.service.NetworkStatsCollectionKeyProto;
import android.service.NetworkStatsCollectionProto;
@@ -71,6 +74,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@@ -80,7 +85,7 @@ import java.util.Set;
*
* @hide
*/
// @SystemApi(client = MODULE_LIBRARIES)
@SystemApi(client = MODULE_LIBRARIES)
public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.Writer {
private static final String TAG = NetworkStatsCollection.class.getSimpleName();
/** File header magic number: "ANET" */
@@ -809,6 +814,71 @@ public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.W
return false;
}
/**
* Get the all historical stats of the collection {@link NetworkStatsCollection}.
*
* @return All {@link NetworkStatsHistory} in this collection.
*/
@NonNull
public Map<Key, NetworkStatsHistory> getEntries() {
return new ArrayMap(mStats);
}
/**
* Builder class for {@link NetworkStatsCollection}.
*/
public static final class Builder {
private final long mBucketDuration;
private final ArrayMap<Key, NetworkStatsHistory> mEntries = new ArrayMap<>();
/**
* Creates a new Builder with given bucket duration.
*
* @param bucketDuration Duration of the buckets of the object, in milliseconds.
*/
public Builder(long bucketDuration) {
mBucketDuration = bucketDuration;
}
/**
* Add association of the history with the specified key in this map.
*
* @param key The object used to identify a network, see {@link Key}.
* @param history {@link NetworkStatsHistory} instance associated to the given {@link Key}.
* @return The builder object.
*/
@NonNull
public NetworkStatsCollection.Builder addEntry(@NonNull Key key,
@NonNull NetworkStatsHistory history) {
Objects.requireNonNull(key);
Objects.requireNonNull(history);
final List<Entry> historyEntries = history.getEntries();
final NetworkStatsHistory.Builder historyBuilder =
new NetworkStatsHistory.Builder(mBucketDuration, historyEntries.size());
for (Entry entry : historyEntries) {
historyBuilder.addEntry(entry);
}
mEntries.put(key, historyBuilder.build());
return this;
}
/**
* Builds the instance of the {@link NetworkStatsCollection}.
*
* @return the built instance of {@link NetworkStatsCollection}.
*/
@NonNull
public NetworkStatsCollection build() {
final NetworkStatsCollection collection = new NetworkStatsCollection(mBucketDuration);
for (int i = 0; i < mEntries.size(); i++) {
collection.recordHistory(mEntries.keyAt(i), mEntries.valueAt(i));
}
return collection;
}
}
/**
* the identifier that associate with the {@link NetworkStatsHistory} object to identify
* a certain record in the {@link NetworkStatsCollection} object.