[DU09-0]Adding the NetworkStatsCollection Builder
The purpose is provide NetworkStatsCollection.Builder as
public API for adding the stats {@link NetworkStatsHistory}
Bug: 215862801
Test: atest NetworkStatsCollectionTest
Change-Id: I65ad589473cbb7785cbe8297f793ce9f18a55c35
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package android.net;
|
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_NO;
|
||||||
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
|
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
|
||||||
import static android.net.NetworkStats.IFACE_ALL;
|
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.NonNull;
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
|
import android.annotation.SystemApi;
|
||||||
|
import android.net.NetworkStatsHistory.Entry;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.service.NetworkStatsCollectionKeyProto;
|
import android.service.NetworkStatsCollectionKeyProto;
|
||||||
import android.service.NetworkStatsCollectionProto;
|
import android.service.NetworkStatsCollectionProto;
|
||||||
@@ -71,6 +74,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -80,7 +85,7 @@ import java.util.Set;
|
|||||||
*
|
*
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
// @SystemApi(client = MODULE_LIBRARIES)
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.Writer {
|
public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.Writer {
|
||||||
private static final String TAG = NetworkStatsCollection.class.getSimpleName();
|
private static final String TAG = NetworkStatsCollection.class.getSimpleName();
|
||||||
/** File header magic number: "ANET" */
|
/** File header magic number: "ANET" */
|
||||||
@@ -809,6 +814,71 @@ public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.W
|
|||||||
return false;
|
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
|
* the identifier that associate with the {@link NetworkStatsHistory} object to identify
|
||||||
* a certain record in the {@link NetworkStatsCollection} object.
|
* a certain record in the {@link NetworkStatsCollection} object.
|
||||||
|
|||||||
Reference in New Issue
Block a user