Search in sources :

Example 71 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by DirtyUnicorns.

the class NetworkPolicyManagerShellCommand method newPolicy.

private NetworkPolicy newPolicy(String ssid) {
    final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(ssid);
    final NetworkPolicy policy = newWifiPolicy(template, false);
    return policy;
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkPolicy(android.net.NetworkPolicy)

Example 72 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by DirtyUnicorns.

the class NetworkPolicyManagerShellCommand method getWifiPolicies.

private List<NetworkPolicy> getWifiPolicies() throws RemoteException {
    // First gets a list of saved wi-fi networks.
    final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
    final int size = configs != null ? configs.size() : 0;
    final Set<String> ssids = new HashSet<>(size);
    if (configs != null) {
        for (WifiConfiguration config : configs) {
            ssids.add(removeDoubleQuotes(config.SSID));
        }
    }
    // Then gets the saved policies.
    final NetworkPolicy[] policies = mInterface.getNetworkPolicies(null);
    final List<NetworkPolicy> wifiPolicies = new ArrayList<NetworkPolicy>(policies.length);
    for (NetworkPolicy policy : policies) {
        if (!policy.template.isMatchRuleMobile()) {
            wifiPolicies.add(policy);
            final String netId = getNetworkId(policy);
            ssids.remove(netId);
        }
    }
    // Finally, creates new default policies for saved WI-FIs not policied yet.
    for (String ssid : ssids) {
        final NetworkPolicy policy = newPolicy(ssid);
        wifiPolicies.add(policy);
    }
    return wifiPolicies;
}
Also used : WifiConfiguration(android.net.wifi.WifiConfiguration) NetworkPolicy(android.net.NetworkPolicy) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 73 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by DirtyUnicorns.

the class NetworkPolicyManagerService method updateNetworkEnabledNL.

/**
     * Proactively control network data connections when they exceed
     * {@link NetworkPolicy#limitBytes}.
     */
void updateNetworkEnabledNL() {
    if (LOGV)
        Slog.v(TAG, "updateNetworkEnabledNL()");
    // TODO: reset any policy-disabled networks when any policy is removed
    // completely, which is currently rare case.
    final long currentTime = currentTimeMillis();
    for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) {
        final NetworkPolicy policy = mNetworkPolicy.valueAt(i);
        // shortcut when policy has no limit
        if (policy.limitBytes == LIMIT_DISABLED || !policy.hasCycle()) {
            setNetworkTemplateEnabled(policy.template, true);
            continue;
        }
        final long start = computeLastCycleBoundary(currentTime, policy);
        final long end = currentTime;
        final long totalBytes = getTotalBytes(policy.template, start, end);
        // disable data connection when over limit and not snoozed
        final boolean overLimitWithoutSnooze = policy.isOverLimit(totalBytes) && policy.lastLimitSnooze < start;
        final boolean networkEnabled = !overLimitWithoutSnooze;
        setNetworkTemplateEnabled(policy.template, networkEnabled);
    }
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 74 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by DirtyUnicorns.

the class NetworkPolicyManagerServiceTest method testCycleTodayJanuary.

public void testCycleTodayJanuary() throws Exception {
    final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 14, "US/Pacific", 1024L, 1024L, false);
    assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"), computeNextCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy));
    assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"), computeNextCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy));
    assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"), computeNextCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
    assertTimeEquals(parseTime("2012-12-14T00:00:00.000-08:00"), computeLastCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy));
    assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"), computeLastCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy));
    assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"), computeLastCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 75 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by DirtyUnicorns.

the class NetworkPolicyManagerServiceTest method testOverWarningLimitNotification.

@Suppress
public void testOverWarningLimitNotification() throws Exception {
    NetworkState[] state = null;
    NetworkStats stats = null;
    Future<Void> future;
    Future<String> tagFuture;
    final long TIME_FEB_15 = 1171497600000L;
    final long TIME_MAR_10 = 1173484800000L;
    final int CYCLE_DAY = 15;
    setCurrentTimeMillis(TIME_MAR_10);
    // assign wifi policy
    state = new NetworkState[] {};
    stats = new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
    {
        expectCurrentTime();
        expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
        expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())).andReturn(stats.getTotalBytes()).atLeastOnce();
        expectPolicyDataEnable(TYPE_WIFI, true);
        expectClearNotifications();
        expectAdvisePersistThreshold();
        future = expectMeteredIfacesChanged();
        replay();
        setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
        future.get();
        verifyAndReset();
    }
    // bring up wifi network
    incrementCurrentTime(MINUTE_IN_MILLIS);
    state = new NetworkState[] { buildWifi() };
    stats = new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
    {
        expectCurrentTime();
        expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
        expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())).andReturn(stats.getTotalBytes()).atLeastOnce();
        expectPolicyDataEnable(TYPE_WIFI, true);
        expectRemoveInterfaceQuota(TEST_IFACE);
        expectSetInterfaceQuota(TEST_IFACE, 2 * MB_IN_BYTES);
        expectClearNotifications();
        expectAdvisePersistThreshold();
        future = expectMeteredIfacesChanged(TEST_IFACE);
        replay();
        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
        future.get();
        verifyAndReset();
    }
    // go over warning, which should kick notification
    incrementCurrentTime(MINUTE_IN_MILLIS);
    stats = new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 1536 * KB_IN_BYTES, 15L, 0L, 0L);
    {
        expectCurrentTime();
        expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())).andReturn(stats.getTotalBytes()).atLeastOnce();
        expectPolicyDataEnable(TYPE_WIFI, true);
        expectForceUpdate();
        expectClearNotifications();
        tagFuture = expectEnqueueNotification();
        replay();
        mNetworkObserver.limitReached(null, TEST_IFACE);
        assertNotificationType(TYPE_WARNING, tagFuture.get());
        verifyAndReset();
    }
    // go over limit, which should kick notification and dialog
    incrementCurrentTime(MINUTE_IN_MILLIS);
    stats = new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 5 * MB_IN_BYTES, 512L, 0L, 0L);
    {
        expectCurrentTime();
        expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())).andReturn(stats.getTotalBytes()).atLeastOnce();
        expectPolicyDataEnable(TYPE_WIFI, false);
        expectForceUpdate();
        expectClearNotifications();
        tagFuture = expectEnqueueNotification();
        replay();
        mNetworkObserver.limitReached(null, TEST_IFACE);
        assertNotificationType(TYPE_LIMIT, tagFuture.get());
        verifyAndReset();
    }
    // now snooze policy, which should remove quota
    incrementCurrentTime(MINUTE_IN_MILLIS);
    {
        expectCurrentTime();
        expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
        expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())).andReturn(stats.getTotalBytes()).atLeastOnce();
        expectPolicyDataEnable(TYPE_WIFI, true);
        // snoozed interface still has high quota so background data is
        // still restricted.
        expectRemoveInterfaceQuota(TEST_IFACE);
        expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
        expectAdvisePersistThreshold();
        expectMeteredIfacesChanged(TEST_IFACE);
        future = expectClearNotifications();
        tagFuture = expectEnqueueNotification();
        replay();
        mService.snoozeLimit(sTemplateWifi);
        assertNotificationType(TYPE_LIMIT_SNOOZED, tagFuture.get());
        future.get();
        verifyAndReset();
    }
}
Also used : NetworkPolicy(android.net.NetworkPolicy) NetworkStats(android.net.NetworkStats) Intent(android.content.Intent) NetworkState(android.net.NetworkState) Suppress(android.test.suitebuilder.annotation.Suppress)

Aggregations

NetworkPolicy (android.net.NetworkPolicy)213 NetworkTemplate (android.net.NetworkTemplate)33 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)30 NetworkIdentity (android.net.NetworkIdentity)24 IOException (java.io.IOException)22 NetworkState (android.net.NetworkState)21 Time (android.text.format.Time)16 NetworkStats (android.net.NetworkStats)15 Test (org.junit.Test)15 RemoteException (android.os.RemoteException)11 Intent (android.content.Intent)10 NetworkPolicyManager (android.net.NetworkPolicyManager)10 WifiConfiguration (android.net.wifi.WifiConfiguration)10 ArraySet (android.util.ArraySet)10 PrintWriter (java.io.PrintWriter)10 ArrayList (java.util.ArrayList)10 LinkedHashSet (java.util.LinkedHashSet)10 Suppress (android.test.suitebuilder.annotation.Suppress)9 EasyMock.anyLong (org.easymock.EasyMock.anyLong)8 NetworkQuotaInfo (android.net.NetworkQuotaInfo)6