Search in sources :

Example 6 with INetworkStatsSession

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");
    }
}
Also used : INetworkStatsSession(android.net.INetworkStatsSession) NetworkPolicy(android.net.NetworkPolicy) Time(android.text.format.Time) NetworkStatsHistory(android.net.NetworkStatsHistory) RemoteException(android.os.RemoteException) Date(java.util.Date)

Example 7 with INetworkStatsSession

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;
}
Also used : INetworkStatsSession(android.net.INetworkStatsSession) ConnectivityManager(android.net.ConnectivityManager) RemoteException(android.os.RemoteException)

Example 8 with INetworkStatsSession

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");
    }
}
Also used : INetworkStatsSession(android.net.INetworkStatsSession) NetworkPolicy(android.net.NetworkPolicy) Time(android.text.format.Time) NetworkStatsHistory(android.net.NetworkStatsHistory) RemoteException(android.os.RemoteException) Date(java.util.Date)

Example 9 with INetworkStatsSession

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);
    }
}
Also used : INetworkStatsSession(android.net.INetworkStatsSession) NetworkStats(android.net.NetworkStats) RemoteException(android.os.RemoteException)

Example 10 with INetworkStatsSession

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");
    }
}
Also used : INetworkStatsSession(android.net.INetworkStatsSession) NetworkPolicy(android.net.NetworkPolicy) Time(android.text.format.Time) NetworkStatsHistory(android.net.NetworkStatsHistory) RemoteException(android.os.RemoteException) Date(java.util.Date)

Aggregations

INetworkStatsSession (android.net.INetworkStatsSession)11 RemoteException (android.os.RemoteException)11 NetworkPolicy (android.net.NetworkPolicy)5 NetworkStats (android.net.NetworkStats)5 NetworkStatsHistory (android.net.NetworkStatsHistory)5 Time (android.text.format.Time)5 Date (java.util.Date)5 ConnectivityManager (android.net.ConnectivityManager)1