Fix problem where power manager was calling battery stats with bad wl type.

Also fiddle code to reduce duplication.

Change-Id: I3f1b086e53ef88dac1ec1896fe711b3cfe7fdd58
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index af8d7d4..8ab1bb8 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -660,28 +660,38 @@
     }
 
     void noteStartWakeLocked(WakeLock wl, WorkSource ws) {
-        try {
-            if (ws != null) {
-                mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag,
-                        wl.monitorType);
-            } else {
-                mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
+        if (wl.monitorType >= 0) {
+            long origId = Binder.clearCallingIdentity();
+            try {
+                if (ws != null) {
+                    mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag,
+                            wl.monitorType);
+                } else {
+                    mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
+                }
+            } catch (RemoteException e) {
+                // Ignore
+            } finally {
+                Binder.restoreCallingIdentity(origId);
             }
-        } catch (RemoteException e) {
-            // Ignore
         }
     }
 
     void noteStopWakeLocked(WakeLock wl, WorkSource ws) {
-        try {
-            if (ws != null) {
-                mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag,
-                        wl.monitorType);
-            } else {
-                mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
+        if (wl.monitorType >= 0) {
+            long origId = Binder.clearCallingIdentity();
+            try {
+                if (ws != null) {
+                    mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag,
+                            wl.monitorType);
+                } else {
+                    mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
+                }
+            } catch (RemoteException e) {
+                // Ignore
+            } finally {
+                Binder.restoreCallingIdentity(origId);
             }
-        } catch (RemoteException e) {
-            // Ignore
         }
     }
 
@@ -813,21 +823,16 @@
         if (ws != null) {
             enforceWakeSourcePermission(uid, pid);
         }
-        long ident = Binder.clearCallingIdentity();
-        try {
-            synchronized (mLocks) {
-                int index = mLocks.getIndex(lock);
-                if (index < 0) {
-                    throw new IllegalArgumentException("Wake lock not active");
-                }
-                WakeLock wl = mLocks.get(index);
-                WorkSource oldsource = wl.ws;
-                wl.ws = ws != null ? new WorkSource(ws) : null;
-                noteStopWakeLocked(wl, oldsource);
-                noteStartWakeLocked(wl, ws);
+        synchronized (mLocks) {
+            int index = mLocks.getIndex(lock);
+            if (index < 0) {
+                throw new IllegalArgumentException("Wake lock not active");
             }
-        } finally {
-            Binder.restoreCallingIdentity(ident);
+            WakeLock wl = mLocks.get(index);
+            WorkSource oldsource = wl.ws;
+            wl.ws = ws != null ? new WorkSource(ws) : null;
+            noteStopWakeLocked(wl, oldsource);
+            noteStartWakeLocked(wl, ws);
         }
     }
 
@@ -884,14 +889,7 @@
         // Unlink the lock from the binder.
         wl.binder.unlinkToDeath(wl, 0);
 
-        if (wl.monitorType >= 0) {
-            long origId = Binder.clearCallingIdentity();
-            try {
-                noteStopWakeLocked(wl, wl.ws);
-            } finally {
-                Binder.restoreCallingIdentity(origId);
-            }
-        }
+        noteStopWakeLocked(wl, wl.ws);
     }
 
     private class PokeLock implements IBinder.DeathRecipient