Search in sources :

Example 26 with NetworkPolicy

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

the class SettingsBackupAgent method getNetworkPolicies.

private byte[] getNetworkPolicies() {
    NetworkPolicyManager networkPolicyManager = (NetworkPolicyManager) getSystemService(NETWORK_POLICY_SERVICE);
    NetworkPolicy[] policies = networkPolicyManager.getNetworkPolicies();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (policies != null && policies.length != 0) {
        DataOutputStream out = new DataOutputStream(baos);
        try {
            out.writeInt(NETWORK_POLICIES_BACKUP_VERSION);
            out.writeInt(policies.length);
            for (NetworkPolicy policy : policies) {
                if (policy != null) {
                    byte[] marshaledPolicy = policy.getBytesForBackup();
                    out.writeByte(BackupUtils.NOT_NULL);
                    out.writeInt(marshaledPolicy.length);
                    out.write(marshaledPolicy);
                } else {
                    out.writeByte(BackupUtils.NULL);
                }
            }
        } catch (IOException ioe) {
            Log.e(TAG, "Failed to convert NetworkPolicies to byte array " + ioe.getMessage());
            baos.reset();
        }
    }
    return baos.toByteArray();
}
Also used : NetworkPolicy(android.net.NetworkPolicy) DataOutputStream(java.io.DataOutputStream) NetworkPolicyManager(android.net.NetworkPolicyManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 27 with NetworkPolicy

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

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

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

the class NetworkPolicyManagerService method updateNotificationsNL.

/**
     * Check {@link NetworkPolicy} against current {@link INetworkStatsService}
     * to show visible notifications as needed.
     */
void updateNotificationsNL() {
    if (LOGV)
        Slog.v(TAG, "updateNotificationsNL()");
    // keep track of previously active notifications
    final ArraySet<String> beforeNotifs = new ArraySet<String>(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 (int i = mNetworkPolicy.size() - 1; i >= 0; i--) {
        final NetworkPolicy policy = mNetworkPolicy.valueAt(i);
        // 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);
                notifyOverLimitNL(policy.template);
            }
        } else {
            notifyUnderLimitNL(policy.template);
            if (policy.isOverWarning(totalBytes) && policy.lastWarningSnooze < start) {
                enqueueNotification(policy, TYPE_WARNING, totalBytes);
            }
        }
    }
    // cancel stale notifications that we didn't renew above
    for (int i = beforeNotifs.size() - 1; i >= 0; i--) {
        final String tag = beforeNotifs.valueAt(i);
        if (!mActiveNotifs.contains(tag)) {
            cancelNotification(tag);
        }
    }
}
Also used : ArraySet(android.util.ArraySet) NetworkPolicy(android.net.NetworkPolicy) NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString)

Example 29 with NetworkPolicy

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

the class NetworkPolicyManagerService method ensureActiveMobilePolicyNL.

private void ensureActiveMobilePolicyNL(String subscriberId) {
    // Poke around to see if we already have a policy
    final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true);
    for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) {
        final NetworkTemplate template = mNetworkPolicy.keyAt(i);
        if (template.matches(probeIdent)) {
            if (LOGD) {
                Slog.d(TAG, "Found template " + template + " which matches subscriber " + NetworkIdentity.scrubSubscriberId(subscriberId));
            }
            return;
        }
    }
    Slog.i(TAG, "No policy for subscriber " + NetworkIdentity.scrubSubscriberId(subscriberId) + "; generating default policy");
    // Build default mobile policy, and assume usage cycle starts today
    final int dataWarningConfig = mContext.getResources().getInteger(com.android.internal.R.integer.config_networkPolicyDefaultWarning);
    final long warningBytes;
    if (dataWarningConfig == WARNING_DISABLED) {
        warningBytes = WARNING_DISABLED;
    } else {
        warningBytes = dataWarningConfig * MB_IN_BYTES;
    }
    final Time time = new Time();
    time.setToNow();
    final int cycleDay = time.monthDay;
    final String cycleTimezone = time.timezone;
    final NetworkTemplate template = buildTemplateMobileAll(subscriberId);
    final NetworkPolicy policy = new NetworkPolicy(template, cycleDay, cycleTimezone, warningBytes, LIMIT_DISABLED, SNOOZE_NEVER, SNOOZE_NEVER, true, true);
    addNetworkPolicyNL(policy);
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkIdentity(android.net.NetworkIdentity) NetworkPolicy(android.net.NetworkPolicy) Time(android.text.format.Time) TrustedTime(android.util.TrustedTime) NtpTrustedTime(android.util.NtpTrustedTime) NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString)

Example 30 with NetworkPolicy

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

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)

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