diff --git a/res/xml/storage_category_fragment.xml b/res/xml/storage_category_fragment.xml index fb62beeaecf..08f3f272f87 100644 --- a/res/xml/storage_category_fragment.xml +++ b/res/xml/storage_category_fragment.xml @@ -19,7 +19,7 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:settings="http://schemas.android.com/apk/res-auto" android:title="@string/storage_settings"> - - mSecondaryUsers; private boolean mIsWorkProfile; private int mUserId; - private Preference mFreeUpSpacePreference; + private CardPreference mFreeUpSpacePreference; /** * Refresh UI for specified storageEntry. @@ -145,8 +146,11 @@ public class StorageCategoryFragment extends DashboardFragment } private void initializePreference() { - mFreeUpSpacePreference = getPreferenceScreen().findPreference(FREE_UP_SPACE_PREF_KEY); + mFreeUpSpacePreference = (CardPreference) getPreferenceScreen() + .findPreference(FREE_UP_SPACE_PREF_KEY); mFreeUpSpacePreference.setOnPreferenceClickListener(this); + mFreeUpSpacePreference.setCardBackgroundColor(Utils.getColorAttrDefaultColor(getContext(), + com.android.internal.R.attr.colorSurface)); } @Override diff --git a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java index 6dbc3aa9a7e..0fce8fac61b 100644 --- a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java +++ b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java @@ -56,6 +56,7 @@ import com.android.settings.deviceinfo.storage.UserIconLoader; import com.android.settings.deviceinfo.storage.VolumeSizesLoader; import com.android.settings.overlay.FeatureFactory; import com.android.settings.search.BaseSearchIndexProvider; +import com.android.settings.widget.CardPreference; import com.android.settings.widget.EntityHeaderController; import com.android.settingslib.applications.StorageStatsSource; import com.android.settingslib.core.AbstractPreferenceController; @@ -109,7 +110,7 @@ public class StorageDashboardFragment extends DashboardFragment private List mSecondaryUsers; private boolean mIsWorkProfile; private int mUserId; - private Preference mFreeUpSpacePreference; + private CardPreference mFreeUpSpacePreference; private final StorageEventListener mStorageEventListener = new StorageEventListener() { @Override @@ -281,8 +282,11 @@ public class StorageDashboardFragment extends DashboardFragment } private void initializePreference() { - mFreeUpSpacePreference = getPreferenceScreen().findPreference(FREE_UP_SPACE_PREF_KEY); + mFreeUpSpacePreference = (CardPreference) getPreferenceScreen() + .findPreference(FREE_UP_SPACE_PREF_KEY); mFreeUpSpacePreference.setOnPreferenceClickListener(this); + mFreeUpSpacePreference.setCardBackgroundColor(Utils.getColorAttrDefaultColor(getContext(), + com.android.internal.R.attr.colorSurface)); } @Override diff --git a/src/com/android/settings/widget/CardPreference.java b/src/com/android/settings/widget/CardPreference.java index 20ea7109e48..80d9da2f96f 100644 --- a/src/com/android/settings/widget/CardPreference.java +++ b/src/com/android/settings/widget/CardPreference.java @@ -19,7 +19,9 @@ package com.android.settings.widget; import android.content.Context; import android.util.AttributeSet; +import androidx.annotation.ColorInt; import androidx.preference.Preference; +import androidx.preference.PreferenceViewHolder; import com.android.settings.R; @@ -29,6 +31,11 @@ import com.google.android.material.card.MaterialCardView; * Preference that wrapped by {@link MaterialCardView}, only support to set icon, title and summary */ public class CardPreference extends Preference { + + private static final @ColorInt int INVALID_COLOR = -1; + + private @ColorInt int mCardBackgroundColor = INVALID_COLOR; + public CardPreference(Context context) { this(context, null /* attrs */); } @@ -36,4 +43,23 @@ public class CardPreference extends Preference { public CardPreference(Context context, AttributeSet attrs) { super(context, attrs, R.attr.cardPreferenceStyle); } + + /** Set card background color of the MaterialCardView in CardPreference. */ + public void setCardBackgroundColor(@ColorInt int color) { + if (mCardBackgroundColor == color) { + return; + } + mCardBackgroundColor = color; + notifyChanged(); + } + + @Override + public void onBindViewHolder(PreferenceViewHolder view) { + super.onBindViewHolder(view); + + if (mCardBackgroundColor != INVALID_COLOR) { + final MaterialCardView cardView = (MaterialCardView) view.findViewById(R.id.container); + cardView.setCardBackgroundColor(mCardBackgroundColor); + } + } } diff --git a/tests/robotests/src/com/android/settings/widget/CardPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/CardPreferenceTest.java index 6d4a6bbd4d8..617dad58552 100644 --- a/tests/robotests/src/com/android/settings/widget/CardPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/widget/CardPreferenceTest.java @@ -18,13 +18,26 @@ package com.android.settings.widget; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + import android.content.Context; +import android.view.View; + +import androidx.annotation.ColorInt; +import androidx.preference.PreferenceViewHolder; import com.android.settings.R; +import com.google.android.material.card.MaterialCardView; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; @@ -33,12 +46,20 @@ public class CardPreferenceTest { private Context mContext; private CardPreference mCardPreference; + @Mock + private PreferenceViewHolder mPreferenceViewHolder; + @Mock + private MaterialCardView mCardView; @Before public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; mContext.setTheme(R.style.SettingsPreferenceTheme); mCardPreference = new CardPreference(mContext); + mPreferenceViewHolder = spy(PreferenceViewHolder.createInstanceForTests(mock(View.class))); + doReturn(mCardView).when(mPreferenceViewHolder).findViewById(R.id.container); } @Test @@ -46,4 +67,13 @@ public class CardPreferenceTest { assertThat(mCardPreference.getLayoutResource()).isEqualTo(R.layout.card_preference_layout); } + @Test + public void setCardBackgroundColor_shouldUseCorrectColor() { + final @ColorInt int testColor = 0xABCDEF; + + mCardPreference.setCardBackgroundColor(testColor); + mCardPreference.onBindViewHolder(mPreferenceViewHolder); + + verify(mCardView).setCardBackgroundColor(testColor); + } }