<com.android.settings.applications.LayoutPreference
android:key="battery_header"
+ android:title="@string/summary_placeholder"
android:selectable="false"
- android:layout="@layout/battery_header" />
+ android:layout="@layout/battery_header"
+ settings:controller="com.android.settings.fuelgauge.BatteryHeaderPreferenceController" />
<PreferenceCategory
android:key="battery_tip"
- android:layout="@layout/preference_category_no_label" />
+ android:title="@string/summary_placeholder"
+ android:layout="@layout/preference_category_no_label"
+ settings:controller="com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController" />
<Preference
android:fragment="com.android.settings.fuelgauge.batterysaver.BatterySaverSettings"
<SwitchPreference
android:key="battery_percentage"
android:title="@string/battery_percentage"
- android:summary="@string/battery_percentage_description" />
+ android:summary="@string/battery_percentage_description"
+ settings:controller="com.android.settings.display.BatteryPercentagePreferenceController" />
<com.android.settings.fuelgauge.PowerGaugePreference
import android.provider.Settings;
import com.android.internal.R;
+import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
-import com.android.settingslib.core.AbstractPreferenceController;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;
* A controller to manage the switch for showing battery percentage in the status bar.
*/
-public class BatteryPercentagePreferenceController extends AbstractPreferenceController implements
+public class BatteryPercentagePreferenceController extends BasePreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
- private static final String KEY_BATTERY_PERCENTAGE = "battery_percentage";
-
- public BatteryPercentagePreferenceController(Context context) {
- super(context);
- }
-
- @Override
- public boolean isAvailable() {
- return mContext.getResources()
- .getBoolean(R.bool.config_battery_percentage_setting_available);
+ public BatteryPercentagePreferenceController(Context context, String preferenceKey) {
+ super(context, preferenceKey);
}
@Override
- public String getPreferenceKey() {
- return KEY_BATTERY_PERCENTAGE;
+ public int getAvailabilityStatus() {
+ return mContext.getResources().getBoolean(
+ R.bool.config_battery_percentage_setting_available) ? AVAILABLE
+ : UNSUPPORTED_ON_DEVICE;
}
@Override
import com.android.settings.R;
import com.android.settings.applications.LayoutPreference;
+import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.Utils;
/**
* Controller that update the battery header view
*/
-public class BatteryHeaderPreferenceController extends AbstractPreferenceController
+public class BatteryHeaderPreferenceController extends BasePreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnStart {
@VisibleForTesting
static final String KEY_BATTERY_HEADER = "battery_header";
@VisibleForTesting
TextView mSummary2;
- private final Activity mActivity;
- private final PreferenceFragmentCompat mHost;
- private final Lifecycle mLifecycle;
+ private Activity mActivity;
+ private PreferenceFragmentCompat mHost;
+ private Lifecycle mLifecycle;
private final PowerManager mPowerManager;
private LayoutPreference mBatteryLayoutPref;
- public BatteryHeaderPreferenceController(Context context, Activity activity,
- PreferenceFragmentCompat host, Lifecycle lifecycle) {
- super(context);
+ public BatteryHeaderPreferenceController(Context context, String key) {
+ super(context, key);
+ mPowerManager = context.getSystemService(PowerManager.class);
+ }
+
+ public void setActivity(Activity activity) {
mActivity = activity;
- mHost = host;
+ }
+
+ public void setFragment(PreferenceFragmentCompat fragment) {
+ mHost = fragment;
+ }
+
+ public void setLifecycle(Lifecycle lifecycle) {
mLifecycle = lifecycle;
- if (mLifecycle != null) {
- mLifecycle.addObserver(this);
- }
- mPowerManager = context.getSystemService(PowerManager.class);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
- mBatteryLayoutPref = (LayoutPreference) screen.findPreference(KEY_BATTERY_HEADER);
- mBatteryMeterView = (BatteryMeterView) mBatteryLayoutPref
+ mBatteryLayoutPref = (LayoutPreference) screen.findPreference(getPreferenceKey());
+ mBatteryMeterView = mBatteryLayoutPref
.findViewById(R.id.battery_header_icon);
mBatteryPercentText = mBatteryLayoutPref.findViewById(R.id.battery_percent);
mSummary1 = mBatteryLayoutPref.findViewById(R.id.summary1);
}
@Override
- public boolean isAvailable() {
- return true;
- }
-
- @Override
- public String getPreferenceKey() {
- return KEY_BATTERY_HEADER;
+ public int getAvailabilityStatus() {
+ return AVAILABLE_UNSEARCHABLE;
}
@Override
import android.provider.SearchIndexableResource;
import android.text.BidiFormatter;
import android.text.format.Formatter;
-import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.dashboard.SummaryLoader;
-import com.android.settings.display.BatteryPercentagePreferenceController;
import com.android.settings.fuelgauge.batterytip.BatteryTipLoader;
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider;
-import com.android.settingslib.core.AbstractPreferenceController;
-import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.utils.PowerUtil;
import com.android.settingslib.utils.StringUtil;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
};
@Override
+ public void onAttach(Context context) {
+ super.onAttach(context);
+ final SettingsActivity activity = (SettingsActivity) getActivity();
+
+ mBatteryHeaderPreferenceController = use(BatteryHeaderPreferenceController.class);
+ mBatteryHeaderPreferenceController.setActivity(activity);
+ mBatteryHeaderPreferenceController.setFragment(this);
+ mBatteryHeaderPreferenceController.setLifecycle(getSettingsLifecycle());
+
+ mBatteryTipPreferenceController = use(BatteryTipPreferenceController.class);
+ mBatteryTipPreferenceController.setActivity(activity);
+ mBatteryTipPreferenceController.setFragment(this);
+ mBatteryTipPreferenceController.setBatteryTipListener(this::onBatteryTipHandled);
+ }
+
+ @Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setAnimationAllowed(true);
}
@Override
- protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
- final Lifecycle lifecycle = getSettingsLifecycle();
- final SettingsActivity activity = (SettingsActivity) getActivity();
- final List<AbstractPreferenceController> controllers = new ArrayList<>();
- mBatteryHeaderPreferenceController = new BatteryHeaderPreferenceController(
- context, activity, this /* host */, lifecycle);
- controllers.add(mBatteryHeaderPreferenceController);
- mBatteryTipPreferenceController = new BatteryTipPreferenceController(context,
- KEY_BATTERY_TIP, (SettingsActivity) getActivity(), this /* fragment */, this /*
- BatteryTipListener */);
- controllers.add(mBatteryTipPreferenceController);
- controllers.add(new BatteryPercentagePreferenceController(context));
- return controllers;
- }
-
- @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (DEBUG) {
menu.add(Menu.NONE, MENU_STATS_TYPE, Menu.NONE, R.string.menu_stats_total)
InstrumentedPreferenceFragment mFragment;
public BatteryTipPreferenceController(Context context, String preferenceKey) {
- this(context, preferenceKey, null, null, null);
- }
-
- public BatteryTipPreferenceController(Context context, String preferenceKey,
- SettingsActivity settingsActivity, InstrumentedPreferenceFragment fragment,
- BatteryTipListener batteryTipListener) {
super(context, preferenceKey);
- mBatteryTipListener = batteryTipListener;
mBatteryTipMap = new HashMap<>();
- mFragment = fragment;
- mSettingsActivity = settingsActivity;
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
mNeedUpdate = true;
}
+ public void setActivity(SettingsActivity activity) {
+ mSettingsActivity = activity;
+ }
+
+ public void setFragment(InstrumentedPreferenceFragment fragment) {
+ mFragment = fragment;
+ }
+
+ public void setBatteryTipListener(BatteryTipListener lsn) {
+ mBatteryTipListener = lsn;
+ }
+
@Override
public int getAvailabilityStatus() {
- return AVAILABLE;
+ return AVAILABLE_UNSEARCHABLE;
}
@Override
-com.android.settings.fuelgauge.PowerUsageSummary
@RunWith(SettingsRobolectricTestRunner.class)
public class BatteryPercentagePreferenceControllerTest {
+ private static final String PREF_KEY = "battery_percentage";
+
private Context mContext;
private BatteryPercentagePreferenceController mController;
@Before
public void setup() {
mContext = RuntimeEnvironment.application;
- mController = new BatteryPercentagePreferenceController(mContext);
+ mController = new BatteryPercentagePreferenceController(mContext, PREF_KEY);
}
private int getPercentageSetting() {
import com.android.settings.R;
import com.android.settings.applications.LayoutPreference;
+import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
})
public class BatteryHeaderPreferenceControllerTest {
+ private static final String PREF_KEY = "battery_header";
private static final int BATTERY_LEVEL = 60;
private static final String TIME_LEFT = "2h30min";
private static final String BATTERY_STATUS = "Charging";
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
- mController = new BatteryHeaderPreferenceController(
- mContext, mActivity, mPreferenceFragment, mLifecycle);
+ mController = new BatteryHeaderPreferenceController(mContext, PREF_KEY);
+ mLifecycle.addObserver(mController);
+ mController.setActivity(mActivity);
+ mController.setFragment(mPreferenceFragment);
+ mController.setLifecycle(mLifecycle);
mController.mBatteryMeterView = mBatteryMeterView;
mController.mBatteryPercentText = mBatteryPercentText;
mController.mSummary1 = mSummary;
assertThat(mBatteryMeterView.getPowerSave()).isEqualTo(value);
}
}
+
+ @Test
+ public void getAvailabilityStatus_returnAvailableUnsearchable() {
+ assertThat(mController.getAvailabilityStatus()).isEqualTo(
+ BasePreferenceController.AVAILABLE_UNSEARCHABLE);
+ }
}
}
@Test
- public void preferenceControllers_getPreferenceKeys_existInPreferenceScreen() {
- final Context context = RuntimeEnvironment.application;
- final PowerUsageSummary fragment = new PowerUsageSummary();
- final List<String> preferenceScreenKeys =
- XmlTestUtils.getKeysFromPreferenceXml(context, fragment.getPreferenceScreenResId());
- final List<String> preferenceKeys = new ArrayList<>();
-
- for (AbstractPreferenceController controller : fragment.createPreferenceControllers(context)) {
- preferenceKeys.add(controller.getPreferenceKey());
- }
-
- assertThat(preferenceScreenKeys).containsAllIn(preferenceKeys);
- }
-
- @Test
public void restartBatteryTipLoader() {
//TODO: add policy logic here when BatteryTipPolicy is implemented
doReturn(mLoaderManager).when(mFragment).getLoaderManager();
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
+import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.InstrumentedPreferenceFragment;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
mNewBatteryTips = new ArrayList<>();
mNewBatteryTips.add(new SummaryTip(BatteryTip.StateType.INVISIBLE, AVERAGE_TIME_MS));
- mBatteryTipPreferenceController = new BatteryTipPreferenceController(mContext, KEY_PREF,
- mSettingsActivity, mFragment, mBatteryTipListener);
+ mBatteryTipPreferenceController = buildBatteryTipPreferenceController();
mBatteryTipPreferenceController.mPreferenceGroup = mPreferenceGroup;
mBatteryTipPreferenceController.mPrefContext = mContext;
}
final Bundle bundle = new Bundle();
mBatteryTipPreferenceController.saveInstanceState(bundle);
- final BatteryTipPreferenceController controller = new BatteryTipPreferenceController(
- mContext, KEY_PREF, mSettingsActivity, mFragment, mBatteryTipListener);
+ final BatteryTipPreferenceController controller = buildBatteryTipPreferenceController();
controller.mPreferenceGroup = mPreferenceGroup;
controller.mPrefContext = mContext;
controller.restoreInstanceState(bundle);
// Battery tip list is null at this time
mBatteryTipPreferenceController.saveInstanceState(bundle);
- final BatteryTipPreferenceController controller = new BatteryTipPreferenceController(
- mContext, KEY_PREF, mSettingsActivity, mFragment, mBatteryTipListener);
+ final BatteryTipPreferenceController controller = buildBatteryTipPreferenceController();
// Should not crash
controller.restoreInstanceState(bundle);
verify(mBatteryTipListener).onBatteryTipHandled(mBatteryTip);
}
+ @Test
+ public void getAvailabilityStatus_returnAvailableUnsearchable() {
+ assertThat(mBatteryTipPreferenceController.getAvailabilityStatus()).isEqualTo(
+ BasePreferenceController.AVAILABLE_UNSEARCHABLE);
+ }
+
private void assertOnlyContainsSummaryTip(final PreferenceGroup preferenceGroup) {
assertThat(preferenceGroup.getPreferenceCount()).isEqualTo(1);
assertThat(preference.getSummary()).isEqualTo(
mContext.getString(R.string.battery_tip_summary_summary));
}
+
+ private BatteryTipPreferenceController buildBatteryTipPreferenceController() {
+ final BatteryTipPreferenceController controller = new BatteryTipPreferenceController(
+ mContext, KEY_PREF);
+ controller.setActivity(mSettingsActivity);
+ controller.setFragment(mFragment);
+ controller.setBatteryTipListener(mBatteryTipListener);
+
+ return controller;
+ }
}