use of android.net.INetworkStatsSession in project android_frameworks_base by AOSPA.
the class DataUsageController method getDataUsageInfo.
public DataUsageInfo getDataUsageInfo(NetworkTemplate template) {
final INetworkStatsSession session = getSession();
if (session == null) {
return warn("no stats session");
}
final NetworkPolicy policy = findNetworkPolicy(template);
try {
final NetworkStatsHistory history = session.getHistoryForNetwork(template, FIELDS);
final long now = System.currentTimeMillis();
final long start, end;
if (policy != null && policy.cycleDay > 0) {
// period = determined from cycleDay
if (DEBUG)
Log.d(TAG, "Cycle day=" + policy.cycleDay + " tz=" + policy.cycleTimezone);
final Time nowTime = new Time(policy.cycleTimezone);
nowTime.setToNow();
final Time policyTime = new Time(nowTime);
policyTime.set(policy.cycleDay, policyTime.month, policyTime.year);
policyTime.normalize(false);
if (nowTime.after(policyTime)) {
start = policyTime.toMillis(false);
end = addMonth(policyTime, 1).toMillis(false);
} else {
start = addMonth(policyTime, -1).toMillis(false);
end = policyTime.toMillis(false);
}
} else {
// period = last 4 wks
end = now;
start = now - DateUtils.WEEK_IN_MILLIS * 4;
}
final long callStart = System.currentTimeMillis();
final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null);
final long callEnd = System.currentTimeMillis();
if (DEBUG)
Log.d(TAG, String.format("history call from %s to %s now=%s took %sms: %s", new Date(start), new Date(end), new Date(now), callEnd - callStart, historyEntryToString(entry)));
if (entry == null) {
return warn("no entry data");
}
final long totalBytes = entry.rxBytes + entry.txBytes;
final DataUsageInfo usage = new DataUsageInfo();
usage.startDate = start;
usage.usageLevel = totalBytes;
usage.period = formatDateRange(start, end);
if (policy != null) {
usage.limitLevel = policy.limitBytes > 0 ? policy.limitBytes : 0;
usage.warningLevel = policy.warningBytes > 0 ? policy.warningBytes : 0;
} else {
usage.warningLevel = getDefaultWarningLevel();
}
if (usage != null && mNetworkController != null) {
usage.carrier = mNetworkController.getMobileDataNetworkName();
}
return usage;
} catch (RemoteException e) {
return warn("remote call failed");
}
}
use of android.net.INetworkStatsSession in project Resurrection_packages_apps_Settings by ResurrectionRemix.
the class DataUsageSummary method hasEthernet.
/**
* Test if device has an ethernet network connection.
*/
public boolean hasEthernet(Context context) {
if (TEST_RADIOS) {
return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
}
final ConnectivityManager conn = ConnectivityManager.from(context);
final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
final long ethernetBytes;
try {
INetworkStatsSession statsSession = services.mStatsService.openSession();
if (statsSession != null) {
ethernetBytes = statsSession.getSummaryForNetwork(NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE).getTotalBytes();
TrafficStats.closeQuietly(statsSession);
} else {
ethernetBytes = 0;
}
} catch (RemoteException e) {
throw new RuntimeException(e);
}
// only show ethernet when both hardware present and traffic has occurred
return hasEthernet && ethernetBytes > 0;
}
use of android.net.INetworkStatsSession in project android_frameworks_base by ResurrectionRemix.
the class DataUsageController method getDataUsageInfo.
public DataUsageInfo getDataUsageInfo(NetworkTemplate template) {
final INetworkStatsSession session = getSession();
if (session == null) {
return warn("no stats session");
}
final NetworkPolicy policy = findNetworkPolicy(template);
try {
final NetworkStatsHistory history = session.getHistoryForNetwork(template, FIELDS);
final long now = System.currentTimeMillis();
final long start, end;
if (policy != null && policy.cycleDay > 0) {
// period = determined from cycleDay
if (DEBUG)
Log.d(TAG, "Cycle day=" + policy.cycleDay + " tz=" + policy.cycleTimezone);
final Time nowTime = new Time(policy.cycleTimezone);
nowTime.setToNow();
final Time policyTime = new Time(nowTime);
policyTime.set(policy.cycleDay, policyTime.month, policyTime.year);
policyTime.normalize(false);
if (nowTime.after(policyTime)) {
start = policyTime.toMillis(false);
end = addMonth(policyTime, 1).toMillis(false);
} else {
start = addMonth(policyTime, -1).toMillis(false);
end = policyTime.toMillis(false);
}
} else {
// period = last 4 wks
end = now;
start = now - DateUtils.WEEK_IN_MILLIS * 4;
}
final long callStart = System.currentTimeMillis();
final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null);
final long callEnd = System.currentTimeMillis();
if (DEBUG)
Log.d(TAG, String.format("history call from %s to %s now=%s took %sms: %s", new Date(start), new Date(end), new Date(now), callEnd - callStart, historyEntryToString(entry)));
if (entry == null) {
return warn("no entry data");
}
final long totalBytes = entry.rxBytes + entry.txBytes;
final DataUsageInfo usage = new DataUsageInfo();
usage.startDate = start;
usage.usageLevel = totalBytes;
usage.period = formatDateRange(start, end);
if (policy != null) {
usage.limitLevel = policy.limitBytes > 0 ? policy.limitBytes : 0;
usage.warningLevel = policy.warningBytes > 0 ? policy.warningBytes : 0;
} else {
usage.warningLevel = getDefaultWarningLevel();
}
if (usage != null && mNetworkController != null) {
usage.carrier = mNetworkController.getMobileDataNetworkName();
}
return usage;
} catch (RemoteException e) {
return warn("remote call failed");
}
}
use of android.net.INetworkStatsSession in project android_frameworks_base by ResurrectionRemix.
the class DataIdleTest method fetchStats.
/**
* Helper method that fetches all the network stats available and reports it
* to instrumentation out.
* @param template {@link NetworkTemplate} to match.
*/
private void fetchStats(NetworkTemplate template) {
INetworkStatsSession session = null;
try {
mStatsService.forceUpdate();
session = mStatsService.openSession();
final NetworkStats stats = session.getSummaryForAllUid(template, Long.MIN_VALUE, Long.MAX_VALUE, false);
reportStats(stats);
} catch (RemoteException e) {
Log.w(LOG_TAG, "Failed to fetch network stats.");
} finally {
TrafficStats.closeQuietly(session);
}
}
use of android.net.INetworkStatsSession in project android_frameworks_base by crdroidandroid.
the class DataUsageController method getDataUsageInfo.
public DataUsageInfo getDataUsageInfo(NetworkTemplate template) {
final INetworkStatsSession session = getSession();
if (session == null) {
return warn("no stats session");
}
final NetworkPolicy policy = findNetworkPolicy(template);
try {
final NetworkStatsHistory history = session.getHistoryForNetwork(template, FIELDS);
final long now = System.currentTimeMillis();
final long start, end;
if (policy != null && policy.cycleDay > 0) {
// period = determined from cycleDay
if (DEBUG)
Log.d(TAG, "Cycle day=" + policy.cycleDay + " tz=" + policy.cycleTimezone);
final Time nowTime = new Time(policy.cycleTimezone);
nowTime.setToNow();
final Time policyTime = new Time(nowTime);
policyTime.set(policy.cycleDay, policyTime.month, policyTime.year);
policyTime.normalize(false);
if (nowTime.after(policyTime)) {
start = policyTime.toMillis(false);
end = addMonth(policyTime, 1).toMillis(false);
} else {
start = addMonth(policyTime, -1).toMillis(false);
end = policyTime.toMillis(false);
}
} else {
// period = last 4 wks
end = now;
start = now - DateUtils.WEEK_IN_MILLIS * 4;
}
final long callStart = System.currentTimeMillis();
final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null);
final long callEnd = System.currentTimeMillis();
if (DEBUG)
Log.d(TAG, String.format("history call from %s to %s now=%s took %sms: %s", new Date(start), new Date(end), new Date(now), callEnd - callStart, historyEntryToString(entry)));
if (entry == null) {
return warn("no entry data");
}
final long totalBytes = entry.rxBytes + entry.txBytes;
final DataUsageInfo usage = new DataUsageInfo();
usage.startDate = start;
usage.usageLevel = totalBytes;
usage.period = formatDateRange(start, end);
if (policy != null) {
usage.limitLevel = policy.limitBytes > 0 ? policy.limitBytes : 0;
usage.warningLevel = policy.warningBytes > 0 ? policy.warningBytes : 0;
} else {
usage.warningLevel = getDefaultWarningLevel();
}
if (usage != null && mNetworkController != null) {
usage.carrier = mNetworkController.getMobileDataNetworkName();
}
return usage;
} catch (RemoteException e) {
return warn("remote call failed");
}
}
Aggregations