use of android.net.NetworkTemplate in project platform_frameworks_base by android.
the class NetworkPolicyManagerService method writePolicyAL.
void writePolicyAL() {
if (LOGV)
Slog.v(TAG, "writePolicyAL()");
FileOutputStream fos = null;
try {
fos = mPolicyFile.startWrite();
XmlSerializer out = new FastXmlSerializer();
out.setOutput(fos, StandardCharsets.UTF_8.name());
out.startDocument(null, true);
out.startTag(null, TAG_POLICY_LIST);
writeIntAttribute(out, ATTR_VERSION, VERSION_LATEST);
writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground);
// write all known network policies
for (int i = 0; i < mNetworkPolicy.size(); i++) {
final NetworkPolicy policy = mNetworkPolicy.valueAt(i);
final NetworkTemplate template = policy.template;
if (!template.isPersistable())
continue;
out.startTag(null, TAG_NETWORK_POLICY);
writeIntAttribute(out, ATTR_NETWORK_TEMPLATE, template.getMatchRule());
final String subscriberId = template.getSubscriberId();
if (subscriberId != null) {
out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId);
}
final String networkId = template.getNetworkId();
if (networkId != null) {
out.attribute(null, ATTR_NETWORK_ID, networkId);
}
writeIntAttribute(out, ATTR_CYCLE_DAY, policy.cycleDay);
out.attribute(null, ATTR_CYCLE_TIMEZONE, policy.cycleTimezone);
writeLongAttribute(out, ATTR_WARNING_BYTES, policy.warningBytes);
writeLongAttribute(out, ATTR_LIMIT_BYTES, policy.limitBytes);
writeLongAttribute(out, ATTR_LAST_WARNING_SNOOZE, policy.lastWarningSnooze);
writeLongAttribute(out, ATTR_LAST_LIMIT_SNOOZE, policy.lastLimitSnooze);
writeBooleanAttribute(out, ATTR_METERED, policy.metered);
writeBooleanAttribute(out, ATTR_INFERRED, policy.inferred);
out.endTag(null, TAG_NETWORK_POLICY);
}
// write all known uid policies
for (int i = 0; i < mUidPolicy.size(); i++) {
final int uid = mUidPolicy.keyAt(i);
final int policy = mUidPolicy.valueAt(i);
// skip writing empty policies
if (policy == POLICY_NONE)
continue;
out.startTag(null, TAG_UID_POLICY);
writeIntAttribute(out, ATTR_UID, uid);
writeIntAttribute(out, ATTR_POLICY, policy);
out.endTag(null, TAG_UID_POLICY);
}
out.endTag(null, TAG_POLICY_LIST);
// write all whitelists
out.startTag(null, TAG_WHITELIST);
// restrict background whitelist
int size = mRestrictBackgroundWhitelistUids.size();
for (int i = 0; i < size; i++) {
final int uid = mRestrictBackgroundWhitelistUids.keyAt(i);
out.startTag(null, TAG_RESTRICT_BACKGROUND);
writeIntAttribute(out, ATTR_UID, uid);
out.endTag(null, TAG_RESTRICT_BACKGROUND);
}
// revoked restrict background whitelist
size = mRestrictBackgroundWhitelistRevokedUids.size();
for (int i = 0; i < size; i++) {
final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i);
out.startTag(null, TAG_REVOKED_RESTRICT_BACKGROUND);
writeIntAttribute(out, ATTR_UID, uid);
out.endTag(null, TAG_REVOKED_RESTRICT_BACKGROUND);
}
out.endTag(null, TAG_WHITELIST);
out.endDocument();
mPolicyFile.finishWrite(fos);
} catch (IOException e) {
if (fos != null) {
mPolicyFile.failWrite(fos);
}
}
}
use of android.net.NetworkTemplate in project platform_frameworks_base by android.
the class NetworkPolicyManagerShellCommand method newPolicy.
private NetworkPolicy newPolicy(String ssid) {
final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(ssid);
final NetworkPolicy policy = newWifiPolicy(template, false);
return policy;
}
use of android.net.NetworkTemplate in project platform_frameworks_base by android.
the class NetworkStatsService method createSession.
private INetworkStatsSession createSession(final String callingPackage, boolean pollOnCreate) {
assertBandwidthControlEnabled();
if (pollOnCreate) {
final long ident = Binder.clearCallingIdentity();
try {
performPoll(FLAG_PERSIST_ALL);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
return new INetworkStatsSession.Stub() {
private NetworkStatsCollection mUidComplete;
private NetworkStatsCollection mUidTagComplete;
private String mCallingPackage = callingPackage;
private NetworkStatsCollection getUidComplete() {
synchronized (mStatsLock) {
if (mUidComplete == null) {
mUidComplete = mUidRecorder.getOrLoadCompleteLocked();
}
return mUidComplete;
}
}
private NetworkStatsCollection getUidTagComplete() {
synchronized (mStatsLock) {
if (mUidTagComplete == null) {
mUidTagComplete = mUidTagRecorder.getOrLoadCompleteLocked();
}
return mUidTagComplete;
}
}
@Override
public int[] getRelevantUids() {
return getUidComplete().getRelevantUids(checkAccessLevel(mCallingPackage));
}
@Override
public NetworkStats getDeviceSummaryForNetwork(NetworkTemplate template, long start, long end) {
@NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
if (accessLevel < NetworkStatsAccess.Level.DEVICESUMMARY) {
throw new SecurityException("Calling package " + mCallingPackage + " cannot access device summary network stats");
}
NetworkStats result = new NetworkStats(end - start, 1);
final long ident = Binder.clearCallingIdentity();
try {
// Using access level higher than the one we checked for above.
// Reason is that we are combining usage data in a way that is not PII
// anymore.
result.combineAllValues(internalGetSummaryForNetwork(template, start, end, NetworkStatsAccess.Level.DEVICE));
} finally {
Binder.restoreCallingIdentity(ident);
}
return result;
}
@Override
public NetworkStats getSummaryForNetwork(NetworkTemplate template, long start, long end) {
@NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
return internalGetSummaryForNetwork(template, start, end, accessLevel);
}
@Override
public NetworkStatsHistory getHistoryForNetwork(NetworkTemplate template, int fields) {
@NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
return internalGetHistoryForNetwork(template, fields, accessLevel);
}
@Override
public NetworkStats getSummaryForAllUid(NetworkTemplate template, long start, long end, boolean includeTags) {
@NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
final NetworkStats stats = getUidComplete().getSummary(template, start, end, accessLevel);
if (includeTags) {
final NetworkStats tagStats = getUidTagComplete().getSummary(template, start, end, accessLevel);
stats.combineAllValues(tagStats);
}
return stats;
}
@Override
public NetworkStatsHistory getHistoryForUid(NetworkTemplate template, int uid, int set, int tag, int fields) {
@NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
if (tag == TAG_NONE) {
return getUidComplete().getHistory(template, uid, set, tag, fields, accessLevel);
} else {
return getUidTagComplete().getHistory(template, uid, set, tag, fields, accessLevel);
}
}
@Override
public NetworkStatsHistory getHistoryIntervalForUid(NetworkTemplate template, int uid, int set, int tag, int fields, long start, long end) {
@NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
if (tag == TAG_NONE) {
return getUidComplete().getHistory(template, uid, set, tag, fields, start, end, accessLevel);
} else if (uid == Binder.getCallingUid()) {
return getUidTagComplete().getHistory(template, uid, set, tag, fields, start, end, accessLevel);
} else {
throw new SecurityException("Calling package " + mCallingPackage + " cannot access tag information from a different uid");
}
}
@Override
public void close() {
mUidComplete = null;
mUidTagComplete = null;
}
};
}
use of android.net.NetworkTemplate in project platform_frameworks_base by android.
the class DataIdleTest method testWifiIdle.
/**
* Test that dumps all the data usage metrics for wifi to instrumentation out.
*/
public void testWifiIdle() {
NetworkTemplate template = NetworkTemplate.buildTemplateWifiWildcard();
fetchStats(template);
}
use of android.net.NetworkTemplate in project platform_frameworks_base by android.
the class DataIdleTest method testMobile.
/**
* Test that dumps all the data usage metrics for all mobile to instrumentation out.
*/
public void testMobile() {
String subscriberId = mTelephonyManager.getSubscriberId();
NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
fetchStats(template);
}
Aggregations