'Free up space' uses CardPreference
For material next style. Bug: 188599776 Test: make RunSettingsRoboTests ROBOTEST_FILTER=CardPreferenceTest Change-Id: Ibaf4d11ab44017453498676db00cff1e49c83b9d
This commit is contained in:
@@ -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">
|
||||
<Preference
|
||||
<com.android.settings.widget.CardPreference
|
||||
android:key="free_up_space"
|
||||
android:order="4"
|
||||
android:title="@string/storage_free_up_space_title"
|
||||
|
@@ -37,7 +37,7 @@
|
||||
android:icon="@drawable/ic_storage"
|
||||
android:order="3"
|
||||
settings:controller="com.android.settings.deviceinfo.storage.AutomaticStorageManagementSwitchPreferenceController"/>
|
||||
<Preference
|
||||
<com.android.settings.widget.CardPreference
|
||||
android:key="free_up_space"
|
||||
android:order="4"
|
||||
android:title="@string/storage_free_up_space_title"
|
||||
|
@@ -44,6 +44,7 @@ import com.android.settings.deviceinfo.storage.StorageItemPreferenceController;
|
||||
import com.android.settings.deviceinfo.storage.UserIconLoader;
|
||||
import com.android.settings.deviceinfo.storage.VolumeSizesLoader;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.CardPreference;
|
||||
import com.android.settings.widget.EntityHeaderController;
|
||||
import com.android.settingslib.applications.StorageStatsSource;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
@@ -90,7 +91,7 @@ public class StorageCategoryFragment extends DashboardFragment
|
||||
private List<AbstractPreferenceController> 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
|
||||
|
@@ -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<AbstractPreferenceController> 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
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user