Merge changes I3ee2313c,Ibbf9eb2a
* changes: Remove divider line for battery card preference Add CardPreference in battery settings page
This commit is contained in:
30
res/layout/card_preference_layout.xml
Normal file
30
res/layout/card_preference_layout.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2019 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:id="@+id/container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
style="@style/ContextualCardStyle">
|
||||
<include layout="@layout/preference_material" />
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
</FrameLayout>
|
@@ -54,6 +54,8 @@
|
||||
|
||||
<attr name="slicePreferenceStyle" format="reference" />
|
||||
|
||||
<attr name="cardPreferenceStyle" format="reference" />
|
||||
|
||||
<attr name="footerPreferenceStyle" format="reference" />
|
||||
|
||||
<declare-styleable name="FixedLineSummaryPreference">
|
||||
|
@@ -21,6 +21,7 @@
|
||||
|
||||
<style name="PreferenceTheme" parent="@style/PreferenceThemeOverlay.SettingsBase">
|
||||
<item name="apnPreferenceStyle">@style/ApnPreference</item>
|
||||
<item name="cardPreferenceStyle">@style/CardPreference</item>
|
||||
<item name="slicePreferenceStyle">@style/SlicePreference</item>
|
||||
<item name="seekBarPreferenceStyle">@style/SettingsSeekBarPreference</item>
|
||||
<item name="twoStateButtonPreferenceStyle">@style/TwoStateButtonPreference</item>
|
||||
@@ -35,6 +36,10 @@
|
||||
<item name="android:layout">@layout/apn_preference_layout</item>
|
||||
</style>
|
||||
|
||||
<style name="CardPreference" parent="@style/Preference.Material">
|
||||
<item name="android:layout">@layout/card_preference_layout</item>
|
||||
</style>
|
||||
|
||||
<style name="SlicePreference" parent="@style/Preference.Material">
|
||||
<item name="android:layout">@layout/slice_preference_layout</item>
|
||||
</style>
|
||||
|
@@ -28,10 +28,9 @@
|
||||
android:layout="@layout/battery_header"
|
||||
settings:controller="com.android.settings.fuelgauge.BatteryHeaderPreferenceController" />
|
||||
|
||||
<PreferenceCategory
|
||||
<com.android.settings.widget.CardPreference
|
||||
android:key="battery_tip"
|
||||
android:title="@string/summary_placeholder"
|
||||
android:layout="@layout/preference_category_no_label"
|
||||
settings:controller="com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController" />
|
||||
|
||||
<Preference
|
||||
@@ -39,8 +38,7 @@
|
||||
android:key="battery_saver_summary"
|
||||
android:title="@string/battery_saver"
|
||||
settings:controller="com.android.settings.fuelgauge.BatterySaverController"
|
||||
settings:platform_slice="true"
|
||||
settings:allowDividerAbove="true" />
|
||||
settings:platform_slice="true" />
|
||||
|
||||
<Preference
|
||||
android:fragment="com.android.settings.fuelgauge.SmartBatterySettings"
|
||||
|
@@ -21,7 +21,6 @@ import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.SettingsActivity;
|
||||
@@ -32,6 +31,7 @@ import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.CardPreference;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -56,7 +56,7 @@ public class BatteryTipPreferenceController extends BasePreferenceController {
|
||||
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
private boolean mNeedUpdate;
|
||||
@VisibleForTesting
|
||||
PreferenceGroup mPreferenceGroup;
|
||||
CardPreference mCardPreference;
|
||||
@VisibleForTesting
|
||||
Context mPrefContext;
|
||||
InstrumentedPreferenceFragment mFragment;
|
||||
@@ -89,12 +89,12 @@ public class BatteryTipPreferenceController extends BasePreferenceController {
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPrefContext = screen.getContext();
|
||||
mPreferenceGroup = (PreferenceGroup) screen.findPreference(getPreferenceKey());
|
||||
mCardPreference = screen.findPreference(getPreferenceKey());
|
||||
|
||||
// Add summary tip in advance to avoid UI flakiness
|
||||
final SummaryTip summaryTip = new SummaryTip(BatteryTip.StateType.NEW,
|
||||
Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN);
|
||||
mPreferenceGroup.addPreference(summaryTip.buildPreference(mPrefContext));
|
||||
summaryTip.updatePreference(mCardPreference);
|
||||
}
|
||||
|
||||
public void updateBatteryTips(List<BatteryTip> batteryTips) {
|
||||
@@ -110,14 +110,12 @@ public class BatteryTipPreferenceController extends BasePreferenceController {
|
||||
}
|
||||
}
|
||||
|
||||
mPreferenceGroup.removeAll();
|
||||
for (int i = 0, size = batteryTips.size(); i < size; i++) {
|
||||
final BatteryTip batteryTip = mBatteryTips.get(i);
|
||||
batteryTip.sanityCheck(mContext);
|
||||
if (batteryTip.getState() != BatteryTip.StateType.INVISIBLE) {
|
||||
final Preference preference = batteryTip.buildPreference(mPrefContext);
|
||||
mBatteryTipMap.put(preference.getKey(), batteryTip);
|
||||
mPreferenceGroup.addPreference(preference);
|
||||
batteryTip.updatePreference(mCardPreference);
|
||||
mBatteryTipMap.put(mCardPreference.getKey(), batteryTip);
|
||||
batteryTip.log(mContext, mMetricsFeatureProvider);
|
||||
mNeedUpdate = batteryTip.needUpdate();
|
||||
break;
|
||||
|
@@ -26,6 +26,7 @@ import androidx.annotation.IntDef;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.widget.CardPreference;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -146,14 +147,11 @@ public abstract class BatteryTip implements Comparable<BatteryTip>, Parcelable {
|
||||
*/
|
||||
public abstract void log(Context context, MetricsFeatureProvider metricsFeatureProvider);
|
||||
|
||||
public Preference buildPreference(Context context) {
|
||||
Preference preference = new Preference(context);
|
||||
|
||||
preference.setKey(getKey());
|
||||
public void updatePreference(Preference preference) {
|
||||
final Context context = preference.getContext();
|
||||
preference.setTitle(getTitle(context));
|
||||
preference.setSummary(getSummary(context));
|
||||
preference.setIcon(getIconId());
|
||||
return preference;
|
||||
}
|
||||
|
||||
public boolean shouldShowDialog() {
|
||||
|
39
src/com/android/settings/widget/CardPreference.java
Normal file
39
src/com/android/settings/widget/CardPreference.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import com.google.android.material.card.MaterialCardView;
|
||||
|
||||
/**
|
||||
* Preference that wrapped by {@link MaterialCardView}, only support to set icon, title and summary
|
||||
*/
|
||||
public class CardPreference extends Preference {
|
||||
public CardPreference(Context context) {
|
||||
this(context, null /* attrs */);
|
||||
}
|
||||
|
||||
public CardPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs, R.attr.cardPreferenceStyle);
|
||||
}
|
||||
}
|
@@ -44,6 +44,7 @@ import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.widget.CardPreference;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -72,17 +73,14 @@ public class BatteryTipPreferenceControllerTest {
|
||||
private BatteryTip mBatteryTip;
|
||||
@Mock
|
||||
private SettingsActivity mSettingsActivity;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private PreferenceManager mPreferenceManager;
|
||||
@Mock
|
||||
private InstrumentedPreferenceFragment mFragment;
|
||||
|
||||
private Context mContext;
|
||||
private PreferenceGroup mPreferenceGroup;
|
||||
private CardPreference mCardPreference;
|
||||
private BatteryTipPreferenceController mBatteryTipPreferenceController;
|
||||
private List<BatteryTip> mOldBatteryTips;
|
||||
private List<BatteryTip> mNewBatteryTips;
|
||||
private Preference mPreference;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
|
||||
@Before
|
||||
@@ -90,12 +88,9 @@ public class BatteryTipPreferenceControllerTest {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
|
||||
mPreferenceGroup = spy(new PreferenceCategory(mContext));
|
||||
mCardPreference = spy(new CardPreference(mContext));
|
||||
when(mPreferenceScreen.getContext()).thenReturn(mContext);
|
||||
when(mPreferenceGroup.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||
doReturn(mPreferenceGroup).when(mPreferenceScreen).findPreference(KEY_PREF);
|
||||
mPreference = new Preference(mContext);
|
||||
mPreference.setKey(KEY_TIP);
|
||||
doReturn(mCardPreference).when(mPreferenceScreen).findPreference(KEY_PREF);
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
|
||||
mOldBatteryTips = new ArrayList<>();
|
||||
@@ -104,7 +99,7 @@ public class BatteryTipPreferenceControllerTest {
|
||||
mNewBatteryTips.add(new SummaryTip(BatteryTip.StateType.INVISIBLE, AVERAGE_TIME_MS));
|
||||
|
||||
mBatteryTipPreferenceController = buildBatteryTipPreferenceController();
|
||||
mBatteryTipPreferenceController.mPreferenceGroup = mPreferenceGroup;
|
||||
mBatteryTipPreferenceController.mCardPreference = mCardPreference;
|
||||
mBatteryTipPreferenceController.mPrefContext = mContext;
|
||||
}
|
||||
|
||||
@@ -112,18 +107,7 @@ public class BatteryTipPreferenceControllerTest {
|
||||
public void testDisplayPreference_addSummaryTip() {
|
||||
mBatteryTipPreferenceController.displayPreference(mPreferenceScreen);
|
||||
|
||||
assertOnlyContainsSummaryTip(mPreferenceGroup);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateBatteryTips_updateTwice_firstShowSummaryTipThenRemoveIt() {
|
||||
// Display summary tip because its state is new
|
||||
mBatteryTipPreferenceController.updateBatteryTips(mOldBatteryTips);
|
||||
assertOnlyContainsSummaryTip(mPreferenceGroup);
|
||||
|
||||
// Remove summary tip because its new state is invisible
|
||||
mBatteryTipPreferenceController.updateBatteryTips(mNewBatteryTips);
|
||||
assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(0);
|
||||
assertOnlyContainsSummaryTip(mCardPreference);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -142,11 +126,11 @@ public class BatteryTipPreferenceControllerTest {
|
||||
mBatteryTipPreferenceController.saveInstanceState(bundle);
|
||||
|
||||
final BatteryTipPreferenceController controller = buildBatteryTipPreferenceController();
|
||||
controller.mPreferenceGroup = mPreferenceGroup;
|
||||
controller.mCardPreference = mCardPreference;
|
||||
controller.mPrefContext = mContext;
|
||||
controller.restoreInstanceState(bundle);
|
||||
|
||||
assertOnlyContainsSummaryTip(mPreferenceGroup);
|
||||
assertOnlyContainsSummaryTip(mCardPreference);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -166,12 +150,11 @@ public class BatteryTipPreferenceControllerTest {
|
||||
when(mBatteryTip.getType()).thenReturn(SMART_BATTERY_MANAGER);
|
||||
List<BatteryTip> batteryTips = new ArrayList<>();
|
||||
batteryTips.add(mBatteryTip);
|
||||
doReturn(mPreference).when(mBatteryTip).buildPreference(any());
|
||||
doReturn(false).when(mBatteryTip).shouldShowDialog();
|
||||
doReturn(KEY_TIP).when(mBatteryTip).getKey();
|
||||
mBatteryTipPreferenceController.updateBatteryTips(batteryTips);
|
||||
|
||||
mBatteryTipPreferenceController.handlePreferenceTreeClick(mPreference);
|
||||
mBatteryTipPreferenceController.handlePreferenceTreeClick(mCardPreference);
|
||||
|
||||
verify(mBatteryTipListener).onBatteryTipHandled(mBatteryTip);
|
||||
}
|
||||
@@ -182,10 +165,7 @@ public class BatteryTipPreferenceControllerTest {
|
||||
BasePreferenceController.AVAILABLE_UNSEARCHABLE);
|
||||
}
|
||||
|
||||
private void assertOnlyContainsSummaryTip(final PreferenceGroup preferenceGroup) {
|
||||
assertThat(preferenceGroup.getPreferenceCount()).isEqualTo(1);
|
||||
|
||||
final Preference preference = preferenceGroup.getPreference(0);
|
||||
private void assertOnlyContainsSummaryTip(CardPreference preference) {
|
||||
assertThat(preference.getTitle()).isEqualTo(
|
||||
mContext.getString(R.string.battery_tip_summary_title));
|
||||
assertThat(preference.getSummary()).isEqualTo(
|
||||
|
@@ -56,7 +56,8 @@ public class BatteryTipTest {
|
||||
|
||||
@Test
|
||||
public void buildPreference() {
|
||||
final Preference preference = mBatteryTip.buildPreference(mContext);
|
||||
final Preference preference = new Preference(mContext);
|
||||
mBatteryTip.updatePreference(preference);
|
||||
|
||||
assertThat(preference.getTitle()).isEqualTo(TITLE);
|
||||
assertThat(preference.getSummary()).isEqualTo(SUMMARY);
|
||||
|
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.widget;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class CardPreferenceTest {
|
||||
|
||||
private Context mContext;
|
||||
private CardPreference mCardPreference;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mContext.setTheme(R.style.PreferenceTheme);
|
||||
mCardPreference = new CardPreference(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLayoutResource() {
|
||||
assertThat(mCardPreference.getLayoutResource()).isEqualTo(R.layout.card_preference_layout);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user