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

Commit b4b302f9 authored by ykhung's avatar ykhung Committed by YUKAI HUNG
Browse files

Export methods from BatteryEntry and controller for battery usage

Export a method to fetch BatteryEntry for battery usage new design, and
we have to fetch the label name each time when the new UI is shown to
avoid locale is changed from database records.

Bug: 180607705
Test: make RunSettingsRoboTests
Test: make RunSettingsGoogleRoboTests
Change-Id: Iae8f0b4812beb760b690e622f707fb67388221a2
parent 59aa4f9c
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
@@ -257,6 +257,27 @@ public class BatteryAppListPreferenceController extends AbstractPreferenceContro
        BatteryEntry.startRequestQueue();
    }

    /**
     * Gets the BatteryEntry list by using the supplied BatteryUsageStats.
     */
    public List<BatteryEntry> getBatteryEntryList(
            BatteryUsageStats batteryUsageStats, boolean showAllApps) {
        mBatteryUsageStats = USE_FAKE_DATA ? getFakeStats() : batteryUsageStats;
        if (!sConfig.shouldShowBatteryAttributionList(mContext)) {
            return null;
        }
        final int dischargePercentage = getDischargePercentage(batteryUsageStats);
        final List<BatteryEntry> usageList = getCoalescedUsageList(showAllApps);
        final double totalPower = batteryUsageStats.getConsumedPower();
        for (int i = 0; i < usageList.size(); i++) {
            final BatteryEntry entry = usageList.get(i);
            final double percentOfTotal = mBatteryUtils.calculateBatteryPercent(
                    entry.getConsumedPower(), totalPower, dischargePercentage);
            entry.percent = percentOfTotal;
        }
        return usageList;
    }

    private int getDischargePercentage(BatteryUsageStats batteryUsageStats) {
        int dischargePercentage = batteryUsageStats.getDischargePercentage();
        if (dischargePercentage < 0) {
@@ -311,7 +332,7 @@ public class BatteryAppListPreferenceController extends AbstractPreferenceContro
            final int index = batteryEntryList.indexOfKey(realUid);
            if (index < 0) {
                // New entry.
                batteryEntryList.put(realUid, new BatteryEntry(mActivity, mHandler, mUserManager,
                batteryEntryList.put(realUid, new BatteryEntry(mContext, mHandler, mUserManager,
                        consumer, isHidden, packages, null));
            } else {
                // Combine BatterySippers if we already have one with this UID.
@@ -328,7 +349,7 @@ public class BatteryAppListPreferenceController extends AbstractPreferenceContro
                continue;
            }

            results.add(new BatteryEntry(mActivity, mHandler, mUserManager,
            results.add(new BatteryEntry(mContext, mHandler, mUserManager,
                    consumer, /* isHidden */ true, null, null));
        }

@@ -337,7 +358,7 @@ public class BatteryAppListPreferenceController extends AbstractPreferenceContro
                    mBatteryUsageStats.getUserBatteryConsumers();
            for (int i = 0, size = userBatteryConsumers.size(); i < size; i++) {
                final UserBatteryConsumer consumer = userBatteryConsumers.get(i);
                results.add(new BatteryEntry(mActivity, mHandler, mUserManager,
                results.add(new BatteryEntry(mContext, mHandler, mUserManager,
                        consumer, /* isHidden */ true, null, null));
            }
        }
+58 −43
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.os.UserBatteryConsumer;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;
import android.util.Pair;

import androidx.annotation.NonNull;

@@ -182,49 +183,10 @@ public class BatteryEntry {
            getQuickNameIconForUid(uid, packages);
            return;
        } else if (batteryConsumer instanceof SystemBatteryConsumer) {
            switch(((SystemBatteryConsumer) batteryConsumer).getDrainType()) {
                case SystemBatteryConsumer.DRAIN_TYPE_AMBIENT_DISPLAY:
                    name = context.getResources().getString(R.string.ambient_display_screen_title);
                    iconId = R.drawable.ic_settings_aod;
                    break;
                case SystemBatteryConsumer.DRAIN_TYPE_BLUETOOTH:
                    name = context.getResources().getString(R.string.power_bluetooth);
                    iconId = com.android.internal.R.drawable.ic_settings_bluetooth;
                    break;
                case SystemBatteryConsumer.DRAIN_TYPE_CAMERA:
                    name = context.getResources().getString(R.string.power_camera);
                    iconId = R.drawable.ic_settings_camera;
                    break;
                case SystemBatteryConsumer.DRAIN_TYPE_MOBILE_RADIO:
                    name = context.getResources().getString(R.string.power_cell);
                    iconId = R.drawable.ic_cellular_1_bar;
                    break;
                case SystemBatteryConsumer.DRAIN_TYPE_FLASHLIGHT:
                    name = context.getResources().getString(R.string.power_flashlight);
                    iconId = R.drawable.ic_settings_display;
                    break;
                case SystemBatteryConsumer.DRAIN_TYPE_PHONE:
                    name = context.getResources().getString(R.string.power_phone);
                    iconId = R.drawable.ic_settings_voice_calls;
                    break;
                case SystemBatteryConsumer.DRAIN_TYPE_SCREEN:
                    name = context.getResources().getString(R.string.power_screen);
                    iconId = R.drawable.ic_settings_display;
                    break;
                case SystemBatteryConsumer.DRAIN_TYPE_WIFI:
                    name = context.getResources().getString(R.string.power_wifi);
                    iconId = R.drawable.ic_settings_wireless;
                    break;
                case SystemBatteryConsumer.DRAIN_TYPE_IDLE:
                case SystemBatteryConsumer.DRAIN_TYPE_MEMORY:
                    name = context.getResources().getString(R.string.power_idle);
                    iconId = R.drawable.ic_settings_phone_idle;
                    break;
                case SystemBatteryConsumer.DRAIN_TYPE_CUSTOM:
                    name = null;
                    iconId = R.drawable.ic_power_system;
                    break;
            }
            final Pair<Integer, String> resourcePair = getResourcePairFromDrainType(
                    context, ((SystemBatteryConsumer) batteryConsumer).getDrainType());
            iconId = resourcePair.first;
            name = resourcePair.second;
        } else if (batteryConsumer instanceof UserBatteryConsumer) {
            UserInfo info = um.getUserInfo(((UserBatteryConsumer) batteryConsumer).getUserId());
            if (info != null) {
@@ -493,4 +455,57 @@ public class BatteryEntry {
                    ((UidBatteryConsumer) batteryConsumer).getPackageWithHighestDrain();
        }
    }

    /**
     * Gets icon ID and name from system battery consumer drain type.
     */
    public static Pair<Integer, String> getResourcePairFromDrainType(
            Context context, int drainType) {
        String name = null;
        int iconId = 0;
        switch (drainType) {
            case SystemBatteryConsumer.DRAIN_TYPE_AMBIENT_DISPLAY:
                name = context.getResources().getString(R.string.ambient_display_screen_title);
                iconId = R.drawable.ic_settings_aod;
                break;
            case SystemBatteryConsumer.DRAIN_TYPE_BLUETOOTH:
                name = context.getResources().getString(R.string.power_bluetooth);
                iconId = com.android.internal.R.drawable.ic_settings_bluetooth;
                break;
            case SystemBatteryConsumer.DRAIN_TYPE_CAMERA:
                name = context.getResources().getString(R.string.power_camera);
                iconId = R.drawable.ic_settings_camera;
                break;
            case SystemBatteryConsumer.DRAIN_TYPE_MOBILE_RADIO:
                name = context.getResources().getString(R.string.power_cell);
                iconId = R.drawable.ic_cellular_1_bar;
                break;
            case SystemBatteryConsumer.DRAIN_TYPE_FLASHLIGHT:
                name = context.getResources().getString(R.string.power_flashlight);
                iconId = R.drawable.ic_settings_display;
                break;
            case SystemBatteryConsumer.DRAIN_TYPE_PHONE:
                name = context.getResources().getString(R.string.power_phone);
                iconId = R.drawable.ic_settings_voice_calls;
                break;
            case SystemBatteryConsumer.DRAIN_TYPE_SCREEN:
                name = context.getResources().getString(R.string.power_screen);
                iconId = R.drawable.ic_settings_display;
                break;
            case SystemBatteryConsumer.DRAIN_TYPE_WIFI:
                name = context.getResources().getString(R.string.power_wifi);
                iconId = R.drawable.ic_settings_wireless;
                break;
            case SystemBatteryConsumer.DRAIN_TYPE_IDLE:
            case SystemBatteryConsumer.DRAIN_TYPE_MEMORY:
                name = context.getResources().getString(R.string.power_idle);
                iconId = R.drawable.ic_settings_phone_idle;
                break;
            case SystemBatteryConsumer.DRAIN_TYPE_CUSTOM:
                name = null;
                iconId = R.drawable.ic_power_system;
                break;
        }
        return new Pair<>(Integer.valueOf(iconId), name);
    }
}