Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5840f423 authored by Daniel Nishi's avatar Daniel Nishi Committed by Android (Google) Code Review
Browse files

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

parents f764962b 0f703e64
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -10207,6 +10207,13 @@ public final class Settings {
         * @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";
    }

    /**
+1 −0
Original line number Diff line number Diff line
@@ -179,6 +179,7 @@ public class SettingsBackupTest {
                    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,
+55 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}
+14 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.app.usage.ExternalStorageStats;
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.UserManager;
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 @@ public class StorageStatsService extends IStorageStatsManager.Stub {
            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 @@ public class StorageStatsService extends IStorageStatsManager.Stub {
            strategy.recalculateQuotas();
        }
    }

    @VisibleForTesting
    static boolean isCacheQuotaCalculationsEnabled(ContentResolver resolver) {
        return Settings.Global.getInt(
                resolver, Settings.Global.ENABLE_CACHE_QUOTA_CALCULATION, 1) != 0;
    }
}