Merge "[MS68.3] Address comments at aosp/1958144"

This commit is contained in:
Frank Li
2022-03-03 02:26:53 +00:00
committed by Gerrit Code Review
3 changed files with 20 additions and 10 deletions

View File

@@ -744,8 +744,9 @@ public class NetworkStatsManager {
* {@link #unregisterUsageCallback} is called. * {@link #unregisterUsageCallback} is called.
* *
* @param template Template used to match networks. See {@link NetworkTemplate}. * @param template Template used to match networks. See {@link NetworkTemplate}.
* @param thresholdBytes Threshold in bytes to be notified on. The provided value that lower * @param thresholdBytes Threshold in bytes to be notified on. Provided values lower than 2MiB
* than 2MiB will be clamped for non-privileged callers. * will be clamped for callers except callers with the NETWORK_STACK
* permission.
* @param executor The executor on which callback will be invoked. The provided {@link Executor} * @param executor The executor on which callback will be invoked. The provided {@link Executor}
* must run callback sequentially, otherwise the order of callbacks cannot be * must run callback sequentially, otherwise the order of callbacks cannot be
* guaranteed. * guaranteed.
@@ -754,6 +755,9 @@ public class NetworkStatsManager {
* @hide * @hide
*/ */
@SystemApi(client = MODULE_LIBRARIES) @SystemApi(client = MODULE_LIBRARIES)
@RequiresPermission(anyOf = {
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
android.Manifest.permission.NETWORK_STACK}, conditional = true)
public void registerUsageCallback(@NonNull NetworkTemplate template, long thresholdBytes, public void registerUsageCallback(@NonNull NetworkTemplate template, long thresholdBytes,
@NonNull @CallbackExecutor Executor executor, @NonNull UsageCallback callback) { @NonNull @CallbackExecutor Executor executor, @NonNull UsageCallback callback) {
Objects.requireNonNull(template, "NetworkTemplate cannot be null"); Objects.requireNonNull(template, "NetworkTemplate cannot be null");

View File

@@ -19,8 +19,11 @@ package com.android.server.net;
import static android.app.usage.NetworkStatsManager.MIN_THRESHOLD_BYTES; import static android.app.usage.NetworkStatsManager.MIN_THRESHOLD_BYTES;
import android.app.usage.NetworkStatsManager; import android.app.usage.NetworkStatsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.DataUsageRequest; import android.net.DataUsageRequest;
import android.net.NetworkIdentitySet; import android.net.NetworkIdentitySet;
import android.net.NetworkStack;
import android.net.NetworkStats; import android.net.NetworkStats;
import android.net.NetworkStatsAccess; import android.net.NetworkStatsAccess;
import android.net.NetworkStatsCollection; import android.net.NetworkStatsCollection;
@@ -74,9 +77,9 @@ class NetworkStatsObservers {
* *
* @return the normalized request wrapped within {@link RequestInfo}. * @return the normalized request wrapped within {@link RequestInfo}.
*/ */
public DataUsageRequest register(DataUsageRequest inputRequest, IUsageCallback callback, public DataUsageRequest register(Context context, DataUsageRequest inputRequest,
int callingUid, @NetworkStatsAccess.Level int accessLevel) { IUsageCallback callback, int callingUid, @NetworkStatsAccess.Level int accessLevel) {
DataUsageRequest request = buildRequest(inputRequest, callingUid); DataUsageRequest request = buildRequest(context, inputRequest, callingUid);
RequestInfo requestInfo = buildRequestInfo(request, callback, callingUid, RequestInfo requestInfo = buildRequestInfo(request, callback, callingUid,
accessLevel); accessLevel);
@@ -194,10 +197,13 @@ class NetworkStatsObservers {
} }
} }
private DataUsageRequest buildRequest(DataUsageRequest request, int callingUid) { private DataUsageRequest buildRequest(Context context, DataUsageRequest request,
// For non-system uid, cap the minimum threshold to a safe default to avoid too int callingUid) {
// many callbacks. // For non-NETWORK_STACK permission uid, cap the minimum threshold to a safe default to
long thresholdInBytes = (callingUid == Process.SYSTEM_UID ? request.thresholdInBytes // avoid too many callbacks.
final long thresholdInBytes = (context.checkPermission(
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, Process.myPid(), callingUid)
== PackageManager.PERMISSION_GRANTED ? request.thresholdInBytes
: Math.max(MIN_THRESHOLD_BYTES, request.thresholdInBytes)); : Math.max(MIN_THRESHOLD_BYTES, request.thresholdInBytes));
if (thresholdInBytes > request.thresholdInBytes) { if (thresholdInBytes > request.thresholdInBytes) {
Log.w(TAG, "Threshold was too low for " + request Log.w(TAG, "Threshold was too low for " + request

View File

@@ -1285,7 +1285,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
DataUsageRequest normalizedRequest; DataUsageRequest normalizedRequest;
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
normalizedRequest = mStatsObservers.register( normalizedRequest = mStatsObservers.register(mContext,
request, callback, callingUid, accessLevel); request, callback, callingUid, accessLevel);
} finally { } finally {
Binder.restoreCallingIdentity(token); Binder.restoreCallingIdentity(token);