Start tracking radio up time.

We now always turn on network state tracking for mobile,
and push this information down to battery stats.

In battery stats we use this to both log the changes in
the history and keep track of the total time the mobile
radio was active.

Power computation is switched over to using this information
to help determine power use, which will hopefully make it
more accurate (not counting inaccuracies in knowing when it
actually goes down).

Note yet done is aggregating this data per-uid, to better
emphasize which apps are causing the radio to be up.  Right
now we just spread the total time across all uids weighted
by the total number of packets they have sent and received.

Also put in the battery stats infrastructure for bluetooth to
address issue #12973036: Improve power_profile.xml

Change-Id: I39d11b7ff6ae4f336f253d1cba308d8569de7e0d
diff --git a/services/core/java/com/android/server/NetworkManagementService.java b/services/core/java/com/android/server/NetworkManagementService.java
index ad7ec99..f1a3ba5 100644
--- a/services/core/java/com/android/server/NetworkManagementService.java
+++ b/services/core/java/com/android/server/NetworkManagementService.java
@@ -151,6 +151,8 @@
 
     private final Handler mMainHandler = new Handler();
 
+    private IBatteryStats mBatteryStats;
+
     private Thread mThread;
     private CountDownLatch mConnectedSignal = new CountDownLatch(1);
 
@@ -226,6 +228,17 @@
         if (DBG) Slog.d(TAG, "Prepared");
     }
 
+    private IBatteryStats getBatteryStats() {
+        synchronized (this) {
+            if (mBatteryStats != null) {
+                return mBatteryStats;
+            }
+            mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
+                    BatteryStats.SERVICE_NAME));
+            return mBatteryStats;
+        }
+    }
+
     @Override
     public void registerObserver(INetworkManagementEventObserver observer) {
         mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
@@ -323,6 +336,10 @@
      * Notify our observers of a change in the data activity state of the interface
      */
     private void notifyInterfaceClassActivity(String label, boolean active) {
+        try {
+            getBatteryStats().noteDataConnectionActive(label, active);
+        } catch (RemoteException e) {
+        }
         final int length = mObservers.beginBroadcast();
         for (int i = 0; i < length; i++) {
             try {
@@ -359,8 +376,7 @@
 
         if (mBandwidthControlEnabled) {
             try {
-                IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))
-                        .noteNetworkStatsEnabled();
+                getBatteryStats().noteNetworkStatsEnabled();
             } catch (RemoteException e) {
             }
         }
@@ -1192,6 +1208,8 @@
                 throw e.rethrowAsParcelableException();
             }
             mActiveIdleTimers.put(iface, new IdleTimerParams(timeout, label));
+            // Networks start up.
+            notifyInterfaceClassActivity(label, true);
         }
     }