Add a new API in BatteryStatsManager for connectivity service

Connectivity service(CS) is going to be a mainline module and
currently it uses IBatteryStats to communicate with battery
stats service directly. CS cannot use this way after becomes
a mainline module so add new system APIs to communicate with
battery stats service through BatteryStatsManger.

The new API reportNetworkInterfaceForTransports requires
system-only permission NETWORK_STACK or MAINLINE_NETWORK_STACK
so that given the only caller was in ConnectivityService.

This change only add new surface, the usage in CS will be
updated in follow-up CL.

Bug: 171686421
Test: builds, boot, mobile and wifi work.
Change-Id: Ifbd4a9784ed7664751e69d530f8204e292fa6b39
This commit is contained in:
Aaron Huang
2020-10-26 17:34:50 +08:00
parent ca5181f90a
commit 558b3177d7
3 changed files with 30 additions and 3 deletions

View File

@@ -208,6 +208,10 @@ package android.net {
package android.os {
public final class BatteryStatsManager {
method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK}) public void reportNetworkInterfaceForTransports(@NonNull String, @NonNull int[]) throws java.lang.RuntimeException;
}
public class Binder implements android.os.IBinder {
method public final void markVintfStability();
}

View File

@@ -24,6 +24,7 @@ import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.content.Context;
import android.net.NetworkStack;
import android.os.connectivity.CellularBatteryStats;
import android.os.connectivity.WifiBatteryStats;
import android.telephony.DataConnectionRealTimeInfo;
@@ -458,10 +459,31 @@ public final class BatteryStatsManager {
}
}
/**
* Notifies the battery stats of a new interface, and the transport types of the network that
* includes that interface.
*
* @param iface The interface of the network.
* @param transportTypes The transport type of the network {@link Transport}.
* @hide
*/
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@RequiresPermission(anyOf = {
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
android.Manifest.permission.NETWORK_STACK})
public void reportNetworkInterfaceForTransports(@NonNull String iface,
@NonNull int[] transportTypes) throws RuntimeException {
try {
mBatteryStats.noteNetworkInterfaceForTransports(iface, transportTypes);
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
private static int getDataConnectionPowerState(boolean isActive) {
// TODO: DataConnectionRealTimeInfo is under telephony package but the constants are used
// for both Wifi and mobile. It would make more sense to separate the constants to a generic
// class or move it to generic package.
// for both Wifi and mobile. It would make more sense to separate the constants to a
// generic class or move it to generic package.
return isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
: DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
}

View File

@@ -83,6 +83,7 @@ import com.android.internal.util.FrameworkStatsLog;
import com.android.internal.util.ParseUtils;
import com.android.internal.util.function.pooled.PooledLambda;
import com.android.net.module.util.NetworkCapabilitiesUtils;
import com.android.net.module.util.PermissionUtils;
import com.android.server.LocalServices;
import com.android.server.Watchdog;
import com.android.server.net.BaseNetworkObserver;
@@ -1781,7 +1782,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
@Override
public void noteNetworkInterfaceForTransports(final String iface, int[] transportTypes) {
enforceCallingPermission();
PermissionUtils.enforceNetworkStackPermission(mContext);
synchronized (mLock) {
mHandler.post(() -> {
mStats.noteNetworkInterfaceForTransports(iface, transportTypes);