Support metered Wi-Fi NetworkPolicy.

Add networkId field to NetworkIdentity to identify Wi-Fi networks by
SSID.  Add support for policies without usage cycles.

Only apply mobile policies when SIM state is ready, which is cleaner
than just checking for airplane mode.  Also avoids creating no-op
default policies when subscriberId is null.

Bug: 3001465, 3291052
Change-Id: I1f8aaa49a5db306df022c402ea7f3f5d4bc0cfc7
This commit is contained in:
Jeff Sharkey
2012-04-04 20:40:58 -07:00
parent 62b7c11709
commit 2d9e5a5f9e
4 changed files with 21 additions and 22 deletions

View File

@@ -20,6 +20,7 @@ import static com.android.internal.util.Preconditions.checkNotNull;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.content.Context;
import android.os.Binder;
import android.os.Build.VERSION_CODES;
import android.os.RemoteException;
@@ -610,6 +611,11 @@ public class ConnectivityManager {
mService = checkNotNull(service, "missing IConnectivityManager");
}
/** {@hide} */
public static ConnectivityManager from(Context context) {
return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
/**
* {@hide}
*/

View File

@@ -16,6 +16,7 @@
package com.android.internal.net;
import static android.net.NetworkStats.SET_ALL;
import static android.net.NetworkStats.SET_DEFAULT;
import static android.net.NetworkStats.SET_FOREGROUND;
import static android.net.NetworkStats.TAG_NONE;
@@ -84,12 +85,12 @@ public class NetworkStatsFactoryTest extends AndroidTestCase {
final NetworkStats stats = mFactory.readNetworkStatsSummary();
assertEquals(6, stats.size());
assertStatsEntry(stats, "lo", UID_ALL, SET_DEFAULT, TAG_NONE, 8308L, 8308L);
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
assertStatsEntry(stats, "ifb0", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
assertStatsEntry(stats, "ifb1", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
assertStatsEntry(stats, "sit0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
assertStatsEntry(stats, "lo", UID_ALL, SET_ALL, TAG_NONE, 8308L, 8308L);
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 1507570L, 489339L);
assertStatsEntry(stats, "ifb0", UID_ALL, SET_ALL, TAG_NONE, 52454L, 0L);
assertStatsEntry(stats, "ifb1", UID_ALL, SET_ALL, TAG_NONE, 52454L, 0L);
assertStatsEntry(stats, "sit0", UID_ALL, SET_ALL, TAG_NONE, 0L, 0L);
assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_ALL, TAG_NONE, 0L, 0L);
}
public void testNetworkStatsSummaryDown() throws Exception {
@@ -102,8 +103,8 @@ public class NetworkStatsFactoryTest extends AndroidTestCase {
final NetworkStats stats = mFactory.readNetworkStatsSummary();
assertEquals(7, stats.size());
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
assertStatsEntry(stats, "wlan0", UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 2048L);
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 1507570L, 489339L);
assertStatsEntry(stats, "wlan0", UID_ALL, SET_ALL, TAG_NONE, 1024L, 2048L);
}
public void testNetworkStatsCombined() throws Exception {
@@ -115,7 +116,7 @@ public class NetworkStatsFactoryTest extends AndroidTestCase {
stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets"));
final NetworkStats stats = mFactory.readNetworkStatsSummary();
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L + 10L,
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 1507570L + 10L,
2205L + 20L, 489339L + 30L, 2237L + 40L);
}
@@ -128,7 +129,7 @@ public class NetworkStatsFactoryTest extends AndroidTestCase {
stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets"));
final NetworkStats stats = mFactory.readNetworkStatsSummary();
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 10L, 20L, 30L, 40L);
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 10L, 20L, 30L, 40L);
}
public void testKernelTags() throws Exception {
@@ -153,9 +154,9 @@ public class NetworkStatsFactoryTest extends AndroidTestCase {
final NetworkStats stats = mFactory.readNetworkStatsSummary();
assertEquals(6, stats.size());
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 2112L, 24L, 700L, 10L);
assertStatsEntry(stats, "test1", UID_ALL, SET_DEFAULT, TAG_NONE, 6L, 8L, 10L, 12L);
assertStatsEntry(stats, "test2", UID_ALL, SET_DEFAULT, TAG_NONE, 1L, 2L, 3L, 4L);
assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 2112L, 24L, 700L, 10L);
assertStatsEntry(stats, "test1", UID_ALL, SET_ALL, TAG_NONE, 6L, 8L, 10L, 12L);
assertStatsEntry(stats, "test2", UID_ALL, SET_ALL, TAG_NONE, 1L, 2L, 3L, 4L);
}
/**

View File

@@ -84,7 +84,7 @@ import libcore.io.IoUtils;
*/
@LargeTest
public class NetworkStatsServiceTest extends AndroidTestCase {
private static final String TAG = "NetworkStatsServiceTest";
private static final String TAG = "NetworkStatsServiceTest";
private static final String TEST_IFACE = "test0";
private static final String TEST_IFACE2 = "test1";

View File

@@ -20,8 +20,6 @@ import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkIdentity;
import android.net.NetworkStats;
import android.net.NetworkTemplate;
import android.test.AndroidTestCase;
@@ -145,12 +143,6 @@ public class NetworkStatsCollectionTest extends AndroidTestCase {
}
}
public static NetworkIdentitySet buildWifiIdent() {
final NetworkIdentitySet set = new NetworkIdentitySet();
set.add(new NetworkIdentity(ConnectivityManager.TYPE_WIFI, 0, null, false));
return set;
}
private static void assertSummaryTotal(NetworkStatsCollection collection,
NetworkTemplate template, long rxBytes, long rxPackets, long txBytes, long txPackets) {
final NetworkStats.Entry entry = collection.getSummary(