From cf0260b08d627b6665fb7960a77bdf288007470c Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Fri, 27 Jan 2023 16:44:33 +0900 Subject: [PATCH] Factorize describeDifferences Bug: 259479229 Test: FrameworksNetTests Change-Id: Iecbccd10b04ada49f3485097dfcf3511c26c0925 --- .../src/android/net/NetworkCapabilities.java | 23 +++-------------- .../server/connectivity/FullScore.java | 25 +++---------------- 2 files changed, 8 insertions(+), 40 deletions(-) diff --git a/framework/src/android/net/NetworkCapabilities.java b/framework/src/android/net/NetworkCapabilities.java index e07601f1cc..e70d75dd3c 100644 --- a/framework/src/android/net/NetworkCapabilities.java +++ b/framework/src/android/net/NetworkCapabilities.java @@ -18,6 +18,7 @@ package android.net; import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; import static com.android.net.module.util.BitUtils.appendStringRepresentationOfBitMaskToStringBuilder; +import static com.android.net.module.util.BitUtils.describeDifferences; import android.annotation.IntDef; import android.annotation.LongDef; @@ -2069,30 +2070,14 @@ public final class NetworkCapabilities implements Parcelable { * Returns a short but human-readable string of updates from an older set of capabilities. * @param old the old capabilities to diff from * @return a string fit for logging differences, or null if no differences. - * this never returns the empty string. + * this never returns the empty string. See BitUtils#describeDifferences. * @hide */ @Nullable public String describeCapsDifferencesFrom(@Nullable final NetworkCapabilities old) { final long oldCaps = null == old ? 0 : old.mNetworkCapabilities; - final long changed = oldCaps ^ mNetworkCapabilities; - if (0 == changed) return null; - // If the control reaches here, there are changes (additions, removals, or both) so - // the code below is guaranteed to add something to the string and can't return "". - final long removed = oldCaps & changed; - final long added = mNetworkCapabilities & changed; - final StringBuilder sb = new StringBuilder(); - if (0 != removed) { - sb.append("-"); - appendStringRepresentationOfBitMaskToStringBuilder(sb, removed, - NetworkCapabilities::capabilityNameOf, "-"); - } - if (0 != added) { - sb.append("+"); - appendStringRepresentationOfBitMaskToStringBuilder(sb, added, - NetworkCapabilities::capabilityNameOf, "+"); - } - return sb.toString(); + return describeDifferences(oldCaps, mNetworkCapabilities, + NetworkCapabilities::capabilityNameOf); } /** diff --git a/service/src/com/android/server/connectivity/FullScore.java b/service/src/com/android/server/connectivity/FullScore.java index 23038942c2..87ae0c9e79 100644 --- a/service/src/com/android/server/connectivity/FullScore.java +++ b/service/src/com/android/server/connectivity/FullScore.java @@ -23,7 +23,7 @@ import static android.net.NetworkCapabilities.TRANSPORT_VPN; import static android.net.NetworkScore.KEEP_CONNECTED_NONE; import static android.net.NetworkScore.POLICY_YIELD_TO_BAD_WIFI; -import static com.android.net.module.util.BitUtils.appendStringRepresentationOfBitMaskToStringBuilder; +import static com.android.net.module.util.BitUtils.describeDifferences; import android.annotation.NonNull; import android.annotation.Nullable; @@ -338,31 +338,14 @@ public class FullScore { /** * Returns a short but human-readable string of updates from an older score. - * @param old the old capabilities to diff from + * @param old the old score to diff from * @return a string fit for logging differences, or null if no differences. - * this method cannot return the empty string. + * this method cannot return the empty string. See BitUtils#describeDifferences. */ @Nullable public String describeDifferencesFrom(@Nullable final FullScore old) { final long oldPolicies = null == old ? 0 : old.mPolicies; - final long changed = oldPolicies ^ mPolicies; - if (0 == changed) return null; - // If the control reaches here, there are changes (additions, removals, or both) so - // the code below is guaranteed to add something to the string and can't return "". - final long removed = oldPolicies & changed; - final long added = mPolicies & changed; - final StringBuilder sb = new StringBuilder(); - if (0 != removed) { - sb.append("-"); - appendStringRepresentationOfBitMaskToStringBuilder(sb, removed, - FullScore::policyNameOf, "-"); - } - if (0 != added) { - sb.append("+"); - appendStringRepresentationOfBitMaskToStringBuilder(sb, added, - FullScore::policyNameOf, "+"); - } - return sb.toString(); + return describeDifferences(oldPolicies, mPolicies, FullScore::policyNameOf); } // Example output :