Search in sources :

Example 91 with NetworkPolicy

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

the class NetworkPolicyEditor method getOrCreatePolicy.

public NetworkPolicy getOrCreatePolicy(NetworkTemplate template) {
    NetworkPolicy policy = getPolicy(template);
    if (policy == null) {
        policy = buildDefaultPolicy(template);
        mPolicies.add(policy);
    }
    return policy;
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 92 with NetworkPolicy

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

the class NetworkPolicyEditor method setPolicyLimitBytes.

public void setPolicyLimitBytes(NetworkTemplate template, long limitBytes) {
    final NetworkPolicy policy = getOrCreatePolicy(template);
    policy.limitBytes = limitBytes;
    policy.inferred = false;
    policy.clearSnooze();
    writeAsync();
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 93 with NetworkPolicy

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

the class NetworkPolicyEditor method setPolicyMetered.

public void setPolicyMetered(NetworkTemplate template, boolean metered) {
    boolean modified = false;
    NetworkPolicy policy = getPolicy(template);
    if (metered) {
        if (policy == null) {
            policy = buildDefaultPolicy(template);
            policy.metered = true;
            policy.inferred = false;
            mPolicies.add(policy);
            modified = true;
        } else if (!policy.metered) {
            policy.metered = true;
            policy.inferred = false;
            modified = true;
        }
    } else {
        if (policy == null) {
        // ignore when policy doesn't exist
        } else if (policy.metered) {
            policy.metered = false;
            policy.inferred = false;
            modified = true;
        }
    }
    // Remove legacy unquoted policies while we're here
    final NetworkTemplate unquoted = buildUnquotedNetworkTemplate(template);
    final NetworkPolicy unquotedPolicy = getPolicy(unquoted);
    if (unquotedPolicy != null) {
        mPolicies.remove(unquotedPolicy);
        modified = true;
    }
    if (modified)
        writeAsync();
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkPolicy(android.net.NetworkPolicy)

Example 94 with NetworkPolicy

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

the class NetworkPolicyEditor method setPolicyCycleDay.

public void setPolicyCycleDay(NetworkTemplate template, int cycleDay, String cycleTimezone) {
    final NetworkPolicy policy = getOrCreatePolicy(template);
    policy.cycleDay = cycleDay;
    policy.cycleTimezone = cycleTimezone;
    policy.inferred = false;
    policy.clearSnooze();
    writeAsync();
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 95 with NetworkPolicy

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

the class DataUsageController method getDataUsageInfo.

public DataUsageInfo getDataUsageInfo(NetworkTemplate template) {
    final INetworkStatsSession session = getSession();
    if (session == null) {
        return warn("no stats session");
    }
    final NetworkPolicy policy = findNetworkPolicy(template);
    try {
        final NetworkStatsHistory history = session.getHistoryForNetwork(template, FIELDS);
        final long now = System.currentTimeMillis();
        final long start, end;
        if (policy != null && policy.cycleDay > 0) {
            // period = determined from cycleDay
            if (DEBUG)
                Log.d(TAG, "Cycle day=" + policy.cycleDay + " tz=" + policy.cycleTimezone);
            final Time nowTime = new Time(policy.cycleTimezone);
            nowTime.setToNow();
            final Time policyTime = new Time(nowTime);
            policyTime.set(policy.cycleDay, policyTime.month, policyTime.year);
            policyTime.normalize(false);
            if (nowTime.after(policyTime)) {
                start = policyTime.toMillis(false);
                end = addMonth(policyTime, 1).toMillis(false);
            } else {
                start = addMonth(policyTime, -1).toMillis(false);
                end = policyTime.toMillis(false);
            }
        } else {
            // period = last 4 wks
            end = now;
            start = now - DateUtils.WEEK_IN_MILLIS * 4;
        }
        final long callStart = System.currentTimeMillis();
        final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null);
        final long callEnd = System.currentTimeMillis();
        if (DEBUG)
            Log.d(TAG, String.format("history call from %s to %s now=%s took %sms: %s", new Date(start), new Date(end), new Date(now), callEnd - callStart, historyEntryToString(entry)));
        if (entry == null) {
            return warn("no entry data");
        }
        final long totalBytes = entry.rxBytes + entry.txBytes;
        final DataUsageInfo usage = new DataUsageInfo();
        usage.startDate = start;
        usage.usageLevel = totalBytes;
        usage.period = formatDateRange(start, end);
        if (policy != null) {
            usage.limitLevel = policy.limitBytes > 0 ? policy.limitBytes : 0;
            usage.warningLevel = policy.warningBytes > 0 ? policy.warningBytes : 0;
        } else {
            usage.warningLevel = getDefaultWarningLevel();
        }
        if (usage != null && mNetworkController != null) {
            usage.carrier = mNetworkController.getMobileDataNetworkName();
        }
        return usage;
    } catch (RemoteException e) {
        return warn("remote call failed");
    }
}
Also used : INetworkStatsSession(android.net.INetworkStatsSession) NetworkPolicy(android.net.NetworkPolicy) Time(android.text.format.Time) NetworkStatsHistory(android.net.NetworkStatsHistory) RemoteException(android.os.RemoteException) Date(java.util.Date)

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