Adding batterystats history to proto dump.

It will only print out a dump for userdebug or eng builds.

This is the same change as the pi-dev one (http://ag/3909863), but due
to changes in master, that change has a merge conflict, so I have to
create a separate CL for master.

Bug: 77727638
Test: it builds
Change-Id: Ib74d4c664f23a61e6fc33f700ba6a3c6fad32c74
diff --git a/cmds/incidentd/src/Reporter.cpp b/cmds/incidentd/src/Reporter.cpp
index 297a071..b3bab0c 100644
--- a/cmds/incidentd/src/Reporter.cpp
+++ b/cmds/incidentd/src/Reporter.cpp
@@ -22,6 +22,7 @@
 #include "report_directory.h"
 #include "section_list.h"
 
+#include <android-base/properties.h>
 #include <android/os/DropBoxManager.h>
 #include <private/android_filesystem_config.h>
 #include <utils/SystemClock.h>
@@ -31,6 +32,7 @@
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <string>
 
 /**
  * The directory where the incident reports are stored.
@@ -129,6 +131,8 @@
     int mainDest = -1;
     HeaderSection headers;
     MetadataSection metadataSection;
+    std::string buildType = android::base::GetProperty("ro.build.type", "");
+    const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng";
 
     // See if we need the main file
     for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
@@ -175,6 +179,11 @@
     // and report to those that care that we're doing it.
     for (const Section** section = SECTION_LIST; *section; section++) {
         const int id = (*section)->id;
+        if ((*section)->userdebugAndEngOnly && !isUserdebugOrEng) {
+            ALOGD("Skipping incident report section %d '%s' because it's limited to userdebug/eng",
+                  id, (*section)->name.string());
+            continue;
+        }
         if (this->batch.containsSection(id)) {
             ALOGD("Taking incident report section %d '%s'", id, (*section)->name.string());
             for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
diff --git a/cmds/incidentd/src/Section.cpp b/cmds/incidentd/src/Section.cpp
index 78876a3..bdfcd98 100644
--- a/cmds/incidentd/src/Section.cpp
+++ b/cmds/incidentd/src/Section.cpp
@@ -151,8 +151,11 @@
 }
 
 // ================================================================================
-Section::Section(int i, int64_t timeoutMs, bool deviceSpecific)
-    : id(i), timeoutMs(timeoutMs), deviceSpecific(deviceSpecific) {}
+Section::Section(int i, int64_t timeoutMs, bool userdebugAndEngOnly, bool deviceSpecific)
+    : id(i),
+      timeoutMs(timeoutMs),
+      userdebugAndEngOnly(userdebugAndEngOnly),
+      deviceSpecific(deviceSpecific) {}
 
 Section::~Section() {}
 
@@ -239,7 +242,7 @@
 
 FileSection::FileSection(int id, const char* filename, const bool deviceSpecific,
                          const int64_t timeoutMs)
-    : Section(id, timeoutMs, deviceSpecific), mFilename(filename) {
+    : Section(id, timeoutMs, false, deviceSpecific), mFilename(filename) {
     name = "file ";
     name += filename;
     mIsSysfs = isSysfs(filename);
@@ -399,8 +402,8 @@
 WorkerThreadData::~WorkerThreadData() {}
 
 // ================================================================================
-WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs)
-    : Section(id, timeoutMs) {}
+WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs, bool userdebugAndEngOnly)
+    : Section(id, timeoutMs, userdebugAndEngOnly) {}
 
 WorkerThreadSection::~WorkerThreadSection() {}
 
@@ -575,8 +578,8 @@
 }
 
 // ================================================================================
-DumpsysSection::DumpsysSection(int id, const char* service, ...)
-    : WorkerThreadSection(id), mService(service) {
+DumpsysSection::DumpsysSection(int id, bool userdebugAndEngOnly, const char* service, ...)
+    : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS, userdebugAndEngOnly), mService(service) {
     name = "dumpsys ";
     name += service;
 
diff --git a/cmds/incidentd/src/Section.h b/cmds/incidentd/src/Section.h
index 577892e..a031a15 100644
--- a/cmds/incidentd/src/Section.h
+++ b/cmds/incidentd/src/Section.h
@@ -40,10 +40,12 @@
 public:
     const int id;
     const int64_t timeoutMs;  // each section must have a timeout
+    const bool userdebugAndEngOnly;
     const bool deviceSpecific;
     String8 name;
 
-    Section(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS, bool deviceSpecific = false);
+    Section(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS, bool userdebugAndEngOnly = false,
+            bool deviceSpecific = false);
     virtual ~Section();
 
     virtual status_t Execute(ReportRequestSet* requests) const = 0;
@@ -107,7 +109,8 @@
  */
 class WorkerThreadSection : public Section {
 public:
-    WorkerThreadSection(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
+    WorkerThreadSection(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS,
+                        bool userdebugAndEngOnly = false);
     virtual ~WorkerThreadSection();
 
     virtual status_t Execute(ReportRequestSet* requests) const;
@@ -137,7 +140,7 @@
  */
 class DumpsysSection : public WorkerThreadSection {
 public:
-    DumpsysSection(int id, const char* service, ...);
+    DumpsysSection(int id, bool userdebugAndEngOnly, const char* service, ...);
     virtual ~DumpsysSection();
 
     virtual status_t BlockingCall(int pipeWriteFd) const;