From 19921cdd1f86ffb4488d4e30b93e0a46653b41cc Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Thu, 30 Dec 2021 10:49:29 +0000 Subject: [PATCH] [MS31] Fix several hidden API usages This includes: 1. Replace UserHandle.getUid(userId, appId) with public API 2. Remove Preconditions.checkArgument 3. Replace MathUtil.constrain with the one in NetworkStatsUtils 4. Remove Sets 5. Replace NetworkStack.checkNetworkStackPermission with the one in the PermissionUtils 6. Replace internal CollectionUtils with the one in the module 7. Inline ProtoOutputStream(fd) 8. Replace Intent.EXTRA_USER_HANDLE with Intent.EXTRA_USER Test: atest NetworkStatsSubscriptionsMonitorTest \ NetworkStatsServiceTest Bug: 204830222 Change-Id: If06a27b04ad15c29052b670eefc2f6dc0d199420 --- .../server/net/NetworkStatsObservers.java | 7 ++--- .../server/net/NetworkStatsRecorder.java | 8 +++--- .../server/net/NetworkStatsService.java | 20 +++++++------- .../net/NetworkStatsSubscriptionsMonitor.java | 26 ++++++++++--------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/service-t/src/com/android/server/net/NetworkStatsObservers.java b/service-t/src/com/android/server/net/NetworkStatsObservers.java index 9aa20a4830..b57a4f920b 100644 --- a/service-t/src/com/android/server/net/NetworkStatsObservers.java +++ b/service-t/src/com/android/server/net/NetworkStatsObservers.java @@ -18,8 +18,6 @@ package com.android.server.net; import static android.app.usage.NetworkStatsManager.MIN_THRESHOLD_BYTES; -import static com.android.internal.util.Preconditions.checkArgument; - import android.app.usage.NetworkStatsManager; import android.net.DataUsageRequest; import android.net.NetworkIdentitySet; @@ -216,7 +214,10 @@ class NetworkStatsObservers { accessLevel); } else { // Safety check in case a new access level is added and we forgot to update this - checkArgument(accessLevel >= NetworkStatsAccess.Level.DEVICESUMMARY); + if (accessLevel < NetworkStatsAccess.Level.DEVICESUMMARY) { + throw new IllegalArgumentException( + "accessLevel " + accessLevel + " is less than DEVICESUMMARY."); + } return new NetworkUsageRequestInfo(this, request, messenger, binder, callingUid, accessLevel); } diff --git a/service-t/src/com/android/server/net/NetworkStatsRecorder.java b/service-t/src/com/android/server/net/NetworkStatsRecorder.java index cec6609dc4..c371f0859a 100644 --- a/service-t/src/com/android/server/net/NetworkStatsRecorder.java +++ b/service-t/src/com/android/server/net/NetworkStatsRecorder.java @@ -34,12 +34,10 @@ import android.os.DropBoxManager; import android.service.NetworkStatsRecorderProto; import android.util.IndentingPrintWriter; import android.util.Log; -import android.util.MathUtils; import android.util.proto.ProtoOutputStream; import com.android.internal.util.FileRotator; - -import com.google.android.collect.Sets; +import com.android.net.module.util.NetworkStatsUtils; import libcore.io.IoUtils; @@ -132,7 +130,7 @@ public class NetworkStatsRecorder { public void setPersistThreshold(long thresholdBytes) { if (LOGV) Log.v(TAG, "setPersistThreshold() with " + thresholdBytes); - mPersistThresholdBytes = MathUtils.constrain( + mPersistThresholdBytes = NetworkStatsUtils.constrain( thresholdBytes, 1 * KB_IN_BYTES, 100 * MB_IN_BYTES); } @@ -206,7 +204,7 @@ public class NetworkStatsRecorder { */ public void recordSnapshotLocked(NetworkStats snapshot, Map ifaceIdent, long currentTimeMillis) { - final HashSet unknownIfaces = Sets.newHashSet(); + final HashSet unknownIfaces = new HashSet<>(); // skip recording when snapshot missing if (snapshot == null) return; diff --git a/service-t/src/com/android/server/net/NetworkStatsService.java b/service-t/src/com/android/server/net/NetworkStatsService.java index 44d7d1f668..4fd62c1ad5 100644 --- a/service-t/src/com/android/server/net/NetworkStatsService.java +++ b/service-t/src/com/android/server/net/NetworkStatsService.java @@ -25,7 +25,6 @@ import static android.content.Intent.ACTION_USER_REMOVED; import static android.content.Intent.EXTRA_UID; import static android.content.pm.PackageManager.PERMISSION_GRANTED; import static android.net.NetworkIdentity.SUBTYPE_COMBINED; -import static android.net.NetworkStack.checkNetworkStackPermission; import static android.net.NetworkStats.DEFAULT_NETWORK_ALL; import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.IFACE_VT; @@ -158,6 +157,7 @@ import com.android.server.LocalServices; import java.io.File; import java.io.FileDescriptor; +import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.time.Clock; @@ -985,7 +985,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { @NonNull NetworkStateSnapshot[] networkStates, @Nullable String activeIface, @NonNull UnderlyingNetworkInfo[] underlyingNetworkInfos) { - checkNetworkStackPermission(mContext); + PermissionUtils.enforceNetworkStackPermission(mContext); final long token = Binder.clearCallingIdentity(); try { @@ -1201,13 +1201,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // On background handler thread, and USER_REMOVED is protected // broadcast. - final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); - if (userId == -1) return; + final UserHandle userHandle = intent.getParcelableExtra(Intent.EXTRA_USER); + if (userHandle == null) return; synchronized (mStatsLock) { mWakeLock.acquire(); try { - removeUserLocked(userId); + removeUserLocked(userHandle); } finally { mWakeLock.release(); } @@ -1232,7 +1232,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { @Override public void limitReached(String limitName, String iface) { // only someone like NMS should be calling us - NetworkStack.checkNetworkStackPermission(mContext); + PermissionUtils.enforceNetworkStackPermission(mContext); if (LIMIT_GLOBAL_ALERT.equals(limitName)) { // kick off background poll to collect network stats unless there is already @@ -1652,8 +1652,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub { * Clean up {@link #mUidRecorder} after user is removed. */ @GuardedBy("mStatsLock") - private void removeUserLocked(int userId) { - if (LOGV) Log.v(TAG, "removeUserLocked() for userId=" + userId); + private void removeUserLocked(@NonNull UserHandle userHandle) { + if (LOGV) Log.v(TAG, "removeUserLocked() for UserHandle=" + userHandle); // Build list of UIDs that we should clean up final ArrayList uids = new ArrayList<>(); @@ -1661,7 +1661,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { PackageManager.MATCH_ANY_USER | PackageManager.MATCH_DISABLED_COMPONENTS); for (ApplicationInfo app : apps) { - final int uid = UserHandle.getUid(userId, app.uid); + final int uid = userHandle.getUid(app.uid); uids.add(uid); } @@ -1869,7 +1869,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { @GuardedBy("mStatsLock") private void dumpProtoLocked(FileDescriptor fd) { - final ProtoOutputStream proto = new ProtoOutputStream(fd); + final ProtoOutputStream proto = new ProtoOutputStream(new FileOutputStream(fd)); // TODO Right now it writes all history. Should it limit to the "since-boot" log? diff --git a/service-t/src/com/android/server/net/NetworkStatsSubscriptionsMonitor.java b/service-t/src/com/android/server/net/NetworkStatsSubscriptionsMonitor.java index 5646c752fc..93d0ae7da0 100644 --- a/service-t/src/com/android/server/net/NetworkStatsSubscriptionsMonitor.java +++ b/service-t/src/com/android/server/net/NetworkStatsSubscriptionsMonitor.java @@ -33,7 +33,7 @@ import android.util.Log; import android.util.Pair; import com.android.internal.annotations.VisibleForTesting; -import com.android.internal.util.CollectionUtils; +import com.android.net.module.util.CollectionUtils; import java.util.ArrayList; import java.util.List; @@ -99,18 +99,19 @@ public class NetworkStatsSubscriptionsMonitor extends // prevent binder call to telephony when querying RAT. Keep listener registration with empty // IMSI is meaningless since the RAT type changed is ambiguous for multi-SIM if reported // with empty IMSI. So filter the subs w/o a valid IMSI to prevent such registration. - final List> filteredNewSubs = - CollectionUtils.mapNotNull(newSubs, subId -> { - final String subscriberId = mTeleManager.getSubscriberId(subId); - return TextUtils.isEmpty(subscriberId) ? null : new Pair(subId, subscriberId); - }); + final List> filteredNewSubs = new ArrayList<>(); + for (final int subId : newSubs) { + final String subscriberId = mTeleManager.getSubscriberId(subId); + if (!TextUtils.isEmpty(subscriberId)) { + filteredNewSubs.add(new Pair(subId, subscriberId)); + } + } for (final Pair sub : filteredNewSubs) { // Fully match listener with subId and IMSI, since in some rare cases, IMSI might be // suddenly change regardless of subId, such as switch IMSI feature in modem side. // If that happens, register new listener with new IMSI and remove old one later. - if (CollectionUtils.find(mRatListeners, - it -> it.equalsKey(sub.first, sub.second)) != null) { + if (CollectionUtils.any(mRatListeners, it -> it.equalsKey(sub.first, sub.second))) { continue; } @@ -126,8 +127,8 @@ public class NetworkStatsSubscriptionsMonitor extends for (final RatTypeListener listener : new ArrayList<>(mRatListeners)) { // If there is no subId and IMSI matched the listener, removes it. - if (CollectionUtils.find(filteredNewSubs, - it -> listener.equalsKey(it.first, it.second)) == null) { + if (!CollectionUtils.any(filteredNewSubs, + it -> listener.equalsKey(it.first, it.second))) { handleRemoveRatTypeListener(listener); } } @@ -148,9 +149,10 @@ public class NetworkStatsSubscriptionsMonitor extends * @return collapsed RatType for the given subscriberId */ public int getRatTypeForSubscriberId(@NonNull String subscriberId) { - final RatTypeListener match = CollectionUtils.find(mRatListeners, + final int index = CollectionUtils.indexOf(mRatListeners, it -> TextUtils.equals(subscriberId, it.mSubscriberId)); - return match != null ? match.mLastCollapsedRatType : TelephonyManager.NETWORK_TYPE_UNKNOWN; + return index != -1 ? mRatListeners.get(index).mLastCollapsedRatType + : TelephonyManager.NETWORK_TYPE_UNKNOWN; } /**