|
44 | 44 | import static android.provider.Settings.Secure.NETSTATS_DEV_ROTATE_AGE; |
45 | 45 | import static android.provider.Settings.Secure.NETSTATS_GLOBAL_ALERT_BYTES; |
46 | 46 | import static android.provider.Settings.Secure.NETSTATS_POLL_INTERVAL; |
| 47 | +import static android.provider.Settings.Secure.NETSTATS_REPORT_XT_OVER_DEV; |
47 | 48 | import static android.provider.Settings.Secure.NETSTATS_SAMPLE_ENABLED; |
48 | 49 | import static android.provider.Settings.Secure.NETSTATS_TIME_CACHE_MAX_AGE; |
49 | 50 | import static android.provider.Settings.Secure.NETSTATS_UID_BUCKET_DURATION; |
@@ -177,6 +178,7 @@ public interface NetworkStatsSettings { |
177 | 178 | public long getPollInterval(); |
178 | 179 | public long getTimeCacheMaxAge(); |
179 | 180 | public boolean getSampleEnabled(); |
| 181 | + public boolean getReportXtOverDev(); |
180 | 182 |
|
181 | 183 | public static class Config { |
182 | 184 | public final long bucketDuration; |
@@ -221,6 +223,8 @@ public Config(long bucketDuration, long rotateAgeMillis, long deleteAgeMillis) { |
221 | 223 |
|
222 | 224 | /** Cached {@link #mDevRecorder} stats. */ |
223 | 225 | private NetworkStatsCollection mDevStatsCached; |
| 226 | + /** Cached {@link #mXtRecorder} stats. */ |
| 227 | + private NetworkStatsCollection mXtStatsCached; |
224 | 228 |
|
225 | 229 | /** Current counter sets for each UID. */ |
226 | 230 | private SparseIntArray mActiveUidCounterSet = new SparseIntArray(); |
@@ -295,6 +299,7 @@ public void systemReady() { |
295 | 299 | // read historical network stats from disk, since policy service |
296 | 300 | // might need them right away. |
297 | 301 | mDevStatsCached = mDevRecorder.getOrLoadCompleteLocked(); |
| 302 | + mXtStatsCached = mXtRecorder.getOrLoadCompleteLocked(); |
298 | 303 |
|
299 | 304 | // bootstrap initial stats to prevent double-counting later |
300 | 305 | bootstrapStatsLocked(); |
@@ -371,6 +376,7 @@ private void shutdownLocked() { |
371 | 376 | mUidTagRecorder = null; |
372 | 377 |
|
373 | 378 | mDevStatsCached = null; |
| 379 | + mXtStatsCached = null; |
374 | 380 |
|
375 | 381 | mSystemReady = false; |
376 | 382 | } |
@@ -469,12 +475,12 @@ private NetworkStatsCollection getUidTagComplete() { |
469 | 475 | @Override |
470 | 476 | public NetworkStats getSummaryForNetwork( |
471 | 477 | NetworkTemplate template, long start, long end) { |
472 | | - return mDevStatsCached.getSummary(template, start, end); |
| 478 | + return internalGetSummaryForNetwork(template, start, end); |
473 | 479 | } |
474 | 480 |
|
475 | 481 | @Override |
476 | 482 | public NetworkStatsHistory getHistoryForNetwork(NetworkTemplate template, int fields) { |
477 | | - return mDevStatsCached.getHistory(template, UID_ALL, SET_ALL, TAG_NONE, fields); |
| 483 | + return internalGetHistoryForNetwork(template, fields); |
478 | 484 | } |
479 | 485 |
|
480 | 486 | @Override |
@@ -507,11 +513,56 @@ public void close() { |
507 | 513 | }; |
508 | 514 | } |
509 | 515 |
|
| 516 | + /** |
| 517 | + * Return network summary, splicing between {@link #mDevStatsCached} |
| 518 | + * and {@link #mXtStatsCached} when appropriate. |
| 519 | + */ |
| 520 | + private NetworkStats internalGetSummaryForNetwork( |
| 521 | + NetworkTemplate template, long start, long end) { |
| 522 | + if (!mSettings.getReportXtOverDev()) { |
| 523 | + // shortcut when XT reporting disabled |
| 524 | + return mDevStatsCached.getSummary(template, start, end); |
| 525 | + } |
| 526 | + |
| 527 | + // splice stats between DEV and XT, switching over from DEV to XT at |
| 528 | + // first atomic bucket. |
| 529 | + final long firstAtomicBucket = mXtStatsCached.getFirstAtomicBucketMillis(); |
| 530 | + final NetworkStats dev = mDevStatsCached.getSummary( |
| 531 | + template, Math.min(start, firstAtomicBucket), Math.min(end, firstAtomicBucket)); |
| 532 | + final NetworkStats xt = mXtStatsCached.getSummary( |
| 533 | + template, Math.max(start, firstAtomicBucket), Math.max(end, firstAtomicBucket)); |
| 534 | + |
| 535 | + xt.combineAllValues(dev); |
| 536 | + return xt; |
| 537 | + } |
| 538 | + |
| 539 | + /** |
| 540 | + * Return network history, splicing between {@link #mDevStatsCached} |
| 541 | + * and {@link #mXtStatsCached} when appropriate. |
| 542 | + */ |
| 543 | + private NetworkStatsHistory internalGetHistoryForNetwork(NetworkTemplate template, int fields) { |
| 544 | + if (!mSettings.getReportXtOverDev()) { |
| 545 | + // shortcut when XT reporting disabled |
| 546 | + return mDevStatsCached.getHistory(template, UID_ALL, SET_ALL, TAG_NONE, fields); |
| 547 | + } |
| 548 | + |
| 549 | + // splice stats between DEV and XT, switching over from DEV to XT at |
| 550 | + // first atomic bucket. |
| 551 | + final long firstAtomicBucket = mXtStatsCached.getFirstAtomicBucketMillis(); |
| 552 | + final NetworkStatsHistory dev = mDevStatsCached.getHistory( |
| 553 | + template, UID_ALL, SET_ALL, TAG_NONE, fields, Long.MIN_VALUE, firstAtomicBucket); |
| 554 | + final NetworkStatsHistory xt = mXtStatsCached.getHistory( |
| 555 | + template, UID_ALL, SET_ALL, TAG_NONE, fields, firstAtomicBucket, Long.MAX_VALUE); |
| 556 | + |
| 557 | + xt.recordEntireHistory(dev); |
| 558 | + return xt; |
| 559 | + } |
| 560 | + |
510 | 561 | @Override |
511 | 562 | public long getNetworkTotalBytes(NetworkTemplate template, long start, long end) { |
512 | 563 | mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); |
513 | 564 | assertBandwidthControlEnabled(); |
514 | | - return mDevStatsCached.getSummary(template, start, end).getTotalBytes(); |
| 565 | + return internalGetSummaryForNetwork(template, start, end).getTotalBytes(); |
515 | 566 | } |
516 | 567 |
|
517 | 568 | @Override |
@@ -1190,6 +1241,10 @@ public boolean getSampleEnabled() { |
1190 | 1241 | return getSecureBoolean(NETSTATS_SAMPLE_ENABLED, true); |
1191 | 1242 | } |
1192 | 1243 | @Override |
| 1244 | + public boolean getReportXtOverDev() { |
| 1245 | + return getSecureBoolean(NETSTATS_REPORT_XT_OVER_DEV, true); |
| 1246 | + } |
| 1247 | + @Override |
1193 | 1248 | public Config getDevConfig() { |
1194 | 1249 | return new Config(getSecureLong(NETSTATS_DEV_BUCKET_DURATION, HOUR_IN_MILLIS), |
1195 | 1250 | getSecureLong(NETSTATS_DEV_ROTATE_AGE, 15 * DAY_IN_MILLIS), |
|
0 commit comments