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

Commit ed6be441 authored by Jing Ji's avatar Jing Ji
Browse files

Move the default configs in AppBatteryTracker into resources

..so it could be overlaid by different device configs.

Bug: 200326767
Bug: 203105544
Test: atest FrameworksMockingServicesTests:BackgroundRestrictionTest
Change-Id: Iffa7ff2b7fb8f14f1a54359d72d936c721d27904
parent e1217fdd
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
@@ -5687,4 +5687,75 @@
     -->
    <string-array name="config_dockExtconStateMapping">
    </string-array>

    <!-- Whether or not the monitoring on the apps' background battery drain is enabled -->
    <bool name="config_bg_current_drain_monitor_enabled">true</bool>

    <!-- The threshold of the background current drain (in percentage) to the restricted
         standby bucket.
    -->
    <array name="config_bg_current_drain_threshold_to_restricted_bucket">
        <item>2.0</item> <!-- regular device -->
        <item>4.0</item> <!-- low ram device -->
    </array>

    <!-- The threshold of the background current drain (in percentage) to the background
         restricted level.
    -->
    <array name="config_bg_current_drain_threshold_to_bg_restricted">
        <item>4.0</item> <!-- regular device -->
        <item>8.0</item> <!-- low ram device -->
    </array>

    <!-- The background current drain monitoring window size. -->
    <integer name="config_bg_current_drain_window">86400</integer>

    <!-- The types of battery drain we're checking on each app; if the sum of the battery drain
        exceeds the threshold, it'll be moved to restricted standby bucket. The value must be
        one of or combination of the definitions in AppBatteryPolicy.
    -->
    <integer name="config_bg_current_drain_types_to_restricted_bucket">4</integer>

    <!-- The types of battery drain we're checking on each app; if the sum of the battery drain
        exceeds the threshold, it'll be moved to background restricted level. The value must be
        one of or combination of the definitions in AppBatteryPolicy.
    -->
    <integer name="config_bg_current_drain_types_to_bg_restricted">12</integer>

    <!-- The power usage components we're monitoring. Must one of the definition in BatteryConsumer.
    -->
    <integer name="config_bg_current_drain_power_components">-1</integer>

    <!-- Whether or not enable the different threshold based on the durations of
         certain event type.
    -->
    <bool name="config_bg_current_drain_event_duration_based_threshold_enabled">false</bool>

    <!-- The threshold of the background current drain (in percentage) to the restricted
         standby bucket for legitimate case with higher background current drain.
    -->
    <array name="config_bg_current_drain_high_threshold_to_restricted_bucket">
        <item>30.0</item> <!-- regular device -->
        <item>60.0</item> <!-- low ram device -->
    </array>

    <!-- The threshold of the background current drain (in percentage) to the background
         restricted level for legitimate case with higher background current drain.
    -->
    <array name="config_bg_current_drain_high_threshold_to_bg_restricted">
        <item>20.0</item> <!-- regular device -->
        <item>40.0</item> <!-- low ram device -->
    </array>

    <!-- The threshold of minimal time of hosting a foreground service with type "mediaPlayback"
         or a media session, over the given window, so it'd subject towards the higher background
         current drain threshold.
    -->
    <integer name="config_bg_current_drain_media_playback_min_duration">1800</integer>

    <!-- The threshold of minimal time of hosting a foreground service with type "location"
         over the given window, so it'd subject towards the higher background
         current drain threshold.
    -->
    <integer name="config_bg_current_drain_location_min_duration">1800</integer>
</resources>
+13 −0
Original line number Diff line number Diff line
@@ -4730,4 +4730,17 @@
  <java-symbol type="integer" name="config_lowPowerStandbyNonInteractiveTimeout" />

  <java-symbol type="color" name="camera_privacy_light"/>

  <java-symbol type="bool" name="config_bg_current_drain_monitor_enabled" />
  <java-symbol type="array" name="config_bg_current_drain_threshold_to_restricted_bucket" />
  <java-symbol type="array" name="config_bg_current_drain_threshold_to_bg_restricted" />
  <java-symbol type="integer" name="config_bg_current_drain_window" />
  <java-symbol type="integer" name="config_bg_current_drain_types_to_restricted_bucket" />
  <java-symbol type="integer" name="config_bg_current_drain_types_to_bg_restricted" />
  <java-symbol type="integer" name="config_bg_current_drain_power_components" />
  <java-symbol type="bool" name="config_bg_current_drain_event_duration_based_threshold_enabled" />
  <java-symbol type="array" name="config_bg_current_drain_high_threshold_to_restricted_bucket" />
  <java-symbol type="array" name="config_bg_current_drain_high_threshold_to_bg_restricted" />
  <java-symbol type="integer" name="config_bg_current_drain_media_playback_min_duration" />
  <java-symbol type="integer" name="config_bg_current_drain_location_min_duration" />
</resources>
+3 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.os.SystemClock;
import android.util.Pair;
import android.util.Slog;

import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.am.AppBatteryExemptionTracker.AppBatteryExemptionPolicy;
import com.android.server.am.AppBatteryExemptionTracker.UidBatteryStates;
@@ -454,7 +455,8 @@ final class AppBatteryExemptionTracker
            super(injector, tracker,
                    KEY_BG_BATTERY_EXEMPTION_ENABLED, DEFAULT_BG_BATTERY_EXEMPTION_ENABLED,
                    AppBatteryPolicy.KEY_BG_CURRENT_DRAIN_WINDOW,
                    AppBatteryPolicy.DEFAULT_BG_CURRENT_DRAIN_WINDOW_MS);
                    tracker.mContext.getResources()
                    .getInteger(R.integer.config_bg_current_drain_window));
        }

        @Override
+86 −48
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import static android.util.TimeUtils.formatTime;
import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
import static com.android.server.am.AppRestrictionController.DEVICE_CONFIG_SUBNAMESPACE_PREFIX;
import static com.android.server.am.BaseAppStateTracker.ONE_DAY;
import static com.android.server.am.BaseAppStateTracker.ONE_MINUTE;

import android.annotation.NonNull;
@@ -47,6 +46,8 @@ import android.annotation.UserIdInt;
import android.app.ActivityManager.RestrictionLevel;
import android.content.Context;
import android.content.pm.ServiceInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.BatteryConsumer;
import android.os.BatteryConsumer.Dimensions;
import android.os.BatteryStatsInternal;
@@ -64,6 +65,7 @@ import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.TimeUtils;

import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.ArrayUtils;
@@ -1024,71 +1026,59 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>
        static final String KEY_BG_CURRENT_DRAIN_POWER_COMPONENTS =
                DEVICE_CONFIG_SUBNAMESPACE_PREFIX + "current_drain_power_components";

        /**
         * Default value to {@link #mTrackerEnabled}.
         */
        static final boolean DEFAULT_BG_CURRENT_DRAIN_MONITOR_ENABLED = true;

        /**
         * Default value to the {@link #INDEX_REGULAR_CURRENT_DRAIN_THRESHOLD} of
         * the {@link #mBgCurrentDrainRestrictedBucketThreshold}.
         */
        static final float DEFAULT_BG_CURRENT_DRAIN_RESTRICTED_BUCKET_THRESHOLD =
                isLowRamDeviceStatic() ? 4.0f : 2.0f;
        final float mDefaultBgCurrentDrainRestrictedBucket;

        /**
         * Default value to the {@link #INDEX_REGULAR_CURRENT_DRAIN_THRESHOLD} of
         * the {@link #mBgCurrentDrainBgRestrictedThreshold}.
         */
        static final float DEFAULT_BG_CURRENT_DRAIN_BG_RESTRICTED_THRESHOLD =
                isLowRamDeviceStatic() ? 8.0f : 4.0f;
        final float mDefaultBgCurrentDrainBgRestrictedThreshold;

        /**
         * Default value to {@link #mBgCurrentDrainWindowMs}.
         */
        static final long DEFAULT_BG_CURRENT_DRAIN_WINDOW_MS = ONE_DAY;
        final long mDefaultBgCurrentDrainWindowMs;

        /**
         * Default value to the {@link #INDEX_HIGH_CURRENT_DRAIN_THRESHOLD} of
         * the {@link #mBgCurrentDrainRestrictedBucketThreshold}.
         */
        static final float DEFAULT_BG_CURRENT_DRAIN_RESTRICTED_BUCKET_HIGH_THRESHOLD =
                isLowRamDeviceStatic() ? 60.0f : 30.0f;
        final float mDefaultBgCurrentDrainRestrictedBucketHighThreshold;

        /**
         * Default value to the {@link #INDEX_HIGH_CURRENT_DRAIN_THRESHOLD} of
         * the {@link #mBgCurrentDrainBgRestrictedThreshold}.
         */
        static final float DEFAULT_BG_CURRENT_DRAIN_BG_RESTRICTED_HIGH_THRESHOLD =
                isLowRamDeviceStatic() ? 60.0f : 30.0f;
        final float mDefaultBgCurrentDrainBgRestrictedHighThreshold;

        /**
         * Default value to {@link #mBgCurrentDrainMediaPlaybackMinDuration}.
         */
        static final long DEFAULT_BG_CURRENT_DRAIN_MEDIA_PLAYBACK_MIN_DURATION = 30 * ONE_MINUTE;
        final long mDefaultBgCurrentDrainMediaPlaybackMinDuration;

        /**
         * Default value to {@link #mBgCurrentDrainLocationMinDuration}.
         */
        static final long DEFAULT_BG_CURRENT_DRAIN_LOCATION_MIN_DURATION = 30 * ONE_MINUTE;
        final long mDefaultBgCurrentDrainLocationMinDuration;

        /**
         * Default value to {@link #mBgCurrentDrainEventDurationBasedThresholdEnabled}.
         */
        static final boolean DEFAULT_BG_CURRENT_DRAIN_EVENT_DURATION_BASED_THRESHOLD_ENABLED =
                false;
        final boolean mDefaultBgCurrentDrainEventDurationBasedThresholdEnabled;

        /**
         * Default value to {@link #mBgCurrentDrainRestrictedBucketTypes}.
         */
        static final int DEFAULT_BG_CURRENT_DRAIN_TYPES_TO_RESTRICTED_BUCKET =
                BATTERY_USAGE_TYPE_BACKGROUND;
        final int mDefaultCurrentDrainTypesToRestrictedBucket;

        /**
         * Default value to {@link #mBgCurrentDrainBgRestrictedTypes}.
         */
        static final int DEFAULT_BG_CURRENT_DRAIN_TYPES_TO_BG_RESTRICTED =
                BATTERY_USAGE_TYPE_BACKGROUND | BATTERY_USAGE_TYPE_FOREGROUND_SERVICE;
        final int mDefaultBgCurrentDrainTypesToBgRestricted;

        /**
         * Default value to {@link #mBgCurrentDrainPowerComponents}.
@@ -1096,6 +1086,8 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>
        @BatteryConsumer.PowerComponent
        static final int DEFAULT_BG_CURRENT_DRAIN_POWER_COMPONENTS = POWER_COMPONENT_ANY;

        final int mDefaultBgCurrentDrainPowerComponent;

        /**
         * The index to {@link #mBgCurrentDrainRestrictedBucketThreshold}
         * and {@link #mBgCurrentDrainBgRestrictedThreshold}.
@@ -1107,36 +1099,28 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>
         * @see #KEY_BG_CURRENT_DRAIN_THRESHOLD_TO_RESTRICTED_BUCKET.
         * @see #KEY_BG_CURRENT_DRAIN_HIGH_THRESHOLD_TO_RESTRICTED_BUCKET.
         */
        volatile float[] mBgCurrentDrainRestrictedBucketThreshold = {
                DEFAULT_BG_CURRENT_DRAIN_RESTRICTED_BUCKET_THRESHOLD,
                DEFAULT_BG_CURRENT_DRAIN_BG_RESTRICTED_HIGH_THRESHOLD,
        };
        volatile float[] mBgCurrentDrainRestrictedBucketThreshold = new float[2];

        /**
         * @see #KEY_BG_CURRENT_DRAIN_THRESHOLD_TO_BG_RESTRICTED.
         * @see #KEY_BG_CURRENT_DRAIN_HIGH_THRESHOLD_TO_BG_RESTRICTED.
         */
        volatile float[] mBgCurrentDrainBgRestrictedThreshold = {
                DEFAULT_BG_CURRENT_DRAIN_BG_RESTRICTED_THRESHOLD,
                DEFAULT_BG_CURRENT_DRAIN_BG_RESTRICTED_HIGH_THRESHOLD,
        };
        volatile float[] mBgCurrentDrainBgRestrictedThreshold = new float[2];

        /**
         * @see #KEY_BG_CURRENT_DRAIN_WINDOW.
         */
        volatile long mBgCurrentDrainWindowMs = DEFAULT_BG_CURRENT_DRAIN_WINDOW_MS;
        volatile long mBgCurrentDrainWindowMs;

        /**
         * @see #KEY_BG_CURRENT_DRAIN_MEDIA_PLAYBACK_MIN_DURATION.
         */
        volatile long mBgCurrentDrainMediaPlaybackMinDuration =
                DEFAULT_BG_CURRENT_DRAIN_MEDIA_PLAYBACK_MIN_DURATION;
        volatile long mBgCurrentDrainMediaPlaybackMinDuration;

        /**
         * @see #KEY_BG_CURRENT_DRAIN_LOCATION_MIN_DURATION.
         */
        volatile long mBgCurrentDrainLocationMinDuration =
                DEFAULT_BG_CURRENT_DRAIN_LOCATION_MIN_DURATION;
        volatile long mBgCurrentDrainLocationMinDuration;

        /**
         * @see #KEY_BG_CURRENT_DRAIN_EVENT_DURATION_BASED_THRESHOLD_ENABLED.
@@ -1183,8 +1167,62 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>

        AppBatteryPolicy(@NonNull Injector injector, @NonNull AppBatteryTracker tracker) {
            super(injector, tracker, KEY_BG_CURRENT_DRAIN_MONITOR_ENABLED,
                    DEFAULT_BG_CURRENT_DRAIN_MONITOR_ENABLED);
                    tracker.mContext.getResources()
                    .getBoolean(R.bool.config_bg_current_drain_monitor_enabled));
            mLock = tracker.mLock;
            final Resources resources = tracker.mContext.getResources();
            float[] val = getFloatArray(resources.obtainTypedArray(
                    R.array.config_bg_current_drain_threshold_to_restricted_bucket));
            mDefaultBgCurrentDrainRestrictedBucket =
                    isLowRamDeviceStatic() ? val[1] : val[0];
            val = getFloatArray(resources.obtainTypedArray(
                    R.array.config_bg_current_drain_threshold_to_bg_restricted));
            mDefaultBgCurrentDrainBgRestrictedThreshold =
                    isLowRamDeviceStatic() ? val[1] : val[0];
            mDefaultBgCurrentDrainWindowMs = resources.getInteger(
                    R.integer.config_bg_current_drain_window);
            val = getFloatArray(resources.obtainTypedArray(
                    R.array.config_bg_current_drain_high_threshold_to_restricted_bucket));
            mDefaultBgCurrentDrainRestrictedBucketHighThreshold =
                    isLowRamDeviceStatic() ? val[1] : val[0];
            val = getFloatArray(resources.obtainTypedArray(
                    R.array.config_bg_current_drain_high_threshold_to_bg_restricted));
            mDefaultBgCurrentDrainBgRestrictedHighThreshold =
                    isLowRamDeviceStatic() ? val[1] : val[0];
            mDefaultBgCurrentDrainMediaPlaybackMinDuration = resources.getInteger(
                    R.integer.config_bg_current_drain_media_playback_min_duration);
            mDefaultBgCurrentDrainLocationMinDuration = resources.getInteger(
                    R.integer.config_bg_current_drain_location_min_duration);
            mDefaultBgCurrentDrainEventDurationBasedThresholdEnabled = resources.getBoolean(
                    R.bool.config_bg_current_drain_event_duration_based_threshold_enabled);
            mDefaultCurrentDrainTypesToRestrictedBucket = resources.getInteger(
                    R.integer.config_bg_current_drain_types_to_restricted_bucket);
            mDefaultBgCurrentDrainTypesToBgRestricted = resources.getInteger(
                    R.integer.config_bg_current_drain_types_to_bg_restricted);
            mDefaultBgCurrentDrainPowerComponent = resources.getInteger(
                    R.integer.config_bg_current_drain_power_components);
            mBgCurrentDrainRestrictedBucketThreshold[0] =
                    mDefaultBgCurrentDrainRestrictedBucket;
            mBgCurrentDrainRestrictedBucketThreshold[1] =
                    mDefaultBgCurrentDrainRestrictedBucketHighThreshold;
            mBgCurrentDrainBgRestrictedThreshold[0] =
                    mDefaultBgCurrentDrainBgRestrictedThreshold;
            mBgCurrentDrainBgRestrictedThreshold[1] =
                    mDefaultBgCurrentDrainBgRestrictedHighThreshold;
            mBgCurrentDrainWindowMs = mDefaultBgCurrentDrainWindowMs;
            mBgCurrentDrainMediaPlaybackMinDuration =
                    mDefaultBgCurrentDrainMediaPlaybackMinDuration;
            mBgCurrentDrainLocationMinDuration = mDefaultBgCurrentDrainLocationMinDuration;
        }

        static float[] getFloatArray(TypedArray array) {
            int length = array.length();
            float[] floatArray = new float[length];
            for (int i = 0; i < length; i++) {
                floatArray[i] = array.getFloat(i, Float.NaN);
            }
            array.recycle();
            return floatArray;
        }

        @Override
@@ -1234,31 +1272,31 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>
            mBgCurrentDrainRestrictedBucketThreshold[INDEX_REGULAR_CURRENT_DRAIN_THRESHOLD] =
                    DeviceConfig.getFloat(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_THRESHOLD_TO_RESTRICTED_BUCKET,
                    DEFAULT_BG_CURRENT_DRAIN_RESTRICTED_BUCKET_THRESHOLD);
                    mDefaultBgCurrentDrainRestrictedBucket);
            mBgCurrentDrainRestrictedBucketThreshold[INDEX_HIGH_CURRENT_DRAIN_THRESHOLD] =
                    DeviceConfig.getFloat(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_HIGH_THRESHOLD_TO_RESTRICTED_BUCKET,
                    DEFAULT_BG_CURRENT_DRAIN_RESTRICTED_BUCKET_HIGH_THRESHOLD);
                    mDefaultBgCurrentDrainRestrictedBucketHighThreshold);
            mBgCurrentDrainBgRestrictedThreshold[INDEX_REGULAR_CURRENT_DRAIN_THRESHOLD] =
                    DeviceConfig.getFloat(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_THRESHOLD_TO_BG_RESTRICTED,
                    DEFAULT_BG_CURRENT_DRAIN_BG_RESTRICTED_THRESHOLD);
                    mDefaultBgCurrentDrainBgRestrictedThreshold);
            mBgCurrentDrainBgRestrictedThreshold[INDEX_HIGH_CURRENT_DRAIN_THRESHOLD] =
                    DeviceConfig.getFloat(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_HIGH_THRESHOLD_TO_BG_RESTRICTED,
                    DEFAULT_BG_CURRENT_DRAIN_BG_RESTRICTED_HIGH_THRESHOLD);
                    mDefaultBgCurrentDrainBgRestrictedHighThreshold);
            mBgCurrentDrainRestrictedBucketTypes =
                    DeviceConfig.getInt(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_TYPES_TO_RESTRICTED_BUCKET,
                    DEFAULT_BG_CURRENT_DRAIN_TYPES_TO_RESTRICTED_BUCKET);
                    mDefaultCurrentDrainTypesToRestrictedBucket);
            mBgCurrentDrainBgRestrictedTypes =
                    DeviceConfig.getInt(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_TYPES_TO_BG_RESTRICTED,
                    DEFAULT_BG_CURRENT_DRAIN_TYPES_TO_BG_RESTRICTED);
                    mDefaultBgCurrentDrainTypesToBgRestricted);
            mBgCurrentDrainPowerComponents =
                    DeviceConfig.getInt(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_POWER_COMPONENTS,
                    DEFAULT_BG_CURRENT_DRAIN_POWER_COMPONENTS);
                    mDefaultBgCurrentDrainPowerComponent);
            if (mBgCurrentDrainPowerComponents == DEFAULT_BG_CURRENT_DRAIN_POWER_COMPONENTS) {
                mBatteryDimensions = BatteryUsage.BATT_DIMENS;
            } else {
@@ -1273,28 +1311,28 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>
            mBgCurrentDrainWindowMs = DeviceConfig.getLong(
                    DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_WINDOW,
                    DEFAULT_BG_CURRENT_DRAIN_WINDOW_MS);
                    mDefaultBgCurrentDrainWindowMs);
        }

        private void updateCurrentDrainMediaPlaybackMinDuration() {
            mBgCurrentDrainMediaPlaybackMinDuration = DeviceConfig.getLong(
                    DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_MEDIA_PLAYBACK_MIN_DURATION,
                    DEFAULT_BG_CURRENT_DRAIN_MEDIA_PLAYBACK_MIN_DURATION);
                    mDefaultBgCurrentDrainMediaPlaybackMinDuration);
        }

        private void updateCurrentDrainLocationMinDuration() {
            mBgCurrentDrainLocationMinDuration = DeviceConfig.getLong(
                    DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_LOCATION_MIN_DURATION,
                    DEFAULT_BG_CURRENT_DRAIN_LOCATION_MIN_DURATION);
                    mDefaultBgCurrentDrainLocationMinDuration);
        }

        private void updateCurrentDrainEventDurationBasedThresholdEnabled() {
            mBgCurrentDrainEventDurationBasedThresholdEnabled = DeviceConfig.getBoolean(
                    DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                    KEY_BG_CURRENT_DRAIN_EVENT_DURATION_BASED_THRESHOLD_ENABLED,
                    DEFAULT_BG_CURRENT_DRAIN_EVENT_DURATION_BASED_THRESHOLD_ENABLED);
                    mDefaultBgCurrentDrainEventDurationBasedThresholdEnabled);
        }

        @Override
+35 −14

File changed.

Preview size limit exceeded, changes collapsed.