Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 53d603a7 authored by Junyu Lai's avatar Junyu Lai
Browse files

[MS37.1] Expose APIs which is used by NPMS

Test: TH
Bug: 204830222
CTS-Coverage-Bug: 213124616
Change-Id: I16418791246b12fc3da7348ed36b58274e20db54
parent 90cd83a0
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -54,7 +54,12 @@ package android.app {
package android.app.usage {

  public class NetworkStatsManager {
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void forceUpdate();
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void notifyNetworkStatus(@NonNull java.util.List<android.net.Network>, @NonNull java.util.List<android.net.NetworkStateSnapshot>, @Nullable String, @NonNull java.util.List<android.net.UnderlyingNetworkInfo>);
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void setDefaultGlobalAlert(long);
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void setPollOnOpen(boolean);
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void setStatsProviderWarningAndLimitAsync(@NonNull String, long, long);
    method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void setUidForeground(int, boolean);
  }

}
+25 −9
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.app.usage;

import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;

import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
@@ -150,7 +151,13 @@ public class NetworkStatsManager {
     * @param pollOnOpen true if poll is needed.
     * @hide
     */
    // @SystemApi(client = MODULE_LIBRARIES)
    // The system will ignore any non-default values for non-privileged
    // processes, so processes that don't hold the appropriate permissions
    // can make no use of this API.
    @SystemApi(client = MODULE_LIBRARIES)
    @RequiresPermission(anyOf = {
            NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
            android.Manifest.permission.NETWORK_STACK})
    public void setPollOnOpen(boolean pollOnOpen) {
        if (pollOnOpen) {
            mFlags |= FLAG_POLL_ON_OPEN;
@@ -877,7 +884,7 @@ public class NetworkStatsManager {
     *
     * @hide
     */
    // @SystemApi
    @SystemApi(client = MODULE_LIBRARIES)
    @RequiresPermission(anyOf = {
            NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
            android.Manifest.permission.NETWORK_STACK})
@@ -890,17 +897,18 @@ public class NetworkStatsManager {
    }

    /**
     * Advise persistence threshold; may be overridden internally.
     * Set default value of global alert bytes, the value will be clamped to [128kB, 2MB].
     *
     * @hide
     */
    // @SystemApi
    @SystemApi(client = MODULE_LIBRARIES)
    @RequiresPermission(anyOf = {
            NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
            android.Manifest.permission.NETWORK_STACK})
    public void advisePersistThreshold(long thresholdBytes) {
            Manifest.permission.NETWORK_STACK})
    public void setDefaultGlobalAlert(long alertBytes) {
        try {
            mService.advisePersistThreshold(thresholdBytes);
            // TODO: Sync internal naming with the API surface.
            mService.advisePersistThreshold(alertBytes);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -911,7 +919,7 @@ public class NetworkStatsManager {
     *
     * @hide
     */
    // @SystemApi
    @SystemApi(client = MODULE_LIBRARIES)
    @RequiresPermission(anyOf = {
            NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
            android.Manifest.permission.NETWORK_STACK})
@@ -927,9 +935,17 @@ public class NetworkStatsManager {
     * Set the warning and limit to all registered custom network stats providers.
     * Note that invocation of any interface will be sent to all providers.
     *
     * Asynchronicity notes : because traffic may be happening on the device at the same time, it
     * doesn't make sense to wait for the warning and limit to be set – a caller still wouldn't
     * know when exactly it was effective. All that can matter is that it's done quickly. Also,
     * this method can't fail, so there is no status to return. All providers will see the new
     * values soon.
     * As such, this method returns immediately and sends the warning and limit to all providers
     * as soon as possible through a one-way binder call.
     *
     * @hide
     */
    // @SystemApi
    @SystemApi(client = MODULE_LIBRARIES)
    @RequiresPermission(anyOf = {
            NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
            android.Manifest.permission.NETWORK_STACK})
+23 −10
Original line number Diff line number Diff line
@@ -707,12 +707,25 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        return now - lastCallTime < POLL_RATE_LIMIT_MS;
    }

    private INetworkStatsSession openSessionInternal(final int flags, final String callingPackage) {
    private int restrictFlagsForCaller(int flags) {
        // All non-privileged callers are not allowed to turn off POLL_ON_OPEN.
        final boolean isPrivileged = PermissionUtils.checkAnyPermissionOf(mContext,
                NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
                android.Manifest.permission.NETWORK_STACK);
        if (!isPrivileged) {
            flags |= NetworkStatsManager.FLAG_POLL_ON_OPEN;
        }
        // Non-system uids are rate limited for POLL_ON_OPEN.
        final int callingUid = Binder.getCallingUid();
        final int usedFlags = isRateLimitedForPoll(callingUid)
        flags = isRateLimitedForPoll(callingUid)
                ? flags & (~NetworkStatsManager.FLAG_POLL_ON_OPEN)
                : flags;
        if ((usedFlags & (NetworkStatsManager.FLAG_POLL_ON_OPEN
        return flags;
    }

    private INetworkStatsSession openSessionInternal(final int flags, final String callingPackage) {
        final int restrictedFlags = restrictFlagsForCaller(flags);
        if ((restrictedFlags & (NetworkStatsManager.FLAG_POLL_ON_OPEN
                | NetworkStatsManager.FLAG_POLL_FORCE)) != 0) {
            final long ident = Binder.clearCallingIdentity();
            try {
@@ -726,7 +739,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        // for its lifetime; when caller closes only weak references remain.

        return new INetworkStatsSession.Stub() {
            private final int mCallingUid = callingUid;
            private final int mCallingUid = Binder.getCallingUid();
            private final String mCallingPackage = callingPackage;
            private final @NetworkStatsAccess.Level int mAccessLevel = checkAccessLevel(
                    callingPackage);
@@ -760,21 +773,21 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
            @Override
            public NetworkStats getDeviceSummaryForNetwork(
                    NetworkTemplate template, long start, long end) {
                return internalGetSummaryForNetwork(template, usedFlags, start, end, mAccessLevel,
                        mCallingUid);
                return internalGetSummaryForNetwork(template, restrictedFlags, start, end,
                        mAccessLevel, mCallingUid);
            }

            @Override
            public NetworkStats getSummaryForNetwork(
                    NetworkTemplate template, long start, long end) {
                return internalGetSummaryForNetwork(template, usedFlags, start, end, mAccessLevel,
                        mCallingUid);
                return internalGetSummaryForNetwork(template, restrictedFlags, start, end,
                        mAccessLevel, mCallingUid);
            }

            @Override
            public NetworkStatsHistory getHistoryForNetwork(NetworkTemplate template, int fields) {
                return internalGetHistoryForNetwork(template, usedFlags, fields, mAccessLevel,
                        mCallingUid);
                return internalGetHistoryForNetwork(template, restrictedFlags, fields,
                        mAccessLevel, mCallingUid);
            }

            @Override
+2 −1
Original line number Diff line number Diff line
@@ -5069,7 +5069,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                    // make sure stats are recorded frequently enough; we aim
                    // for 2MB threshold for 2GB/month rules.
                    final long persistThreshold = lowestRule / 1000;
                    mNetworkStats.advisePersistThreshold(persistThreshold);
                    // TODO: Sync internal naming with the API surface.
                    mNetworkStats.setDefaultGlobalAlert(persistThreshold);
                    return true;
                }
                case MSG_UPDATE_INTERFACE_QUOTAS: {
+1 −1
Original line number Diff line number Diff line
@@ -2094,7 +2094,7 @@ public class NetworkPolicyManagerServiceTest {
    }

    private void verifyAdvisePersistThreshold() throws Exception {
        verify(mStatsManager).advisePersistThreshold(anyLong());
        verify(mStatsManager).setDefaultGlobalAlert(anyLong());
    }

    private static class TestAbstractFuture<T> extends AbstractFuture<T> {