Merge "Revert "Handle multiple isolated uid to host uid mapping""
diff --git a/statsd/src/StatsLogProcessor.cpp b/statsd/src/StatsLogProcessor.cpp
index 8387a3a..7571ba8 100644
--- a/statsd/src/StatsLogProcessor.cpp
+++ b/statsd/src/StatsLogProcessor.cpp
@@ -149,7 +149,12 @@
             }
         }
     } else {
-        mapIsolatedUidsToHostUidInLogEvent(mUidMap, *event);
+        int uidFieldIndex = event->getUidFieldIndex();
+        if (uidFieldIndex != -1) {
+           Value& value = (*event->getMutableValues())[uidFieldIndex].mValue;
+           const int hostUid = mUidMap->getHostUidOrSelf(value.int_value);
+           value.setInt(hostUid);
+        }
     }
 }
 
diff --git a/statsd/src/external/puller_util.cpp b/statsd/src/external/puller_util.cpp
index dd47645..aa99d00 100644
--- a/statsd/src/external/puller_util.cpp
+++ b/statsd/src/external/puller_util.cpp
@@ -19,8 +19,6 @@
 
 #include "puller_util.h"
 
-#include "stats_log_util.h"
-
 namespace android {
 namespace os {
 namespace statsd {
@@ -54,9 +52,9 @@
     // tagId have them or none of them do.
     std::pair<int, int> attrIndexRange;
     const bool hasAttributionChain = data[0]->hasAttributionChain(&attrIndexRange);
-    const uint8_t numUidFields = data[0]->getNumUidFields();
+    bool hasUidField = (data[0]->getUidFieldIndex() != -1);
 
-    if (!hasAttributionChain && numUidFields == 0) {
+    if (!hasAttributionChain && !hasUidField) {
         VLOG("No uid or attribution chain to merge, atom %d", tagId);
         return;
     }
@@ -77,7 +75,14 @@
                 }
             }
         } else {
-            mapIsolatedUidsToHostUidInLogEvent(uidMap, *event);
+            int uidFieldIndex = event->getUidFieldIndex();
+            if (uidFieldIndex != -1) {
+                Value& value = (*event->getMutableValues())[uidFieldIndex].mValue;
+                const int hostUid = uidMap->getHostUidOrSelf(value.int_value);
+                value.setInt(hostUid);
+            } else {
+                ALOGE("Malformed log, uid not found. %s", event->ToString().c_str());
+            }
         }
     }
 
diff --git a/statsd/src/logd/LogEvent.cpp b/statsd/src/logd/LogEvent.cpp
index 85a457c..f56fa62 100644
--- a/statsd/src/logd/LogEvent.cpp
+++ b/statsd/src/logd/LogEvent.cpp
@@ -240,9 +240,7 @@
     }
 
     bool isUid = readNextValue<uint8_t>();
-    if (isUid) {
-        mNumUidFields++;
-    }
+    if (isUid) mUidFieldIndex = static_cast<int8_t>(mValues.size() - 1);
     mValues[mValues.size() - 1].mAnnotations.setUidField(isUid);
 }
 
diff --git a/statsd/src/logd/LogEvent.h b/statsd/src/logd/LogEvent.h
index 20d2848..a5f2460 100644
--- a/statsd/src/logd/LogEvent.h
+++ b/statsd/src/logd/LogEvent.h
@@ -149,8 +149,18 @@
         return mTruncateTimestamp;
     }
 
-    inline uint8_t getNumUidFields() const {
-        return mNumUidFields;
+    // Returns the index of the uid field within the FieldValues vector if the
+    // uid exists. If there is no uid field, returns -1.
+    //
+    // If the index within the atom definition is desired, do the following:
+    //    int vectorIndex = LogEvent.getUidFieldIndex();
+    //    if (vectorIndex != -1) {
+    //        FieldValue& v = LogEvent.getValues()[vectorIndex];
+    //        int atomIndex = v.mField.getPosAtDepth(0);
+    //    }
+    // Note that atomIndex is 1-indexed.
+    inline int getUidFieldIndex() {
+        return static_cast<int>(mUidFieldIndex);
     }
 
     // Returns whether this LogEvent has an AttributionChain.
@@ -306,7 +316,9 @@
     bool mTruncateTimestamp = false;
     int mResetState = -1;
 
-    uint8_t mNumUidFields = 0;
+    // Indexes within the FieldValue vector can be stored in 7 bits because
+    // that's the assumption enforced by the encoding used in FieldValue.
+    int8_t mUidFieldIndex = -1;
     int8_t mAttributionChainStartIndex = -1;
     int8_t mAttributionChainEndIndex = -1;
     int8_t mExclusiveStateFieldIndex = -1;
diff --git a/statsd/src/stats_log_util.cpp b/statsd/src/stats_log_util.cpp
index 448448f..1f5dd01 100644
--- a/statsd/src/stats_log_util.cpp
+++ b/statsd/src/stats_log_util.cpp
@@ -611,19 +611,6 @@
     return success;
 }
 
-void mapIsolatedUidsToHostUidInLogEvent(const sp<UidMap> uidMap, LogEvent& event) {
-    uint8_t remainingUidCount = event.getNumUidFields();
-    vector<FieldValue>* fieldValues = event.getMutableValues();
-    auto it = fieldValues->begin();
-    while (it != fieldValues->end() && remainingUidCount > 0) {
-        if (isUidField(*it)) {
-            it->mValue.setInt(uidMap->getHostUidOrSelf(it->mValue.int_value));
-            remainingUidCount--;
-        }
-        ++it;
-    }
-}
-
 }  // namespace statsd
 }  // namespace os
 }  // namespace android
diff --git a/statsd/src/stats_log_util.h b/statsd/src/stats_log_util.h
index f6cd4be..8034e88 100644
--- a/statsd/src/stats_log_util.h
+++ b/statsd/src/stats_log_util.h
@@ -20,10 +20,9 @@
 
 #include "FieldValue.h"
 #include "HashableDimensionKey.h"
+#include "src/statsd_config.pb.h"
 #include "guardrail/StatsdStats.h"
 #include "logd/LogEvent.h"
-#include "packages/UidMap.h"
-#include "src/statsd_config.pb.h"
 
 using android::util::ProtoOutputStream;
 
@@ -117,8 +116,6 @@
     return atomId >= StatsdStats::kPullAtomStartTag && atomId < StatsdStats::kVendorAtomStartTag;
 }
 
-void mapIsolatedUidsToHostUidInLogEvent(const sp<UidMap> uidMap, LogEvent& event);
-
 }  // namespace statsd
 }  // namespace os
 }  // namespace android
diff --git a/statsd/tests/LogEvent_test.cpp b/statsd/tests/LogEvent_test.cpp
index d524904..bde59f4 100644
--- a/statsd/tests/LogEvent_test.cpp
+++ b/statsd/tests/LogEvent_test.cpp
@@ -289,11 +289,9 @@
     LogEvent event(/*uid=*/0, /*pid=*/0);
     createIntWithBoolAnnotationLogEvent(&event, ANNOTATION_ID_IS_UID, true);
 
-    ASSERT_EQ(event.getNumUidFields(), 1);
-
     const vector<FieldValue>& values = event.getValues();
     ASSERT_EQ(values.size(), 1);
-    EXPECT_TRUE(isUidField(values.at(0)));
+    EXPECT_EQ(event.getUidFieldIndex(), 0);
 }
 
 TEST(LogEventTest, TestAnnotationIdStateNested) {