Optimize the tip detector loading performance

Optimize the tip detector loading performance by removing 3 times
redundant IPC with PowerManager and BatteryManager

Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.fuelgauge.batterytip"
Change-Id: I0df8b0c600b743c93dfcdd66aeae07e2aba6b797
This commit is contained in:
ykhung
2023-02-07 22:49:28 +08:00
parent d6da5c2f24
commit 19b913cd0c
13 changed files with 56 additions and 125 deletions

View File

@@ -18,6 +18,7 @@ package com.android.settings.fuelgauge.batterytip;
import android.content.Context;
import android.os.BatteryUsageStats;
import android.os.PowerManager;
import androidx.annotation.VisibleForTesting;
@@ -61,11 +62,14 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
final BatteryTipPolicy policy = new BatteryTipPolicy(getContext());
final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG);
final Context context = getContext().getApplicationContext();
final boolean isPowerSaveMode =
context.getSystemService(PowerManager.class).isPowerSaveMode();
tips.add(new LowBatteryDetector(context, policy, batteryInfo).detect());
tips.add(new LowBatteryDetector(context, policy, batteryInfo, isPowerSaveMode).detect());
tips.add(new HighUsageDetector(context, policy, mBatteryUsageStats, batteryInfo).detect());
tips.add(new SmartBatteryDetector(
context, policy, batteryInfo, context.getContentResolver()).detect());
context, policy, batteryInfo, context.getContentResolver(), isPowerSaveMode)
.detect());
tips.add(new BatteryDefenderDetector(batteryInfo, context).detect());
tips.add(new DockDefenderDetector(batteryInfo, context).detect());
tips.add(new IncompatibleChargerDetector(context, batteryInfo).detect());

View File

@@ -37,11 +37,13 @@ public class BatteryDefenderDetector implements BatteryTipDetector {
@Override
public BatteryTip detect() {
if (mBatteryInfo.isOverheated && !FeatureFactory.getFactory(mContext)
.getPowerUsageFeatureProvider(mContext)
.isExtraDefend()) {
return new BatteryDefenderTip(BatteryTip.StateType.NEW);
}
return new BatteryDefenderTip(BatteryTip.StateType.INVISIBLE);
final boolean isBasicBatteryDefend = mBatteryInfo.isOverheated
&& !FeatureFactory.getFactory(mContext)
.getPowerUsageFeatureProvider(mContext)
.isExtraDefend();
final int state = isBasicBatteryDefend
? BatteryTip.StateType.NEW : BatteryTip.StateType.INVISIBLE;
final boolean isPluggedIn = mBatteryInfo.pluggedStatus != 0;
return new BatteryDefenderTip(state, isPluggedIn);
}
}

View File

@@ -17,7 +17,6 @@
package com.android.settings.fuelgauge.batterytip.detectors;
import android.content.Context;
import android.os.PowerManager;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
@@ -30,26 +29,27 @@ import java.util.concurrent.TimeUnit;
* Detect whether the battery is too low
*/
public class LowBatteryDetector implements BatteryTipDetector {
private BatteryInfo mBatteryInfo;
private BatteryTipPolicy mPolicy;
private PowerManager mPowerManager;
private int mWarningLevel;
private final BatteryInfo mBatteryInfo;
private final BatteryTipPolicy mPolicy;
private final boolean mIsPowerSaveMode;
private final int mWarningLevel;
public LowBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo) {
public LowBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo,
boolean isPowerSaveMode) {
mPolicy = policy;
mBatteryInfo = batteryInfo;
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWarningLevel = context.getResources().getInteger(
com.android.internal.R.integer.config_lowBatteryWarningLevel);
mIsPowerSaveMode = isPowerSaveMode;
}
@Override
public BatteryTip detect() {
final boolean powerSaveModeOn = mPowerManager.isPowerSaveMode();
final boolean lowBattery = mBatteryInfo.batteryLevel <= mWarningLevel
|| (mBatteryInfo.discharging && mBatteryInfo.remainingTimeUs != 0
&& mBatteryInfo.remainingTimeUs < TimeUnit.HOURS.toMicros(mPolicy.lowBatteryHour));
final boolean lowBatteryEnabled = mPolicy.lowBatteryEnabled && !powerSaveModeOn;
final boolean lowBatteryEnabled = mPolicy.lowBatteryEnabled && !mIsPowerSaveMode;
final boolean dischargingLowBatteryState =
mPolicy.testLowBatteryTip || (mBatteryInfo.discharging && lowBattery);
@@ -61,6 +61,6 @@ public class LowBatteryDetector implements BatteryTipDetector {
state = BatteryTip.StateType.NEW;
}
return new LowBatteryTip(state, powerSaveModeOn);
return new LowBatteryTip(state, mIsPowerSaveMode);
}
}

View File

@@ -18,7 +18,6 @@ package com.android.settings.fuelgauge.batterytip.detectors;
import android.content.ContentResolver;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import com.android.settings.fuelgauge.BatteryInfo;
@@ -32,17 +31,17 @@ import com.android.settings.fuelgauge.batterytip.tips.SmartBatteryTip;
public class SmartBatteryDetector implements BatteryTipDetector {
private static final int EXPECTED_BATTERY_LEVEL = 30;
private BatteryInfo mBatteryInfo;
private BatteryTipPolicy mPolicy;
private ContentResolver mContentResolver;
private PowerManager mPowerManager;
private final BatteryInfo mBatteryInfo;
private final BatteryTipPolicy mPolicy;
private final ContentResolver mContentResolver;
private final boolean mIsPowerSaveMode;
public SmartBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo,
ContentResolver contentResolver) {
ContentResolver contentResolver, boolean isPowerSaveMode) {
mPolicy = policy;
mBatteryInfo = batteryInfo;
mContentResolver = contentResolver;
mPowerManager = context.getSystemService(PowerManager.class);
mIsPowerSaveMode = isPowerSaveMode;
}
@Override
@@ -53,7 +52,7 @@ public class SmartBatteryDetector implements BatteryTipDetector {
mBatteryInfo.batteryLevel <= EXPECTED_BATTERY_LEVEL;
// Show it if in test or smart battery is off.
final boolean enableSmartBatteryTip =
smartBatteryOff && !mPowerManager.isPowerSaveMode() && isUnderExpectedBatteryLevel
smartBatteryOff && !mIsPowerSaveMode && isUnderExpectedBatteryLevel
|| mPolicy.testSmartBatteryTip;
final int state =
enableSmartBatteryTip ? BatteryTip.StateType.NEW : BatteryTip.StateType.INVISIBLE;

View File

@@ -19,9 +19,6 @@ package com.android.settings.fuelgauge.batterytip.tips;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Parcel;
import android.util.Log;
import androidx.preference.Preference;
@@ -39,12 +36,11 @@ public class BatteryDefenderTip extends BatteryTip {
private static final String TAG = "BatteryDefenderTip";
public BatteryDefenderTip(@StateType int state) {
super(TipType.BATTERY_DEFENDER, state, false /* showDialog */);
}
private boolean mIsPluggedIn;
private BatteryDefenderTip(Parcel in) {
super(in);
public BatteryDefenderTip(@StateType int state, boolean isPluggedIn) {
super(TipType.BATTERY_DEFENDER, state, false /* showDialog */);
mIsPluggedIn = isPluggedIn;
}
@Override
@@ -92,7 +88,7 @@ public class BatteryDefenderTip extends BatteryTip {
resumeCharging(context);
preference.setVisible(false);
});
cardPreference.setPrimaryButtonVisible(isPluggedIn(context));
cardPreference.setPrimaryButtonVisible(mIsPluggedIn);
cardPreference.setSecondaryButtonText(context.getString(R.string.learn_more));
cardPreference.setSecondaryButtonClickListener(
@@ -117,23 +113,4 @@ public class BatteryDefenderTip extends BatteryTip {
Log.i(TAG, "send resume charging broadcast intent=" + intent);
}
private boolean isPluggedIn(Context context) {
final Intent batteryIntent =
context.registerReceiver(
/* receiver= */ null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return batteryIntent != null
&& batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
}
public static final Creator CREATOR = new Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new BatteryDefenderTip(in);
}
public BatteryTip[] newArray(int size) {
return new BatteryDefenderTip[size];
}
};
}

View File

@@ -19,7 +19,6 @@ package com.android.settings.fuelgauge.batterytip.tips;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.util.Log;
import androidx.preference.Preference;
@@ -44,10 +43,6 @@ public class DockDefenderTip extends BatteryTip {
mMode = mode;
}
private DockDefenderTip(Parcel in) {
super(in);
}
public int getMode() {
return mMode;
}
@@ -162,14 +157,4 @@ public class DockDefenderTip extends BatteryTip {
Log.i(TAG, "send resume charging broadcast intent=" + intent);
}
public static final Creator CREATOR = new Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new DockDefenderTip(in);
}
public BatteryTip[] newArray(int size) {
return new DockDefenderTip[size];
}
};
}

View File

@@ -19,7 +19,6 @@ package com.android.settings.fuelgauge.batterytip.tips;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.util.Log;
import androidx.preference.Preference;
@@ -37,10 +36,6 @@ public final class IncompatibleChargerTip extends BatteryTip {
super(TipType.INCOMPATIBLE_CHARGER, state, /* showDialog */ false);
}
private IncompatibleChargerTip(Parcel in) {
super(in);
}
@Override
public CharSequence getTitle(Context context) {
return context.getString(R.string.battery_tip_incompatible_charging_title);
@@ -89,14 +84,4 @@ public final class IncompatibleChargerTip extends BatteryTip {
cardPreference.setSecondaryButtonContentDescription(context.getString(
R.string.battery_tip_incompatible_charging_content_description));
}
public static final Creator CREATOR = new Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new IncompatibleChargerTip(in);
}
public BatteryTip[] newArray(int size) {
return new IncompatibleChargerTip[size];
}
};
}

View File

@@ -91,5 +91,4 @@ public class LowBatteryTip extends BatteryTip {
return new LowBatteryTip[size];
}
};
}