From bfb006aa323ea66f81d8f00f6b0081668f6b4fc9 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Wed, 25 Jan 2017 15:32:17 -0800 Subject: [PATCH] Use framework support to customize dividers between prefs. Bug: 33579296 Test: make RunSettingsRoboTests Change-Id: I1bb3b82b88dc727cda05b00058e6f40564f0e5db --- res/values/styles_preference.xml | 33 +++-- .../core/InstrumentedPreferenceFragment.java | 24 ---- .../notification/VolumeSeekBarPreference.java | 9 +- .../widget/PreferenceDividerDecoration.java | 79 ------------ .../settings/DeviceInfoSettingsTest.java | 12 +- .../PreferenceDividerDecorationTest.java | 113 ------------------ 6 files changed, 29 insertions(+), 241 deletions(-) delete mode 100644 src/com/android/settings/widget/PreferenceDividerDecoration.java delete mode 100644 tests/robotests/src/com/android/settings/widget/PreferenceDividerDecorationTest.java diff --git a/res/values/styles_preference.xml b/res/values/styles_preference.xml index a78d87ba353..e7c28525cf8 100644 --- a/res/values/styles_preference.xml +++ b/res/values/styles_preference.xml @@ -21,35 +21,44 @@ 72dp - + + + + + - - - - - - - diff --git a/src/com/android/settings/core/InstrumentedPreferenceFragment.java b/src/com/android/settings/core/InstrumentedPreferenceFragment.java index 91ef3f17e29..746c10d482b 100644 --- a/src/com/android/settings/core/InstrumentedPreferenceFragment.java +++ b/src/com/android/settings/core/InstrumentedPreferenceFragment.java @@ -17,13 +17,7 @@ package com.android.settings.core; import android.content.Context; -import android.graphics.Color; -import android.graphics.drawable.ColorDrawable; -import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; import com.android.settings.core.instrumentation.Instrumentable; import com.android.settings.core.instrumentation.MetricsFeatureProvider; @@ -31,7 +25,6 @@ import com.android.settings.core.instrumentation.VisibilityLoggerMixin; import com.android.settings.core.lifecycle.ObservablePreferenceFragment; import com.android.settings.overlay.FeatureFactory; import com.android.settings.survey.SurveyMixin; -import com.android.settings.widget.PreferenceDividerDecoration; /** * Instrumented fragment that logs visibility state. @@ -44,9 +37,6 @@ public abstract class InstrumentedPreferenceFragment extends ObservablePreferenc // metrics placeholder value. Only use this for development. protected final int PLACEHOLDER_METRIC = 10000; - private final PreferenceDividerDecoration mDividerDecoration = - new PreferenceDividerDecoration(); - private final VisibilityLoggerMixin mVisibilityLoggerMixin; public InstrumentedPreferenceFragment() { @@ -62,14 +52,6 @@ public abstract class InstrumentedPreferenceFragment extends ObservablePreferenc mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); } - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - final View view = super.onCreateView(inflater, container, savedInstanceState); - getListView().addItemDecoration(mDividerDecoration); - return view; - } - @Override public void onResume() { mVisibilityLoggerMixin.setSourceMetricsCategory(getActivity()); @@ -80,12 +62,6 @@ public abstract class InstrumentedPreferenceFragment extends ObservablePreferenc public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { } - @Override - public void setDivider(Drawable divider) { - mDividerDecoration.setDivider(divider); - super.setDivider(new ColorDrawable(Color.TRANSPARENT)); - } - protected final Context getPrefContext() { return getPreferenceManager().getContext(); } diff --git a/src/com/android/settings/notification/VolumeSeekBarPreference.java b/src/com/android/settings/notification/VolumeSeekBarPreference.java index 5ab1d29a26c..8a51961dca2 100644 --- a/src/com/android/settings/notification/VolumeSeekBarPreference.java +++ b/src/com/android/settings/notification/VolumeSeekBarPreference.java @@ -59,15 +59,18 @@ public class VolumeSeekBarPreference extends SeekBarPreference { } public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) { - this(context, attrs, defStyleAttr, 0); + super(context, attrs, defStyleAttr); + setLayoutResource(R.layout.preference_volume_slider); } public VolumeSeekBarPreference(Context context, AttributeSet attrs) { - this(context, attrs, 0); + super(context, attrs); + setLayoutResource(R.layout.preference_volume_slider); } public VolumeSeekBarPreference(Context context) { - this(context, null); + super(context); + setLayoutResource(R.layout.preference_volume_slider); } public void setStream(int stream) { diff --git a/src/com/android/settings/widget/PreferenceDividerDecoration.java b/src/com/android/settings/widget/PreferenceDividerDecoration.java deleted file mode 100644 index 4466a3dc3b8..00000000000 --- a/src/com/android/settings/widget/PreferenceDividerDecoration.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2016 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.widget; - -import android.graphics.Canvas; -import android.graphics.drawable.Drawable; -import android.support.v4.view.ViewCompat; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceCategory; -import android.support.v7.preference.PreferenceGroupAdapter; -import android.support.v7.widget.RecyclerView; -import android.view.View; - -public class PreferenceDividerDecoration extends RecyclerView.ItemDecoration { - - private Drawable mDivider; - private int mDividerHeight; - - public void setDivider(Drawable divider) { - if (divider != null) { - mDividerHeight = divider.getIntrinsicHeight(); - } else { - mDividerHeight = 0; - } - mDivider = divider; - } - - public void setDividerHeight(int dividerHeight) { - mDividerHeight = dividerHeight; - } - - @Override - public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { - if (mDivider == null) { - return; - } - final int childCount = parent.getChildCount(); - final int width = parent.getWidth(); - for (int childViewIndex = 0; childViewIndex < childCount; childViewIndex++) { - final View view = parent.getChildAt(childViewIndex); - if (shouldDrawDividerAbove(view, parent)) { - int top = (int) ViewCompat.getY(view); - mDivider.setBounds(0, top, width, top + mDividerHeight); - mDivider.draw(c); - } - } - } - - private boolean shouldDrawDividerAbove(View view, RecyclerView parent) { - final RecyclerView.Adapter adapter = parent.getAdapter(); - if (adapter == null || !(adapter instanceof PreferenceGroupAdapter)) { - return false; - } - final PreferenceGroupAdapter prefAdapter = (PreferenceGroupAdapter) adapter; - final int adapterPosition = parent.getChildAdapterPosition(view); - if (adapterPosition == RecyclerView.NO_POSITION) { - return false; - } - final Preference pref = prefAdapter.getItem(adapterPosition); - if (pref instanceof PreferenceCategory) { - return adapterPosition != 0; - } - return pref instanceof FooterPreference; - } -} diff --git a/tests/robotests/src/com/android/settings/DeviceInfoSettingsTest.java b/tests/robotests/src/com/android/settings/DeviceInfoSettingsTest.java index 17ca85c49c8..ea889a50d5b 100644 --- a/tests/robotests/src/com/android/settings/DeviceInfoSettingsTest.java +++ b/tests/robotests/src/com/android/settings/DeviceInfoSettingsTest.java @@ -19,10 +19,6 @@ package com.android.settings; import android.content.Context; import android.os.UserManager; import android.support.v7.preference.PreferenceScreen; -import android.test.AndroidTestCase; - -import com.android.settings.testutils.FakeFeatureFactory; -import com.android.settingslib.DeviceInfoUtils; import org.junit.Before; import org.junit.Test; @@ -33,14 +29,13 @@ import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) -public class DeviceInfoSettingsTest extends AndroidTestCase { +public class DeviceInfoSettingsTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext; @@ -49,21 +44,18 @@ public class DeviceInfoSettingsTest extends AndroidTestCase { @Mock private UserManager mUserManager; - private FakeFeatureFactory mFeatureFactory; private DeviceInfoSettings mSettings; @Before public void setUp() { MockitoAnnotations.initMocks(this); - FakeFeatureFactory.setupForTest(mContext); - mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); mSettings = spy(new DeviceInfoSettings()); doReturn(mScreen).when(mSettings).getPreferenceScreen(); } @Test - public void getPrefXml_shoudlReturnDeviceInfoXml() { + public void getPrefXml_shouldReturnDeviceInfoXml() { assertThat(mSettings.getPreferenceScreenResId()).isEqualTo(R.xml.device_info_settings); } } diff --git a/tests/robotests/src/com/android/settings/widget/PreferenceDividerDecorationTest.java b/tests/robotests/src/com/android/settings/widget/PreferenceDividerDecorationTest.java deleted file mode 100644 index f69de54abb2..00000000000 --- a/tests/robotests/src/com/android/settings/widget/PreferenceDividerDecorationTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2016 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.widget; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.drawable.Drawable; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceCategory; -import android.support.v7.preference.PreferenceGroupAdapter; -import android.support.v7.widget.RecyclerView; -import android.view.View; - -import com.android.settings.SettingsRobolectricTestRunner; -import com.android.settings.TestConfig; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.annotation.Config; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) -public class PreferenceDividerDecorationTest { - - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private Context mContext; - @Mock - private Drawable mDrawable; - @Mock - private Canvas mCanvas; - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private RecyclerView mRecyclerView; - @Mock - private PreferenceGroupAdapter mAdapter; - @Mock - private PreferenceCategory mPrefCategory; - @Mock - private Preference mNormalPref; - @Mock - private FooterPreference mFooterPref; - private PreferenceDividerDecoration mDecoration; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - mDecoration = new PreferenceDividerDecoration(); - mDecoration.setDivider(mDrawable); - mDecoration.setDividerHeight(3); - } - - @Test - public void drawOver_footerPreference_shouldDrawDivider() { - when(mRecyclerView.getAdapter()).thenReturn(mAdapter); - when(mRecyclerView.getChildCount()).thenReturn(1); - when(mAdapter.getItem(anyInt())).thenReturn(mFooterPref); - - mDecoration.onDrawOver(mCanvas, mRecyclerView, null /* state */); - - verify(mDrawable).draw(mCanvas); - } - - @Test - public void drawOver_preferenceCategory_shouldSkipFirst() { - when(mRecyclerView.getAdapter()).thenReturn(mAdapter); - when(mRecyclerView.getChildCount()).thenReturn(3); - when(mAdapter.getItem(anyInt())).thenReturn(mPrefCategory); - when(mRecyclerView.getChildAdapterPosition(any(View.class))) - .thenReturn(0) - .thenReturn(1) - .thenReturn(2); - - mDecoration.onDrawOver(mCanvas, mRecyclerView, null /* state */); - - // 3 prefCategory, should skip first draw - verify(mDrawable, times(2)).draw(mCanvas); - } - - @Test - public void drawOver_preference_doNotDrawDivider() { - when(mRecyclerView.getAdapter()).thenReturn(mAdapter); - when(mRecyclerView.getChildCount()).thenReturn(1); - when(mAdapter.getItem(anyInt())).thenReturn(mNormalPref); - - mDecoration.onDrawOver(mCanvas, mRecyclerView, null /* state */); - - verify(mDrawable, never()).draw(mCanvas); - } -}