android:textColor="?android:attr/textColorSecondary"
android:text="@string/data_usage_title" />
- <TextView
- android:id="@android:id/title"
- android:paddingTop="12dp"
- android:paddingBottom="4dp"
+ <LinearLayout
+ android:id="@+id/usage_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:fontFamily="@*android:string/config_headlineFontFamily"
- android:textColor="?android:attr/colorAccent"
- android:textAppearance="@android:style/TextAppearance.Material.Large" />
+ android:paddingTop="12dp"
+ android:paddingBottom="4dp"
+ android:orientation="horizontal">
+
+ <TextView android:id="@+id/data_usage_view"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:fontFamily="@*android:string/config_headlineFontFamily"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:textColor="?android:attr/colorAccent" />
+
+ <Space
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_weight="1" />
+
+ <TextView android:id="@+id/data_remaining_view"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:fontFamily="@*android:string/config_headlineFontFamily"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:textColor="?android:attr/colorAccent" />
+
+ </LinearLayout>
<android.widget.ProgressBar
android:id="@+id/determinateBar"
<string name="data_used"><xliff:g name="bytes" example="2 GB">^1</xliff:g> used</string>
<!-- Optional part of data usage showing the remaining amount [CHAR LIMIT=30] -->
- <string name="data_remaining"><xliff:g name="bytes" example="2 GB">, ^1</xliff:g> left</string>
+ <string name="data_remaining"><xliff:g name="bytes" example="2 GB">^1</xliff:g> left</string>
<!-- Informational text about time left in billing cycle [CHAR LIMIT=30] -->
<string name="cycle_left_time_text"><xliff:g name="time" example="2d">%1$s</xliff:g> left in this cycle</string>
static CharSequence formatUsage(Context context, String template, long usageLevel) {
final float LARGER_SIZE = 1.25f * 1.25f; // (1/0.8)^2
final float SMALLER_SIZE = 1.0f / LARGER_SIZE; // 0.8^2
+ return formatUsage(context, template, usageLevel, LARGER_SIZE, SMALLER_SIZE);
+ }
+
+ static CharSequence formatUsage(Context context, String template, long usageLevel,
+ float larger, float smaller) {
final int FLAGS = Spannable.SPAN_INCLUSIVE_INCLUSIVE;
final Formatter.BytesResult usedResult = Formatter.formatBytes(context.getResources(),
usageLevel, Formatter.FLAG_CALCULATE_ROUNDED);
final SpannableString enlargedValue = new SpannableString(usedResult.value);
- enlargedValue.setSpan(new RelativeSizeSpan(LARGER_SIZE), 0, enlargedValue.length(), FLAGS);
+ enlargedValue.setSpan(new RelativeSizeSpan(larger), 0, enlargedValue.length(), FLAGS);
final SpannableString amountTemplate = new SpannableString(
context.getString(com.android.internal.R.string.fileSizeSuffix)
enlargedValue, usedResult.units);
final SpannableString fullTemplate = new SpannableString(template);
- fullTemplate.setSpan(new RelativeSizeSpan(SMALLER_SIZE), 0, fullTemplate.length(), FLAGS);
+ fullTemplate.setSpan(new RelativeSizeSpan(smaller), 0, fullTemplate.length(), FLAGS);
return TextUtils.expandTemplate(fullTemplate,
BidiFormatter.getInstance().unicodeWrap(formattedUsage.toString()));
}
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.text.TextUtils;
+import android.text.format.Formatter;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
private String mStartLabel;
private String mEndLabel;
+ /** large vs small size is 36/16 ~ 2.25 */
+ private static final float LARGER_FONT_RATIO = 2.25f;
+ private static final float SMALLER_FONT_RATIO = 1.0f;
+
private int mNumPlans;
/** The ending time of the billing cycle in milliseconds since epoch. */
private long mCycleEndTimeMs;
/** Progress to display on ProgressBar */
private float mProgress;
+ private boolean mHasMobileData;
+
+ /**
+ * The size of the first registered plan if one exists or the size of the warning if it is set.
+ * -1 if no information is available.
+ */
+ private long mDataplanSize;
+
+ /** The number of bytes used since the start of the cycle. */
+ private long mDataplanUse;
public DataUsageSummaryPreference(Context context, AttributeSet attrs) {
super(context, attrs);
notifyChanged();
}
+ void setUsageNumbers(long used, long dataPlanSize, boolean hasMobileData) {
+ mDataplanUse = used;
+ mDataplanSize = dataPlanSize;
+ mHasMobileData = hasMobileData;
+ notifyChanged();
+ }
+
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
+
if (mChartEnabled && (!TextUtils.isEmpty(mStartLabel) || !TextUtils.isEmpty(mEndLabel))) {
holder.findViewById(R.id.label_bar).setVisibility(View.VISIBLE);
ProgressBar bar = (ProgressBar) holder.findViewById(R.id.determinateBar);
holder.findViewById(R.id.label_bar).setVisibility(View.GONE);
}
+ TextView usageNumberField = (TextView) holder.findViewById(R.id.data_usage_view);
+ usageNumberField.setText(TextUtils.expandTemplate(
+ getContext().getString(R.string.data_used),
+ Formatter.formatFileSize(getContext(), mDataplanUse)));
+ if (mHasMobileData && mNumPlans >= 0 && mDataplanSize > 0L) {
+ TextView usageRemainingField = (TextView) holder.findViewById(R.id.data_remaining_view);
+ usageRemainingField.setText(
+ TextUtils.expandTemplate(getContext().getText(R.string.data_remaining),
+ Formatter.formatFileSize(getContext(), mDataplanSize - mDataplanUse)));
+ }
+
TextView usageTitle = (TextView) holder.findViewById(R.id.usage_title);
usageTitle.setVisibility(mNumPlans > 1 ? View.VISIBLE : View.GONE);
summaryPreference.setLimitInfo(null);
}
- final StringBuilder title = new StringBuilder();
- if (mHasMobileData) {
- title.append(formatUsage(mContext, mContext.getString(R.string.data_used),
- mDataplanUse));
- if (mDataplanCount >= 0 && mDataplanSize > 0L) {
- title.append(formatUsage(mContext, mContext.getString(R.string.data_remaining),
- mDataplanSize - mDataplanUse));
- }
- } else {
- title.append(formatUsage(mContext, mContext.getString(mDataUsageTemplate),
- mDataplanUse));
- }
- summaryPreference.setTitle(title.toString());
+ summaryPreference.setUsageNumbers(mDataplanUse, mDataplanSize, mHasMobileData);
if (mDataplanSize <= 0) {
summaryPreference.setChartEnabled(false);
private TextView mCycleTime;
private TextView mCarrierInfo;
private TextView mDataLimits;
+ private TextView mDataUsed;
+ private TextView mDataRemaining;
private Button mLaunchButton;
private LinearLayout mLabelBar;
private TextView mLabel1;
mSummaryPreference.setLabels("0.0 GB", "5.0 GB");
}
+ public void testSetUsageAndRemainingInfo_withUsageInfo_dataUsageAndRemainingShown() {
+ mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 1 /* numPlans */,
+ new Intent());
+ mSummaryPreference.setUsageNumbers(1000000L, 10000000L, true);
+
+ bindViewHolder();
+ assertThat(mDataUsed.getText().toString()).isEqualTo("1.00 MB used");
+ assertThat(mDataRemaining.getText().toString()).isEqualTo("9.00 MB left");
+ }
+
+ @Test
+ public void testSetUsageInfo_withUsageInfo_dataUsageShown() {
+ mSummaryPreference.setUsageInfo(mCycleEnd, mUpdateTime, DUMMY_CARRIER, 0 /* numPlans */,
+ new Intent());
+ mSummaryPreference.setUsageNumbers(1000000L, -1L, true);
+
+ bindViewHolder();
+ assertThat(mDataUsed.getText().toString()).isEqualTo("1.00 MB used");
+ assertThat(mDataRemaining.getText()).isEqualTo("");
+ }
+
private void bindViewHolder() {
mSummaryPreference.onBindViewHolder(mHolder);
mUsageTitle = (TextView) mHolder.findViewById(R.id.usage_title);
mCycleTime = (TextView) mHolder.findViewById(R.id.cycle_left_time);
mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
mDataLimits = (TextView) mHolder.findViewById(R.id.data_limits);
+ mDataUsed = (TextView) mHolder.findViewById(R.id.data_usage_view);
+ mDataRemaining = (TextView) mHolder.findViewById(R.id.data_remaining_view);
mLaunchButton = (Button) mHolder.findViewById(R.id.launch_mdp_app_button);
mLabelBar = (LinearLayout) mHolder.findViewById(R.id.label_bar);
mLabel1 = (TextView) mHolder.findViewById(R.id.text1);