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

Commit 9753fe6e authored by Raff Tsai's avatar Raff Tsai
Browse files

Change batteryTip text

Fixes: 121042353
Test: Robolectric
Change-Id: I96ee0d88b69b97bed09c6a5d12bf736283503cc7
parent 0879ad0b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -950,6 +950,8 @@
    <string name="power_discharge_by_only">Should last until about <xliff:g id="time">%1$s</xliff:g></string>
    <!-- [CHAR_LIMIT=100] Label for estimated time that phone will run out of battery -->
    <string name="power_discharge_by_only_short">Until <xliff:g id="time" example="12 PM">%1$s</xliff:g></string>
    <!-- [CHAR_LIMIT=100] Extend the battery life past a certain time -->
    <string name="power_suggestion_extend_battery">Extend battery life past <xliff:g id="time" example="12 PM">%1$s</xliff:g></string>

    <!-- [CHAR_LIMIT=60] label for estimated remaining duration of battery when under a certain amount -->
    <string name="power_remaining_less_than_duration_only">Less than <xliff:g id="threshold">%1$s</xliff:g> remaining</string>
+42 −14
Original line number Diff line number Diff line
@@ -101,7 +101,29 @@ public class PowerUtil {
        if (drainTimeMs <= ONE_DAY_MILLIS) {
            return getRegularTimeRemainingShortString(context, drainTimeMs);
        } else {
            return getMoreThanOneDayShortString(context, drainTimeMs);
            return getMoreThanOneDayShortString(context, drainTimeMs,
                R.string.power_remaining_duration_only_short);
        }
    }

    /**
     * This method produces the text used in Settings battery tip to describe the effect after
     * use the tip.
     *
     * @param context
     * @param drainTimeMs The estimated time remaining before the phone dies in milliseconds.
     * @return a properly formatted and localized string
     */
    public static String getBatteryTipStringFormatted(Context context, long drainTimeMs) {
        if (drainTimeMs <= 0) {
            return null;
        }
        if (drainTimeMs <= ONE_DAY_MILLIS) {
            return context.getString(R.string.power_suggestion_extend_battery,
                getDateTimeStringFromMs(context, drainTimeMs));
        } else {
            return getMoreThanOneDayShortString(context, drainTimeMs,
                R.string.power_remaining_only_more_than_subtext);
        }
    }

@@ -144,12 +166,13 @@ public class PowerUtil {
        }
    }

    private static String getMoreThanOneDayShortString(Context context, long drainTimeMs) {
    private static String getMoreThanOneDayShortString(Context context, long drainTimeMs,
            int resId) {
        final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS);
        CharSequence timeString = StringUtil.formatElapsedTime(context, roundedTimeMs,
                false /* withSeconds */);

        return context.getString(R.string.power_remaining_duration_only_short, timeString);
        return context.getString(resId, timeString);
    }

    private static String getMoreThanTwoDaysString(Context context, String percentageString) {
@@ -169,17 +192,8 @@ public class PowerUtil {

    private static String getRegularTimeRemainingString(Context context, long drainTimeMs,
            String percentageString, boolean basedOnUsage) {
        // Get the time of day we think device will die rounded to the nearest 15 min.
        final long roundedTimeOfDayMs =
                roundTimeToNearestThreshold(
                        System.currentTimeMillis() + drainTimeMs,
                        FIFTEEN_MINUTES_MILLIS);

        // convert the time to a properly formatted string.
        String skeleton = android.text.format.DateFormat.getTimeFormatString(context);
        DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton);
        Date date = Date.from(Instant.ofEpochMilli(roundedTimeOfDayMs));
        CharSequence timeString = fmt.format(date);
        CharSequence timeString = getDateTimeStringFromMs(context, drainTimeMs);

        if (TextUtils.isEmpty(percentageString)) {
            int id = basedOnUsage
@@ -194,6 +208,20 @@ public class PowerUtil {
        }
    }

    private static CharSequence getDateTimeStringFromMs(Context context, long drainTimeMs) {
        // Get the time of day we think device will die rounded to the nearest 15 min.
        final long roundedTimeOfDayMs =
                roundTimeToNearestThreshold(
                        System.currentTimeMillis() + drainTimeMs,
                        FIFTEEN_MINUTES_MILLIS);

        // convert the time to a properly formatted string.
        String skeleton = android.text.format.DateFormat.getTimeFormatString(context);
        DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton);
        Date date = Date.from(Instant.ofEpochMilli(roundedTimeOfDayMs));
        return fmt.format(date);
    }

    private static String getRegularTimeRemainingShortString(Context context, long drainTimeMs) {
        // Get the time remaining rounded to the nearest 15 min
        final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, FIFTEEN_MINUTES_MILLIS);
+19 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ public class PowerUtilTest {
    private static final long THIRTY_HOURS_MILLIS = Duration.ofHours(30).toMillis();
    private static final String NORMAL_CASE_EXPECTED_PREFIX = "Should last until about";
    private static final String ENHANCED_SUFFIX = " based on your usage";
    private static final String EXTEND_PREFIX = "Extend battery life past";
    // matches a time (ex: '1:15 PM', '2 AM', '23:00')
    private static final String TIME_OF_DAY_REGEX = " (\\d)+:?(\\d)* ((AM)*)|((PM)*)";
    // matches a percentage with parenthesis (ex: '(10%)')
@@ -175,6 +176,24 @@ public class PowerUtilTest {
        assertThat(info2).isEqualTo("More than 2 days remaining (10%)");
    }

    @Test
    public void getBatteryTipStringFormatted_moreThanOneDay_usesCorrectString() {
        String info = PowerUtil.getBatteryTipStringFormatted(mContext,
                THREE_DAYS_MILLIS);

        assertThat(info).isEqualTo("More than 3 days remaining");
    }

    @Test
    public void getBatteryTipStringFormatted_lessThanOneDay_usesCorrectString() {
        String info = PowerUtil.getBatteryTipStringFormatted(mContext,
                SEVENTEEN_MIN_MILLIS);

        // ex: Extend battery life past 1:15 PM
        assertThat(info).containsMatch(Pattern.compile(
                EXTEND_PREFIX + TIME_OF_DAY_REGEX));
    }

    @Test
    public void testRoundToNearestThreshold_roundsCorrectly() {
        // test some pretty normal values