Search in sources :

Example 1 with NetworkPolicyManager

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

the class SettingsBackupAgent method restoreNetworkPolicies.

private void restoreNetworkPolicies(byte[] data) {
    NetworkPolicyManager networkPolicyManager = (NetworkPolicyManager) getSystemService(NETWORK_POLICY_SERVICE);
    if (data != null && data.length != 0) {
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
        try {
            int version = in.readInt();
            if (version < 1 || version > NETWORK_POLICIES_BACKUP_VERSION) {
                throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
            }
            int length = in.readInt();
            NetworkPolicy[] policies = new NetworkPolicy[length];
            for (int i = 0; i < length; i++) {
                byte isNull = in.readByte();
                if (isNull == BackupUtils.NULL)
                    continue;
                int byteLength = in.readInt();
                byte[] policyData = new byte[byteLength];
                in.read(policyData, 0, byteLength);
                policies[i] = NetworkPolicy.getNetworkPolicyFromBackup(new DataInputStream(new ByteArrayInputStream(policyData)));
            }
            // Only set the policies if there was no error in the restore operation
            networkPolicyManager.setNetworkPolicies(policies);
        } catch (NullPointerException | IOException | BackupUtils.BadVersionException e) {
            // NPE can be thrown when trying to instantiate a NetworkPolicy
            Log.e(TAG, "Failed to convert byte array to NetworkPolicies " + e.getMessage());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NetworkPolicy(android.net.NetworkPolicy) NetworkPolicyManager(android.net.NetworkPolicyManager) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 2 with NetworkPolicyManager

use of android.net.NetworkPolicyManager 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 3 with NetworkPolicyManager

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

the class SettingsBackupAgent method restoreNetworkPolicies.

private void restoreNetworkPolicies(byte[] data) {
    NetworkPolicyManager networkPolicyManager = (NetworkPolicyManager) getSystemService(NETWORK_POLICY_SERVICE);
    if (data != null && data.length != 0) {
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
        try {
            int version = in.readInt();
            if (version < 1 || version > NETWORK_POLICIES_BACKUP_VERSION) {
                throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
            }
            int length = in.readInt();
            NetworkPolicy[] policies = new NetworkPolicy[length];
            for (int i = 0; i < length; i++) {
                byte isNull = in.readByte();
                if (isNull == BackupUtils.NULL)
                    continue;
                int byteLength = in.readInt();
                byte[] policyData = new byte[byteLength];
                in.read(policyData, 0, byteLength);
                policies[i] = NetworkPolicy.getNetworkPolicyFromBackup(new DataInputStream(new ByteArrayInputStream(policyData)));
            }
            // Only set the policies if there was no error in the restore operation
            networkPolicyManager.setNetworkPolicies(policies);
        } catch (NullPointerException | IOException | BackupUtils.BadVersionException e) {
            // NPE can be thrown when trying to instantiate a NetworkPolicy
            Log.e(TAG, "Failed to convert byte array to NetworkPolicies " + e.getMessage());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NetworkPolicy(android.net.NetworkPolicy) NetworkPolicyManager(android.net.NetworkPolicyManager) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 4 with NetworkPolicyManager

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

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 5 with NetworkPolicyManager

use of android.net.NetworkPolicyManager in project platform_frameworks_base by android.

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)

Aggregations

NetworkPolicyManager (android.net.NetworkPolicyManager)23 Context (android.content.Context)12 NetworkPolicyEditor (com.android.settingslib.NetworkPolicyEditor)12 DataUsageController (com.android.settingslib.net.DataUsageController)12 NetworkPolicy (android.net.NetworkPolicy)10 IOException (java.io.IOException)10 SubscriptionInfo (android.telephony.SubscriptionInfo)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataInputStream (java.io.DataInputStream)5 DataOutputStream (java.io.DataOutputStream)5 AppItem (com.android.settingslib.AppItem)1 EnforcedAdmin (com.android.settingslib.RestrictedLockUtils.EnforcedAdmin)1 RestrictedSwitchPreference (com.android.settingslib.RestrictedSwitchPreference)1 Test (org.junit.Test)1