1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.fuelgauge.batteryusage; 18 19 import android.content.Context; 20 import android.util.ArrayMap; 21 22 import androidx.annotation.VisibleForTesting; 23 24 import com.android.settings.fuelgauge.BatteryOptimizeUtils; 25 import com.android.settingslib.fuelgauge.PowerAllowlistBackend; 26 27 import java.util.Map; 28 29 /** A cache to log battery optimization mode of an app */ 30 final class BatteryOptimizationModeCache { 31 private static final String TAG = "BatteryOptimizationModeCache"; 32 33 @VisibleForTesting final Map<Integer, BatteryOptimizationMode> mBatteryOptimizeModeCacheMap; 34 35 private final Context mContext; 36 BatteryOptimizationModeCache(final Context context)37 BatteryOptimizationModeCache(final Context context) { 38 mContext = context; 39 mBatteryOptimizeModeCacheMap = new ArrayMap<>(); 40 PowerAllowlistBackend.getInstance(mContext).refreshList(); 41 } 42 getBatteryOptimizeMode(final int uid, final String packageName)43 BatteryOptimizationMode getBatteryOptimizeMode(final int uid, final String packageName) { 44 if (!mBatteryOptimizeModeCacheMap.containsKey(uid)) { 45 final BatteryOptimizeUtils batteryOptimizeUtils = 46 new BatteryOptimizeUtils(mContext, uid, packageName); 47 mBatteryOptimizeModeCacheMap.put( 48 uid, 49 BatteryOptimizationMode.forNumber( 50 batteryOptimizeUtils.getAppOptimizationMode(/* refreshList= */ false))); 51 } 52 return mBatteryOptimizeModeCacheMap.get(uid); 53 } 54 } 55