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

Commit 2c9ed7f3 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Note network interfaces based on transport

Instead of classifying interfaces by network type in BatteryStats,
classify them based on the transports array provided by the
NetworkAgent.

Network types are deprecated and transports should be used instead. This
change allows BatteryStats to stop depending on unstable APIs such as
isNetworkTypeMobile.

This change also updates nullability annotations in ConnectivityService
and NetworkAgentInfo to show that the NetworkCapabilities are non-null
(as provided by the network agent) when calling
noteNetworkInterfaceTransports.

Bug: 174436414
Test: atest
  atest ConnectivityServiceTest#testBatteryStatsNetworkType \
      --rerun-until-failure 40

Merged-In: I4e928fac8a57a9b1fc758a44af2a5719b8c871b8

Change-Id: I4e928fac8a57a9b1fc758a44af2a5719b8c871b8
parent 286dc55b
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -129,7 +129,7 @@ interface IBatteryStats {
    void noteWifiBatchedScanStartedFromSource(in WorkSource ws, int csph);
    void noteWifiBatchedScanStartedFromSource(in WorkSource ws, int csph);
    void noteWifiBatchedScanStoppedFromSource(in WorkSource ws);
    void noteWifiBatchedScanStoppedFromSource(in WorkSource ws);
    void noteWifiRadioPowerState(int powerState, long timestampNs, int uid);
    void noteWifiRadioPowerState(int powerState, long timestampNs, int uid);
    void noteNetworkInterfaceType(String iface, int type);
    void noteNetworkInterfaceForTransports(String iface, in int[] transportTypes);
    void noteNetworkStatsEnabled();
    void noteNetworkStatsEnabled();
    void noteDeviceIdleMode(int mode, String activeReason, int activeUid);
    void noteDeviceIdleMode(int mode, String activeReason, int activeUid);
    void setBatteryState(int status, int health, int plugType, int level, int temp, int volt,
    void setBatteryState(int status, int health, int plugType, int level, int temp, int volt,
+4 −5
Original line number Original line Diff line number Diff line
@@ -23,7 +23,6 @@ import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.Resources;
import android.hardware.SensorManager;
import android.hardware.SensorManager;
import android.net.ConnectivityManager;
import android.os.BatteryStats;
import android.os.BatteryStats;
import android.os.BatteryStats.Uid;
import android.os.BatteryStats.Uid;
import android.os.Build;
import android.os.Build;
@@ -37,6 +36,7 @@ import android.os.SELinux;
import android.os.ServiceManager;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserHandle;
import android.telephony.TelephonyManager;
import android.text.format.DateUtils;
import android.text.format.DateUtils;
import android.util.ArrayMap;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Log;
@@ -148,12 +148,11 @@ public class BatteryStatsHelper {
    boolean mHasBluetoothPowerReporting = false;
    boolean mHasBluetoothPowerReporting = false;


    public static boolean checkWifiOnly(Context context) {
    public static boolean checkWifiOnly(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
        final TelephonyManager tm = context.getSystemService(TelephonyManager.class);
                Context.CONNECTIVITY_SERVICE);
        if (tm == null) {
        if (cm == null) {
            return false;
            return false;
        }
        }
        return !cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
        return !tm.isDataCapable();
    }
    }


    public static boolean checkHasWifiPowerReporting(BatteryStats stats, PowerProfile profile) {
    public static boolean checkHasWifiPowerReporting(BatteryStats stats, PowerProfile profile) {
+7 −4
Original line number Original line Diff line number Diff line
@@ -16,6 +16,8 @@
package com.android.internal.os;
package com.android.internal.os;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import static android.os.BatteryStatsManager.NUM_WIFI_STATES;
import static android.os.BatteryStatsManager.NUM_WIFI_STATES;
import static android.os.BatteryStatsManager.NUM_WIFI_SUPPL_STATES;
import static android.os.BatteryStatsManager.NUM_WIFI_SUPPL_STATES;
@@ -32,7 +34,6 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.database.ContentObserver;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager;
import android.net.INetworkStatsService;
import android.net.INetworkStatsService;
import android.net.NetworkStats;
import android.net.NetworkStats;
import android.net.Uri;
import android.net.Uri;
@@ -101,6 +102,7 @@ import com.android.internal.util.FastPrintWriter;
import com.android.internal.util.FastXmlSerializer;
import com.android.internal.util.FastXmlSerializer;
import com.android.internal.util.FrameworkStatsLog;
import com.android.internal.util.FrameworkStatsLog;
import com.android.internal.util.XmlUtils;
import com.android.internal.util.XmlUtils;
import com.android.net.module.util.NetworkCapabilitiesUtils;
import libcore.util.EmptyArray;
import libcore.util.EmptyArray;
@@ -6099,11 +6101,12 @@ public class BatteryStatsImpl extends BatteryStats {
    }
    }
    /** @hide */
    /** @hide */
    public void noteNetworkInterfaceType(String iface, int networkType) {
    public void noteNetworkInterfaceForTransports(String iface, int[] transportTypes) {
        if (TextUtils.isEmpty(iface)) return;
        if (TextUtils.isEmpty(iface)) return;
        final int displayTransport = NetworkCapabilitiesUtils.getDisplayTransport(transportTypes);
        synchronized (mModemNetworkLock) {
        synchronized (mModemNetworkLock) {
            if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
            if (displayTransport == TRANSPORT_CELLULAR) {
                mModemIfaces = includeInStringArray(mModemIfaces, iface);
                mModemIfaces = includeInStringArray(mModemIfaces, iface);
                if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mModemIfaces);
                if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mModemIfaces);
            } else {
            } else {
@@ -6113,7 +6116,7 @@ public class BatteryStatsImpl extends BatteryStats {
        }
        }
        synchronized (mWifiNetworkLock) {
        synchronized (mWifiNetworkLock) {
            if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
            if (displayTransport == TRANSPORT_WIFI) {
                mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
                mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
                if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
                if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
            } else {
            } else {
+5 −5
Original line number Original line Diff line number Diff line
@@ -6050,7 +6050,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
        nai.networkAgentPortalData = lp.getCaptivePortalData();
        nai.networkAgentPortalData = lp.getCaptivePortalData();
    }
    }


    private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties newLp,
    private void updateLinkProperties(NetworkAgentInfo networkAgent, @NonNull LinkProperties newLp,
            @NonNull LinkProperties oldLp) {
            @NonNull LinkProperties oldLp) {
        int netId = networkAgent.network.getNetId();
        int netId = networkAgent.network.getNetId();


@@ -6059,8 +6059,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
        // the LinkProperties for the network are accurate.
        // the LinkProperties for the network are accurate.
        networkAgent.clatd.fixupLinkProperties(oldLp, newLp);
        networkAgent.clatd.fixupLinkProperties(oldLp, newLp);


        updateInterfaces(newLp, oldLp, netId, networkAgent.networkCapabilities,
        updateInterfaces(newLp, oldLp, netId, networkAgent.networkCapabilities);
                networkAgent.networkInfo.getType());


        // update filtering rules, need to happen after the interface update so netd knows about the
        // update filtering rules, need to happen after the interface update so netd knows about the
        // new interface (the interface name -> index map becomes initialized)
        // new interface (the interface name -> index map becomes initialized)
@@ -6199,7 +6198,7 @@ public class ConnectivityService extends IConnectivityManager.Stub


    private void updateInterfaces(final @Nullable LinkProperties newLp,
    private void updateInterfaces(final @Nullable LinkProperties newLp,
            final @Nullable LinkProperties oldLp, final int netId,
            final @Nullable LinkProperties oldLp, final int netId,
            final @Nullable NetworkCapabilities caps, final int legacyType) {
            final @NonNull NetworkCapabilities caps) {
        final CompareResult<String> interfaceDiff = new CompareResult<>(
        final CompareResult<String> interfaceDiff = new CompareResult<>(
                oldLp != null ? oldLp.getAllInterfaceNames() : null,
                oldLp != null ? oldLp.getAllInterfaceNames() : null,
                newLp != null ? newLp.getAllInterfaceNames() : null);
                newLp != null ? newLp.getAllInterfaceNames() : null);
@@ -6210,7 +6209,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
                    if (DBG) log("Adding iface " + iface + " to network " + netId);
                    if (DBG) log("Adding iface " + iface + " to network " + netId);
                    mNetd.networkAddInterface(netId, iface);
                    mNetd.networkAddInterface(netId, iface);
                    wakeupModifyInterface(iface, caps, true);
                    wakeupModifyInterface(iface, caps, true);
                    bs.noteNetworkInterfaceType(iface, legacyType);
                    bs.noteNetworkInterfaceForTransports(iface, caps.getTransportTypes());
                } catch (Exception e) {
                } catch (Exception e) {
                    loge("Exception adding interface: " + e);
                    loge("Exception adding interface: " + e);
                }
                }
@@ -6482,6 +6481,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
     * maintained here that the NetworkAgent is not aware of (e.g., validated, captive portal,
     * maintained here that the NetworkAgent is not aware of (e.g., validated, captive portal,
     * and foreground status).
     * and foreground status).
     */
     */
    @NonNull
    private NetworkCapabilities mixInCapabilities(NetworkAgentInfo nai, NetworkCapabilities nc) {
    private NetworkCapabilities mixInCapabilities(NetworkAgentInfo nai, NetworkCapabilities nc) {
        // Once a NetworkAgent is connected, complain if some immutable capabilities are removed.
        // Once a NetworkAgent is connected, complain if some immutable capabilities are removed.
         // Don't complain for VPNs since they're not driven by requests and there is no risk of
         // Don't complain for VPNs since they're not driven by requests and there is no risk of
+2 −2
Original line number Original line Diff line number Diff line
@@ -1066,9 +1066,9 @@ public final class BatteryStatsService extends IBatteryStats.Stub
    }
    }


    @Override
    @Override
    public void noteNetworkInterfaceType(String iface, int networkType) {
    public void noteNetworkInterfaceForTransports(final String iface, int[] transportTypes) {
        enforceCallingPermission();
        enforceCallingPermission();
        mStats.noteNetworkInterfaceType(iface, networkType);
        mStats.noteNetworkInterfaceForTransports(iface, transportTypes);
    }
    }


    @Override
    @Override
Loading