Add a flag to enable/disable the cache quota calc.

Bug: 34770259
Test: ServicesTests
Change-Id: I74155203c4802b1a3b89117859002bf40b1ca435
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index ebc5b88..6d4b443 100755
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -10200,6 +10200,13 @@
          * @hide
          */
         public static final String ENABLE_DISKSTATS_LOGGING = "enable_diskstats_logging";
+
+        /**
+         * Whether the cache quota calculation task is enabled/disabled.
+         * @hide
+         */
+        public static final String ENABLE_CACHE_QUOTA_CALCULATION =
+                "enable_cache_quota_calculation";
     }
 
     /**
diff --git a/core/tests/coretests/src/android/provider/SettingsBackupTest.java b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
index 48435bd..74bf07c 100644
--- a/core/tests/coretests/src/android/provider/SettingsBackupTest.java
+++ b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
@@ -179,6 +179,7 @@
                     Settings.Global.DROPBOX_TAG_PREFIX,
                     Settings.Global.EMERGENCY_AFFORDANCE_NEEDED,
                     Settings.Global.ENABLE_ACCESSIBILITY_GLOBAL_GESTURE_ENABLED,
+                    Settings.Global.ENABLE_CACHE_QUOTA_CALCULATION,
                     Settings.Global.ENABLE_CELLULAR_ON_BOOT,
                     Settings.Global.ENABLE_DISKSTATS_LOGGING,
                     Settings.Global.ENABLE_EPHEMERAL_FEATURE,
diff --git a/services/tests/servicestests/src/com/android/server/usage/StorageStatsServiceTest.java b/services/tests/servicestests/src/com/android/server/usage/StorageStatsServiceTest.java
new file mode 100644
index 0000000..baee865
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/usage/StorageStatsServiceTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.usage;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.provider.Settings;
+import android.test.AndroidTestCase;
+import android.test.mock.MockContentResolver;
+
+import com.android.internal.util.test.FakeSettingsProvider;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class StorageStatsServiceTest extends AndroidTestCase {
+    private MockContentResolver mContentResolver;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        mContentResolver = new MockContentResolver();
+        mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
+    }
+
+    @Test
+    public void testDontRunWhenDisabledFromSettingsGlobal() throws Exception {
+        Settings.Global.putInt(mContentResolver, Settings.Global.ENABLE_CACHE_QUOTA_CALCULATION, 0);
+
+        assertThat(StorageStatsService.isCacheQuotaCalculationsEnabled(mContentResolver)).isFalse();
+    }
+
+    @Test
+    public void testCalculationTaskIsEnabledByDefault() throws Exception {
+        // Put null to act as though there is no value here.
+        Settings.Global.putString(
+                mContentResolver, Settings.Global.ENABLE_CACHE_QUOTA_CALCULATION, null);
+
+        assertThat(StorageStatsService.isCacheQuotaCalculationsEnabled(mContentResolver)).isTrue();
+    }
+}
diff --git a/services/usage/java/com/android/server/usage/StorageStatsService.java b/services/usage/java/com/android/server/usage/StorageStatsService.java
index a44860e..632c045 100644
--- a/services/usage/java/com/android/server/usage/StorageStatsService.java
+++ b/services/usage/java/com/android/server/usage/StorageStatsService.java
@@ -21,6 +21,7 @@
 import android.app.usage.IStorageStatsManager;
 import android.app.usage.StorageStats;
 import android.app.usage.UsageStatsManagerInternal;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
@@ -39,9 +40,11 @@
 import android.os.storage.StorageEventListener;
 import android.os.storage.StorageManager;
 import android.os.storage.VolumeInfo;
+import android.provider.Settings;
 import android.text.format.DateUtils;
 import android.util.Slog;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.Preconditions;
 import com.android.server.IoThread;
@@ -328,6 +331,11 @@
             if (DEBUG) {
                 Slog.v(TAG, ">>> handling " + msg.what);
             }
+
+            if (!isCacheQuotaCalculationsEnabled(mContext.getContentResolver())) {
+                return;
+            }
+
             switch (msg.what) {
                 case MSG_CHECK_STORAGE_DELTA: {
                     long bytesDelta = Math.abs(mPreviousBytes - mStats.getFreeBytes());
@@ -359,4 +367,10 @@
             strategy.recalculateQuotas();
         }
     }
+
+    @VisibleForTesting
+    static boolean isCacheQuotaCalculationsEnabled(ContentResolver resolver) {
+        return Settings.Global.getInt(
+                resolver, Settings.Global.ENABLE_CACHE_QUOTA_CALCULATION, 1) != 0;
+    }
 }