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

Commit 3a6a98f5 authored by Junyu Lai's avatar Junyu Lai Committed by Android (Google) Code Review
Browse files

Merge changes from topics "am-ad04c8ad832c4674be35bfc5fe4280dd-rvc-dev",...

Merge changes from topics "am-ad04c8ad832c4674be35bfc5fe4280dd-rvc-dev", "sp21-api-review-feedback", "sp25-networkstats-rename" into rvc-dev

* changes:
  [SP25] Rename functions that add Entry conditionally
  [SP24] Rename functions that add Entry unconditionally
  [SP23] Address misc API review comment about NetworkStats
  [SP21] Address comments for API council review about aosp/1172143
  [SP20] Check signature permission when accessing network stats provider
parents 0d4f4e46 32fb2a0e
Loading
Loading
Loading
Loading
+12 −15
Original line number Diff line number Diff line
@@ -148,6 +148,7 @@ package android {
    field public static final String NETWORK_SETUP_WIZARD = "android.permission.NETWORK_SETUP_WIZARD";
    field public static final String NETWORK_SIGNAL_STRENGTH_WAKEUP = "android.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP";
    field public static final String NETWORK_STACK = "android.permission.NETWORK_STACK";
    field public static final String NETWORK_STATS_PROVIDER = "android.permission.NETWORK_STATS_PROVIDER";
    field public static final String NOTIFICATION_DURING_SETUP = "android.permission.NOTIFICATION_DURING_SETUP";
    field public static final String NOTIFY_TV_INPUTS = "android.permission.NOTIFY_TV_INPUTS";
    field public static final String OBSERVE_APP_USAGE = "android.permission.OBSERVE_APP_USAGE";
@@ -1409,7 +1410,8 @@ package android.app.usage {
  }
  public class NetworkStatsManager {
    method @NonNull @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) public android.net.netstats.provider.NetworkStatsProviderCallback registerNetworkStatsProvider(@NonNull String, @NonNull android.net.netstats.provider.AbstractNetworkStatsProvider);
    method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_STATS_PROVIDER, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void registerNetworkStatsProvider(@NonNull String, @NonNull android.net.netstats.provider.NetworkStatsProvider);
    method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_STATS_PROVIDER, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void unregisterNetworkStatsProvider(@NonNull android.net.netstats.provider.NetworkStatsProvider);
  }
  public static final class UsageEvents.Event {
@@ -6406,14 +6408,13 @@ package android.net {
  public final class NetworkStats implements 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 addValues(@NonNull android.net.NetworkStats.Entry);
    method @NonNull public android.net.NetworkStats addEntry(@NonNull android.net.NetworkStats.Entry);
    method public int describeContents();
    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;
    field public static final int DEFAULT_NETWORK_NO = 0; // 0x0
    field public static final int DEFAULT_NETWORK_YES = 1; // 0x1
    field @Nullable public static final String IFACE_ALL;
    field public static final String IFACE_VT = "vt_data0";
    field public static final int METERED_NO = 0; // 0x0
    field public static final int METERED_YES = 1; // 0x1
@@ -6813,21 +6814,17 @@ package android.net.metrics {
package android.net.netstats.provider {
  public abstract class AbstractNetworkStatsProvider {
    ctor public AbstractNetworkStatsProvider();
    method public abstract void requestStatsUpdate(int);
    method public abstract void setAlert(long);
    method public abstract void setLimit(@NonNull String, long);
  public abstract class NetworkStatsProvider {
    ctor public NetworkStatsProvider();
    method public void notifyAlertReached();
    method public void notifyLimitReached();
    method public void notifyStatsUpdated(int, @NonNull android.net.NetworkStats, @NonNull android.net.NetworkStats);
    method public abstract void onRequestStatsUpdate(int);
    method public abstract void onSetAlert(long);
    method public abstract void onSetLimit(@NonNull String, long);
    field public static final int QUOTA_UNLIMITED = -1; // 0xffffffff
  }
  public class NetworkStatsProviderCallback {
    method public void onAlertReached();
    method public void onLimitReached();
    method public void onStatsUpdated(int, @NonNull android.net.NetworkStats, @NonNull android.net.NetworkStats);
    method public void unregister();
  }
}
package android.net.sip {
+37 −16
Original line number Diff line number Diff line
@@ -29,10 +29,10 @@ import android.net.ConnectivityManager;
import android.net.DataUsageRequest;
import android.net.INetworkStatsService;
import android.net.NetworkIdentity;
import android.net.NetworkStack;
import android.net.NetworkTemplate;
import android.net.netstats.provider.AbstractNetworkStatsProvider;
import android.net.netstats.provider.NetworkStatsProviderCallback;
import android.net.netstats.provider.NetworkStatsProviderWrapper;
import android.net.netstats.provider.INetworkStatsProviderCallback;
import android.net.netstats.provider.NetworkStatsProvider;
import android.os.Binder;
import android.os.Handler;
import android.os.Looper;
@@ -527,32 +527,53 @@ public class NetworkStatsManager {

    /**
     * Registers a custom provider of {@link android.net.NetworkStats} to provide network statistics
     * to the system. To unregister, invoke {@link NetworkStatsProviderCallback#unregister()}.
     * to the system. To unregister, invoke {@link #unregisterNetworkStatsProvider}.
     * Note that no de-duplication of statistics between providers is performed, so each provider
     * must only report network traffic that is not being reported by any other provider.
     * must only report network traffic that is not being reported by any other provider. Also note
     * that the provider cannot be re-registered after unregistering.
     *
     * @param tag a human readable identifier of the custom network stats provider. This is only
     *            used for debugging.
     * @param provider the subclass of {@link AbstractNetworkStatsProvider} that needs to be
     * @param provider the subclass of {@link NetworkStatsProvider} that needs to be
     *                 registered to the system.
     * @return a {@link NetworkStatsProviderCallback}, which can be used to report events to the
     *         system or unregister the provider.
     * @hide
     */
    @SystemApi
    @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
    @NonNull public NetworkStatsProviderCallback registerNetworkStatsProvider(
    @RequiresPermission(anyOf = {
            android.Manifest.permission.NETWORK_STATS_PROVIDER,
            NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
    @NonNull public void registerNetworkStatsProvider(
            @NonNull String tag,
            @NonNull AbstractNetworkStatsProvider provider) {
            @NonNull NetworkStatsProvider provider) {
        try {
            final NetworkStatsProviderWrapper wrapper = new NetworkStatsProviderWrapper(provider);
            return new NetworkStatsProviderCallback(
                    mService.registerNetworkStatsProvider(tag, wrapper));
            if (provider.getProviderCallbackBinder() != null) {
                throw new IllegalArgumentException("provider is already registered");
            }
            final INetworkStatsProviderCallback cbBinder =
                    mService.registerNetworkStatsProvider(tag, provider.getProviderBinder());
            provider.setProviderCallbackBinder(cbBinder);
        } catch (RemoteException e) {
            e.rethrowAsRuntimeException();
        }
    }

    /**
     * Unregisters an instance of {@link NetworkStatsProvider}.
     *
     * @param provider the subclass of {@link NetworkStatsProvider} that needs to be
     *                 unregistered to the system.
     * @hide
     */
    @SystemApi
    @RequiresPermission(anyOf = {
            android.Manifest.permission.NETWORK_STATS_PROVIDER,
            NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK})
    @NonNull public void unregisterNetworkStatsProvider(@NonNull NetworkStatsProvider provider) {
        try {
            provider.getProviderCallbackBinderOrThrow().unregister();
        } catch (RemoteException e) {
            e.rethrowAsRuntimeException();
        }
        // Unreachable code, but compiler doesn't know about it.
        return null;
    }

    private static NetworkTemplate createTemplate(int networkType, String subscriberId) {
+54 −17
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static android.os.Process.CLAT_UID;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.Parcel;
@@ -58,9 +57,12 @@ import java.util.function.Predicate;
public final class NetworkStats implements Parcelable {
    private static final String TAG = "NetworkStats";

    /** {@link #iface} value when interface details unavailable. */
    @SuppressLint("CompileTimeConstant")
    /**
     * {@link #iface} value when interface details unavailable.
     * @hide
     */
    @Nullable public static final String IFACE_ALL = null;

    /**
     * Virtual network interface for video telephony. This is for VT data usage counting
     * purpose.
@@ -248,7 +250,13 @@ public final class NetworkStats implements Parcelable {
    @UnsupportedAppUsage
    private long[] operations;

    /** @hide */
    /**
     * Basic element of network statistics. Contains the number of packets and number of bytes
     * transferred on both directions in a given set of conditions. See
     * {@link Entry#Entry(String, int, int, int, int, int, int, long, long, long, long, long)}.
     *
     * @hide
     */
    @SystemApi
    public static class Entry {
        /** @hide */
@@ -319,6 +327,35 @@ public final class NetworkStats implements Parcelable {
                    rxBytes, rxPackets, txBytes, txPackets, operations);
        }

        /**
         * Construct a {@link Entry} object by giving statistics of packet and byte transferred on
         * both direction, and associated with a set of given conditions.
         *
         * @param iface interface name of this {@link Entry}. Or null if not specified.
         * @param uid uid of this {@link Entry}. {@link #UID_TETHERING} if this {@link Entry} is
         *            for tethering. Or {@link #UID_ALL} if this {@link NetworkStats} is only
         *            counting iface stats.
         * @param set usage state of this {@link Entry}. Should be one of the following
         *            values: {@link #SET_DEFAULT}, {@link #SET_FOREGROUND}.
         * @param tag tag of this {@link Entry}.
         * @param metered metered state of this {@link Entry}. Should be one of the following
         *                values: {link #METERED_YES}, {link #METERED_NO}.
         * @param roaming roaming state of this {@link Entry}. Should be one of the following
         *                values: {link #ROAMING_YES}, {link #ROAMING_NO}.
         * @param defaultNetwork default network status of this {@link Entry}. Should be one
         *                       of the following values: {link #DEFAULT_NETWORK_YES},
         *                       {link #DEFAULT_NETWORK_NO}.
         * @param rxBytes Number of bytes received for this {@link Entry}. Statistics should
         *                represent the contents of IP packets, including IP headers.
         * @param rxPackets Number of packets received for this {@link Entry}. Statistics should
         *                  represent the contents of IP packets, including IP headers.
         * @param txBytes Number of bytes transmitted for this {@link Entry}. Statistics should
         *                represent the contents of IP packets, including IP headers.
         * @param txPackets Number of bytes transmitted for this {@link Entry}. Statistics should
         *                  represent the contents of IP packets, including IP headers.
         * @param operations count of network operations performed for this {@link Entry}. This can
         *                   be used to derive bytes-per-operation.
         */
        public Entry(@Nullable String iface, int uid, @State int set, int tag,
                @Meteredness int metered, @Roaming int roaming, @DefaultNetwork int defaultNetwork,
                long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
@@ -466,7 +503,7 @@ public final class NetworkStats implements Parcelable {
        NetworkStats.Entry entry = null;
        for (int i = 0; i < size; i++) {
            entry = getValues(i, entry);
            clone.addEntry(entry);
            clone.insertEntry(entry);
        }
        return clone;
    }
@@ -493,26 +530,26 @@ public final class NetworkStats implements Parcelable {

    /** @hide */
    @VisibleForTesting
    public NetworkStats addIfaceValues(
    public NetworkStats insertEntry(
            String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
        return addEntry(
        return insertEntry(
                iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
    }

    /** @hide */
    @VisibleForTesting
    public NetworkStats addEntry(String iface, int uid, int set, int tag, long rxBytes,
    public NetworkStats insertEntry(String iface, int uid, int set, int tag, long rxBytes,
            long rxPackets, long txBytes, long txPackets, long operations) {
        return addEntry(new Entry(
        return insertEntry(new Entry(
                iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
    }

    /** @hide */
    @VisibleForTesting
    public NetworkStats addEntry(String iface, int uid, int set, int tag, int metered, int roaming,
            int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets,
            long operations) {
        return addEntry(new Entry(
    public NetworkStats insertEntry(String iface, int uid, int set, int tag, int metered,
            int roaming, int defaultNetwork, long rxBytes, long rxPackets, long txBytes,
            long txPackets, long operations) {
        return insertEntry(new Entry(
                iface, uid, set, tag, metered, roaming, defaultNetwork, rxBytes, rxPackets,
                txBytes, txPackets, operations));
    }
@@ -522,7 +559,7 @@ public final class NetworkStats implements Parcelable {
     * object can be recycled across multiple calls.
     * @hide
     */
    public NetworkStats addEntry(Entry entry) {
    public NetworkStats insertEntry(Entry entry) {
        if (size >= capacity) {
            final int newLength = Math.max(size, 10) * 3 / 2;
            iface = Arrays.copyOf(iface, newLength);
@@ -665,7 +702,7 @@ public final class NetworkStats implements Parcelable {
                entry.roaming, entry.defaultNetwork);
        if (i == -1) {
            // only create new entry when positive contribution
            addEntry(entry);
            insertEntry(entry);
        } else {
            rxBytes[i] += entry.rxBytes;
            rxPackets[i] += entry.rxPackets;
@@ -684,7 +721,7 @@ public final class NetworkStats implements Parcelable {
     * @param entry the {@link Entry} to add.
     * @return a new constructed {@link NetworkStats} object that contains the result.
     */
    public @NonNull NetworkStats addValues(@NonNull Entry entry) {
    public @NonNull NetworkStats addEntry(@NonNull Entry entry) {
        return this.clone().combineValues(entry);
    }

@@ -1003,7 +1040,7 @@ public final class NetworkStats implements Parcelable {
                entry.operations = Math.max(entry.operations, 0);
            }

            result.addEntry(entry);
            result.insertEntry(entry);
        }

        return result;
+0 −70
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.netstats.provider;

import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.net.NetworkStats;

/**
 * A base class that allows external modules to implement a custom network statistics provider.
 * @hide
 */
@SystemApi
public abstract class AbstractNetworkStatsProvider {
    /**
     * A value used by {@link #setLimit} and {@link #setAlert} indicates there is no limit.
     */
    public static final int QUOTA_UNLIMITED = -1;

    /**
     * Called by {@code NetworkStatsService} when global polling is needed. Custom
     * implementation of providers MUST respond to it by calling
     * {@link NetworkStatsProviderCallback#onStatsUpdated} within one minute. Responding
     * later than this may cause the stats to be dropped.
     *
     * @param token a positive number identifying the new state of the system under which
     *              {@link NetworkStats} have to be gathered from now on. When this is called,
     *              custom implementations of providers MUST report the latest stats with the
     *              previous token, under which stats were being gathered so far.
     */
    public abstract void requestStatsUpdate(int token);

    /**
     * Called by {@code NetworkStatsService} when setting the interface quota for the specified
     * upstream interface. When this is called, the custom implementation should block all egress
     * packets on the {@code iface} associated with the provider when {@code quotaBytes} bytes have
     * been reached, and MUST respond to it by calling
     * {@link NetworkStatsProviderCallback#onLimitReached()}.
     *
     * @param iface the interface requiring the operation.
     * @param quotaBytes the quota defined as the number of bytes, starting from zero and counting
     *                   from now. A value of {@link #QUOTA_UNLIMITED} indicates there is no limit.
     */
    public abstract void setLimit(@NonNull String iface, long quotaBytes);

    /**
     * Called by {@code NetworkStatsService} when setting the alert bytes. Custom implementations
     * MUST call {@link NetworkStatsProviderCallback#onAlertReached()} when {@code quotaBytes} bytes
     * have been reached. Unlike {@link #setLimit(String, long)}, the custom implementation should
     * not block all egress packets.
     *
     * @param quotaBytes the quota defined as the number of bytes, starting from zero and counting
     *                   from now. A value of {@link #QUOTA_UNLIMITED} indicates there is no alert.
     */
    public abstract void setAlert(long quotaBytes);
}
+3 −3
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ package android.net.netstats.provider;
 * @hide
 */
oneway interface INetworkStatsProvider {
    void requestStatsUpdate(int token);
    void setLimit(String iface, long quotaBytes);
    void setAlert(long quotaBytes);
    void onRequestStatsUpdate(int token);
    void onSetLimit(String iface, long quotaBytes);
    void onSetAlert(long quotaBytes);
}
Loading