OSDN Git Service

Fix the ProgressBar in DataUsageSummaryV2 page.
authorSundeep Ghuman <sghuman@google.com>
Tue, 13 Mar 2018 05:25:23 +0000 (22:25 -0700)
committerSundeep Ghuman <sghuman@google.com>
Wed, 14 Mar 2018 23:21:46 +0000 (16:21 -0700)
Bug: 70950124
Test: make RunSettingsRoboTests ROBOTEST_FILTER=DataUsageSummaryPreferenceTest
Change-Id: I83f268c0824d9e3520899725fa572753e1de0814

res/drawable/data_usage_progress.xml [new file with mode: 0644]
res/layout/data_usage_summary_preference.xml
src/com/android/settings/datausage/DataUsageSummaryPreference.java
src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
src/com/android/settings/widget/LinearColorBar.java
tests/robotests/src/com/android/settings/datausage/DataUsageSummaryPreferenceTest.java

diff --git a/res/drawable/data_usage_progress.xml b/res/drawable/data_usage_progress.xml
new file mode 100644 (file)
index 0000000..46b9a12
--- /dev/null
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:id="@android:id/background">
+        <shape android:shape="rectangle"
+               android:tint="?android:attr/colorControlActivated">
+            <corners android:radius="5dp" />
+            <size android:height="10dp" />
+            <solid android:color="@color/white_disabled" />
+        </shape>
+    </item>
+    <item android:id="@android:id/secondaryProgress">
+        <scale android:scaleWidth="100%">
+            <shape android:shape="rectangle"
+                   android:tint="?android:attr/colorControlActivated">
+                <corners android:radius="5dp" />
+                <size android:height="10dp" />
+                <solid android:color="@color/white_disabled" />
+            </shape>
+        </scale>
+    </item>
+    <item android:id="@android:id/progress">
+        <scale android:scaleWidth="100%">
+            <shape android:shape="rectangle"
+                   android:tint="?android:attr/colorControlActivated">
+                <corners android:radius="5dp" />
+                <size android:height="10dp" />
+                <solid android:color="@android:color/black" />
+            </shape>
+        </scale>
+    </item>
+</layer-list>
index c1a676a..c2e1581 100644 (file)
         android:textColor="?android:attr/colorAccent"
         android:textAppearance="@android:style/TextAppearance.Material.Large" />
 
-    <com.android.settings.widget.LinearColorBar
-        android:id="@+id/color_bar"
+    <android.widget.ProgressBar
+        android:id="@+id/determinateBar"
+        style="?android:attr/progressBarStyleHorizontal"
         android:layout_width="match_parent"
-        android:layout_height="28dp" />
+        android:layout_height="10dp"
+        android:progressDrawable="@drawable/data_usage_progress"/>
 
     <LinearLayout
         android:id="@+id/label_bar"
index d886154..dbf87fe 100644 (file)
@@ -18,14 +18,16 @@ package com.android.settings.datausage;
 
 import android.content.Context;
 import android.content.Intent;
+import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceViewHolder;
+import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.View;
 import android.widget.Button;
+import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import com.android.settings.R;
-import com.android.settings.SummaryPreference;
 import com.android.settingslib.utils.StringUtil;
 
 import java.util.Objects;
@@ -33,7 +35,11 @@ import java.util.Objects;
 /**
  * Provides a summary of data usage.
  */
-public class DataUsageSummaryPreference extends SummaryPreference {
+public class DataUsageSummaryPreference extends Preference {
+
+    private boolean mChartEnabled = true;
+    private String mStartLabel;
+    private String mEndLabel;
 
     private int mNumPlans;
     /** The ending time of the billing cycle in milliseconds since epoch. */
@@ -45,6 +51,9 @@ public class DataUsageSummaryPreference extends SummaryPreference {
     private String mLimitInfoText;
     private Intent mLaunchIntent;
 
+    /** Progress to display on ProgressBar */
+    private float mProgress;
+
     public DataUsageSummaryPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
         setLayoutResource(R.layout.data_usage_summary_preference);
@@ -57,6 +66,11 @@ public class DataUsageSummaryPreference extends SummaryPreference {
         }
     }
 
+    public void setProgress(float progress) {
+        mProgress = progress;
+        notifyChanged();
+    }
+
     public void setUsageInfo(long cycleEnd, long snapshotTime, CharSequence carrierName,
             int numPlans, Intent launchIntent) {
         mCycleEndTimeMs = cycleEnd;
@@ -67,10 +81,33 @@ public class DataUsageSummaryPreference extends SummaryPreference {
         notifyChanged();
     }
 
+    public void setChartEnabled(boolean enabled) {
+        if (mChartEnabled != enabled) {
+            mChartEnabled = enabled;
+            notifyChanged();
+        }
+    }
+
+    public void setLabels(String start, String end) {
+        mStartLabel = start;
+        mEndLabel = end;
+        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);
+            bar.setProgress((int) (mProgress * 100));
+            ((TextView) holder.findViewById(android.R.id.text1)).setText(mStartLabel);
+            ((TextView) holder.findViewById(android.R.id.text2)).setText(mEndLabel);
+        } else {
+            holder.findViewById(R.id.label_bar).setVisibility(View.GONE);
+        }
+
         TextView usageTitle = (TextView) holder.findViewById(R.id.usage_title);
         usageTitle.setVisibility(mNumPlans > 1 ? View.VISIBLE : View.GONE);
 
index 11da829..0729fff 100644 (file)
@@ -194,8 +194,7 @@ public class DataUsageSummaryPreferenceController extends BasePreferenceControll
             summaryPreference.setChartEnabled(true);
             summaryPreference.setLabels(Formatter.formatFileSize(mContext, 0 /* sizeBytes */),
                     Formatter.formatFileSize(mContext, mDataplanSize));
-            summaryPreference.setRatios(mDataplanUse / (float) mDataplanSize, 0 /* middle */,
-                    (mDataplanSize - mDataplanUse) / (float) mDataplanSize);
+            summaryPreference.setProgress(mDataplanUse / (float) mDataplanSize);
         }
         summaryPreference.setUsageInfo(mCycleEnd, mSnapshotTime, mCarrierName,
                 mDataplanCount, mManageSubscriptionIntent);
index b3e685e..df1403e 100644 (file)
@@ -26,6 +26,9 @@ import android.widget.LinearLayout;
 
 import com.android.settings.Utils;
 
+/**
+ * @Deprecated Use {@link android.widget.ProgressBar} instead.
+ */
 public class LinearColorBar extends LinearLayout {
 
     static final int RIGHT_COLOR = 0xffced7db;
index ecd556d..57f1f8c 100644 (file)
@@ -24,6 +24,8 @@ import android.support.v7.preference.PreferenceViewHolder;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.Button;
+import android.widget.LinearLayout;
+import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import com.android.settings.R;
@@ -54,6 +56,10 @@ public class DataUsageSummaryPreferenceTest {
     private TextView mCarrierInfo;
     private TextView mDataLimits;
     private Button mLaunchButton;
+    private LinearLayout mLabelBar;
+    private TextView mLabel1;
+    private TextView mLabel2;
+    private ProgressBar mProgressBar;
 
     private long mCycleEnd;
     private long mUpdateTime;
@@ -159,6 +165,46 @@ public class DataUsageSummaryPreferenceTest {
         assertThat(mDataLimits.getVisibility()).isEqualTo(View.GONE);
     }
 
+    @Test
+    public void testSetChartEnabledFalse_hidesLabelBar() {
+        setValidLabels();
+        mSummaryPreference.setChartEnabled(false);
+
+        bindViewHolder();
+        assertThat(mLabelBar.getVisibility()).isEqualTo(View.GONE);
+    }
+
+    @Test
+    public void testSetEmptyLabels_hidesLabelBar() {
+        mSummaryPreference.setLabels("", "");
+
+        bindViewHolder();
+        assertThat(mLabelBar.getVisibility()).isEqualTo(View.GONE);
+    }
+
+    @Test
+    public void testLabelBar_isVisible_whenLabelsSet() {
+        setValidLabels();
+        //mChartEnabled defaults to true
+
+        bindViewHolder();
+        assertThat(mLabelBar.getVisibility()).isEqualTo(View.VISIBLE);
+    }
+
+
+    @Test
+    public void testSetProgress_updatesProgressBar() {
+        setValidLabels();
+        mSummaryPreference.setProgress(.5f);
+
+        bindViewHolder();
+        assertThat(mProgressBar.getProgress()).isEqualTo(50);
+    }
+
+    private void setValidLabels() {
+        mSummaryPreference.setLabels("0.0 GB", "5.0 GB");
+    }
+
     private void bindViewHolder() {
         mSummaryPreference.onBindViewHolder(mHolder);
         mUsageTitle = (TextView) mHolder.findViewById(R.id.usage_title);
@@ -166,5 +212,9 @@ public class DataUsageSummaryPreferenceTest {
         mCarrierInfo = (TextView) mHolder.findViewById(R.id.carrier_and_update);
         mDataLimits = (TextView) mHolder.findViewById(R.id.data_limits);
         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);
+        mLabel2 = (TextView) mHolder.findViewById(R.id.text2);
+        mProgressBar = (ProgressBar) mHolder.findViewById(R.id.determinateBar);
     }
 }
\ No newline at end of file