MediaMetrics: Add const correctness for items in service

Allows multithreaded use of items without lock.

Test: mediametrics dumpsys, atest mediametrics_tests
Bug: 138583596
Change-Id: Ieb901076b9acc33a89737b320a4fc8ce82f2608d
diff --git a/services/mediaanalytics/MediaAnalyticsService.cpp b/services/mediaanalytics/MediaAnalyticsService.cpp
index 33a5a90..f1f4761 100644
--- a/services/mediaanalytics/MediaAnalyticsService.cpp
+++ b/services/mediaanalytics/MediaAnalyticsService.cpp
@@ -140,12 +140,12 @@
         return BAD_VALUE;
     }
 
-    // send to statsd
-    extern bool dump2Statsd(MediaAnalyticsItem *item);  // extern hook
-    (void)dump2Statsd(item);  // failure should be logged in function.
+    // now attach either the item or its dup to a const shared pointer
+    std::shared_ptr<const MediaAnalyticsItem> sitem(release ? item : item->dup());
 
-    if (!release) item = item->dup();
-    saveItem(item);
+    extern bool dump2Statsd(const std::shared_ptr<const MediaAnalyticsItem>& item);
+    (void)dump2Statsd(sitem);  // failure should be logged in function.
+    saveItem(sitem);
     return NO_ERROR;
 }
 
@@ -325,7 +325,7 @@
 
 // if item != NULL, it's the item we just inserted
 // true == more items eligible to be recovered
-bool MediaAnalyticsService::expirations_l(MediaAnalyticsItem *item)
+bool MediaAnalyticsService::expirations_l(const std::shared_ptr<const MediaAnalyticsItem>& item)
 {
     bool more = false;
 
@@ -348,7 +348,7 @@
         for (; i < mItems.size(); ++i) {
             auto &oitem = mItems[i];
             nsecs_t when = oitem->getTimestamp();
-            if (oitem.get() == item) {
+            if (oitem.get() == item.get()) {
                 break;
             }
             if (now > when && (now - when) <= mMaxRecordAgeNs) {
@@ -384,7 +384,7 @@
     } while (more);
 }
 
-void MediaAnalyticsService::saveItem(MediaAnalyticsItem *item)
+void MediaAnalyticsService::saveItem(const std::shared_ptr<const MediaAnalyticsItem>& item)
 {
     std::lock_guard _l(mLock);
     // we assume the items are roughly in time order.