Add a flag to enable/disable LooperStats collection

Test: Manually tested

Change-Id: I9cd4c819297ea34ab59c3cf9cfd11136987acc22
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 942a700..e007398 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -13320,6 +13320,19 @@
         public static final String BINDER_CALLS_STATS = "binder_calls_stats";
 
         /**
+         * Looper stats settings.
+         *
+         * The following strings are supported as keys:
+         * <pre>
+         *     enabled              (boolean)
+         *     sampling_interval    (int)
+         * </pre>
+         *
+         * @hide
+         */
+        public static final String LOOPER_STATS = "looper_stats";
+
+        /**
          * Default user id to boot into. They map to user ids, for example, 10, 11, 12.
          *
          * @hide
diff --git a/core/java/com/android/internal/os/LooperStats.java b/core/java/com/android/internal/os/LooperStats.java
index edf475f..5b8224e 100644
--- a/core/java/com/android/internal/os/LooperStats.java
+++ b/core/java/com/android/internal/os/LooperStats.java
@@ -37,8 +37,6 @@
  */
 public class LooperStats implements Looper.Observer {
     private static final int TOKEN_POOL_SIZE = 50;
-    private static final int DEFAULT_ENTRIES_SIZE_CAP = 2000;
-    private static final int DEFAULT_SAMPLING_INTERVAL = 100;
 
     @GuardedBy("mLock")
     private final SparseArray<Entry> mEntries = new SparseArray<>(256);
@@ -47,12 +45,8 @@
     private final Entry mHashCollisionEntry = new Entry("HASH_COLLISION");
     private final ConcurrentLinkedQueue<DispatchSession> mSessionPool =
             new ConcurrentLinkedQueue<>();
-    private final int mSamplingInterval;
     private final int mEntriesSizeCap;
-
-    public LooperStats() {
-        this(DEFAULT_SAMPLING_INTERVAL, DEFAULT_ENTRIES_SIZE_CAP);
-    }
+    private int mSamplingInterval;
 
     public LooperStats(int samplingInterval, int entriesSizeCap) {
         this.mSamplingInterval = samplingInterval;
@@ -142,6 +136,10 @@
         }
     }
 
+    public void setSamplingInterval(int samplingInterval) {
+        mSamplingInterval = samplingInterval;
+    }
+
     @NonNull
     private Entry getOrCreateEntry(Message msg) {
         final int id = Entry.idFor(msg);
diff --git a/core/tests/coretests/src/android/provider/SettingsBackupTest.java b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
index 517cf4d..e84aed1 100644
--- a/core/tests/coretests/src/android/provider/SettingsBackupTest.java
+++ b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
@@ -282,6 +282,7 @@
                     Settings.Global.LOCATION_GLOBAL_KILL_SWITCH,
                     Settings.Global.LOCATION_SETTINGS_LINK_TO_PERMISSIONS_ENABLED,
                     Settings.Global.LOCK_SOUND,
+                    Settings.Global.LOOPER_STATS,
                     Settings.Global.LOW_BATTERY_SOUND,
                     Settings.Global.LOW_BATTERY_SOUND_TIMEOUT,
                     Settings.Global.LOW_POWER_MODE,