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

Commit 85877dfb authored by lucaslin's avatar lucaslin Committed by Chiachang Wang
Browse files

[IT4.5] Update radio power from CS directly

There were two ways to update the network activity from
NetworkManagementService to BatteryStatsService.
  1. The Netd unsolicited event onInterfaceClassActivityChanged
  2. The idle timer setup and removal

The first path was replaced by previous patch to listen netd
event from BSS directly. BSS does not rely on NMS to notify
event from netd now.

This patch is going to replace the second path. In order to clear
the dependency between NMS and CS, the idle timer setup and
removal will be sent from CS to INetd directly without going via
NMS in the follow up patches. NMS will no longer receive the
idle timer update. Thus, update the radio power status from CS
to BSS directly to separate the network activity logic from NMS.

Bug: 170598012
Test: atest FrameworksNetTests
Change-Id: I716bd77168896b29a6e04f592adcf27b82edebca
parent 76529725
Loading
Loading
Loading
Loading
+37 −10
Original line number Diff line number Diff line
@@ -147,6 +147,7 @@ import android.net.netlink.InetDiagMessage;
import android.net.shared.PrivateDnsConfig;
import android.net.util.MultinetworkPolicyTracker;
import android.net.util.NetdService;
import android.os.BatteryStatsManager;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
@@ -8634,6 +8635,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
     * changes.
     */
    private static final class LegacyNetworkActivityTracker {
        private static final int NO_UID = -1;
        private final Context mContext;
        private final INetworkManagementService mNMS;

@@ -8723,6 +8725,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
                return; // do not track any other networks
            }

            updateRadioPowerState(true /* isActive */, type);

            if (timeout > 0 && iface != null) {
                try {
                    // TODO: Access INetd directly instead of NMS
@@ -8741,10 +8745,20 @@ public class ConnectivityService extends IConnectivityManager.Stub
            final String iface = networkAgent.linkProperties.getInterfaceName();
            final NetworkCapabilities caps = networkAgent.networkCapabilities;

            if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
                    || caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) {
            if (iface == null) return;

            final int type;
            if (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                type = NetworkCapabilities.TRANSPORT_CELLULAR;
            } else if (caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                type = NetworkCapabilities.TRANSPORT_WIFI;
            } else {
                return; // do not track any other networks
            }

            try {
                    // the call fails silently if no idle timer setup for this interface
                updateRadioPowerState(false /* isActive */, type);
                // The call fails silently if no idle timer setup for this interface.
                // TODO: Access INetd directly instead of NMS
                mNMS.removeIdleTimer(iface);
            } catch (Exception e) {
@@ -8752,7 +8766,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
                loge("Exception in removeDataActivityTracking " + e);
            }
        }
        }

        /**
         * Update data activity tracking when network state is updated.
@@ -8766,6 +8779,20 @@ public class ConnectivityService extends IConnectivityManager.Stub
                removeDataActivityTracking(oldNetwork);
            }
        }

        private void updateRadioPowerState(boolean isActive, int transportType) {
            final BatteryStatsManager bs = mContext.getSystemService(BatteryStatsManager.class);
            switch (transportType) {
                case NetworkCapabilities.TRANSPORT_CELLULAR:
                    bs.reportMobileRadioPowerState(isActive, NO_UID);
                    break;
                case NetworkCapabilities.TRANSPORT_WIFI:
                    bs.reportWifiRadioPowerState(isActive, NO_UID);
                    break;
                default:
                    logw("Untracked transport type:" + transportType);
            }
        }
    }

    /**
+0 −37
Original line number Diff line number Diff line
@@ -80,7 +80,6 @@ import android.os.StrictMode;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.Trace;
import android.telephony.DataConnectionRealTimeInfo;
import android.text.TextUtils;
import android.util.Log;
import android.util.Slog;
@@ -247,10 +246,6 @@ public class NetworkManagementService extends INetworkManagementService.Stub {
    private volatile boolean mFirewallEnabled;
    private volatile boolean mStrictEnabled;

    private boolean mMobileActivityFromRadio = false;
    private int mLastPowerStateFromRadio = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
    private int mLastPowerStateFromWifi = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;

    private final RemoteCallbackList<INetworkActivityListener> mNetworkActivityListeners =
            new RemoteCallbackList<>();
    private boolean mNetworkActive;
@@ -397,36 +392,6 @@ public class NetworkManagementService extends INetworkManagementService.Stub {
     */
    private void notifyInterfaceClassActivity(int type, boolean isActive, long tsNanos,
            int uid) {
        final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(type);
        int powerState = isActive
                ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
                : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
        if (isMobile) {
            if (mLastPowerStateFromRadio != powerState) {
                mLastPowerStateFromRadio = powerState;
                try {
                    // TODO: The interface changes that comes from netd are handled by BSS itself.
                    // There are still events caused by setting or removing idle timer, so keep
                    // reporting from here until setting idler timer moved to CS.
                    getBatteryStats().noteMobileRadioPowerState(powerState, tsNanos, uid);
                } catch (RemoteException e) {
                }
            }
        }

        if (ConnectivityManager.isNetworkTypeWifi(type)) {
            if (mLastPowerStateFromWifi != powerState) {
                mLastPowerStateFromWifi = powerState;
                try {
                    // TODO: The interface changes that comes from netd are handled by BSS itself.
                    // There are still events caused by setting or removing idle timer, so keep
                    // reporting from here until setting idler timer moved to CS.
                    getBatteryStats().noteWifiRadioPowerState(powerState, tsNanos, uid);
                } catch (RemoteException e) {
                }
            }
        }

        final boolean active = isActive;
        invokeForAllObservers(o -> o.interfaceClassDataActivityChanged(
                type, active, tsNanos, uid));
@@ -1847,8 +1812,6 @@ public class NetworkManagementService extends INetworkManagementService.Stub {
    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;

        pw.print("mMobileActivityFromRadio="); pw.print(mMobileActivityFromRadio);
                pw.print(" mLastPowerStateFromRadio="); pw.println(mLastPowerStateFromRadio);
        pw.print("mNetworkActive="); pw.println(mNetworkActive);

        synchronized (mQuotaLock) {