[Performance] avoid setting the same optimization mode again

Add a checking condition to avoid setting the same optimization mode again to improve the performance, since setting the optimization mode is a heavy operation when binding with system service.

Bug: 192523697
Test: make SettingsRoboTests
Change-Id: I4065e3c56201c1c9285d9cebedf670bc0c42c614
This commit is contained in:
ykhung
2021-08-30 15:25:13 +08:00
committed by YUKAI HUNG
parent adec23a106
commit 9ac072dafe
2 changed files with 48 additions and 25 deletions

View File

@@ -36,13 +36,12 @@ public class BatteryOptimizeUtils {
@VisibleForTesting AppOpsManager mAppOpsManager;
@VisibleForTesting BatteryUtils mBatteryUtils;
@VisibleForTesting PowerAllowlistBackend mPowerAllowListBackend;
@VisibleForTesting int mMode;
@VisibleForTesting boolean mAllowListed;
private final String mPackageName;
private final int mUid;
private int mMode;
private boolean mAllowListed;
// Optimization modes.
static final int MODE_UNKNOWN = 0;
static final int MODE_RESTRICTED = 1;
@@ -92,6 +91,10 @@ public class BatteryOptimizeUtils {
/** Sets the {@link OptimizationMode} for associated app. */
public void setAppOptimizationMode(@OptimizationMode int mode) {
if (getAppOptimizationMode(mMode, mAllowListed) == mode) {
Log.w(TAG, "set the same optimization mode for: " + mPackageName);
return;
}
switch (mode) {
case MODE_RESTRICTED:
mBatteryUtils.setForceAppStandby(mUid, mPackageName, AppOpsManager.MODE_IGNORED);