Search in sources :

Example 1 with NetworkPolicy

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

the class NetworkPolicyManagerService method updateNetworkEnabledLocked.

/**
     * Proactively control network data connections when they exceed
     * {@link NetworkPolicy#limitBytes}.
     */
private void updateNetworkEnabledLocked() {
    if (LOGV)
        Slog.v(TAG, "updateNetworkEnabledLocked()");
    // TODO: reset any policy-disabled networks when any policy is removed
    // completely, which is currently rare case.
    final long currentTime = currentTimeMillis();
    for (NetworkPolicy policy : mNetworkPolicy.values()) {
        // 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 2 with NetworkPolicy

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

the class NetworkPolicyManagerService method updateNotificationsLocked.

/**
     * Check {@link NetworkPolicy} against current {@link INetworkStatsService}
     * to show visible notifications as needed.
     */
private void updateNotificationsLocked() {
    if (LOGV)
        Slog.v(TAG, "updateNotificationsLocked()");
    // keep track of previously active notifications
    final HashSet<String> beforeNotifs = Sets.newHashSet();
    beforeNotifs.addAll(mActiveNotifs);
    mActiveNotifs.clear();
    // TODO: when switching to kernel notifications, compute next future
    // cycle boundary to recompute notifications.
    // examine stats for each active policy
    final long currentTime = currentTimeMillis();
    for (NetworkPolicy policy : mNetworkPolicy.values()) {
        // ignore policies that aren't relevant to user
        if (!isTemplateRelevant(policy.template))
            continue;
        if (!policy.hasCycle())
            continue;
        final long start = computeLastCycleBoundary(currentTime, policy);
        final long end = currentTime;
        final long totalBytes = getTotalBytes(policy.template, start, end);
        if (policy.isOverLimit(totalBytes)) {
            if (policy.lastLimitSnooze >= start) {
                enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes);
            } else {
                enqueueNotification(policy, TYPE_LIMIT, totalBytes);
                notifyOverLimitLocked(policy.template);
            }
        } else {
            notifyUnderLimitLocked(policy.template);
            if (policy.isOverWarning(totalBytes) && policy.lastWarningSnooze < start) {
                enqueueNotification(policy, TYPE_WARNING, totalBytes);
            }
        }
    }
    // ongoing notification when restricting background data
    if (mRestrictBackground) {
        enqueueRestrictedNotification(TAG_ALLOW_BACKGROUND);
    }
    // cancel stale notifications that we didn't renew above
    for (String tag : beforeNotifs) {
        if (!mActiveNotifs.contains(tag)) {
            cancelNotification(tag);
        }
    }
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 3 with NetworkPolicy

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

the class NetworkPolicyManagerService method isNetworkMetered.

@Override
public boolean isNetworkMetered(NetworkState state) {
    final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state);
    // roaming networks are always considered metered
    if (ident.getRoaming()) {
        return true;
    }
    final NetworkPolicy policy;
    synchronized (mRulesLock) {
        policy = findPolicyForNetworkLocked(ident);
    }
    if (policy != null) {
        return policy.metered;
    } else {
        final int type = state.networkInfo.getType();
        if (isNetworkTypeMobile(type) || type == TYPE_WIMAX) {
            return true;
        }
        return false;
    }
}
Also used : NetworkIdentity(android.net.NetworkIdentity) NetworkPolicy(android.net.NetworkPolicy)

Example 4 with NetworkPolicy

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

the class NetworkPolicyManagerService method setNetworkPolicies.

@Override
public void setNetworkPolicies(NetworkPolicy[] policies) {
    mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
    maybeRefreshTrustedTime();
    synchronized (mRulesLock) {
        mNetworkPolicy.clear();
        for (NetworkPolicy policy : policies) {
            mNetworkPolicy.put(policy.template, policy);
        }
        updateNetworkEnabledLocked();
        updateNetworkRulesLocked();
        updateNotificationsLocked();
        writePolicyLocked();
    }
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 5 with NetworkPolicy

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

the class NetworkPolicyManagerServiceTest method testLastCycleSane.

public void testLastCycleSane() throws Exception {
    final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
    final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
    // walk backwards, ensuring that cycle boundaries look sane
    long currentCycle = computeLastCycleBoundary(parseTime("2011-08-04T00:00:00.000Z"), policy);
    for (int i = 0; i < 128; i++) {
        long lastCycle = computeLastCycleBoundary(currentCycle, policy);
        assertEqualsFuzzy(DAY_IN_MILLIS * 30, currentCycle - lastCycle, DAY_IN_MILLIS * 3);
        assertUnique(seen, lastCycle);
        currentCycle = lastCycle;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NetworkPolicy(android.net.NetworkPolicy) EasyMock.anyLong(org.easymock.EasyMock.anyLong)

Aggregations

NetworkPolicy (android.net.NetworkPolicy)275 Test (org.junit.Test)57 NetworkTemplate (android.net.NetworkTemplate)40 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 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 View (android.view.View)8 AdapterView (android.widget.AdapterView)8