1

I'm using the NetworkStatsManager class to retrieve the data usage of the apps.

If I provide 01.01.2018 as start time, it shows that my phone has sent out 5,4 GB.
If I provide 01.01.2017, it still shows 5,4 GB.

This lets me assume that there is a limit somehow regarding the start time. The documentation unfortunately does not mention anything regarding this.

So, how much in time can we go back?

Code:
This is the code which retrieves the data using querySummary:

private long[] getBytesSummary(Context context, int networkType, Calendar calendar) {
    NetworkStatsManager networkStatsManager = (NetworkStatsManager) context.getSystemService(Context.NETWORK_STATS_SERVICE);
    NetworkStats networkStats = null;
    try {
        networkStats = networkStatsManager.querySummary(
                networkType,
                Util.getSubscriberId(context, networkType),
                calendar.getTimeInMillis(),
                System.currentTimeMillis());
    } catch (RemoteException e) {
        if (debug) Log.e(TAG, "getBytesSummary: " + e.toString());
    }
    long[] result = new long[2];
    long totalRxBytes = 0;
    long totalTxBytes = 0;

    NetworkStats.Bucket bucket = new NetworkStats.Bucket();
    if (networkStats != null) {
        while (networkStats.hasNextBucket()) {
            networkStats.getNextBucket(bucket);
            int uid = bucket.getUid();
            long uidRxBytes = bucket.getRxBytes();
            long uidTxBytes = bucket.getTxBytes();
            if (uidsWithNetworkUsageMap.indexOfKey(uid) < 0) {
                long[] uidBytes = new long[2];
                uidBytes[0] = uidRxBytes;
                uidBytes[1] = uidTxBytes;
                uidsWithNetworkUsageMap.put(uid, uidBytes);
            } else {
                long[] value = uidsWithNetworkUsageMap.get(uid);
                value[0] = value[0] + uidRxBytes;
                value[1] = value[1] + uidTxBytes;
                uidsWithNetworkUsageMap.put(uid, value);
            }
            totalRxBytes += bucket.getRxBytes();
            totalTxBytes += bucket.getTxBytes();
        }
        networkStats.close();
    }
    result[0] = totalRxBytes;
    result[1] = totalTxBytes;
    return result;
}

As you can see above, I give a calendar object to the method.
This is how I get the calendar for the current year:

public static Calendar getCalendarCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        // for 0-12 clocks
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.AM_PM, Calendar.AM);
        // for 0-24 clocks
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
//        calendar.set(Calendar.YEAR, 2017);
        Log.i("DataUsage", "getCalendarCurrentYear: " + calendar.getTime());
        return calendar;
    }

This returns Mon Jan 01 00:00:00 GMT+01:00 2018.
If I set it now to 2017, the result is the same as for 2018.

Am I doing something wrong?

EDIT:
I just did some testing, the limit seems to be 3 months somehow.
Anything greater than 3 months as start time results in the same value.
Can anyone confirm that managed to get it working for more than 3 months from current time?
Or is there any official documentation addressing this?

Patze
  • 859
  • 10
  • 19
  • Do you have usage access? – TheWanderer Sep 09 '18 at 16:36
  • @TheWanderer yes, I have the usage access permissions – Patze Sep 09 '18 at 16:44
  • Did you explicitly grant them? – TheWanderer Sep 09 '18 at 16:44
  • Also, is this a network-connected app? – TheWanderer Sep 09 '18 at 16:45
  • Yes, you mean the android.permission.PACKAGE_USAGE_STATS right? I'm absolutely sure they are granted. – Patze Sep 09 '18 at 16:46
  • It's an app which displays the outgoing data usage from all installed apps. – Patze Sep 09 '18 at 16:47
  • But does it do any of its own network transfer? – TheWanderer Sep 09 '18 at 16:48
  • I'm not sure why this is relevant, no it does not do any network transfer itself. – Patze Sep 09 '18 at 16:49
  • "Am I doing something wrong?" -- there's probably not that much history. Call `getStartTimeStamp()` and `getEndTimeStamp()` on the `Bucket` and see what they return. – CommonsWare Sep 09 '18 at 16:51
  • Well the docs state that `querySummary()` only returns stats for anything in your app's UID, and that `querySummaryForDevice()` should be used for full stats. But since that 5.4GB number probably doesn't belong to your app, I'm not so sure how correct the docs are. – TheWanderer Sep 09 '18 at 16:51
  • Do you know if the NetworkUsageStatsManager supports statistics from like 1 year ago? I just did some testing, the limit seems to be 3 months somehow. Anything greater than 3 months results in the same value. So 9th June 2018 and anything before that as start time returns always 5,4 GB somehow. – Patze Sep 09 '18 at 16:53
  • "there's probably not that much history" - that's not possible as I have a screenshot where it showed 5,6 GB. I even remember that it was 7,X GB at a certain point of time. – Patze Sep 09 '18 at 16:57
  • @CommonsWare from the querySummary documentation: "Result is aggregated over time, hence all buckets will have the same start and end timestamps." `getStartTimeStamp()` definitely is different between 2018 and 2017 for example if that's what you mean – Patze Sep 09 '18 at 17:55
  • @TheWanderer `querySummaryForDevice()` shows the same behaviour. Whether 2018 or 2017, still same value. – Patze Sep 09 '18 at 17:56

1 Answers1

0

It shows the data usage since last factory reset of your device. May be the date that you are providing is older than your factory reset of your device.

hope i was helpful.