Register for kernel global data usage alerts.

Instead of polling every 15 minutes, register for alerts that trigger
when system-wide traffic passes a threshold.  Still mixed with polling
to persist UID stats, but relaxed to 30 minutes.  Currently watches
for every 512kB.

Make persistence decision separately for network versus UID, and use
total delta bytes when making decision.  Use light bootstrap during
systemReady() instead of heavy poll, which had been force-loading all
UID data unnecessarily.

Bug: 5023631
Change-Id: I04b723d6c4bf872fb1028071122dba66a8e1b576
diff --git a/core/java/android/net/NetworkStats.java b/core/java/android/net/NetworkStats.java
index 272545d..5b883a0 100644
--- a/core/java/android/net/NetworkStats.java
+++ b/core/java/android/net/NetworkStats.java
@@ -313,6 +313,22 @@
     }
 
     /**
+     * Return total bytes represented by this snapshot object, usually used when
+     * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
+     */
+    public long getTotalBytes() {
+        long totalBytes = 0;
+        for (int i = 0; i < size; i++) {
+            // skip specific tags, since already counted in TAG_NONE
+            if (tag[i] != TAG_NONE) continue;
+
+            totalBytes += rxBytes[i];
+            totalBytes += txBytes[i];
+        }
+        return totalBytes;
+    }
+
+    /**
      * Subtract the given {@link NetworkStats}, effectively leaving the delta
      * between two snapshots in time. Assumes that statistics rows collect over
      * time, and that none of them have disappeared.