Search in sources :

Example 41 with NetworkPolicy

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

the class NetworkPolicyManagerService method readPolicyAL.

private void readPolicyAL() {
    if (LOGV)
        Slog.v(TAG, "readPolicyAL()");
    // clear any existing policy and read from disk
    mNetworkPolicy.clear();
    mUidPolicy.clear();
    FileInputStream fis = null;
    try {
        fis = mPolicyFile.openRead();
        final XmlPullParser in = Xml.newPullParser();
        in.setInput(fis, StandardCharsets.UTF_8.name());
        int type;
        int version = VERSION_INIT;
        boolean insideWhitelist = false;
        while ((type = in.next()) != END_DOCUMENT) {
            final String tag = in.getName();
            if (type == START_TAG) {
                if (TAG_POLICY_LIST.equals(tag)) {
                    final boolean oldValue = mRestrictBackground;
                    version = readIntAttribute(in, ATTR_VERSION);
                    if (version >= VERSION_ADDED_RESTRICT_BACKGROUND) {
                        mRestrictBackground = readBooleanAttribute(in, ATTR_RESTRICT_BACKGROUND);
                    } else {
                        mRestrictBackground = false;
                    }
                    if (mRestrictBackground != oldValue) {
                        // Some early services may have read the default value,
                        // so notify them that it's changed
                        mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, mRestrictBackground ? 1 : 0, 0).sendToTarget();
                    }
                } else if (TAG_NETWORK_POLICY.equals(tag)) {
                    final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE);
                    final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID);
                    final String networkId;
                    if (version >= VERSION_ADDED_NETWORK_ID) {
                        networkId = in.getAttributeValue(null, ATTR_NETWORK_ID);
                    } else {
                        networkId = null;
                    }
                    final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY);
                    final String cycleTimezone;
                    if (version >= VERSION_ADDED_TIMEZONE) {
                        cycleTimezone = in.getAttributeValue(null, ATTR_CYCLE_TIMEZONE);
                    } else {
                        cycleTimezone = Time.TIMEZONE_UTC;
                    }
                    final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES);
                    final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES);
                    final long lastLimitSnooze;
                    if (version >= VERSION_SPLIT_SNOOZE) {
                        lastLimitSnooze = readLongAttribute(in, ATTR_LAST_LIMIT_SNOOZE);
                    } else if (version >= VERSION_ADDED_SNOOZE) {
                        lastLimitSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE);
                    } else {
                        lastLimitSnooze = SNOOZE_NEVER;
                    }
                    final boolean metered;
                    if (version >= VERSION_ADDED_METERED) {
                        metered = readBooleanAttribute(in, ATTR_METERED);
                    } else {
                        switch(networkTemplate) {
                            case MATCH_MOBILE_3G_LOWER:
                            case MATCH_MOBILE_4G:
                            case MATCH_MOBILE_ALL:
                                metered = true;
                                break;
                            default:
                                metered = false;
                        }
                    }
                    final long lastWarningSnooze;
                    if (version >= VERSION_SPLIT_SNOOZE) {
                        lastWarningSnooze = readLongAttribute(in, ATTR_LAST_WARNING_SNOOZE);
                    } else {
                        lastWarningSnooze = SNOOZE_NEVER;
                    }
                    final boolean inferred;
                    if (version >= VERSION_ADDED_INFERRED) {
                        inferred = readBooleanAttribute(in, ATTR_INFERRED);
                    } else {
                        inferred = false;
                    }
                    final NetworkTemplate template = new NetworkTemplate(networkTemplate, subscriberId, networkId);
                    if (template.isPersistable()) {
                        mNetworkPolicy.put(template, new NetworkPolicy(template, cycleDay, cycleTimezone, warningBytes, limitBytes, lastWarningSnooze, lastLimitSnooze, metered, inferred));
                    }
                } else if (TAG_UID_POLICY.equals(tag)) {
                    final int uid = readIntAttribute(in, ATTR_UID);
                    final int policy = readIntAttribute(in, ATTR_POLICY);
                    if (UserHandle.isApp(uid)) {
                        setUidPolicyUncheckedUL(uid, policy, false);
                    } else {
                        Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring");
                    }
                } else if (TAG_APP_POLICY.equals(tag)) {
                    final int appId = readIntAttribute(in, ATTR_APP_ID);
                    final int policy = readIntAttribute(in, ATTR_POLICY);
                    // TODO: set for other users during upgrade
                    // app policy is deprecated so this is only used in pre system user split.
                    final int uid = UserHandle.getUid(UserHandle.USER_SYSTEM, appId);
                    if (UserHandle.isApp(uid)) {
                        setUidPolicyUncheckedUL(uid, policy, false);
                    } else {
                        Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring");
                    }
                } else if (TAG_WHITELIST.equals(tag)) {
                    insideWhitelist = true;
                } else if (TAG_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) {
                    final int uid = readIntAttribute(in, ATTR_UID);
                    mRestrictBackgroundWhitelistUids.put(uid, true);
                } else if (TAG_REVOKED_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) {
                    final int uid = readIntAttribute(in, ATTR_UID);
                    mRestrictBackgroundWhitelistRevokedUids.put(uid, true);
                }
            } else if (type == END_TAG) {
                if (TAG_WHITELIST.equals(tag)) {
                    insideWhitelist = false;
                }
            }
        }
    } catch (FileNotFoundException e) {
        // missing policy is okay, probably first boot
        upgradeLegacyBackgroundDataUL();
    } catch (IOException e) {
        Log.wtf(TAG, "problem reading network policy", e);
    } catch (XmlPullParserException e) {
        Log.wtf(TAG, "problem reading network policy", e);
    } finally {
        IoUtils.closeQuietly(fis);
    }
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkPolicy(android.net.NetworkPolicy) XmlPullParser(org.xmlpull.v1.XmlPullParser) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 42 with NetworkPolicy

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

the class NetworkPolicyManagerService method normalizePoliciesNL.

private void normalizePoliciesNL(NetworkPolicy[] policies) {
    final TelephonyManager tele = TelephonyManager.from(mContext);
    final String[] merged = tele.getMergedSubscriberIds();
    mNetworkPolicy.clear();
    for (NetworkPolicy policy : policies) {
        // When two normalized templates conflict, prefer the most
        // restrictive policy
        policy.template = NetworkTemplate.normalize(policy.template, merged);
        final NetworkPolicy existing = mNetworkPolicy.get(policy.template);
        if (existing == null || existing.compareTo(policy) > 0) {
            if (existing != null) {
                Slog.d(TAG, "Normalization replaced " + existing + " with " + policy);
            }
            mNetworkPolicy.put(policy.template, policy);
        }
    }
}
Also used : TelephonyManager(android.telephony.TelephonyManager) NetworkPolicy(android.net.NetworkPolicy) NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString)

Example 43 with NetworkPolicy

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

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)

Example 44 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 45 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)

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