Add adaptive charging to the top level settings menu

Bug: 172009945
Test: Trigger adaptive charging and verify string in menu
Change-Id: Iba2d81e779e2b4aaa3c7b7c38710a03c6792c4a9
This commit is contained in:
Stephane Lee
2020-11-04 15:21:05 -08:00
parent 65ced1482d
commit b60870cb7d
6 changed files with 96 additions and 25 deletions

View File

@@ -26,18 +26,21 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.FeatureFlags;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
public class TopLevelBatteryPreferenceController extends BasePreferenceController implements
LifecycleObserver, OnStart, OnStop {
LifecycleObserver, OnStart, OnStop, BatteryPreferenceController {
@VisibleForTesting
boolean mIsBatteryPresent = true;
protected boolean mIsBatteryPresent = true;
private final BatteryBroadcastReceiver mBatteryBroadcastReceiver;
private Preference mPreference;
private BatteryInfo mBatteryInfo;
private BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
private String mBatteryStatusLabel;
public TopLevelBatteryPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
@@ -51,6 +54,9 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
updateState(mPreference);
}, true /* shortString */);
});
mBatteryStatusFeatureProvider = FeatureFactory.getFactory(context)
.getBatteryStatusFeatureProvider(context);
}
@Override
@@ -77,6 +83,10 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
@Override
public CharSequence getSummary() {
return getSummary(true /* batteryStatusUpdate */);
}
private CharSequence getSummary(boolean batteryStatusUpdate) {
// Remove homepage summaries for silky home.
if (FeatureFlagUtils.isEnabled(mContext, FeatureFlags.SILKY_HOME)) {
return null;
@@ -85,23 +95,48 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
if (!mIsBatteryPresent) {
return mContext.getText(R.string.battery_missing_message);
}
return getDashboardLabel(mContext, mBatteryInfo);
return getDashboardLabel(mContext, mBatteryInfo, batteryStatusUpdate);
}
static CharSequence getDashboardLabel(Context context, BatteryInfo info) {
protected CharSequence getDashboardLabel(Context context, BatteryInfo info,
boolean batteryStatusUpdate) {
if (info == null || context == null) {
return null;
}
CharSequence label;
if (batteryStatusUpdate) {
if (!mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(this, info)) {
mBatteryStatusLabel = null; // will generateLabel()
}
}
return (mBatteryStatusLabel == null) ? generateLabel(info) : mBatteryStatusLabel;
}
private CharSequence generateLabel(BatteryInfo info) {
if (!info.discharging && info.chargeLabel != null) {
label = info.chargeLabel;
return info.chargeLabel;
} else if (info.remainingLabel == null) {
label = info.batteryPercentString;
return info.batteryPercentString;
} else {
label = context.getString(R.string.power_remaining_settings_home_page,
return mContext.getString(R.string.power_remaining_settings_home_page,
info.batteryPercentString,
info.remainingLabel);
}
return label;
}
/**
* Callback which receives text for the label.
*/
public void updateBatteryStatus(String label, BatteryInfo info) {
mBatteryStatusLabel = label; // Null if adaptive charging is not active
if (mPreference != null) {
// Do not triggerBatteryStatusUpdate(), otherwise there will be an infinite loop
final CharSequence summary = getSummary(false /* batteryStatusUpdate */);
if (summary != null) {
mPreference.setSummary(summary);
}
}
}
}