Refine the app/system section divider content show/hide rule

Bug: 177406865
Bug: 185187729
Test: make SettingsRoboTests
Change-Id: I5440a93078417df20ca61687f6c464dbad80a419
This commit is contained in:
ykhung
2021-04-28 11:56:52 +08:00
committed by YUKAI HUNG
parent 9246f6f8b0
commit 5407d1b16d
2 changed files with 34 additions and 103 deletions

View File

@@ -66,8 +66,6 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
@VisibleForTesting PreferenceGroup mAppListPrefGroup; @VisibleForTesting PreferenceGroup mAppListPrefGroup;
@VisibleForTesting BatteryChartView mBatteryChartView; @VisibleForTesting BatteryChartView mBatteryChartView;
@VisibleForTesting ExpandDividerPreference mExpandDividerPreference; @VisibleForTesting ExpandDividerPreference mExpandDividerPreference;
@VisibleForTesting CategoryTitleType mCategoryTitleType =
CategoryTitleType.TYPE_UNKNOWN;
@VisibleForTesting int[] mBatteryHistoryLevels; @VisibleForTesting int[] mBatteryHistoryLevels;
@VisibleForTesting long[] mBatteryHistoryKeys; @VisibleForTesting long[] mBatteryHistoryKeys;
@@ -87,14 +85,6 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
@VisibleForTesting @VisibleForTesting
final List<BatteryDiffEntry> mSystemEntries = new ArrayList<>(); final List<BatteryDiffEntry> mSystemEntries = new ArrayList<>();
/** Which component data will be shown in the screen. */
enum CategoryTitleType {
TYPE_UNKNOWN,
TYPE_APP_COMPONENT,
TYPE_SYSTEM_COMPONENT,
TYPE_ALL_COMPONENTS
}
public BatteryChartPreferenceController( public BatteryChartPreferenceController(
Context context, String preferenceKey, Context context, String preferenceKey,
Lifecycle lifecycle, SettingsActivity activity, Lifecycle lifecycle, SettingsActivity activity,
@@ -283,7 +273,6 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
} }
private void addAllPreferences() { private void addAllPreferences() {
mCategoryTitleType = CategoryTitleType.TYPE_UNKNOWN;
final List<BatteryDiffEntry> entries = final List<BatteryDiffEntry> entries =
mBatteryIndexedMap.get(Integer.valueOf(mTrapezoidIndex)); mBatteryIndexedMap.get(Integer.valueOf(mTrapezoidIndex));
if (entries == null) { if (entries == null) {
@@ -312,10 +301,9 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
// Adds app entries to the list if it is not empty. // Adds app entries to the list if it is not empty.
if (!appEntries.isEmpty()) { if (!appEntries.isEmpty()) {
addPreferenceToScreen(appEntries); addPreferenceToScreen(appEntries);
mCategoryTitleType = CategoryTitleType.TYPE_APP_COMPONENT;
} }
// Adds the expabable divider if we have two sections data. // Adds the expabable divider if we have system entries data.
if (!appEntries.isEmpty() && !mSystemEntries.isEmpty()) { if (!mSystemEntries.isEmpty()) {
if (mExpandDividerPreference == null) { if (mExpandDividerPreference == null) {
mExpandDividerPreference = new ExpandDividerPreference(mPrefContext); mExpandDividerPreference = new ExpandDividerPreference(mPrefContext);
mExpandDividerPreference.setOnExpandListener(this); mExpandDividerPreference.setOnExpandListener(this);
@@ -323,9 +311,6 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
mExpandDividerPreference.setOrder( mExpandDividerPreference.setOrder(
mAppListPrefGroup.getPreferenceCount()); mAppListPrefGroup.getPreferenceCount());
mAppListPrefGroup.addPreference(mExpandDividerPreference); mAppListPrefGroup.addPreference(mExpandDividerPreference);
mCategoryTitleType = CategoryTitleType.TYPE_ALL_COMPONENTS;
} else if (appEntries.isEmpty() && !mSystemEntries.isEmpty()) {
mCategoryTitleType = CategoryTitleType.TYPE_SYSTEM_COMPONENT;
} }
refreshExpandUi(); refreshExpandUi();
} }
@@ -408,38 +393,14 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
@VisibleForTesting @VisibleForTesting
void refreshCategoryTitle() { void refreshCategoryTitle() {
final String slotInformation = getSlotInformation(); final String slotInformation = getSlotInformation();
Log.d(TAG, String.format("refreshCategoryTitle:%s slotInfo:%s", Log.d(TAG, String.format("refreshCategoryTitle:%s", slotInformation));
mCategoryTitleType, slotInformation)); if (mAppListPrefGroup != null) {
refreshPreferenceCategoryTitle(slotInformation);
refreshExpandableDividerTitle(slotInformation);
}
private void refreshExpandableDividerTitle(String slotInformation) {
if (mExpandDividerPreference == null) {
return;
}
mExpandDividerPreference.setTitle(
mCategoryTitleType == CategoryTitleType.TYPE_ALL_COMPONENTS
? getSlotInformation(/*isApp=*/ false, slotInformation)
: null);
}
private void refreshPreferenceCategoryTitle(String slotInformation) {
if (mAppListPrefGroup == null) {
return;
}
switch (mCategoryTitleType) {
case TYPE_APP_COMPONENT:
case TYPE_ALL_COMPONENTS:
mAppListPrefGroup.setTitle( mAppListPrefGroup.setTitle(
getSlotInformation(/*isApp=*/ true, slotInformation)); getSlotInformation(/*isApp=*/ true, slotInformation));
break; }
case TYPE_SYSTEM_COMPONENT: if (mExpandDividerPreference != null) {
mAppListPrefGroup.setTitle( mExpandDividerPreference.setTitle(
getSlotInformation(/*isApp=*/ false, slotInformation)); getSlotInformation(/*isApp=*/ false, slotInformation));
break;
default:
mAppListPrefGroup.setTitle(R.string.battery_app_usage_for_past_24);
} }
} }

View File

@@ -33,6 +33,7 @@ import android.graphics.drawable.Drawable;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
import com.android.settings.R; import com.android.settings.R;
@@ -482,52 +483,13 @@ public final class BatteryChartPreferenceControllerTest {
} }
@Test @Test
public void testRefreshCategoryTitle_appComponent_setHourIntoPreferenceTitle() { public void testRefreshCategoryTitle_setHourIntoBothTitleTextView() {
mBatteryChartPreferenceController = createController();
setUpBatteryHistoryKeys(); setUpBatteryHistoryKeys();
mBatteryChartPreferenceController.mCategoryTitleType = mBatteryChartPreferenceController.mAppListPrefGroup =
BatteryChartPreferenceController.CategoryTitleType.TYPE_APP_COMPONENT; spy(new PreferenceCategory(mContext));
mBatteryChartPreferenceController.mExpandDividerPreference = mBatteryChartPreferenceController.mExpandDividerPreference =
mExpandDividerPreference; spy(new ExpandDividerPreference(mContext));
// Simulates select the first slot.
mBatteryChartPreferenceController.mTrapezoidIndex = 0;
mBatteryChartPreferenceController.refreshCategoryTitle();
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
// Verifies the title in the preference group.
verify(mAppListGroup).setTitle(captor.capture());
assertThat(captor.getValue())
.isEqualTo("App usage for 4 pm-7 am");
verify(mExpandDividerPreference).setTitle(null);
}
@Test
public void testRefreshCategoryTitle_systemComponent_setHourIntoPreferenceTitle() {
setUpBatteryHistoryKeys();
mBatteryChartPreferenceController.mCategoryTitleType =
BatteryChartPreferenceController.CategoryTitleType.TYPE_SYSTEM_COMPONENT;
mBatteryChartPreferenceController.mExpandDividerPreference =
mExpandDividerPreference;
// Simulates select the first slot.
mBatteryChartPreferenceController.mTrapezoidIndex = 0;
mBatteryChartPreferenceController.refreshCategoryTitle();
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
// Verifies the title in the preference group.
verify(mAppListGroup).setTitle(captor.capture());
assertThat(captor.getValue())
.isEqualTo("System usage for 4 pm-7 am");
verify(mExpandDividerPreference).setTitle(null);
}
@Test
public void testRefreshCategoryTitle_allComponents_setHourIntoBothTitleTextView() {
setUpBatteryHistoryKeys();
mBatteryChartPreferenceController.mCategoryTitleType =
BatteryChartPreferenceController.CategoryTitleType.TYPE_ALL_COMPONENTS;
mBatteryChartPreferenceController.mExpandDividerPreference =
mExpandDividerPreference;
// Simulates select the first slot. // Simulates select the first slot.
mBatteryChartPreferenceController.mTrapezoidIndex = 0; mBatteryChartPreferenceController.mTrapezoidIndex = 0;
@@ -535,22 +497,25 @@ public final class BatteryChartPreferenceControllerTest {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
// Verifies the title in the preference group. // Verifies the title in the preference group.
verify(mAppListGroup).setTitle(captor.capture()); verify(mBatteryChartPreferenceController.mAppListPrefGroup)
.setTitle(captor.capture());
assertThat(captor.getValue()) assertThat(captor.getValue())
.isEqualTo("App usage for 4 pm-7 am"); .isEqualTo("App usage for 4 pm-7 am");
// Verifies the title in the expandable divider. // Verifies the title in the expandable divider.
captor = ArgumentCaptor.forClass(String.class); captor = ArgumentCaptor.forClass(String.class);
verify(mExpandDividerPreference).setTitle(captor.capture()); verify(mBatteryChartPreferenceController.mExpandDividerPreference)
.setTitle(captor.capture());
assertThat(captor.getValue()) assertThat(captor.getValue())
.isEqualTo("System usage for 4 pm-7 am"); .isEqualTo("System usage for 4 pm-7 am");
} }
@Test @Test
public void testRefreshCategoryTitle_allComponents_setLast24HrIntoBothTitleTextView() { public void testRefreshCategoryTitle_setLast24HrIntoBothTitleTextView() {
mBatteryChartPreferenceController.mCategoryTitleType = mBatteryChartPreferenceController = createController();
BatteryChartPreferenceController.CategoryTitleType.TYPE_ALL_COMPONENTS; mBatteryChartPreferenceController.mAppListPrefGroup =
spy(new PreferenceCategory(mContext));
mBatteryChartPreferenceController.mExpandDividerPreference = mBatteryChartPreferenceController.mExpandDividerPreference =
mExpandDividerPreference; spy(new ExpandDividerPreference(mContext));
// Simulates select all condition. // Simulates select all condition.
mBatteryChartPreferenceController.mTrapezoidIndex = mBatteryChartPreferenceController.mTrapezoidIndex =
BatteryChartView.SELECTED_INDEX_ALL; BatteryChartView.SELECTED_INDEX_ALL;
@@ -559,12 +524,14 @@ public final class BatteryChartPreferenceControllerTest {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
// Verifies the title in the preference group. // Verifies the title in the preference group.
verify(mAppListGroup).setTitle(captor.capture()); verify(mBatteryChartPreferenceController.mAppListPrefGroup)
.setTitle(captor.capture());
assertThat(captor.getValue()) assertThat(captor.getValue())
.isEqualTo("App usage for past 24 hr"); .isEqualTo("App usage for past 24 hr");
// Verifies the title in the expandable divider. // Verifies the title in the expandable divider.
captor = ArgumentCaptor.forClass(String.class); captor = ArgumentCaptor.forClass(String.class);
verify(mExpandDividerPreference).setTitle(captor.capture()); verify(mBatteryChartPreferenceController.mExpandDividerPreference)
.setTitle(captor.capture());
assertThat(captor.getValue()) assertThat(captor.getValue())
.isEqualTo("System usage for past 24 hr"); .isEqualTo("System usage for past 24 hr");
} }
@@ -643,8 +610,11 @@ public final class BatteryChartPreferenceControllerTest {
} }
private BatteryChartPreferenceController createController() { private BatteryChartPreferenceController createController() {
return new BatteryChartPreferenceController( final BatteryChartPreferenceController controller =
new BatteryChartPreferenceController(
mContext, "app_list", /*lifecycle=*/ null, mContext, "app_list", /*lifecycle=*/ null,
mSettingsActivity, mFragment); mSettingsActivity, mFragment);
controller.mPrefContext = mContext;
return controller;
} }
} }