Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7144b6fe authored by YUKAI HUNG's avatar YUKAI HUNG Committed by Android (Google) Code Review
Browse files

Merge "Implement expandable item for system component usage data" into sc-dev

parents a1fe5ec0 e1cec0e0
Loading
Loading
Loading
Loading
+48 −8
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 */

package com.android.settings.fuelgauge;
@@ -51,7 +50,7 @@ import java.util.Map;
/** Controls the update for chart graph and the list items. */
public class BatteryChartPreferenceController extends AbstractPreferenceController
        implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy,
                BatteryChartView.OnSelectListener {
                BatteryChartView.OnSelectListener, ExpandDividerPreference.OnExpandListener {
    private static final String TAG = "BatteryChartPreferenceController";
    private static final int CHART_KEY_ARRAY_SIZE = 25;
    private static final int CHART_LEVEL_ARRAY_SIZE = 13;
@@ -65,6 +64,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
    @VisibleForTesting BatteryUtils mBatteryUtils;
    @VisibleForTesting PreferenceGroup mAppListPrefGroup;
    @VisibleForTesting BatteryChartView mBatteryChartView;
    @VisibleForTesting ExpandDividerPreference mExpandDividerPreference;

    @VisibleForTesting int[] mBatteryHistoryLevels;
    @VisibleForTesting long[] mBatteryHistoryKeys;
@@ -76,9 +76,13 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
    private final Handler mHandler = new Handler(Looper.getMainLooper());
    private final CharSequence[] mNotAllowShowSummaryPackages;

    private boolean mIsExpanded = false;

    // Preference cache to avoid create new instance each time.
    @VisibleForTesting
    final Map<String, Preference> mPreferenceCache = new HashMap<>();
    @VisibleForTesting
    final List<BatteryDiffEntry> mSystemEntries = new ArrayList<>();

    public BatteryChartPreferenceController(
            Context context, String preferenceKey,
@@ -163,6 +167,12 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
        refreshUi(trapezoidIndex, /*isForce=*/ false);
    }

    @Override
    public void onExpand(boolean isExpanded) {
        mIsExpanded = isExpanded;
        refreshExpandUi();
    }

    void setBatteryHistoryMap(
            final Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
        mHandler.post(() -> setBatteryHistoryMapInner(batteryHistoryMap));
@@ -266,10 +276,10 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
        }
        // Separates data into two groups and sort them individually.
        final List<BatteryDiffEntry> appEntries = new ArrayList<>();
        final List<BatteryDiffEntry> systemEntries = new ArrayList<>();
        mSystemEntries.clear();
        entries.forEach(entry -> {
            if (entry.isSystemEntry()) {
                systemEntries.add(entry);
                mSystemEntries.add(entry);
            } else {
                appEntries.add(entry);
            }
@@ -279,11 +289,25 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
            }
        });
        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",
            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(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
@@ -337,6 +361,22 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
        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
    void setPreferenceSummary(
            PowerGaugePreference preference, BatteryDiffEntry entry) {
+37 −3
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@@ -416,23 +417,56 @@ public final class BatteryChartPreferenceControllerTest {

    @Test
    public void testValidateSlotTimestamp_returnExpectedResult() {
        final List<Long> slotTimestampList =
        final ArrayList<Long> slotTimestampList = new ArrayList<Long>(
            Arrays.asList(
                Long.valueOf(0),
                Long.valueOf(DateUtils.HOUR_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.
        assertThat(BatteryChartPreferenceController.validateSlotTimestamp(slotTimestampList))
            .isTrue();

        // Insert invalid timestamp into the list.
        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))
            .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) {
        final Map<Long, List<BatteryHistEntry>> batteryHistoryMap = new HashMap<>();
        for (int index = 0; index < size; index++) {