Better recording wake lock accounting

Count wake lock use against the app that initiated the recording,
not against the media server.

b/10985160

Change-Id: Iae6e6c030b7f1c6081d9c79725d6990f60dadaaa
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 2d9d485..c31babc 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -476,13 +476,13 @@
     }
 }
 
-void AudioFlinger::ThreadBase::acquireWakeLock()
+void AudioFlinger::ThreadBase::acquireWakeLock(int uid)
 {
     Mutex::Autolock _l(mLock);
-    acquireWakeLock_l();
+    acquireWakeLock_l(uid);
 }
 
-void AudioFlinger::ThreadBase::acquireWakeLock_l()
+void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid)
 {
     if (mPowerManager == 0) {
         // use checkService() to avoid blocking if power service is not up yet
@@ -497,10 +497,19 @@
     }
     if (mPowerManager != 0) {
         sp<IBinder> binder = new BBinder();
-        status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
-                                                         binder,
-                                                         String16(mName),
-                                                         String16("media"));
+        status_t status;
+        if (uid >= 0) {
+            mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK,
+                    binder,
+                    String16(mName),
+                    String16("media"),
+                    uid);
+        } else {
+            mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
+                    binder,
+                    String16(mName),
+                    String16("media"));
+        }
         if (status == NO_ERROR) {
             mWakeLockToken = binder;
         }
@@ -4274,7 +4283,7 @@
     snprintf(mName, kNameLength, "AudioIn_%X", id);
 
     readInputParameters();
-
+    mClientUid = IPCThreadState::self()->getCallingUid();
 }
 
 
@@ -4306,7 +4315,7 @@
     nsecs_t lastWarning = 0;
 
     inputStandBy();
-    acquireWakeLock();
+    acquireWakeLock(mClientUid);
 
     // used to verify we've read at least once before evaluating how many bytes were read
     bool readOnce = false;
@@ -4331,7 +4340,7 @@
                 // go to sleep
                 mWaitWorkCV.wait(mLock);
                 ALOGV("RecordThread: loop starting");
-                acquireWakeLock_l();
+                acquireWakeLock_l(mClientUid);
                 continue;
             }
             if (mActiveTrack != 0) {
@@ -4553,7 +4562,6 @@
         ALOGE("Audio driver not initialized.");
         goto Exit;
     }
-
     // client expresses a preference for FAST, but we get the final say
     if (*flags & IAudioFlinger::TRACK_FAST) {
       if (
diff --git a/services/audioflinger/Threads.h b/services/audioflinger/Threads.h
index 241424f..0cb3ef7 100644
--- a/services/audioflinger/Threads.h
+++ b/services/audioflinger/Threads.h
@@ -237,8 +237,8 @@
                     effect_uuid_t mType;    // effect type UUID
                 };
 
-                void        acquireWakeLock();
-                void        acquireWakeLock_l();
+                void        acquireWakeLock(int uid = -1);
+                void        acquireWakeLock_l(int uid = -1);
                 void        releaseWakeLock();
                 void        releaseWakeLock_l();
                 void setEffectSuspended_l(const effect_uuid_t *type,
@@ -951,4 +951,5 @@
 
             // For dumpsys
             const sp<NBAIO_Sink>                mTeeSink;
+            int                                 mClientUid;
 };