diff options
| author | 2022-01-14 23:34:54 +0800 | |
|---|---|---|
| committer | 2022-01-19 15:54:17 +0800 | |
| commit | 1f7ddfeda2747f19ec40e12559a7099d270caf7d (patch) | |
| tree | 9668d6eb489d47c52d39e5428ed1b99ac74f35ff | |
| parent | b52609172ecd725fb1829b79ec09b476f9380784 (diff) | |
Have NetworkStats implements iterable
Make NetworkStats be iterable and expose iterator as system API
which is better than expose size() and getValues().
This API could be used by the caller who needs to get entry
from NetworkStats.
Bug: 210073043
CTS-Coverage-Bug: 215061403
Test: build, TH
Change-Id: I6cb4c8f63d7067133f2722b2be7e16b4098a697d
| -rw-r--r-- | core/api/system-current.txt | 3 | ||||
| -rw-r--r-- | packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java | 32 |
2 files changed, 33 insertions, 2 deletions
diff --git a/core/api/system-current.txt b/core/api/system-current.txt index a8ef47e29d67..3f7fc3fde287 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -7523,11 +7523,12 @@ package android.net { field public static final String PERMISSION_MAINLINE_NETWORK_STACK = "android.permission.MAINLINE_NETWORK_STACK"; } - public final class NetworkStats implements android.os.Parcelable { + public final class NetworkStats implements java.lang.Iterable<android.net.NetworkStats.Entry> android.os.Parcelable { ctor public NetworkStats(long, int); method @NonNull public android.net.NetworkStats add(@NonNull android.net.NetworkStats); method @NonNull public android.net.NetworkStats addEntry(@NonNull android.net.NetworkStats.Entry); method public int describeContents(); + method @NonNull public java.util.Iterator<android.net.NetworkStats.Entry> iterator(); method @NonNull public android.net.NetworkStats subtract(@NonNull android.net.NetworkStats); method public void writeToParcel(@NonNull android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator<android.net.NetworkStats> CREATOR; diff --git a/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java b/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java index 9d532e7929a6..9175809d9c7c 100644 --- a/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java +++ b/packages/ConnectivityT/framework-t/src/android/net/NetworkStats.java @@ -41,6 +41,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Arrays; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; @@ -57,7 +58,7 @@ import java.util.function.Predicate; */ // @NotThreadSafe @SystemApi -public final class NetworkStats implements Parcelable { +public final class NetworkStats implements Parcelable, Iterable<NetworkStats.Entry> { private static final String TAG = "NetworkStats"; /** @@ -678,6 +679,35 @@ public final class NetworkStats implements Parcelable { } /** + * Iterate over Entry objects. + * + * Return an iterator of this object that will iterate through all contained Entry objects. + * + * This iterator does not support concurrent modification and makes no guarantee of fail-fast + * behavior. If any method that can mutate the contents of this object is called while + * iteration is in progress, either inside the loop or in another thread, then behavior is + * undefined. + * The remove() method is not implemented and will throw UnsupportedOperationException. + * @hide + */ + @SystemApi + @NonNull public Iterator<Entry> iterator() { + return new Iterator<Entry>() { + int mIndex = 0; + + @Override + public boolean hasNext() { + return mIndex < size; + } + + @Override + public Entry next() { + return getValues(mIndex++, null); + } + }; + } + + /** * Return specific stats entry. * @hide */ |