Search in sources :

Example 31 with NetworkPolicy

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

the class NetworkPolicyManagerShellCommand method listWifiNetworks.

private int listWifiNetworks() throws RemoteException {
    final PrintWriter pw = getOutPrintWriter();
    final String arg = getNextArg();
    final Boolean filter = arg == null ? null : Boolean.valueOf(arg);
    for (NetworkPolicy policy : getWifiPolicies()) {
        if (filter != null && filter.booleanValue() != policy.metered) {
            continue;
        }
        pw.print(getNetworkId(policy));
        pw.print(';');
        pw.println(policy.metered);
    }
    return 0;
}
Also used : NetworkPolicy(android.net.NetworkPolicy) PrintWriter(java.io.PrintWriter)

Example 32 with NetworkPolicy

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

the class NetworkPolicyManagerService method getNetworkQuotaInfoUnchecked.

private NetworkQuotaInfo getNetworkQuotaInfoUnchecked(NetworkState state) {
    final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state);
    final NetworkPolicy policy;
    synchronized (mNetworkPoliciesSecondLock) {
        policy = findPolicyForNetworkNL(ident);
    }
    if (policy == null || !policy.hasCycle()) {
        // missing policy means we can't derive useful quota info
        return null;
    }
    final long currentTime = currentTimeMillis();
    // find total bytes used under policy
    final long start = computeLastCycleBoundary(currentTime, policy);
    final long end = currentTime;
    final long totalBytes = getTotalBytes(policy.template, start, end);
    // report soft and hard limits under policy
    final long softLimitBytes = policy.warningBytes != WARNING_DISABLED ? policy.warningBytes : NetworkQuotaInfo.NO_LIMIT;
    final long hardLimitBytes = policy.limitBytes != LIMIT_DISABLED ? policy.limitBytes : NetworkQuotaInfo.NO_LIMIT;
    return new NetworkQuotaInfo(totalBytes, softLimitBytes, hardLimitBytes);
}
Also used : NetworkQuotaInfo(android.net.NetworkQuotaInfo) NetworkIdentity(android.net.NetworkIdentity) NetworkPolicy(android.net.NetworkPolicy)

Example 33 with NetworkPolicy

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

the class NetworkPolicyManagerService method performSnooze.

void performSnooze(NetworkTemplate template, int type) {
    maybeRefreshTrustedTime();
    final long currentTime = currentTimeMillis();
    synchronized (mUidRulesFirstLock) {
        synchronized (mNetworkPoliciesSecondLock) {
            // find and snooze local policy that matches
            final NetworkPolicy policy = mNetworkPolicy.get(template);
            if (policy == null) {
                throw new IllegalArgumentException("unable to find policy for " + template);
            }
            switch(type) {
                case TYPE_WARNING:
                    policy.lastWarningSnooze = currentTime;
                    break;
                case TYPE_LIMIT:
                    policy.lastLimitSnooze = currentTime;
                    break;
                default:
                    throw new IllegalArgumentException("unexpected type");
            }
            normalizePoliciesNL();
            updateNetworkEnabledNL();
            updateNetworkRulesNL();
            updateNotificationsNL();
            writePolicyAL();
        }
    }
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 34 with NetworkPolicy

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

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 35 with NetworkPolicy

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

the class NetworkPolicyManagerShellCommand method setMeteredWifiNetwork.

private int setMeteredWifiNetwork() throws RemoteException {
    final PrintWriter pw = getOutPrintWriter();
    final String id = getNextArg();
    if (id == null) {
        pw.println("Error: didn't specify ID");
        return -1;
    }
    final String arg = getNextArg();
    if (arg == null) {
        pw.println("Error: didn't specify BOOLEAN");
        return -1;
    }
    final boolean metered = Boolean.valueOf(arg);
    final NetworkPolicy[] policies = mInterface.getNetworkPolicies(null);
    boolean changed = false;
    // First try to find a policy with such id
    for (NetworkPolicy policy : policies) {
        if (policy.template.isMatchRuleMobile() || policy.metered == metered) {
            continue;
        }
        final String networkId = getNetworkId(policy);
        if (id.equals(networkId)) {
            Log.i(TAG, "Changing " + networkId + " metered status to " + metered);
            policy.metered = metered;
            changed = true;
        }
    }
    if (changed) {
        mInterface.setNetworkPolicies(policies);
        return 0;
    }
    // Policy not found: check if there is a saved wi-fi with such id.
    for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) {
        final String ssid = removeDoubleQuotes(config.SSID);
        if (id.equals(ssid)) {
            final NetworkPolicy policy = newPolicy(ssid);
            policy.metered = true;
            Log.i(TAG, "Creating new policy for " + ssid + ": " + policy);
            final NetworkPolicy[] newPolicies = new NetworkPolicy[policies.length + 1];
            System.arraycopy(policies, 0, newPolicies, 0, policies.length);
            newPolicies[newPolicies.length - 1] = policy;
            mInterface.setNetworkPolicies(newPolicies);
        }
    }
    return 0;
}
Also used : WifiConfiguration(android.net.wifi.WifiConfiguration) NetworkPolicy(android.net.NetworkPolicy) PrintWriter(java.io.PrintWriter)

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