Search in sources :

Example 1 with BatteryConsumer

use of android.os.BatteryConsumer in project android_packages_apps_Settings by omnirom.

the class BatteryAppListPreferenceController method getCoalescedUsageList.

/**
 * We want to coalesce some UIDs. For example, dex2oat runs under a shared gid that
 * exists for all users of the same app. We detect this case and merge the power use
 * for dex2oat to the device OWNER's use of the app.
 *
 * @return A sorted list of apps using power.
 */
private List<BatteryEntry> getCoalescedUsageList(boolean showAllApps, boolean loadDataInBackground) {
    final SparseArray<BatteryEntry> batteryEntryList = new SparseArray<>();
    final ArrayList<BatteryEntry> results = new ArrayList<>();
    final List<UidBatteryConsumer> uidBatteryConsumers = mBatteryUsageStats.getUidBatteryConsumers();
    // Sort to have all apps with "real" UIDs first, followed by apps that are supposed
    // to be combined with the real ones.
    uidBatteryConsumers.sort(Comparator.comparingInt(consumer -> consumer.getUid() == getRealUid(consumer) ? 0 : 1));
    for (int i = 0, size = uidBatteryConsumers.size(); i < size; i++) {
        final UidBatteryConsumer consumer = uidBatteryConsumers.get(i);
        final int uid = getRealUid(consumer);
        final String[] packages = mPackageManager.getPackagesForUid(uid);
        if (mBatteryUtils.shouldHideUidBatteryConsumerUnconditionally(consumer, packages)) {
            continue;
        }
        final boolean isHidden = mBatteryUtils.shouldHideUidBatteryConsumer(consumer, packages);
        if (isHidden && !showAllApps) {
            continue;
        }
        final int index = batteryEntryList.indexOfKey(uid);
        if (index < 0) {
            // New entry.
            batteryEntryList.put(uid, new BatteryEntry(mContext, mHandler, mUserManager, consumer, isHidden, uid, packages, null, loadDataInBackground));
        } else {
            // Combine BatterySippers if we already have one with this UID.
            final BatteryEntry existingSipper = batteryEntryList.valueAt(index);
            existingSipper.add(consumer);
        }
    }
    final BatteryConsumer deviceConsumer = mBatteryUsageStats.getAggregateBatteryConsumer(BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE);
    final BatteryConsumer appsConsumer = mBatteryUsageStats.getAggregateBatteryConsumer(BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS);
    for (int componentId = 0; componentId < BatteryConsumer.POWER_COMPONENT_COUNT; componentId++) {
        if (!showAllApps && mBatteryUtils.shouldHideDevicePowerComponent(deviceConsumer, componentId)) {
            continue;
        }
        results.add(new BatteryEntry(mContext, componentId, deviceConsumer.getConsumedPower(componentId), appsConsumer.getConsumedPower(componentId), deviceConsumer.getUsageDurationMillis(componentId)));
    }
    for (int componentId = BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID; componentId < BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID + deviceConsumer.getCustomPowerComponentCount(); componentId++) {
        if (!showAllApps) {
            continue;
        }
        results.add(new BatteryEntry(mContext, componentId, deviceConsumer.getCustomPowerComponentName(componentId), deviceConsumer.getConsumedPowerForCustomComponent(componentId), appsConsumer.getConsumedPowerForCustomComponent(componentId)));
    }
    if (showAllApps) {
        final List<UserBatteryConsumer> userBatteryConsumers = mBatteryUsageStats.getUserBatteryConsumers();
        for (int i = 0, size = userBatteryConsumers.size(); i < size; i++) {
            final UserBatteryConsumer consumer = userBatteryConsumers.get(i);
            results.add(new BatteryEntry(mContext, mHandler, mUserManager, consumer, /* isHidden */
            true, Process.INVALID_UID, null, null, loadDataInBackground));
        }
    }
    final int numUidSippers = batteryEntryList.size();
    for (int i = 0; i < numUidSippers; i++) {
        results.add(batteryEntryList.valueAt(i));
    }
    // The sort order must have changed, so re-sort based on total power use.
    results.sort(BatteryEntry.COMPARATOR);
    return results;
}
Also used : PreferenceGroup(androidx.preference.PreferenceGroup) Context(android.content.Context) DateUtils(android.text.format.DateUtils) PackageManager(android.content.pm.PackageManager) PowerProfile(com.android.internal.os.PowerProfile) PreferenceControllerMixin(com.android.settings.core.PreferenceControllerMixin) Drawable(android.graphics.drawable.Drawable) Process(android.os.Process) ArrayList(java.util.ArrayList) InstrumentedPreferenceFragment(com.android.settings.core.InstrumentedPreferenceFragment) BatteryUsageStats(android.os.BatteryUsageStats) Handler(android.os.Handler) Looper(android.os.Looper) UserHandle(android.os.UserHandle) StringUtil(com.android.settingslib.utils.StringUtil) UidBatteryConsumer(android.os.UidBatteryConsumer) ArrayMap(android.util.ArrayMap) R(com.android.settings.R) OnPause(com.android.settingslib.core.lifecycle.events.OnPause) UserManager(android.os.UserManager) PreferenceScreen(androidx.preference.PreferenceScreen) UserBatteryConsumer(android.os.UserBatteryConsumer) OnDestroy(com.android.settingslib.core.lifecycle.events.OnDestroy) LifecycleObserver(com.android.settingslib.core.lifecycle.LifecycleObserver) TextUtils(android.text.TextUtils) Preference(androidx.preference.Preference) AggregateBatteryConsumer(android.os.AggregateBatteryConsumer) SparseArray(android.util.SparseArray) List(java.util.List) Message(android.os.Message) SettingsActivity(com.android.settings.SettingsActivity) AbstractPreferenceController(com.android.settingslib.core.AbstractPreferenceController) BatteryConsumer(android.os.BatteryConsumer) Comparator(java.util.Comparator) Activity(android.app.Activity) VisibleForTesting(androidx.annotation.VisibleForTesting) Lifecycle(com.android.settingslib.core.lifecycle.Lifecycle) UidBatteryConsumer(android.os.UidBatteryConsumer) UserBatteryConsumer(android.os.UserBatteryConsumer) AggregateBatteryConsumer(android.os.AggregateBatteryConsumer) BatteryConsumer(android.os.BatteryConsumer) ArrayList(java.util.ArrayList) UidBatteryConsumer(android.os.UidBatteryConsumer) UserBatteryConsumer(android.os.UserBatteryConsumer) SparseArray(android.util.SparseArray)

Aggregations

Activity (android.app.Activity)1 Context (android.content.Context)1 PackageManager (android.content.pm.PackageManager)1 Drawable (android.graphics.drawable.Drawable)1 AggregateBatteryConsumer (android.os.AggregateBatteryConsumer)1 BatteryConsumer (android.os.BatteryConsumer)1 BatteryUsageStats (android.os.BatteryUsageStats)1 Handler (android.os.Handler)1 Looper (android.os.Looper)1 Message (android.os.Message)1 Process (android.os.Process)1 UidBatteryConsumer (android.os.UidBatteryConsumer)1 UserBatteryConsumer (android.os.UserBatteryConsumer)1 UserHandle (android.os.UserHandle)1 UserManager (android.os.UserManager)1 TextUtils (android.text.TextUtils)1 DateUtils (android.text.format.DateUtils)1 ArrayMap (android.util.ArrayMap)1 SparseArray (android.util.SparseArray)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1