From c6f0b9d6f37dbb7a003d380629537504982a7ce8 Mon Sep 17 00:00:00 2001 From: Rajeev Kumar Date: Tue, 11 Jul 2017 15:44:46 -0700 Subject: [PATCH] Data plans setting peference. Change-Id: Ieb2aa67ab98140aa90501227345a15ad64cf124c Note: These changes are required for Data balance UI. Bug: 62349208 Test: make RunSettingsRoboTests -j40 --- .../settings_data_plan_summary_preference.xml | 79 +++++++++++++ .../datausage/DataPlanSummaryPreference.java | 90 +++++++++++++++ .../android/settings/widget/DonutView.java | 30 +++-- .../DataPlanSummaryPreferenceTest.java | 106 ++++++++++++++++++ 4 files changed, 297 insertions(+), 8 deletions(-) create mode 100644 res/layout/settings_data_plan_summary_preference.xml create mode 100644 src/com/android/settings/datausage/DataPlanSummaryPreference.java create mode 100644 tests/robotests/src/com/android/settings/datausage/DataPlanSummaryPreferenceTest.java diff --git a/res/layout/settings_data_plan_summary_preference.xml b/res/layout/settings_data_plan_summary_preference.xml new file mode 100644 index 00000000000..e8874745b1c --- /dev/null +++ b/res/layout/settings_data_plan_summary_preference.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/android/settings/datausage/DataPlanSummaryPreference.java b/src/com/android/settings/datausage/DataPlanSummaryPreference.java new file mode 100644 index 00000000000..f48e04d704f --- /dev/null +++ b/src/com/android/settings/datausage/DataPlanSummaryPreference.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2017 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.datausage; + +import android.content.Context; +import android.support.annotation.ColorRes; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceViewHolder; +import android.util.AttributeSet; +import android.widget.TextView; +import com.android.settings.R; +import com.android.settings.widget.DonutView; + +/** + * Provides a summary of data plans as preferences on settings page. + */ +public final class DataPlanSummaryPreference extends Preference { + private String mName; + private String mDescription; + private double mPercentageUsage; + private int mUsageTextColor; + private int mMeterBackgroundColor; + private int mMeterConsumedColor; + + public DataPlanSummaryPreference(Context context) { + super(context); + setLayoutResource(R.layout.settings_data_plan_summary_preference); + } + + public DataPlanSummaryPreference(Context context, AttributeSet attrs) { + super(context, attrs); + setLayoutResource(R.layout.settings_data_plan_summary_preference); + } + + public void setName(String planName) { + mName = planName; + notifyChanged(); + } + + public void setDescription(String planDescription) { + mDescription = planDescription; + notifyChanged(); + } + + public void setPercentageUsage(double percentageUsage) { + mPercentageUsage = percentageUsage; + notifyChanged(); + } + + public void setUsageTextColor(@ColorRes int planUsageTextColor) { + mUsageTextColor = planUsageTextColor; + notifyChanged(); + } + + public void setMeterBackgroundColor(@ColorRes int meterBackgroundColor) { + mMeterBackgroundColor = meterBackgroundColor; + notifyChanged(); + } + + public void setMeterConsumedColor(@ColorRes int meterConsumedColor) { + mMeterConsumedColor = meterConsumedColor; + notifyChanged(); + } + + @Override + public void onBindViewHolder(PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + holder.setDividerAllowedAbove(false); + TextView titleView = (TextView) holder.findViewById(android.R.id.title); + titleView.setTextColor(mUsageTextColor); + ((TextView) holder.findViewById(android.R.id.text1)).setText(mName); + ((TextView) holder.findViewById(android.R.id.text2)).setText(mDescription); + DonutView donutView = (DonutView) holder.findViewById(R.id.donut); + donutView.setPercentage(mPercentageUsage); + donutView.setMeterBackgroundColor(mMeterBackgroundColor); + donutView.setMeterConsumedColor(mMeterConsumedColor); + } +} diff --git a/src/com/android/settings/widget/DonutView.java b/src/com/android/settings/widget/DonutView.java index d7366166af7..629e35e2f63 100644 --- a/src/com/android/settings/widget/DonutView.java +++ b/src/com/android/settings/widget/DonutView.java @@ -48,6 +48,8 @@ public class DonutView extends View { private String mPercentString; private String mFullString; private boolean mShowPercentString = true; + private int mMeterBackgroundColor; + private int mMeterConsumedColor; public DonutView(Context context) { super(context); @@ -55,18 +57,18 @@ public class DonutView extends View { public DonutView(Context context, AttributeSet attrs) { super(context, attrs); - int meterBackgroundColor = context.getColor(R.color.meter_background_color); - int meterConsumedColor = Utils.getDefaultColor(mContext, R.color.meter_consumed_color); + mMeterBackgroundColor = context.getColor(R.color.meter_background_color); + mMeterConsumedColor = Utils.getDefaultColor(mContext, R.color.meter_consumed_color); boolean applyColorAccent = true; Resources resources = context.getResources(); mStrokeWidth = resources.getDimension(R.dimen.storage_donut_thickness); if (attrs != null) { TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.DonutView); - meterBackgroundColor = styledAttrs.getColor(R.styleable.DonutView_meterBackgroundColor, - meterBackgroundColor); - meterConsumedColor = styledAttrs.getColor(R.styleable.DonutView_meterConsumedColor, - meterConsumedColor); + mMeterBackgroundColor = styledAttrs.getColor(R.styleable.DonutView_meterBackgroundColor, + mMeterBackgroundColor); + mMeterConsumedColor = styledAttrs.getColor(R.styleable.DonutView_meterConsumedColor, + mMeterConsumedColor); applyColorAccent = styledAttrs.getBoolean(R.styleable.DonutView_applyColorAccent, true); mShowPercentString = styledAttrs.getBoolean(R.styleable.DonutView_showPercentString, @@ -81,14 +83,14 @@ public class DonutView extends View { mBackgroundCircle.setStrokeCap(Paint.Cap.BUTT); mBackgroundCircle.setStyle(Paint.Style.STROKE); mBackgroundCircle.setStrokeWidth(mStrokeWidth); - mBackgroundCircle.setColor(meterBackgroundColor); + mBackgroundCircle.setColor(mMeterBackgroundColor); mFilledArc = new Paint(); mFilledArc.setAntiAlias(true); mFilledArc.setStrokeCap(Paint.Cap.BUTT); mFilledArc.setStyle(Paint.Style.STROKE); mFilledArc.setStrokeWidth(mStrokeWidth); - mFilledArc.setColor(meterConsumedColor); + mFilledArc.setColor(mMeterConsumedColor); if (applyColorAccent) { final ColorFilter mAccentColorFilter = @@ -179,12 +181,24 @@ public class DonutView extends View { invalidate(); } + @ColorRes + public int getMeterBackgroundColor() { + return mMeterBackgroundColor; + } + public void setMeterBackgroundColor(@ColorRes int meterBackgroundColor) { + mMeterBackgroundColor = meterBackgroundColor; mBackgroundCircle.setColor(meterBackgroundColor); invalidate(); } + @ColorRes + public int getMeterConsumedColor() { + return mMeterConsumedColor; + } + public void setMeterConsumedColor(@ColorRes int meterConsumedColor) { + mMeterConsumedColor = meterConsumedColor; mFilledArc.setColor(meterConsumedColor); invalidate(); } diff --git a/tests/robotests/src/com/android/settings/datausage/DataPlanSummaryPreferenceTest.java b/tests/robotests/src/com/android/settings/datausage/DataPlanSummaryPreferenceTest.java new file mode 100644 index 00000000000..aafeed08049 --- /dev/null +++ b/tests/robotests/src/com/android/settings/datausage/DataPlanSummaryPreferenceTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2017 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.datausage; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Context; +import android.graphics.Color; +import android.support.v7.preference.PreferenceViewHolder; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.TextView; +import com.android.settings.R; +import com.android.settings.TestConfig; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settings.testutils.shadow.SettingsShadowResources; +import com.android.settings.widget.DonutView; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, + shadows = { + SettingsShadowResources.class, + SettingsShadowResources.SettingsShadowTheme.class + } +) +public final class DataPlanSummaryPreferenceTest { + + private static final String TEST_PLAN_USAGE = "Test plan usage"; + private static final String TEST_PLAN_NAME = "Test plan name"; + private static final String TEST_PLAN_DESCRIPTION = "Test plan description"; + private static final int PLAN_USAGE_TEXT_COLOR = Color.parseColor("#FF5C94F1"); + private static final int METER_BACKGROUND_COLOR = Color.parseColor("#FFDBDCDC"); + private static final int METER_CONSUMED_COLOR = Color.parseColor("#FF5C94F1"); + + private DataPlanSummaryPreference mPreference; + private PreferenceViewHolder mHolder; + + @Before + public void setUp() { + Context context = RuntimeEnvironment.application; + mPreference = new DataPlanSummaryPreference(context); + LayoutInflater inflater = LayoutInflater.from(context); + View view = inflater.inflate(mPreference.getLayoutResource(), + new LinearLayout(context), false); + mHolder = PreferenceViewHolder.createInstanceForTests(view); + } + + @Test + public void shouldRender_withoutData() { + mPreference.onBindViewHolder(mHolder); + + TextView planUsageTextView = (TextView) mHolder.findViewById(android.R.id.title); + assertThat(planUsageTextView.getText().toString()).isEmpty(); + TextView planNameTextView = (TextView) mHolder.findViewById(android.R.id.text1); + assertThat(planNameTextView.getText().toString()).isEmpty(); + TextView planDescriptionTextView = (TextView) mHolder.findViewById(android.R.id.text2); + assertThat(planDescriptionTextView.getText().toString()).isEmpty(); + } + + @Test + public void shouldRender_withData() { + mPreference.setTitle(TEST_PLAN_USAGE); + mPreference.setUsageTextColor(PLAN_USAGE_TEXT_COLOR); + mPreference.setName(TEST_PLAN_NAME); + mPreference.setDescription(TEST_PLAN_DESCRIPTION); + mPreference.setPercentageUsage(0.25D); + mPreference.setMeterBackgroundColor(METER_BACKGROUND_COLOR); + mPreference.setMeterConsumedColor(METER_CONSUMED_COLOR); + + mPreference.onBindViewHolder(mHolder); + + TextView planUsageTextView = (TextView) mHolder.findViewById(android.R.id.title); + assertThat(planUsageTextView.getTextColors().getDefaultColor()) + .isEqualTo(PLAN_USAGE_TEXT_COLOR); + assertThat(planUsageTextView.getText()).isEqualTo(TEST_PLAN_USAGE); + + TextView planNameTextView = (TextView) mHolder.findViewById(android.R.id.text1); + assertThat(planNameTextView.getText()).isEqualTo(TEST_PLAN_NAME); + + TextView planDescriptionTextView = (TextView) mHolder.findViewById(android.R.id.text2); + assertThat(planDescriptionTextView.getText()).isEqualTo(TEST_PLAN_DESCRIPTION); + + DonutView donutView = (DonutView) mHolder.findViewById(R.id.donut); + assertThat(donutView.getMeterBackgroundColor()).isEqualTo(METER_BACKGROUND_COLOR); + assertThat(donutView.getMeterConsumedColor()).isEqualTo(METER_CONSUMED_COLOR); + } +}