Fix issue #2438980: Implement package watcher for voice recognizer service setting

I am getting tired of writing package monitor code, realized this is missing in
a number of places, and at this point it has gotten complicated enough that I
don't think anyone actually does it 100% right so:

Introducing PackageMonitor.

Yes there are no Java docs.  I am still playing around with just what this
thing is to figure out what makes sense and how people will use it.  It is
being used to fix this bug for monitoring voice recognizers (integrating the
code from the settings provider for setting an initial value), to replace
the existing code for monitoring input methods (and fix the bug where we
wouldn't remove an input method from the enabled list when it got
uninstalled), to now monitor live wallpaper package changes (now allowing
us to avoid reverting back to the default live wallpaper when the current
one is updated!), and to monitor device admin changes.

Also includes a fix so you can't uninstall an .apk that is currently enabled
as a device admin.

Also includes a fix where the default time zone was not initialized early
enough which should fix issue #2455507 (Observed Google services frame work crash).

In addition, this finally introduces a mechanism to determine if the
"force stop" button should be enabled, with convenience in PackageMonitor
for system services to handle it.  All services have been updated to support
this.  There is also new infrastructure for reporting battery usage as an
applicatin error report.
diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java
index b706c5c..56a05ee 100644
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -750,11 +750,8 @@
      * Checkin server version of dump to produce more compact, computer-readable log.
      * 
      * NOTE: all times are expressed in 'ms'.
-     * @param fd
-     * @param pw
-     * @param which
      */
-    private final void dumpCheckinLocked(PrintWriter pw, int which) {
+    public final void dumpCheckinLocked(PrintWriter pw, int which, int reqUid) {
         final long rawUptime = SystemClock.uptimeMillis() * 1000;
         final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
         final long batteryUptime = getBatteryUptime(rawUptime);
@@ -856,19 +853,24 @@
                     getDischargeCurrentLevel());
         }
         
-        Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
-        if (kernelWakelocks.size() > 0) {
-            for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
-                sb.setLength(0);
-                printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
-
-                dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(), 
-                        sb.toString());
+        if (reqUid < 0) {
+            Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
+            if (kernelWakelocks.size() > 0) {
+                for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
+                    sb.setLength(0);
+                    printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
+    
+                    dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(), 
+                            sb.toString());
+                }
             }
         }
         
         for (int iu = 0; iu < NU; iu++) {
             final int uid = uidStats.keyAt(iu);
+            if (reqUid >= 0 && uid != reqUid) {
+                continue;
+            }
             Uid u = uidStats.valueAt(iu);
             // Dump Network stats per uid, if any
             long rx = u.getTcpBytesReceived(which);
@@ -987,7 +989,7 @@
     }
 
     @SuppressWarnings("unused")
-    private final void dumpLocked(PrintWriter pw, String prefix, int which) {
+    public final void dumpLocked(PrintWriter pw, String prefix, int which, int reqUid) {
         final long rawUptime = SystemClock.uptimeMillis() * 1000;
         final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
         final long batteryUptime = getBatteryUptime(rawUptime);
@@ -1063,23 +1065,25 @@
         long fullWakeLockTimeTotalMicros = 0;
         long partialWakeLockTimeTotalMicros = 0;
         
-        Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
-        if (kernelWakelocks.size() > 0) {
-            for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
-                
-                String linePrefix = ": ";
-                sb.setLength(0);
-                sb.append(prefix);
-                sb.append("  Kernel Wake lock ");
-                sb.append(ent.getKey());
-                linePrefix = printWakeLock(sb, ent.getValue(), batteryRealtime, null, which, 
-                        linePrefix);
-                if (!linePrefix.equals(": ")) {
-                    sb.append(" realtime");
-                } else {
-                    sb.append(": (nothing executed)");
+        if (reqUid < 0) {
+            Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
+            if (kernelWakelocks.size() > 0) {
+                for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
+                    
+                    String linePrefix = ": ";
+                    sb.setLength(0);
+                    sb.append(prefix);
+                    sb.append("  Kernel Wake lock ");
+                    sb.append(ent.getKey());
+                    linePrefix = printWakeLock(sb, ent.getValue(), batteryRealtime, null, which, 
+                            linePrefix);
+                    if (!linePrefix.equals(": ")) {
+                        sb.append(" realtime");
+                    } else {
+                        sb.append(": (nothing executed)");
+                    }
+                    pw.println(sb.toString());
                 }
-                pw.println(sb.toString());
             }
         }
     
@@ -1212,7 +1216,12 @@
 
         for (int iu=0; iu<NU; iu++) {
             final int uid = uidStats.keyAt(iu);
+            if (reqUid >= 0 && uid != reqUid) {
+                continue;
+            }
+            
             Uid u = uidStats.valueAt(iu);
+            
             pw.println(prefix + "  #" + uid + ":");
             boolean uidActivity = false;
             
@@ -1421,16 +1430,16 @@
         pw.println("Total Statistics (Current and Historic):");
         pw.println("  System starts: " + getStartCount()
                 + ", currently on battery: " + getIsOnBattery());
-        dumpLocked(pw, "", STATS_TOTAL);
+        dumpLocked(pw, "", STATS_TOTAL, -1);
         pw.println("");
         pw.println("Last Run Statistics (Previous run of system):");
-        dumpLocked(pw, "", STATS_LAST);
+        dumpLocked(pw, "", STATS_LAST, -1);
         pw.println("");
         pw.println("Current Battery Statistics (Currently running system):");
-        dumpLocked(pw, "", STATS_CURRENT);
+        dumpLocked(pw, "", STATS_CURRENT, -1);
         pw.println("");
         pw.println("Unplugged Statistics (Since last unplugged from power):");
-        dumpLocked(pw, "", STATS_UNPLUGGED);
+        dumpLocked(pw, "", STATS_UNPLUGGED, -1);
     }
     
     @SuppressWarnings("unused")
@@ -1445,13 +1454,13 @@
         }
         
         if (isUnpluggedOnly) {
-            dumpCheckinLocked(pw, STATS_UNPLUGGED);
+            dumpCheckinLocked(pw, STATS_UNPLUGGED, -1);
         }
         else {
-            dumpCheckinLocked(pw, STATS_TOTAL);
-            dumpCheckinLocked(pw, STATS_LAST);
-            dumpCheckinLocked(pw, STATS_UNPLUGGED);
-            dumpCheckinLocked(pw, STATS_CURRENT);
+            dumpCheckinLocked(pw, STATS_TOTAL, -1);
+            dumpCheckinLocked(pw, STATS_LAST, -1);
+            dumpCheckinLocked(pw, STATS_UNPLUGGED, -1);
+            dumpCheckinLocked(pw, STATS_CURRENT, -1);
         }
     }