use of android.net.NetworkPolicy in project android_frameworks_base by ResurrectionRemix.
the class NetworkPolicyManagerServiceTest method testNextCycleTimezoneBeforeUtc.
public void testNextCycleTimezoneBeforeUtc() throws Exception {
// Israel is UTC+2
final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 10, "Israel", 1024L, 1024L, false);
assertTimeEquals(parseTime("2012-01-09T22:00:00.000Z"), computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
}
use of android.net.NetworkPolicy 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());
}
}
}
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();
}
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();
}
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);
}
}
}
Aggregations