Make sure to call the original reply-to receiver when...

replacing a queued broadcast.

- Also don't replace a broadcast for a different user.

Test: Manual test with the following test code:

Intent intent = new Intent(Intent.ACTION_PROVIDER_CHANGED)
        .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
AlarmManager alm = this.getSystemService(AlarmManager.class);
long time = SystemClock.elapsedRealtime() + 5 * 1000;
for (int i = 0; i < 5; i++) {
    alm.setExact(AlarmManager.ELAPSED_REALTIME, time,
            PendingIntent.getBroadcast(this, i, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}

Without this CL, after the alarm fires, AlarmManagerService.mBroadcastRefCount
is left > 0 and the wake lock is held forever.

With this CL, mBroadcastRefCount eventually gets back to 0.

Bug: 35779096
Change-Id: I4e21c94b08f25f9ca1242182670ff4a69f8bd9f2
diff --git a/services/core/java/com/android/server/AlarmManagerService.java b/services/core/java/com/android/server/AlarmManagerService.java
index 5ffb738..0d438cb 100644
--- a/services/core/java/com/android/server/AlarmManagerService.java
+++ b/services/core/java/com/android/server/AlarmManagerService.java
@@ -99,6 +99,7 @@
     static final boolean DEBUG_VALIDATE = localLOGV || false;
     static final boolean DEBUG_ALARM_CLOCK = localLOGV || false;
     static final boolean DEBUG_LISTENER_CALLBACK = localLOGV || false;
+    static final boolean DEBUG_WAKELOCK = localLOGV || false;
     static final boolean RECORD_ALARMS_IN_HISTORY = true;
     static final boolean RECORD_DEVICE_IDLE_ALARMS = false;
     static final int ALARM_EVENT = 1;
@@ -2934,6 +2935,9 @@
                 updateStatsLocked(inflight);
             }
             mBroadcastRefCount--;
+            if (DEBUG_WAKELOCK) {
+                Slog.d(TAG, "mBroadcastRefCount -> " + mBroadcastRefCount);
+            }
             if (mBroadcastRefCount == 0) {
                 mHandler.obtainMessage(AlarmHandler.REPORT_ALARMS_ACTIVE, 0).sendToTarget();
                 mWakeLock.release();
@@ -3074,6 +3078,9 @@
             }
 
             // The alarm is now in flight; now arrange wakelock and stats tracking
+            if (DEBUG_WAKELOCK) {
+                Slog.d(TAG, "mBroadcastRefCount -> " + (mBroadcastRefCount + 1));
+            }
             if (mBroadcastRefCount == 0) {
                 setWakelockWorkSource(alarm.operation, alarm.workSource,
                         alarm.type, alarm.statsTag, (alarm.operation == null) ? alarm.uid : -1,