Merge changes I16418791,I210f5ddc
* changes: [MS37.1] Expose APIs which is used by NPMS [MS55] Remove INetworkStats dependency from DataIdleTest
This commit is contained in:
@@ -54,7 +54,12 @@ package android.app {
|
|||||||
package android.app.usage {
|
package android.app.usage {
|
||||||
|
|
||||||
public class NetworkStatsManager {
|
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 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package android.app.usage;
|
|||||||
|
|
||||||
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
|
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
import android.annotation.RequiresPermission;
|
import android.annotation.RequiresPermission;
|
||||||
@@ -150,7 +151,13 @@ public class NetworkStatsManager {
|
|||||||
* @param pollOnOpen true if poll is needed.
|
* @param pollOnOpen true if poll is needed.
|
||||||
* @hide
|
* @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) {
|
public void setPollOnOpen(boolean pollOnOpen) {
|
||||||
if (pollOnOpen) {
|
if (pollOnOpen) {
|
||||||
mFlags |= FLAG_POLL_ON_OPEN;
|
mFlags |= FLAG_POLL_ON_OPEN;
|
||||||
@@ -877,7 +884,7 @@ public class NetworkStatsManager {
|
|||||||
*
|
*
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
// @SystemApi
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
@RequiresPermission(anyOf = {
|
@RequiresPermission(anyOf = {
|
||||||
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
||||||
android.Manifest.permission.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
|
* @hide
|
||||||
*/
|
*/
|
||||||
// @SystemApi
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
@RequiresPermission(anyOf = {
|
@RequiresPermission(anyOf = {
|
||||||
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
||||||
android.Manifest.permission.NETWORK_STACK})
|
Manifest.permission.NETWORK_STACK})
|
||||||
public void advisePersistThreshold(long thresholdBytes) {
|
public void setDefaultGlobalAlert(long alertBytes) {
|
||||||
try {
|
try {
|
||||||
mService.advisePersistThreshold(thresholdBytes);
|
// TODO: Sync internal naming with the API surface.
|
||||||
|
mService.advisePersistThreshold(alertBytes);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
throw e.rethrowFromSystemServer();
|
throw e.rethrowFromSystemServer();
|
||||||
}
|
}
|
||||||
@@ -911,7 +919,7 @@ public class NetworkStatsManager {
|
|||||||
*
|
*
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
// @SystemApi
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
@RequiresPermission(anyOf = {
|
@RequiresPermission(anyOf = {
|
||||||
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
||||||
android.Manifest.permission.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.
|
* Set the warning and limit to all registered custom network stats providers.
|
||||||
* Note that invocation of any interface will be sent to all 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
|
* @hide
|
||||||
*/
|
*/
|
||||||
// @SystemApi
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
@RequiresPermission(anyOf = {
|
@RequiresPermission(anyOf = {
|
||||||
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
||||||
android.Manifest.permission.NETWORK_STACK})
|
android.Manifest.permission.NETWORK_STACK})
|
||||||
|
|||||||
@@ -707,12 +707,25 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
return now - lastCallTime < POLL_RATE_LIMIT_MS;
|
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 callingUid = Binder.getCallingUid();
|
||||||
final int usedFlags = isRateLimitedForPoll(callingUid)
|
flags = isRateLimitedForPoll(callingUid)
|
||||||
? flags & (~NetworkStatsManager.FLAG_POLL_ON_OPEN)
|
? flags & (~NetworkStatsManager.FLAG_POLL_ON_OPEN)
|
||||||
: flags;
|
: 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) {
|
| NetworkStatsManager.FLAG_POLL_FORCE)) != 0) {
|
||||||
final long ident = Binder.clearCallingIdentity();
|
final long ident = Binder.clearCallingIdentity();
|
||||||
try {
|
try {
|
||||||
@@ -726,7 +739,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
// for its lifetime; when caller closes only weak references remain.
|
// for its lifetime; when caller closes only weak references remain.
|
||||||
|
|
||||||
return new INetworkStatsSession.Stub() {
|
return new INetworkStatsSession.Stub() {
|
||||||
private final int mCallingUid = callingUid;
|
private final int mCallingUid = Binder.getCallingUid();
|
||||||
private final String mCallingPackage = callingPackage;
|
private final String mCallingPackage = callingPackage;
|
||||||
private final @NetworkStatsAccess.Level int mAccessLevel = checkAccessLevel(
|
private final @NetworkStatsAccess.Level int mAccessLevel = checkAccessLevel(
|
||||||
callingPackage);
|
callingPackage);
|
||||||
@@ -760,21 +773,21 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
@Override
|
@Override
|
||||||
public NetworkStats getDeviceSummaryForNetwork(
|
public NetworkStats getDeviceSummaryForNetwork(
|
||||||
NetworkTemplate template, long start, long end) {
|
NetworkTemplate template, long start, long end) {
|
||||||
return internalGetSummaryForNetwork(template, usedFlags, start, end, mAccessLevel,
|
return internalGetSummaryForNetwork(template, restrictedFlags, start, end,
|
||||||
mCallingUid);
|
mAccessLevel, mCallingUid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NetworkStats getSummaryForNetwork(
|
public NetworkStats getSummaryForNetwork(
|
||||||
NetworkTemplate template, long start, long end) {
|
NetworkTemplate template, long start, long end) {
|
||||||
return internalGetSummaryForNetwork(template, usedFlags, start, end, mAccessLevel,
|
return internalGetSummaryForNetwork(template, restrictedFlags, start, end,
|
||||||
mCallingUid);
|
mAccessLevel, mCallingUid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NetworkStatsHistory getHistoryForNetwork(NetworkTemplate template, int fields) {
|
public NetworkStatsHistory getHistoryForNetwork(NetworkTemplate template, int fields) {
|
||||||
return internalGetHistoryForNetwork(template, usedFlags, fields, mAccessLevel,
|
return internalGetHistoryForNetwork(template, restrictedFlags, fields,
|
||||||
mCallingUid);
|
mAccessLevel, mCallingUid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -5098,7 +5098,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
|
|||||||
// make sure stats are recorded frequently enough; we aim
|
// make sure stats are recorded frequently enough; we aim
|
||||||
// for 2MB threshold for 2GB/month rules.
|
// for 2MB threshold for 2GB/month rules.
|
||||||
final long persistThreshold = lowestRule / 1000;
|
final long persistThreshold = lowestRule / 1000;
|
||||||
mNetworkStats.advisePersistThreshold(persistThreshold);
|
// TODO: Sync internal naming with the API surface.
|
||||||
|
mNetworkStats.setDefaultGlobalAlert(persistThreshold);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case MSG_UPDATE_INTERFACE_QUOTAS: {
|
case MSG_UPDATE_INTERFACE_QUOTAS: {
|
||||||
|
|||||||
@@ -2092,7 +2092,7 @@ public class NetworkPolicyManagerServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void verifyAdvisePersistThreshold() throws Exception {
|
private void verifyAdvisePersistThreshold() throws Exception {
|
||||||
verify(mStatsManager).advisePersistThreshold(anyLong());
|
verify(mStatsManager).setDefaultGlobalAlert(anyLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TestAbstractFuture<T> extends AbstractFuture<T> {
|
private static class TestAbstractFuture<T> extends AbstractFuture<T> {
|
||||||
|
|||||||
@@ -15,20 +15,19 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.tests.dataidle;
|
package com.android.tests.dataidle;
|
||||||
|
|
||||||
|
import static android.net.NetworkStats.METERED_YES;
|
||||||
|
|
||||||
|
import android.app.usage.NetworkStats;
|
||||||
|
import android.app.usage.NetworkStatsManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.INetworkStatsService;
|
|
||||||
import android.net.INetworkStatsSession;
|
|
||||||
import android.net.NetworkStats;
|
|
||||||
import android.net.NetworkStats.Entry;
|
|
||||||
import android.net.NetworkTemplate;
|
import android.net.NetworkTemplate;
|
||||||
import android.net.TrafficStats;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.RemoteException;
|
|
||||||
import android.os.ServiceManager;
|
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
import android.test.InstrumentationTestCase;
|
import android.test.InstrumentationTestCase;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A test that dumps data usage to instrumentation out, used for measuring data usage for idle
|
* A test that dumps data usage to instrumentation out, used for measuring data usage for idle
|
||||||
* devices.
|
* devices.
|
||||||
@@ -36,7 +35,7 @@ import android.util.Log;
|
|||||||
public class DataIdleTest extends InstrumentationTestCase {
|
public class DataIdleTest extends InstrumentationTestCase {
|
||||||
|
|
||||||
private TelephonyManager mTelephonyManager;
|
private TelephonyManager mTelephonyManager;
|
||||||
private INetworkStatsService mStatsService;
|
private NetworkStatsManager mStatsManager;
|
||||||
|
|
||||||
private static final String LOG_TAG = "DataIdleTest";
|
private static final String LOG_TAG = "DataIdleTest";
|
||||||
private final static int INSTRUMENTATION_IN_PROGRESS = 2;
|
private final static int INSTRUMENTATION_IN_PROGRESS = 2;
|
||||||
@@ -44,8 +43,7 @@ public class DataIdleTest extends InstrumentationTestCase {
|
|||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
Context c = getInstrumentation().getTargetContext();
|
Context c = getInstrumentation().getTargetContext();
|
||||||
mStatsService = INetworkStatsService.Stub.asInterface(
|
mStatsManager = c.getSystemService(NetworkStatsManager.class);
|
||||||
ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
|
|
||||||
mTelephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
|
mTelephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +51,9 @@ public class DataIdleTest extends InstrumentationTestCase {
|
|||||||
* Test that dumps all the data usage metrics for wifi to instrumentation out.
|
* Test that dumps all the data usage metrics for wifi to instrumentation out.
|
||||||
*/
|
*/
|
||||||
public void testWifiIdle() {
|
public void testWifiIdle() {
|
||||||
NetworkTemplate template = NetworkTemplate.buildTemplateWifiWildcard();
|
final NetworkTemplate template = new NetworkTemplate
|
||||||
|
.Builder(NetworkTemplate.MATCH_WIFI)
|
||||||
|
.build();
|
||||||
fetchStats(template);
|
fetchStats(template);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,8 +61,11 @@ public class DataIdleTest extends InstrumentationTestCase {
|
|||||||
* Test that dumps all the data usage metrics for all mobile to instrumentation out.
|
* Test that dumps all the data usage metrics for all mobile to instrumentation out.
|
||||||
*/
|
*/
|
||||||
public void testMobile() {
|
public void testMobile() {
|
||||||
String subscriberId = mTelephonyManager.getSubscriberId();
|
final String subscriberId = mTelephonyManager.getSubscriberId();
|
||||||
NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
|
NetworkTemplate template = new NetworkTemplate
|
||||||
|
.Builder(NetworkTemplate.MATCH_MOBILE)
|
||||||
|
.setMeteredness(METERED_YES)
|
||||||
|
.setSubscriberIds(Set.of(subscriberId)).build();
|
||||||
fetchStats(template);
|
fetchStats(template);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,49 +75,26 @@ public class DataIdleTest extends InstrumentationTestCase {
|
|||||||
* @param template {@link NetworkTemplate} to match.
|
* @param template {@link NetworkTemplate} to match.
|
||||||
*/
|
*/
|
||||||
private void fetchStats(NetworkTemplate template) {
|
private void fetchStats(NetworkTemplate template) {
|
||||||
INetworkStatsSession session = null;
|
|
||||||
try {
|
try {
|
||||||
mStatsService.forceUpdate();
|
mStatsManager.forceUpdate();
|
||||||
session = mStatsService.openSession();
|
final NetworkStats.Bucket bucket =
|
||||||
final NetworkStats stats = session.getSummaryForAllUid(
|
mStatsManager.querySummaryForDevice(template, Long.MIN_VALUE, Long.MAX_VALUE);
|
||||||
template, Long.MIN_VALUE, Long.MAX_VALUE, false);
|
reportStats(bucket);
|
||||||
reportStats(stats);
|
} catch (RuntimeException e) {
|
||||||
} catch (RemoteException e) {
|
|
||||||
Log.w(LOG_TAG, "Failed to fetch network stats.");
|
Log.w(LOG_TAG, "Failed to fetch network stats.");
|
||||||
} finally {
|
|
||||||
TrafficStats.closeQuietly(session);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print network data usage stats to instrumentation out
|
* Print network data usage stats to instrumentation out
|
||||||
* @param stats {@link NetworkorStats} to print
|
* @param bucket {@link NetworkStats} to print
|
||||||
*/
|
*/
|
||||||
void reportStats(NetworkStats stats) {
|
void reportStats(NetworkStats.Bucket bucket) {
|
||||||
Bundle result = new Bundle();
|
Bundle result = new Bundle();
|
||||||
long rxBytes = 0;
|
result.putLong("Total rx Bytes", bucket.getRxBytes());
|
||||||
long txBytes = 0;
|
result.putLong("Total tx Bytes", bucket.getTxBytes());
|
||||||
long rxPackets = 0;
|
result.putLong("Total rx Packets", bucket.getRxPackets());
|
||||||
long txPackets = 0;
|
result.putLong("Total tx Packets", bucket.getTxPackets());
|
||||||
for (int i = 0; i < stats.size(); ++i) {
|
|
||||||
// Label will be iface_uid_tag_set
|
|
||||||
Entry statsEntry = stats.getValues(i, null);
|
|
||||||
// Debugging use.
|
|
||||||
/*
|
|
||||||
String labelTemplate = String.format("%s_%d_%d_%d", statsEntry.iface, statsEntry.uid,
|
|
||||||
statsEntry.tag, statsEntry.set) + "_%s";
|
|
||||||
result.putLong(String.format(labelTemplate, "rxBytes"), statsEntry.rxBytes);
|
|
||||||
result.putLong(String.format(labelTemplate, "txBytes"), statsEntry.txBytes);
|
|
||||||
*/
|
|
||||||
rxPackets += statsEntry.rxPackets;
|
|
||||||
rxBytes += statsEntry.rxBytes;
|
|
||||||
txPackets += statsEntry.txPackets;
|
|
||||||
txBytes += statsEntry.txBytes;
|
|
||||||
}
|
|
||||||
result.putLong("Total rx Bytes", rxBytes);
|
|
||||||
result.putLong("Total tx Bytes", txBytes);
|
|
||||||
result.putLong("Total rx Packets", rxPackets);
|
|
||||||
result.putLong("Total tx Packets", txPackets);
|
|
||||||
getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, result);
|
getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, result);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user