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

Commit 9bfbc3f1 authored by Sundeep Ghuman's avatar Sundeep Ghuman
Browse files

Modify "Updated ..." text.

Use d/h/m "narrow" time formatting with no period.

Change font style when update is "old."

Bug: 70950124
Test: make RunSettingsRoboTests
ROBOTEST_FILTER=DataUsageSummaryPreferenceTest

Change-Id: I25902c98eb0fdf4dd2f64c3f5d668b5efdcdff0c
parent d0699182
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -8800,10 +8800,16 @@
    <string name="billing_cycle_less_than_one_day_left">Less than 1 day left</string>
    <!-- Informational text about carrier and update time [CHAR LIMIT=30] -->
    <string name="carrier_and_update_text">Updated by <xliff:g name="carrier" example="T-mobile">%1$s</xliff:g> <xliff:g name="time" example="3m">%2$s</xliff:g></string>
    <string name="carrier_and_update_text">Updated by <xliff:g name="carrier" example="T-mobile">^1</xliff:g> <xliff:g name="time" example="3m">^2</xliff:g> ago</string>
    <!-- Informational text about update time only, without carrier [CHAR LIMIT=30] -->
    <string name="no_carrier_update_text">Updated <xliff:g name="time" example="3m">%1$s</xliff:g></string>
    <!-- Informational text about update time only, without carrier. First argument intentionally skipped. [CHAR LIMIT=30] -->
    <string name="no_carrier_update_text">Updated <xliff:g name="time" example="3m">^2</xliff:g> ago</string>
    <!-- Informational text about a recent carrier and update time [CHAR LIMIT=30] -->
    <string name="carrier_and_update_now_text">Updated by <xliff:g name="carrier" example="T-mobile">^1</xliff:g> just now</string>
    <!-- Informational text about recent update time only, without carrier [CHAR LIMIT=30] -->
    <string name="no_carrier_update_now_text">Updated just now</string>
    <!-- Button to launch external data plan app [CHAR LIMIT=30] -->
    <string name="launch_mdp_app_text">VIEW PLAN</string>
+58 −13
Original line number Diff line number Diff line
@@ -16,8 +16,10 @@

package com.android.settings.datausage;

import android.annotation.AttrRes;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.text.Spannable;
@@ -31,6 +33,7 @@ import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settingslib.Utils;
import com.android.settingslib.utils.StringUtil;
@@ -44,6 +47,8 @@ import java.util.concurrent.TimeUnit;
public class DataUsageSummaryPreference extends Preference {
    private static final long MILLIS_IN_A_DAY = TimeUnit.DAYS.toMillis(1);
    private static final long WARNING_AGE = TimeUnit.HOURS.toMillis(6L);
    @VisibleForTesting static final Typeface SANS_SERIF_MEDIUM =
            Typeface.create("sans-serif-medium", Typeface.NORMAL);

    private boolean mChartEnabled = true;
    private String mStartLabel;
@@ -216,24 +221,64 @@ public class DataUsageSummaryPreference extends Preference {

    private void updateCarrierInfo(TextView carrierInfo) {
        if (mNumPlans > 0 && mSnapshotTimeMs >= 0L) {
            long updateAge = System.currentTimeMillis() - mSnapshotTimeMs;
            carrierInfo.setVisibility(View.VISIBLE);
            long updateAgeMillis = calculateTruncatedUpdateAge();

            int textResourceId;
            CharSequence updateTime = null;
            if (updateAgeMillis == 0) {
                if (mCarrierName != null) {
                carrierInfo.setText(getContext().getString(R.string.carrier_and_update_text,
                        mCarrierName, StringUtil.formatRelativeTime(
                                getContext(), updateAge, false /* withSeconds */)));
                    textResourceId = R.string.carrier_and_update_now_text;
                } else {
                carrierInfo.setText(getContext().getString(R.string.no_carrier_update_text,
                        StringUtil.formatRelativeTime(
                                getContext(), updateAge, false /* withSeconds */)));
                    textResourceId = R.string.no_carrier_update_now_text;
                }
            } else {
                if (mCarrierName != null) {
                    textResourceId = R.string.carrier_and_update_text;
                } else {
                    textResourceId = R.string.no_carrier_update_text;
                }
                updateTime = StringUtil.formatElapsedTime(
                        getContext(), updateAgeMillis, false /* withSeconds */);
            }
            carrierInfo.setText(TextUtils.expandTemplate(
                    getContext().getText(textResourceId),
                    mCarrierName,
                    updateTime));

            if (updateAgeMillis <= WARNING_AGE) {
                setCarrierInfoTextStyle(
                        carrierInfo, android.R.attr.textColorSecondary, Typeface.SANS_SERIF);
            } else {
                setCarrierInfoTextStyle(carrierInfo, android.R.attr.colorError, SANS_SERIF_MEDIUM);
            }

            carrierInfo.setTextColor(
                    updateAge <= WARNING_AGE
                    ? Utils.getColorAttr(getContext(), android.R.attr.textColorPrimary)
                    : Utils.getColorAttr(getContext(), android.R.attr.colorError));
        } else {
            carrierInfo.setVisibility(View.GONE);
        }
    }

    /**
     * Returns the time since the last carrier update, as defined by {@link #mSnapshotTimeMs},
     * truncated to the nearest day / hour / minute in milliseconds, or 0 if less than 1 min.
     */
    private long calculateTruncatedUpdateAge() {
        long updateAgeMillis = System.currentTimeMillis() - mSnapshotTimeMs;

        // Round to nearest whole unit
        if (updateAgeMillis >= TimeUnit.DAYS.toMillis(1)) {
            return (updateAgeMillis / TimeUnit.DAYS.toMillis(1)) * TimeUnit.DAYS.toMillis(1);
        } else if (updateAgeMillis >= TimeUnit.HOURS.toMillis(1)) {
            return (updateAgeMillis / TimeUnit.HOURS.toMillis(1)) * TimeUnit.HOURS.toMillis(1);
        } else if (updateAgeMillis >= TimeUnit.MINUTES.toMillis(1)) {
            return (updateAgeMillis / TimeUnit.MINUTES.toMillis(1)) * TimeUnit.MINUTES.toMillis(1);
        } else {
            return 0;
        }
    }

    private void setCarrierInfoTextStyle(
            TextView carrierInfo, @AttrRes int colorId, Typeface typeface) {
        carrierInfo.setTextColor(Utils.getColorAttr(getContext(), colorId));
        carrierInfo.setTypeface(typeface);
    }
}
+91 −8
Original line number Diff line number Diff line
@@ -16,8 +16,11 @@

package com.android.settings.datausage;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.preference.PreferenceViewHolder;
import android.view.LayoutInflater;
import android.view.View;
@@ -30,8 +33,6 @@ import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
import com.android.settingslib.Utils;
import com.android.settingslib.utils.StringUtil;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;
@@ -42,8 +43,6 @@ import org.robolectric.annotation.Config;

import java.util.concurrent.TimeUnit;

import static com.google.common.truth.Truth.assertThat;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = SettingsShadowResourcesImpl.class)
public class DataUsageSummaryPreferenceTest {
@@ -123,7 +122,88 @@ public class DataUsageSummaryPreferenceTest {
    }

    @Test
    public void testSetUsageInfo_withRecentCarrierUpdate_doesNotSetCarrierInfoWarningColor() {
    public void testCarrierUpdateTime_shouldFormatDaysCorrectly() {
        int baseUnit = 2;
        int smudge = 6;
        final long updateTime = System.currentTimeMillis()
                - TimeUnit.DAYS.toMillis(baseUnit) - TimeUnit.HOURS.toMillis(smudge);
        mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
        mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
                new Intent());

        bindViewHolder();
        assertThat(mCarrierInfo.getText().toString())
                .isEqualTo("Updated by " + DUMMY_CARRIER + " " + baseUnit + "d ago");
    }

    @Test
    public void testCarrierUpdateTime_shouldFormatHoursCorrectly() {
        int baseUnit = 2;
        int smudge = 6;
        final long updateTime = System.currentTimeMillis()
                - TimeUnit.HOURS.toMillis(baseUnit) - TimeUnit.MINUTES.toMillis(smudge);
        mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
        mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
                new Intent());

        bindViewHolder();
        assertThat(mCarrierInfo.getText().toString())
                .isEqualTo("Updated by " + DUMMY_CARRIER + " " + baseUnit + "h ago");
    }

    @Test
    public void testCarrierUpdateTime_shouldFormatMinutesCorrectly() {
        int baseUnit = 2;
        int smudge = 6;
        final long updateTime = System.currentTimeMillis()
                - TimeUnit.MINUTES.toMillis(baseUnit) - TimeUnit.SECONDS.toMillis(smudge);
        mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
        mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
                new Intent());

        bindViewHolder();
        assertThat(mCarrierInfo.getText().toString())
                .isEqualTo("Updated by " + DUMMY_CARRIER + " " + baseUnit + "m ago");
    }

    @Test
    public void testCarrierUpdateTime_shouldFormatLessThanMinuteCorrectly() {
        final long updateTime = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(45);
        mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
        mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
                new Intent());

        bindViewHolder();
        assertThat(mCarrierInfo.getText().toString())
                .isEqualTo("Updated by " + DUMMY_CARRIER + " just now");
    }

    @Test
    public void testCarrierUpdateTimeWithNoCarrier_shouldSayJustNow() {
        final long updateTime = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(45);
        mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
        mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, null /* carrier */,
                1 /* numPlans */, new Intent());

        bindViewHolder();
        assertThat(mCarrierInfo.getText().toString())
                .isEqualTo("Updated just now");
    }

    @Test
    public void testCarrierUpdateTimeWithNoCarrier_shouldFormatTime() {
        final long updateTime = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(2);
        mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
        mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, null /* carrier */,
                1 /* numPlans */, new Intent());

        bindViewHolder();
        assertThat(mCarrierInfo.getText().toString())
                .isEqualTo("Updated 2m ago");
    }

    @Test
    public void testSetUsageInfo_withRecentCarrierUpdate_doesNotSetCarrierInfoWarningColorAndFont() {
        final long updateTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1);
        mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
        mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
@@ -132,11 +212,12 @@ public class DataUsageSummaryPreferenceTest {
        bindViewHolder();
        assertThat(mCarrierInfo.getVisibility()).isEqualTo(View.VISIBLE);
        assertThat(mCarrierInfo.getCurrentTextColor()).isEqualTo(
                Utils.getColorAttr(mContext, android.R.attr.textColorPrimary));
                Utils.getColorAttr(mContext, android.R.attr.textColorSecondary));
        assertThat(mCarrierInfo.getTypeface()).isEqualTo(Typeface.SANS_SERIF);
    }

    @Test
    public void testSetUsageInfo_withStaleCarrierUpdate_setsCarrierInfoWarningColor() {
    public void testSetUsageInfo_withStaleCarrierUpdate_setsCarrierInfoWarningColorAndFont() {
        final long updateTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(7);
        mSummaryPreference.setUsageInfo(mCycleEnd, updateTime, DUMMY_CARRIER, 1 /* numPlans */,
                new Intent());
@@ -145,6 +226,8 @@ public class DataUsageSummaryPreferenceTest {
        assertThat(mCarrierInfo.getVisibility()).isEqualTo(View.VISIBLE);
        assertThat(mCarrierInfo.getCurrentTextColor()).isEqualTo(
                Utils.getColorAttr(mContext, android.R.attr.colorError));
        assertThat(mCarrierInfo.getTypeface()).isEqualTo(
                DataUsageSummaryPreference.SANS_SERIF_MEDIUM);
    }

    @Test