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

Commit ca102fac authored by jackqdyulei's avatar jackqdyulei
Browse files

Add high usage battery tip

1. Add both model and detector
2. Move the screen usage method to BatteryUtils
so we could reuse it.
3. Add and update the tests

Bug: 70570352
Test: RunSettingsRoboTests

Change-Id: I6a7248d9d48ee8cb6fc2c18c8c225210d49b6bc9
parent 5ff1df89
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -4773,6 +4773,21 @@
    <string name="battery_tip_low_battery_title">Low battery capacity</string>
    <!-- Summary for the low battery tip [CHAR LIMIT=NONE] -->
    <string name="battery_tip_low_battery_summary">Battery can\'t provide good battery life</string>
    <!-- Title for the battery high usage tip [CHAR LIMIT=NONE] -->
    <string name="battery_tip_high_usage_title" product="default">Phone used heavily</string>
    <!-- Title for the battery high usage tip [CHAR LIMIT=NONE] -->
    <string name="battery_tip_high_usage_title" product="tablet">Tablet used heavily</string>
    <!-- Title for the battery high usage tip [CHAR LIMIT=NONE] -->
    <string name="battery_tip_high_usage_title" product="device">Device used heavily</string>
    <!-- Summary for the battery high usage tip, which presents how many hours the device been used since last full charge [CHAR LIMIT=NONE] -->
    <string name="battery_tip_high_usage_summary">About <xliff:g id="hour">%1$s</xliff:g> used since last full charge</string>
    <!-- Message for battery tip dialog to show the status about the battery [CHAR LIMIT=NONE] -->
    <string name="battery_tip_dialog_message" product="default">Your phone was used heavily and this consumed a lot of battery. Your battery is behaving normally.\n\n Your phone was used for about <xliff:g id="hour">%1$s</xliff:g> since last full charge.\n\n Total usage:</string>
    <!-- Message for battery tip dialog to show the status about the battery [CHAR LIMIT=NONE] -->
    <string name="battery_tip_dialog_message" product="tablet">Your tablet was used heavily and this consumed a lot of battery. Your battery is behaving normally.\n\n Your tablet was used for about <xliff:g id="hour">%1$s</xliff:g> since last full charge.\n\n Total usage:</string>
    <!-- Message for battery tip dialog to show the status about the battery [CHAR LIMIT=NONE] -->
    <string name="battery_tip_dialog_message" product="device">Your device was used heavily and this consumed a lot of battery. Your battery is behaving normally.\n\n Your device was used for about <xliff:g id="hour">%1$s</xliff:g> since last full charge.\n\n Total usage:</string>
    <!-- Title for the smart battery manager preference [CHAR LIMIT=NONE] -->
    <string name="smart_battery_manager_title">Smart battery manager</string>
    <!-- Title for the smart battery toggle [CHAR LIMIT=NONE] -->
+25 −0
Original line number Diff line number Diff line
@@ -345,6 +345,17 @@ public class BatteryUtils {

    }

    /**
     * Calculate the screen usage time since last full charge.
     * @param batteryStatsHelper utility class that contains the screen usage data
     * @return time in millis
     */
    public long calculateScreenUsageTime(BatteryStatsHelper batteryStatsHelper) {
        final BatterySipper sipper = findBatterySipperByType(
                batteryStatsHelper.getUsageList(), BatterySipper.DrainType.SCREEN);
        return sipper != null ? sipper.usageTimeMs : 0;
    }

    public static void logRuntime(String tag, String message, long startTime) {
        Log.d(tag, message + ": " + (System.currentTimeMillis() - startTime) + "ms");
    }
@@ -432,6 +443,20 @@ public class BatteryUtils {
        return batteryInfo;
    }

    /**
     * Find the {@link BatterySipper} with the corresponding {@link BatterySipper.DrainType}
     */
    public BatterySipper findBatterySipperByType(List<BatterySipper> usageList,
            BatterySipper.DrainType type) {
        for (int i = 0, size = usageList.size(); i < size; i++) {
            final BatterySipper sipper = usageList.get(i);
            if (sipper.drainType == type) {
                return sipper;
            }
        }
        return null;
    }

    private boolean isDataCorrupted() {
        return mPackageManager == null || mAppOpsManager == null;
    }
+2 −21
Original line number Diff line number Diff line
@@ -369,8 +369,9 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
        restartBatteryInfoLoader();
        final long lastFullChargeTime = mBatteryUtils.calculateLastFullChargeTime(mStatsHelper,
                System.currentTimeMillis());
        updateScreenPreference();
        updateLastFullChargePreference(lastFullChargeTime);
        mScreenUsagePref.setSubtitle(Utils.formatElapsedTime(getContext(),
                mBatteryUtils.calculateScreenUsageTime(mStatsHelper), false));

        final CharSequence timeSequence = Utils.formatRelativeTime(context, lastFullChargeTime,
                false);
@@ -393,26 +394,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
        return new AnomalyDetectionPolicy(getContext());
    }

    @VisibleForTesting
    BatterySipper findBatterySipperByType(List<BatterySipper> usageList, DrainType type) {
        for (int i = 0, size = usageList.size(); i < size; i++) {
            final BatterySipper sipper = usageList.get(i);
            if (sipper.drainType == type) {
                return sipper;
            }
        }
        return null;
    }

    @VisibleForTesting
    void updateScreenPreference() {
        final BatterySipper sipper = findBatterySipperByType(
                mStatsHelper.getUsageList(), DrainType.SCREEN);
        final long usageTimeMs = sipper != null ? sipper.usageTimeMs : 0;

        mScreenUsagePref.setSubtitle(Utils.formatElapsedTime(getContext(), usageTimeMs, false));
    }

    @VisibleForTesting
    void updateLastFullChargePreference(long timeMs) {
        final CharSequence timeSequence = Utils.formatRelativeTime(getContext(), timeMs, false);
+3 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.batterytip.detectors.BatteryTipDetector;
import com.android.settings.fuelgauge.batterytip.detectors.HighUsageDetector;
import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector;
import com.android.settings.fuelgauge.batterytip.detectors.SummaryDetector;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
@@ -65,6 +66,8 @@ public class BatteryTipLoader extends AsyncLoader<List<BatteryTip>> {
        mVisibleTips = 0;

        addBatteryTipFromDetector(tips, new LowBatteryDetector(policy, batteryInfo));
        addBatteryTipFromDetector(tips,
                new HighUsageDetector(getContext(), policy, mBatteryStatsHelper));
        // Add summary detector at last since it need other detectors to update the mVisibleTips
        addBatteryTipFromDetector(tips, new SummaryDetector(policy, mVisibleTips));

+84 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.fuelgauge.batterytip.detectors;

import android.content.Context;
import android.os.BatteryStats;
import android.support.annotation.VisibleForTesting;
import android.text.format.DateUtils;

import com.android.internal.os.BatterySipper;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.Utils;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Detector whether to show summary tip. This detector should be executed as the last
 * {@link BatteryTipDetector} since it need the most up-to-date {@code visibleTips}
 */
public class HighUsageDetector implements BatteryTipDetector {
    private BatteryTipPolicy mPolicy;
    private BatteryStatsHelper mBatteryStatsHelper;
    private List<HighUsageTip.HighUsageApp> mHighUsageAppList;
    private Context mContext;
    @VisibleForTesting
    BatteryUtils mBatteryUtils;

    public HighUsageDetector(Context context, BatteryTipPolicy policy,
            BatteryStatsHelper batteryStatsHelper) {
        mContext = context;
        mPolicy = policy;
        mBatteryStatsHelper = batteryStatsHelper;
        mHighUsageAppList = new ArrayList<>();
        mBatteryUtils = BatteryUtils.getInstance(context);
    }

    @Override
    public BatteryTip detect() {
        final long screenUsageTimeMs = mBatteryUtils.calculateScreenUsageTime(mBatteryStatsHelper);
        //TODO(b/70570352): Change it to detect whether battery drops 25% in last 2 hours
        if (mPolicy.highUsageEnabled && screenUsageTimeMs > DateUtils.HOUR_IN_MILLIS) {
            final List<BatterySipper> batterySippers = mBatteryStatsHelper.getUsageList();
            for (int i = 0, size = batterySippers.size(); i < size; i++) {
                final BatterySipper batterySipper = batterySippers.get(i);
                if (!mBatteryUtils.shouldHideSipper(batterySipper)) {
                    final long foregroundTimeMs = mBatteryUtils.getProcessTimeMs(
                            BatteryUtils.StatusType.FOREGROUND, batterySipper.uidObj,
                            BatteryStats.STATS_SINCE_CHARGED);
                    mHighUsageAppList.add(new HighUsageTip.HighUsageApp(
                            mBatteryUtils.getPackageName(batterySipper.getUid()),
                            foregroundTimeMs));
                }
            }

            mHighUsageAppList = mHighUsageAppList.subList(0,
                    Math.min(mPolicy.highUsageAppCount, mHighUsageAppList.size()));
            Collections.sort(mHighUsageAppList, Collections.reverseOrder());
        }

        return new HighUsageTip(Utils.formatElapsedTime(mContext, screenUsageTimeMs, false),
                mHighUsageAppList);
    }
}
Loading