Get ethernet data usage from NetworkStatsManager.
- change to use NetworkStatsManager.querySummaryForUser() to check for ethernet usage data instead of getting it from INetworkStatsSession. Bug: 111751694 Test: make RunSettingsRoboTests Change-Id: I60364b4a5f7879906beba194c50955154d56803a
This commit is contained in:
@@ -18,6 +18,8 @@ import static android.net.ConnectivityManager.TYPE_MOBILE;
|
|||||||
import static android.net.ConnectivityManager.TYPE_WIFI;
|
import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||||
import static android.telephony.TelephonyManager.SIM_STATE_READY;
|
import static android.telephony.TelephonyManager.SIM_STATE_READY;
|
||||||
|
|
||||||
|
import android.app.usage.NetworkStats.Bucket;
|
||||||
|
import android.app.usage.NetworkStatsManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.INetworkStatsService;
|
import android.net.INetworkStatsService;
|
||||||
@@ -33,8 +35,11 @@ import android.telephony.TelephonyManager;
|
|||||||
import android.text.BidiFormatter;
|
import android.text.BidiFormatter;
|
||||||
import android.text.format.Formatter;
|
import android.text.format.Formatter;
|
||||||
import android.text.format.Formatter.BytesResult;
|
import android.text.format.Formatter.BytesResult;
|
||||||
|
import android.util.FeatureFlagUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.android.settings.core.FeatureFlags;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,28 +74,48 @@ public final class DataUsageUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final ConnectivityManager conn = ConnectivityManager.from(context);
|
final ConnectivityManager conn = ConnectivityManager.from(context);
|
||||||
final boolean hasEthernet = conn.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET);
|
if (!conn.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET)) {
|
||||||
|
return false;
|
||||||
final long ethernetBytes;
|
|
||||||
try {
|
|
||||||
INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
|
|
||||||
ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
|
|
||||||
|
|
||||||
INetworkStatsSession statsSession = statsService.openSession();
|
|
||||||
if (statsSession != null) {
|
|
||||||
ethernetBytes = statsSession.getSummaryForNetwork(
|
|
||||||
NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
|
|
||||||
.getTotalBytes();
|
|
||||||
TrafficStats.closeQuietly(statsSession);
|
|
||||||
} else {
|
|
||||||
ethernetBytes = 0;
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// only show ethernet when both hardware present and traffic has occurred
|
if (FeatureFlagUtils.isEnabled(context, FeatureFlags.DATA_USAGE_V2)) {
|
||||||
return hasEthernet && ethernetBytes > 0;
|
final TelephonyManager telephonyManager = TelephonyManager.from(context);;
|
||||||
|
final NetworkStatsManager networkStatsManager =
|
||||||
|
context.getSystemService(NetworkStatsManager.class);
|
||||||
|
boolean hasEthernetUsage = false;
|
||||||
|
try {
|
||||||
|
final Bucket bucket = networkStatsManager.querySummaryForUser(
|
||||||
|
ConnectivityManager.TYPE_ETHERNET, telephonyManager.getSubscriberId(),
|
||||||
|
0L /* startTime */, System.currentTimeMillis() /* endTime */);
|
||||||
|
if (bucket != null) {
|
||||||
|
hasEthernetUsage = bucket.getRxBytes() > 0 || bucket.getTxBytes() > 0;
|
||||||
|
}
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
Log.e(TAG, "Exception querying network detail.", e);
|
||||||
|
}
|
||||||
|
return hasEthernetUsage;
|
||||||
|
} else {
|
||||||
|
final long ethernetBytes;
|
||||||
|
try {
|
||||||
|
INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
|
||||||
|
ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
|
||||||
|
|
||||||
|
INetworkStatsSession statsSession = statsService.openSession();
|
||||||
|
if (statsSession != null) {
|
||||||
|
ethernetBytes = statsSession.getSummaryForNetwork(
|
||||||
|
NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
|
||||||
|
.getTotalBytes();
|
||||||
|
TrafficStats.closeQuietly(statsSession);
|
||||||
|
} else {
|
||||||
|
ethernetBytes = 0;
|
||||||
|
}
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// only show ethernet when both hardware present and traffic has occurred
|
||||||
|
return ethernetBytes > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -19,13 +19,19 @@ package com.android.settings.datausage;
|
|||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.Matchers.anyInt;
|
import static org.mockito.Matchers.anyInt;
|
||||||
|
import static org.mockito.Matchers.anyLong;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.usage.NetworkStatsManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
import android.util.DataUnit;
|
import android.util.DataUnit;
|
||||||
|
import android.util.FeatureFlagUtils;
|
||||||
|
|
||||||
|
import com.android.settings.core.FeatureFlags;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -42,6 +48,9 @@ public final class DataUsageUtilsTest {
|
|||||||
private ConnectivityManager mManager;
|
private ConnectivityManager mManager;
|
||||||
@Mock
|
@Mock
|
||||||
private TelephonyManager mTelephonyManager;
|
private TelephonyManager mTelephonyManager;
|
||||||
|
@Mock
|
||||||
|
private NetworkStatsManager mNetworkStatsManager;
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@@ -51,6 +60,7 @@ public final class DataUsageUtilsTest {
|
|||||||
mContext = shadowContext.getApplicationContext();
|
mContext = shadowContext.getApplicationContext();
|
||||||
shadowContext.setSystemService(Context.CONNECTIVITY_SERVICE, mManager);
|
shadowContext.setSystemService(Context.CONNECTIVITY_SERVICE, mManager);
|
||||||
shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
|
shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
|
||||||
|
shadowContext.setSystemService(Context.NETWORK_STATS_SERVICE, mNetworkStatsManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -88,4 +98,17 @@ public final class DataUsageUtilsTest {
|
|||||||
|
|
||||||
assertThat(formattedDataUsage).isEqualTo("1.00 GB");
|
assertThat(formattedDataUsage).isEqualTo("1.00 GB");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hasEthernet_shouldQueryEthernetSummaryForUser() throws Exception {
|
||||||
|
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.DATA_USAGE_V2, true);
|
||||||
|
when(mManager.isNetworkSupported(anyInt())).thenReturn(true);
|
||||||
|
final String subscriber = "TestSub";
|
||||||
|
when(mTelephonyManager.getSubscriberId()).thenReturn(subscriber);
|
||||||
|
|
||||||
|
DataUsageUtils.hasEthernet(mContext);
|
||||||
|
|
||||||
|
verify(mNetworkStatsManager).querySummaryForUser(eq(ConnectivityManager.TYPE_ETHERNET),
|
||||||
|
eq(subscriber), anyLong() /* startTime */, anyLong() /* endTime */);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user