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

Commit 43ace6ed authored by Zaiyue Xue's avatar Zaiyue Xue Committed by Android (Google) Code Review
Browse files

Merge changes from topic "feature0"

* changes:
  Refactor processBatteryDiffData() from DataProcessor to BatteryDiffData class.
  Refactor PowerUsageFeatureProvider: Cache the config set to avoid generating the set again.
parents 24dbaeba f080429d
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -33,12 +33,12 @@ public interface PowerUsageFeatureProvider {
    /**
     * Check whether the battery usage button is enabled in the battery page
     */
    boolean isBatteryUsageEnabled(Context context);
    boolean isBatteryUsageEnabled();

    /**
     * Returns an allowlist of app names combined into the system-apps item
     */
    List<String> getSystemAppsAllowlist(Context context);
    List<String> getSystemAppsAllowlist();

    /**
     * Check whether location setting is enabled
@@ -135,25 +135,25 @@ public interface PowerUsageFeatureProvider {
    /**
     * Returns {@link Set} for the system component ids which are combined into others.
     */
    Set<Integer> getOthersSystemComponentSet(Context context);
    Set<Integer> getOthersSystemComponentSet();

    /**
     * Returns {@link Set} for hiding system component ids in the usage screen.
     */
    Set<Integer> getHideSystemComponentSet(Context context);
    Set<Integer> getHideSystemComponentSet();

    /**
     * Returns {@link Set} for hiding application package names in the usage screen.
     */
    Set<CharSequence> getHideApplicationSet(Context context);
    Set<String> getHideApplicationSet();

    /**
     * Returns {@link Set} for hiding applications background usage time.
     */
    Set<CharSequence> getHideBackgroundUsageTimeSet(Context context);
    Set<String> getHideBackgroundUsageTimeSet();

    /**
     * Returns {@link Set} for ignoring task root class names for screen on time.
     */
    Set<CharSequence> getIgnoreScreenOnTimeTaskRootSet(Context context);
    Set<String> getIgnoreScreenOnTimeTaskRootSet();
}
+7 −7
Original line number Diff line number Diff line
@@ -67,12 +67,12 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
    }

    @Override
    public boolean isBatteryUsageEnabled(Context context) {
    public boolean isBatteryUsageEnabled() {
        return true;
    }

    @Override
    public List<String> getSystemAppsAllowlist(Context context) {
    public List<String> getSystemAppsAllowlist() {
        return null;
    }

@@ -153,27 +153,27 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
    }

    @Override
    public Set<Integer> getOthersSystemComponentSet(Context context) {
    public Set<Integer> getOthersSystemComponentSet() {
        return new ArraySet<>();
    }

    @Override
    public Set<Integer> getHideSystemComponentSet(Context context) {
    public Set<Integer> getHideSystemComponentSet() {
        return new ArraySet<>();
    }

    @Override
    public Set<CharSequence> getHideApplicationSet(Context context) {
    public Set<String> getHideApplicationSet() {
        return new ArraySet<>();
    }

    @Override
    public Set<CharSequence> getHideBackgroundUsageTimeSet(Context context) {
    public Set<String> getHideBackgroundUsageTimeSet() {
        return new ArraySet<>();
    }

    @Override
    public Set<CharSequence> getIgnoreScreenOnTimeTaskRootSet(Context context) {
    public Set<String> getIgnoreScreenOnTimeTaskRootSet() {
        return new ArraySet<>();
    }
}
+150 −9
Original line number Diff line number Diff line
@@ -16,10 +16,20 @@

package com.android.settings.fuelgauge.batteryusage;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.applications.ApplicationsState;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/** Wraps the battery usage diff data for each entry used for battery usage app list. */
public class BatteryDiffData {
@@ -28,10 +38,22 @@ public class BatteryDiffData {

    /** Constructor for the diff entries. */
    public BatteryDiffData(
            @NonNull List<BatteryDiffEntry> appDiffEntries,
            @NonNull List<BatteryDiffEntry> systemDiffEntries) {
            final Context context,
            final @NonNull List<BatteryDiffEntry> appDiffEntries,
            final @NonNull List<BatteryDiffEntry> systemDiffEntries,
            final boolean isAccumulated) {
        mAppEntries = appDiffEntries;
        mSystemEntries = systemDiffEntries;

        if (!isAccumulated) {
            final PowerUsageFeatureProvider featureProvider =
                    FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);
            purgeFakeAndHiddenPackages(featureProvider);
            combineBatteryDiffEntry(context, featureProvider);
        }

        setTotalConsumePower();
        sortEntries();
    }

    public List<BatteryDiffEntry> getAppDiffEntryList() {
@@ -42,20 +64,111 @@ public class BatteryDiffData {
        return mSystemEntries;
    }

    // Sorts entries based on consumed percentage.
    void sortEntries() {
        Collections.sort(mAppEntries, BatteryDiffEntry.COMPARATOR);
        Collections.sort(mSystemEntries, BatteryDiffEntry.COMPARATOR);
    /** Removes fake usage data and hidden packages. */
    private void purgeFakeAndHiddenPackages(final PowerUsageFeatureProvider featureProvider) {
        purgeFakeAndHiddenPackages(featureProvider, mAppEntries);
        purgeFakeAndHiddenPackages(featureProvider, mSystemEntries);
    }

    /** Combines into SystemAppsBatteryDiffEntry and OthersBatteryDiffEntry. */
    private void combineBatteryDiffEntry(
            final Context context, final PowerUsageFeatureProvider featureProvider) {
        combineIntoSystemApps(context, featureProvider, mAppEntries);
        combineSystemItemsIntoOthers(context, featureProvider, mSystemEntries);
    }

    // Sets total consume power for app and system entries separately.
    void setTotalConsumePower() {
    /** Sets total consume power for app and system entries separately. */
    private void setTotalConsumePower() {
        setTotalConsumePowerForAllEntries(mAppEntries);
        setTotalConsumePowerForAllEntries(mSystemEntries);
    }

    /** Sorts entries based on consumed percentage. */
    private void sortEntries() {
        Collections.sort(mAppEntries, BatteryDiffEntry.COMPARATOR);
        Collections.sort(mSystemEntries, BatteryDiffEntry.COMPARATOR);
    }

    private static void purgeFakeAndHiddenPackages(
            final PowerUsageFeatureProvider featureProvider,
            final List<BatteryDiffEntry> entries) {
        final Set<Integer> hideSystemComponentSet = featureProvider.getHideSystemComponentSet();
        final Set<String> hideBackgroundUsageTimeSet =
                featureProvider.getHideBackgroundUsageTimeSet();
        final Set<String> hideApplicationSet = featureProvider.getHideApplicationSet();
        final Iterator<BatteryDiffEntry> iterator = entries.iterator();
        while (iterator.hasNext()) {
            final BatteryDiffEntry entry = iterator.next();
            final String packageName = entry.getPackageName();
            final Integer componentId = entry.mBatteryHistEntry.mDrainType;
            if (ConvertUtils.FAKE_PACKAGE_NAME.equals(packageName)
                    || hideSystemComponentSet.contains(componentId)
                    || (packageName != null && hideApplicationSet.contains(packageName))) {
                iterator.remove();
            }
            if (packageName != null && hideBackgroundUsageTimeSet.contains(packageName)) {
                entry.mBackgroundUsageTimeInMs = 0;
            }
        }
    }

    private static void combineIntoSystemApps(
            final Context context,
            final PowerUsageFeatureProvider featureProvider,
            final List<BatteryDiffEntry> appEntries) {
        final List<String> systemAppsAllowlist = featureProvider.getSystemAppsAllowlist();
        final Application application = (Application) context.getApplicationContext();
        final ApplicationsState applicationsState =
                application == null ? null : ApplicationsState.getInstance(application);

        BatteryDiffEntry.SystemAppsBatteryDiffEntry systemAppsDiffEntry = null;
        final Iterator<BatteryDiffEntry> appListIterator = appEntries.iterator();
        while (appListIterator.hasNext()) {
            final BatteryDiffEntry batteryDiffEntry = appListIterator.next();
            if (needsCombineInSystemApp(batteryDiffEntry, systemAppsAllowlist, applicationsState)) {
                if (systemAppsDiffEntry == null) {
                    systemAppsDiffEntry = new BatteryDiffEntry.SystemAppsBatteryDiffEntry(context);
                }
                systemAppsDiffEntry.mConsumePower += batteryDiffEntry.mConsumePower;
                systemAppsDiffEntry.mForegroundUsageTimeInMs +=
                        batteryDiffEntry.mForegroundUsageTimeInMs;
                systemAppsDiffEntry.setTotalConsumePower(
                        batteryDiffEntry.getTotalConsumePower());
                appListIterator.remove();
            }
        }
        if (systemAppsDiffEntry != null) {
            appEntries.add(systemAppsDiffEntry);
        }
    }

    private static void combineSystemItemsIntoOthers(
            final Context context,
            final PowerUsageFeatureProvider featureProvider,
            final List<BatteryDiffEntry> systemEntries) {
        final Set<Integer> othersSystemComponentSet = featureProvider.getOthersSystemComponentSet();
        BatteryDiffEntry.OthersBatteryDiffEntry othersDiffEntry = null;
        final Iterator<BatteryDiffEntry> systemListIterator = systemEntries.iterator();
        while (systemListIterator.hasNext()) {
            final BatteryDiffEntry batteryDiffEntry = systemListIterator.next();
            if (othersSystemComponentSet.contains(batteryDiffEntry.mBatteryHistEntry.mDrainType)) {
                if (othersDiffEntry == null) {
                    othersDiffEntry = new BatteryDiffEntry.OthersBatteryDiffEntry(context);
                }
                othersDiffEntry.mConsumePower += batteryDiffEntry.mConsumePower;
                othersDiffEntry.setTotalConsumePower(
                        batteryDiffEntry.getTotalConsumePower());
                systemListIterator.remove();
            }
        }
        if (othersDiffEntry != null) {
            systemEntries.add(othersDiffEntry);
        }
    }

    // Sets total consume power for each entry.
    private void setTotalConsumePowerForAllEntries(List<BatteryDiffEntry> batteryDiffEntries) {
    private static void setTotalConsumePowerForAllEntries(
            final List<BatteryDiffEntry> batteryDiffEntries) {
        double totalConsumePower = 0.0;
        for (BatteryDiffEntry batteryDiffEntry : batteryDiffEntries) {
            totalConsumePower += batteryDiffEntry.mConsumePower;
@@ -64,4 +177,32 @@ public class BatteryDiffData {
            batteryDiffEntry.setTotalConsumePower(totalConsumePower);
        }
    }

    @VisibleForTesting
    static boolean needsCombineInSystemApp(final BatteryDiffEntry batteryDiffEntry,
            final List<String> systemAppsAllowlist, final ApplicationsState applicationsState) {
        if (batteryDiffEntry.mBatteryHistEntry.mIsHidden) {
            return true;
        }

        final String packageName = batteryDiffEntry.getPackageName();
        if (packageName == null || packageName.isEmpty()) {
            return false;
        }

        if (systemAppsAllowlist != null && systemAppsAllowlist.contains(packageName)) {
            return true;
        }

        if (applicationsState == null) {
            return false;
        }
        final ApplicationsState.AppEntry appEntry =
                applicationsState.getEntry(packageName, /* userId= */ 0);
        if (appEntry == null || appEntry.info == null) {
            return false;
        }
        return !ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER_AND_INSTANT.filterApp(
                appEntry);
    }
}
+46 −2
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ public class BatteryDiffEntry {

    /** A comparator for {@link BatteryDiffEntry} based on consumed percentage. */
    public static final Comparator<BatteryDiffEntry> COMPARATOR =
            (a, b) -> Double.compare(b.getPercentOfTotal(), a.getPercentOfTotal());
            (a, b) -> Double.compare(b.getSortingKey(), a.getSortingKey());

    public long mForegroundUsageTimeInMs;
    public long mBackgroundUsageTimeInMs;
@@ -121,6 +121,11 @@ public class BatteryDiffEntry {
        return mPercentOfTotal;
    }

    /** Gets the key for sorting */
    public double getSortingKey() {
        return getPercentOfTotal();
    }

    /** Clones a new instance. */
    public BatteryDiffEntry clone() {
        return new BatteryDiffEntry(
@@ -265,7 +270,6 @@ public class BatteryDiffEntry {
        }
    }

    @VisibleForTesting
    String getKey() {
        return mBatteryHistEntry.getKey();
    }
@@ -434,6 +438,26 @@ public class BatteryDiffEntry {
        public boolean isSystemEntry() {
            return false;
        }

        @Override
        public double getSortingKey() {
            // Always on the bottom of the app list.
            return -1;
        }

        @Override
        public BatteryDiffEntry clone() {
            SystemAppsBatteryDiffEntry newEntry = new SystemAppsBatteryDiffEntry(this.mContext);
            newEntry.mForegroundUsageTimeInMs = this.mForegroundUsageTimeInMs;
            newEntry.mBackgroundUsageTimeInMs = this.mBackgroundUsageTimeInMs;
            newEntry.mScreenOnTimeInMs = this.mScreenOnTimeInMs;
            newEntry.mConsumePower = this.mConsumePower;
            newEntry.mForegroundUsageConsumePower = this.mForegroundUsageConsumePower;
            newEntry.mForegroundServiceUsageConsumePower = this.mForegroundServiceUsageConsumePower;
            newEntry.mBackgroundUsageConsumePower = this.mBackgroundUsageConsumePower;
            newEntry.mCachedUsageConsumePower = this.mCachedUsageConsumePower;
            return newEntry;
        }
    }

    /** Specific battery diff entry for others. */
@@ -475,5 +499,25 @@ public class BatteryDiffEntry {
        public boolean isSystemEntry() {
            return true;
        }

        @Override
        public double getSortingKey() {
            // Always on the bottom of the system list.
            return -1;
        }

        @Override
        public BatteryDiffEntry clone() {
            OthersBatteryDiffEntry newEntry = new OthersBatteryDiffEntry(this.mContext);
            newEntry.mForegroundUsageTimeInMs = this.mForegroundUsageTimeInMs;
            newEntry.mBackgroundUsageTimeInMs = this.mBackgroundUsageTimeInMs;
            newEntry.mScreenOnTimeInMs = this.mScreenOnTimeInMs;
            newEntry.mConsumePower = this.mConsumePower;
            newEntry.mForegroundUsageConsumePower = this.mForegroundUsageConsumePower;
            newEntry.mForegroundServiceUsageConsumePower = this.mForegroundServiceUsageConsumePower;
            newEntry.mBackgroundUsageConsumePower = this.mBackgroundUsageConsumePower;
            newEntry.mCachedUsageConsumePower = this.mCachedUsageConsumePower;
            return newEntry;
        }
    }
}
+15 −168

File changed.

Preview size limit exceeded, changes collapsed.

Loading