Implement expandable item for system component usage data
Bug: 177406865 Bug: 185187729 Test: make SettingsRoboTests Test: make SettingsGoogleRoboTests Change-Id: Id488ab23039ff12480da5e02b6a96ff70e879007
This commit is contained in:
@@ -12,7 +12,6 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.android.settings.fuelgauge;
|
package com.android.settings.fuelgauge;
|
||||||
@@ -51,7 +50,7 @@ import java.util.Map;
|
|||||||
/** Controls the update for chart graph and the list items. */
|
/** Controls the update for chart graph and the list items. */
|
||||||
public class BatteryChartPreferenceController extends AbstractPreferenceController
|
public class BatteryChartPreferenceController extends AbstractPreferenceController
|
||||||
implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy,
|
implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy,
|
||||||
BatteryChartView.OnSelectListener {
|
BatteryChartView.OnSelectListener, ExpandDividerPreference.OnExpandListener {
|
||||||
private static final String TAG = "BatteryChartPreferenceController";
|
private static final String TAG = "BatteryChartPreferenceController";
|
||||||
private static final int CHART_KEY_ARRAY_SIZE = 25;
|
private static final int CHART_KEY_ARRAY_SIZE = 25;
|
||||||
private static final int CHART_LEVEL_ARRAY_SIZE = 13;
|
private static final int CHART_LEVEL_ARRAY_SIZE = 13;
|
||||||
@@ -65,6 +64,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
@VisibleForTesting BatteryUtils mBatteryUtils;
|
@VisibleForTesting BatteryUtils mBatteryUtils;
|
||||||
@VisibleForTesting PreferenceGroup mAppListPrefGroup;
|
@VisibleForTesting PreferenceGroup mAppListPrefGroup;
|
||||||
@VisibleForTesting BatteryChartView mBatteryChartView;
|
@VisibleForTesting BatteryChartView mBatteryChartView;
|
||||||
|
@VisibleForTesting ExpandDividerPreference mExpandDividerPreference;
|
||||||
|
|
||||||
@VisibleForTesting int[] mBatteryHistoryLevels;
|
@VisibleForTesting int[] mBatteryHistoryLevels;
|
||||||
@VisibleForTesting long[] mBatteryHistoryKeys;
|
@VisibleForTesting long[] mBatteryHistoryKeys;
|
||||||
@@ -76,9 +76,13 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
||||||
private final CharSequence[] mNotAllowShowSummaryPackages;
|
private final CharSequence[] mNotAllowShowSummaryPackages;
|
||||||
|
|
||||||
|
private boolean mIsExpanded = false;
|
||||||
|
|
||||||
// Preference cache to avoid create new instance each time.
|
// Preference cache to avoid create new instance each time.
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
final Map<String, Preference> mPreferenceCache = new HashMap<>();
|
final Map<String, Preference> mPreferenceCache = new HashMap<>();
|
||||||
|
@VisibleForTesting
|
||||||
|
final List<BatteryDiffEntry> mSystemEntries = new ArrayList<>();
|
||||||
|
|
||||||
public BatteryChartPreferenceController(
|
public BatteryChartPreferenceController(
|
||||||
Context context, String preferenceKey,
|
Context context, String preferenceKey,
|
||||||
@@ -163,6 +167,12 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
refreshUi(trapezoidIndex, /*isForce=*/ false);
|
refreshUi(trapezoidIndex, /*isForce=*/ false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onExpand(boolean isExpanded) {
|
||||||
|
mIsExpanded = isExpanded;
|
||||||
|
refreshExpandUi();
|
||||||
|
}
|
||||||
|
|
||||||
void setBatteryHistoryMap(
|
void setBatteryHistoryMap(
|
||||||
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
|
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
|
||||||
mHandler.post(() -> setBatteryHistoryMapInner(batteryHistoryMap));
|
mHandler.post(() -> setBatteryHistoryMapInner(batteryHistoryMap));
|
||||||
@@ -266,10 +276,10 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
}
|
}
|
||||||
// Separates data into two groups and sort them individually.
|
// Separates data into two groups and sort them individually.
|
||||||
final List<BatteryDiffEntry> appEntries = new ArrayList<>();
|
final List<BatteryDiffEntry> appEntries = new ArrayList<>();
|
||||||
final List<BatteryDiffEntry> systemEntries = new ArrayList<>();
|
mSystemEntries.clear();
|
||||||
entries.forEach(entry -> {
|
entries.forEach(entry -> {
|
||||||
if (entry.isSystemEntry()) {
|
if (entry.isSystemEntry()) {
|
||||||
systemEntries.add(entry);
|
mSystemEntries.add(entry);
|
||||||
} else {
|
} else {
|
||||||
appEntries.add(entry);
|
appEntries.add(entry);
|
||||||
}
|
}
|
||||||
@@ -279,11 +289,25 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
Collections.sort(appEntries, BatteryDiffEntry.COMPARATOR);
|
Collections.sort(appEntries, BatteryDiffEntry.COMPARATOR);
|
||||||
Collections.sort(systemEntries, BatteryDiffEntry.COMPARATOR);
|
Collections.sort(mSystemEntries, BatteryDiffEntry.COMPARATOR);
|
||||||
Log.d(TAG, String.format("addAllPreferences() app=%d system=%d",
|
Log.d(TAG, String.format("addAllPreferences() app=%d system=%d",
|
||||||
appEntries.size(), systemEntries.size()));
|
appEntries.size(), mSystemEntries.size()));
|
||||||
|
|
||||||
|
// Adds app entries to the list if it is not empty.
|
||||||
|
if (!appEntries.isEmpty()) {
|
||||||
addPreferenceToScreen(appEntries);
|
addPreferenceToScreen(appEntries);
|
||||||
addPreferenceToScreen(systemEntries);
|
}
|
||||||
|
// Adds the expabable divider if we have two sections data.
|
||||||
|
if (!appEntries.isEmpty() && !mSystemEntries.isEmpty()) {
|
||||||
|
if (mExpandDividerPreference == null) {
|
||||||
|
mExpandDividerPreference = new ExpandDividerPreference(mPrefContext);
|
||||||
|
mExpandDividerPreference.setOnExpandListener(this);
|
||||||
|
}
|
||||||
|
mExpandDividerPreference.setOrder(
|
||||||
|
mAppListPrefGroup.getPreferenceCount());
|
||||||
|
mAppListPrefGroup.addPreference(mExpandDividerPreference);
|
||||||
|
}
|
||||||
|
refreshExpandUi();
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -337,6 +361,22 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
mAppListPrefGroup.removeAll();
|
mAppListPrefGroup.removeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void refreshExpandUi() {
|
||||||
|
if (mIsExpanded) {
|
||||||
|
addPreferenceToScreen(mSystemEntries);
|
||||||
|
} else {
|
||||||
|
// Removes and recycles all system entries to hide all of them.
|
||||||
|
for (BatteryDiffEntry entry : mSystemEntries) {
|
||||||
|
final String prefKey = entry.mBatteryHistEntry.getKey();
|
||||||
|
final Preference pref = mAppListPrefGroup.findPreference(prefKey);
|
||||||
|
if (pref != null) {
|
||||||
|
mAppListPrefGroup.removePreference(pref);
|
||||||
|
mPreferenceCache.put(pref.getKey(), pref);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void setPreferenceSummary(
|
void setPreferenceSummary(
|
||||||
PowerGaugePreference preference, BatteryDiffEntry entry) {
|
PowerGaugePreference preference, BatteryDiffEntry entry) {
|
||||||
|
@@ -43,6 +43,7 @@ import com.android.settings.testutils.FakeFeatureFactory;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
@@ -416,23 +417,56 @@ public final class BatteryChartPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testValidateSlotTimestamp_returnExpectedResult() {
|
public void testValidateSlotTimestamp_returnExpectedResult() {
|
||||||
final List<Long> slotTimestampList =
|
final ArrayList<Long> slotTimestampList = new ArrayList<Long>(
|
||||||
Arrays.asList(
|
Arrays.asList(
|
||||||
Long.valueOf(0),
|
Long.valueOf(0),
|
||||||
Long.valueOf(DateUtils.HOUR_IN_MILLIS),
|
Long.valueOf(DateUtils.HOUR_IN_MILLIS),
|
||||||
Long.valueOf(DateUtils.HOUR_IN_MILLIS * 2 + DateUtils.MINUTE_IN_MILLIS),
|
Long.valueOf(DateUtils.HOUR_IN_MILLIS * 2 + DateUtils.MINUTE_IN_MILLIS),
|
||||||
Long.valueOf(DateUtils.HOUR_IN_MILLIS * 3 + DateUtils.MINUTE_IN_MILLIS * 2));
|
Long.valueOf(DateUtils.HOUR_IN_MILLIS * 3 + DateUtils.MINUTE_IN_MILLIS * 2)));
|
||||||
// Verifies the testing data is correct before we added invalid data into it.
|
// Verifies the testing data is correct before we added invalid data into it.
|
||||||
assertThat(BatteryChartPreferenceController.validateSlotTimestamp(slotTimestampList))
|
assertThat(BatteryChartPreferenceController.validateSlotTimestamp(slotTimestampList))
|
||||||
.isTrue();
|
.isTrue();
|
||||||
|
|
||||||
// Insert invalid timestamp into the list.
|
// Insert invalid timestamp into the list.
|
||||||
slotTimestampList.add(
|
slotTimestampList.add(
|
||||||
Long.valueOf(DateUtils.HOUR_IN_MILLIS * 4 + DateUtils.MINUTE_IN_MILLIS * 3));
|
Long.valueOf(DateUtils.HOUR_IN_MILLIS * 4 + DateUtils.MINUTE_IN_MILLIS * 6));
|
||||||
assertThat(BatteryChartPreferenceController.validateSlotTimestamp(slotTimestampList))
|
assertThat(BatteryChartPreferenceController.validateSlotTimestamp(slotTimestampList))
|
||||||
.isFalse();
|
.isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnExpand_expandedIsTrue_addSystemEntriesToPreferenceGroup() {
|
||||||
|
final String prefKey = "preference_key";
|
||||||
|
doReturn(1).when(mAppListGroup).getPreferenceCount();
|
||||||
|
mBatteryChartPreferenceController.mSystemEntries.add(mBatteryDiffEntry);
|
||||||
|
doReturn("label").when(mBatteryDiffEntry).getAppLabel();
|
||||||
|
doReturn(mDrawable).when(mBatteryDiffEntry).getAppIcon();
|
||||||
|
doReturn(prefKey).when(mBatteryHistEntry).getKey();
|
||||||
|
|
||||||
|
mBatteryChartPreferenceController.onExpand(/*isExpanded=*/ true);
|
||||||
|
|
||||||
|
final ArgumentCaptor<Preference> captor = ArgumentCaptor.forClass(Preference.class);
|
||||||
|
verify(mAppListGroup).addPreference(captor.capture());
|
||||||
|
// Verifies the added preference.
|
||||||
|
assertThat(captor.getValue().getKey()).isEqualTo(prefKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnExpand_expandedIsFalse_removeSystemEntriesFromPreferenceGroup() {
|
||||||
|
final String prefKey = "preference_key";
|
||||||
|
doReturn(prefKey).when(mBatteryHistEntry).getKey();
|
||||||
|
doReturn(mPowerGaugePreference).when(mAppListGroup).findPreference(prefKey);
|
||||||
|
mBatteryChartPreferenceController.mSystemEntries.add(mBatteryDiffEntry);
|
||||||
|
// Verifies the cache is empty first.
|
||||||
|
assertThat(mBatteryChartPreferenceController.mPreferenceCache).isEmpty();
|
||||||
|
|
||||||
|
mBatteryChartPreferenceController.onExpand(/*isExpanded=*/ false);
|
||||||
|
|
||||||
|
verify(mAppListGroup).findPreference(prefKey);
|
||||||
|
verify(mAppListGroup).removePreference(mPowerGaugePreference);
|
||||||
|
assertThat(mBatteryChartPreferenceController.mPreferenceCache).hasSize(1);
|
||||||
|
}
|
||||||
|
|
||||||
private static Map<Long, List<BatteryHistEntry>> createBatteryHistoryMap(int size) {
|
private static Map<Long, List<BatteryHistEntry>> createBatteryHistoryMap(int size) {
|
||||||
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap = new HashMap<>();
|
final Map<Long, List<BatteryHistEntry>> batteryHistoryMap = new HashMap<>();
|
||||||
for (int index = 0; index < size; index++) {
|
for (int index = 0; index < size; index++) {
|
||||||
|
Reference in New Issue
Block a user