BroadcastQueue: temporarily unfreeze for parallel broadcasts

Ensure that recipients of broadcast messages and replies are not
frozen to avoid saturation of their binder async queue.

Bug: 172001574
Test: booted device, unplugged the usb charger, verified that some
frozen processes were unfrozen

Change-Id: I1f359103984b115a91fb9134e912b80d6acbaecf
Merged-In: I1f359103984b115a91fb9134e912b80d6acbaecf
diff --git a/services/core/java/com/android/server/am/BroadcastQueue.java b/services/core/java/com/android/server/am/BroadcastQueue.java
index a2eea13..9a6f130 100644
--- a/services/core/java/com/android/server/am/BroadcastQueue.java
+++ b/services/core/java/com/android/server/am/BroadcastQueue.java
@@ -791,7 +791,10 @@
                 mService.updateOomAdjLocked(r.curApp, true,
                         OomAdjuster.OOM_ADJ_REASON_START_RECEIVER);
             }
+        } else if (filter.receiverList.app != null) {
+            mService.mOomAdjuster.mCachedAppOptimizer.unfreezeTemporarily(filter.receiverList.app);
         }
+
         try {
             if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
                     "Delivering to " + filter + " : " + r);
@@ -1121,6 +1124,10 @@
                         }
                     }
                     if (sendResult) {
+                        if (r.callerApp != null) {
+                            mService.mOomAdjuster.mCachedAppOptimizer.unfreezeTemporarily(
+                                    r.callerApp);
+                        }
                         try {
                             if (DEBUG_BROADCAST) {
                                 Slog.i(TAG_BROADCAST, "Finishing broadcast [" + mQueueName + "] "
diff --git a/services/core/java/com/android/server/am/CachedAppOptimizer.java b/services/core/java/com/android/server/am/CachedAppOptimizer.java
index cd0d5b4..36d4a38 100644
--- a/services/core/java/com/android/server/am/CachedAppOptimizer.java
+++ b/services/core/java/com/android/server/am/CachedAppOptimizer.java
@@ -720,6 +720,16 @@
         }
     }
 
+    // This will ensure app will be out of the freezer for at least FREEZE_TIMEOUT_MS
+    void unfreezeTemporarily(ProcessRecord app) {
+        synchronized (mAm) {
+            if (app.frozen) {
+                unfreezeAppLocked(app);
+                freezeAppAsync(app);
+            }
+        }
+    }
+
     @GuardedBy("mAm")
     void freezeAppAsync(ProcessRecord app) {
         mFreezeHandler.removeMessages(SET_FROZEN_PROCESS_MSG, app);