Search in sources :

Example 1 with UidBatteryConsumer

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

the class BatteryEntry method add.

/**
 * Adds the consumed power of the supplied BatteryConsumer to this entry. Also
 * uses its package with highest drain, if necessary.
 */
public void add(BatteryConsumer batteryConsumer) {
    mConsumedPower += batteryConsumer.getConsumedPower();
    if (batteryConsumer instanceof UidBatteryConsumer) {
        UidBatteryConsumer uidBatteryConsumer = (UidBatteryConsumer) batteryConsumer;
        mTimeInForegroundMs += uidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_FOREGROUND);
        mTimeInBackgroundMs += uidBatteryConsumer.getTimeInStateMs(UidBatteryConsumer.STATE_BACKGROUND);
        if (mDefaultPackageName == null) {
            mDefaultPackageName = uidBatteryConsumer.getPackageWithHighestDrain();
        }
    }
}
Also used : UidBatteryConsumer(android.os.UidBatteryConsumer)

Example 2 with UidBatteryConsumer

use of android.os.UidBatteryConsumer 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)

Example 3 with UidBatteryConsumer

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

the class HighUsageDetector method detect.

@Override
public BatteryTip detect() {
    final long lastFullChargeTimeMs = mBatteryUtils.calculateLastFullChargeTime(mBatteryUsageStats, System.currentTimeMillis());
    if (mPolicy.highUsageEnabled && mDischarging) {
        parseBatteryData();
        if (mDataParser.isDeviceHeavilyUsed() || mPolicy.testHighUsageTip) {
            final double totalPower = mBatteryUsageStats.getConsumedPower();
            final int dischargeAmount = mBatteryUsageStats.getDischargePercentage();
            final List<UidBatteryConsumer> uidBatteryConsumers = mBatteryUsageStats.getUidBatteryConsumers();
            // Sort by descending power
            uidBatteryConsumers.sort((consumer1, consumer2) -> Double.compare(consumer2.getConsumedPower(), consumer1.getConsumedPower()));
            for (UidBatteryConsumer consumer : uidBatteryConsumers) {
                final double percent = mBatteryUtils.calculateBatteryPercent(consumer.getConsumedPower(), totalPower, dischargeAmount);
                if ((percent + 0.5f < 1f) || mBatteryUtils.shouldHideUidBatteryConsumer(consumer)) {
                    // Don't show it if we should hide or usage percentage is lower than 1%
                    continue;
                }
                mHighUsageAppList.add(new AppInfo.Builder().setUid(consumer.getUid()).setPackageName(mBatteryUtils.getPackageName(consumer.getUid())).build());
                if (mHighUsageAppList.size() >= mPolicy.highUsageAppCount) {
                    break;
                }
            }
            // When in test mode, add an app if necessary
            if (mPolicy.testHighUsageTip && mHighUsageAppList.isEmpty()) {
                mHighUsageAppList.add(new AppInfo.Builder().setPackageName(SETTINGS_PACKAGE_NAME).setScreenOnTimeMs(TimeUnit.HOURS.toMillis(3)).build());
            }
        }
    }
    return new HighUsageTip(lastFullChargeTimeMs, mHighUsageAppList);
}
Also used : HighUsageTip(com.android.settings.fuelgauge.batterytip.tips.HighUsageTip) UidBatteryConsumer(android.os.UidBatteryConsumer)

Example 4 with UidBatteryConsumer

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

the class BatteryEntryTest method createBatteryEntryForApp.

private BatteryEntry createBatteryEntryForApp(String[] packages, String packageName, String highDrainPackage) {
    UidBatteryConsumer consumer = mock(UidBatteryConsumer.class);
    when(consumer.getUid()).thenReturn(APP_UID);
    when(consumer.getPackageWithHighestDrain()).thenReturn(highDrainPackage);
    return new BatteryEntry(mMockContext, mockHandler, mockUserManager, consumer, false, APP_UID, packages, packageName);
}
Also used : UidBatteryConsumer(android.os.UidBatteryConsumer)

Aggregations

UidBatteryConsumer (android.os.UidBatteryConsumer)4 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 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