Draw preference divider based on preference group.
Bug: 32179750 Test: RunSettingsRoboTests Change-Id: I3ea1cd662c5baec825fae615883b94b26e648409
This commit is contained in:
@@ -19,12 +19,12 @@
|
|||||||
android:title="@string/display_settings"
|
android:title="@string/display_settings"
|
||||||
settings:keywords="@string/keywords_display">
|
settings:keywords="@string/keywords_display">
|
||||||
|
|
||||||
<PreferenceScreen
|
<Preference
|
||||||
android:key="brightness"
|
android:key="brightness"
|
||||||
android:title="@string/brightness"
|
android:title="@string/brightness"
|
||||||
settings:keywords="@string/keywords_display_brightness_level">
|
settings:keywords="@string/keywords_display_brightness_level">
|
||||||
<intent android:action="android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
|
<intent android:action="android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
|
||||||
</PreferenceScreen>
|
</Preference>
|
||||||
|
|
||||||
<com.android.settingslib.RestrictedPreference
|
<com.android.settingslib.RestrictedPreference
|
||||||
android:key="wallpaper"
|
android:key="wallpaper"
|
||||||
|
@@ -17,9 +17,9 @@
|
|||||||
<PreferenceScreen
|
<PreferenceScreen
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:title="@string/storage_settings">
|
android:title="@string/storage_settings">
|
||||||
<com.android.settings.dashboard.DashboardTilePreference
|
<Preference
|
||||||
android:key="pref_manage_storage"
|
android:key="pref_manage_storage"
|
||||||
android:title="@string/storage_menu_manage"
|
android:title="@string/storage_menu_manage"
|
||||||
android:fragment="com.android.settings.deletionhelper.AutomaticStorageManagerSettings">
|
android:fragment="com.android.settings.deletionhelper.AutomaticStorageManagerSettings">
|
||||||
</com.android.settings.dashboard.DashboardTilePreference>
|
</Preference>
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
@@ -19,21 +19,21 @@
|
|||||||
android:title="@string/header_category_system">
|
android:title="@string/header_category_system">
|
||||||
|
|
||||||
<!-- System updates -->
|
<!-- System updates -->
|
||||||
<com.android.settings.dashboard.DashboardTilePreference
|
<Preference
|
||||||
android:key="system_update_settings"
|
android:key="system_update_settings"
|
||||||
android:title="@string/system_update_settings_list_item_title"
|
android:title="@string/system_update_settings_list_item_title"
|
||||||
android:summary="@string/system_update_settings_list_item_summary"
|
android:summary="@string/system_update_settings_list_item_summary"
|
||||||
android:order="-30">
|
android:order="-30">
|
||||||
<intent android:action="android.settings.SYSTEM_UPDATE_SETTINGS"/>
|
<intent android:action="android.settings.SYSTEM_UPDATE_SETTINGS"/>
|
||||||
</com.android.settings.dashboard.DashboardTilePreference>
|
</Preference>
|
||||||
|
|
||||||
<com.android.settings.dashboard.DashboardTilePreference
|
<Preference
|
||||||
android:key="additional_system_update_settings"
|
android:key="additional_system_update_settings"
|
||||||
android:title="@string/additional_system_update_settings_list_item_title"
|
android:title="@string/additional_system_update_settings_list_item_title"
|
||||||
android:order="-31">
|
android:order="-31">
|
||||||
<intent android:action="android.intent.action.MAIN"
|
<intent android:action="android.intent.action.MAIN"
|
||||||
android:targetPackage="@string/additional_system_update"
|
android:targetPackage="@string/additional_system_update"
|
||||||
android:targetClass="@string/additional_system_update_menu"/>
|
android:targetClass="@string/additional_system_update_menu"/>
|
||||||
</com.android.settings.dashboard.DashboardTilePreference>
|
</Preference>
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
@@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* 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.dashboard;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
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.PreferenceGroupAdapter;
|
||||||
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
|
|
||||||
|
public class DashboardDividerDecoration extends RecyclerView.ItemDecoration {
|
||||||
|
|
||||||
|
private final DashboardFeatureProvider mDashboardFeatureProvider;
|
||||||
|
|
||||||
|
private Drawable mDivider;
|
||||||
|
private int mDividerHeight;
|
||||||
|
|
||||||
|
public DashboardDividerDecoration(Context context) {
|
||||||
|
mDashboardFeatureProvider = FeatureFactory.getFactory(context)
|
||||||
|
.getDashboardFeatureProvider(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 - 1; childViewIndex++) {
|
||||||
|
final View view = parent.getChildAt(childViewIndex);
|
||||||
|
if (shouldDrawDividerBelow(view, parent)) {
|
||||||
|
int top = (int) ViewCompat.getY(view) + view.getHeight();
|
||||||
|
mDivider.setBounds(0, top, width, top + mDividerHeight);
|
||||||
|
mDivider.draw(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldDrawDividerBelow(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);
|
||||||
|
final Preference pref = prefAdapter.getItem(adapterPosition);
|
||||||
|
final Preference nextPref = prefAdapter.getItem(adapterPosition + 1);
|
||||||
|
if (nextPref == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return mDashboardFeatureProvider.getPriorityGroup(pref)
|
||||||
|
!= mDashboardFeatureProvider.getPriorityGroup(nextPref);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.settings.dashboard;
|
package com.android.settings.dashboard;
|
||||||
|
|
||||||
|
import android.support.v7.preference.Preference;
|
||||||
|
|
||||||
import com.android.settingslib.drawer.DashboardCategory;
|
import com.android.settingslib.drawer.DashboardCategory;
|
||||||
import com.android.settingslib.drawer.Tile;
|
import com.android.settingslib.drawer.Tile;
|
||||||
|
|
||||||
@@ -45,7 +47,7 @@ public interface DashboardFeatureProvider {
|
|||||||
* priority 100 - 199 belongs to priority level 100, tiles with priority 200 - 299 is in
|
* priority 100 - 199 belongs to priority level 100, tiles with priority 200 - 299 is in
|
||||||
* group 200, and so on.
|
* group 200, and so on.
|
||||||
*/
|
*/
|
||||||
int getPriorityGroup(Tile tile);
|
int getPriorityGroup(Preference preference);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an unique string key for the tile.
|
* Returns an unique string key for the tile.
|
||||||
|
@@ -18,8 +18,8 @@ package com.android.settings.dashboard;
|
|||||||
|
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.support.v7.preference.Preference;
|
||||||
|
|
||||||
import com.android.settingslib.drawer.CategoryKey;
|
|
||||||
import com.android.settingslib.drawer.CategoryManager;
|
import com.android.settingslib.drawer.CategoryManager;
|
||||||
import com.android.settingslib.drawer.DashboardCategory;
|
import com.android.settingslib.drawer.DashboardCategory;
|
||||||
import com.android.settingslib.drawer.Tile;
|
import com.android.settingslib.drawer.Tile;
|
||||||
@@ -58,8 +58,8 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getPriorityGroup(Tile tile) {
|
public int getPriorityGroup(Preference preference) {
|
||||||
return tile.priority / 100;
|
return preference.getOrder() / 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -18,6 +18,9 @@ package com.android.settings.dashboard;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.drawable.ColorDrawable;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
@@ -25,6 +28,9 @@ import android.text.TextUtils;
|
|||||||
import android.util.ArrayMap;
|
import android.util.ArrayMap;
|
||||||
import android.util.ArraySet;
|
import android.util.ArraySet;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import com.android.settings.SettingsActivity;
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.SettingsPreferenceFragment;
|
import com.android.settings.SettingsPreferenceFragment;
|
||||||
@@ -50,6 +56,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
private final Map<Class, PreferenceController> mPreferenceControllers =
|
private final Map<Class, PreferenceController> mPreferenceControllers =
|
||||||
new ArrayMap<>();
|
new ArrayMap<>();
|
||||||
private final Set<String> mDashboardTilePrefKeys = new ArraySet<>();
|
private final Set<String> mDashboardTilePrefKeys = new ArraySet<>();
|
||||||
|
private DashboardDividerDecoration mDividerDecoration;
|
||||||
|
|
||||||
protected DashboardFeatureProvider mDashboardFeatureProvider;
|
protected DashboardFeatureProvider mDashboardFeatureProvider;
|
||||||
private boolean mListeningToCategoryChange;
|
private boolean mListeningToCategoryChange;
|
||||||
@@ -83,6 +90,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||||
super.onCreatePreferences(savedInstanceState, rootKey);
|
super.onCreatePreferences(savedInstanceState, rootKey);
|
||||||
|
mDividerDecoration = new DashboardDividerDecoration(getContext());
|
||||||
refreshAllPreferences(getLogTag());
|
refreshAllPreferences(getLogTag());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,6 +258,14 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
Bundle savedInstanceState) {
|
||||||
|
final View view = super.onCreateView(inflater, container, savedInstanceState);
|
||||||
|
getListView().addItemDecoration(mDividerDecoration);
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update state of each preference managed by PreferenceController.
|
* Update state of each preference managed by PreferenceController.
|
||||||
*/
|
*/
|
||||||
@@ -261,6 +277,14 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDivider(Drawable divider) {
|
||||||
|
// Intercept divider and set it transparent so system divider decoration is disabled.
|
||||||
|
// We will use our decoration to draw divider more intelligently.
|
||||||
|
mDividerDecoration.setDivider(divider);
|
||||||
|
super.setDivider(new ColorDrawable(Color.TRANSPARENT));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refresh all preference items, including both static prefs from xml, and dynamic items from
|
* Refresh all preference items, including both static prefs from xml, and dynamic items from
|
||||||
* DashboardCategory.
|
* DashboardCategory.
|
||||||
@@ -272,7 +296,6 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
screen.removeAll();
|
screen.removeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Add resource based tiles.
|
// Add resource based tiles.
|
||||||
displayResourceTiles();
|
displayResourceTiles();
|
||||||
|
|
||||||
@@ -293,5 +316,4 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
|||||||
mDashboardTilePrefKeys.clear();
|
mDashboardTilePrefKeys.clear();
|
||||||
displayDashboardTiles(TAG, getPreferenceScreen());
|
displayDashboardTiles(TAG, getPreferenceScreen());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,109 @@
|
|||||||
|
/*
|
||||||
|
* 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.dashboard;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.support.v7.preference.Preference;
|
||||||
|
import android.support.v7.preference.PreferenceGroupAdapter;
|
||||||
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
|
|
||||||
|
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.RobolectricTestRunner;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class DashboardDividerDecorationTest {
|
||||||
|
|
||||||
|
@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 Preference pref1;
|
||||||
|
@Mock
|
||||||
|
private Preference pref2;
|
||||||
|
private DashboardDividerDecoration mDecoration;
|
||||||
|
private FakeFeatureFactory mFactory;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
FakeFeatureFactory.setupForTest(mContext);
|
||||||
|
mFactory = (FakeFeatureFactory) FeatureFactory.getFactory(mContext);
|
||||||
|
mDecoration = new DashboardDividerDecoration(mContext);
|
||||||
|
mDecoration.setDivider(mDrawable);
|
||||||
|
mDecoration.setDividerHeight(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void drawOver_differentPriorityGroup_shouldDrawDivider() {
|
||||||
|
when(mRecyclerView.getAdapter()).thenReturn(mAdapter);
|
||||||
|
when(mRecyclerView.getChildCount()).thenReturn(2);
|
||||||
|
when(mRecyclerView.getChildAdapterPosition(any(View.class)))
|
||||||
|
.thenReturn(0)
|
||||||
|
.thenReturn(1);
|
||||||
|
when(mAdapter.getItem(0)).thenReturn(pref1);
|
||||||
|
when(mAdapter.getItem(1)).thenReturn(pref2);
|
||||||
|
when(mFactory.dashboardFeatureProvider.getPriorityGroup(pref1)).thenReturn(1);
|
||||||
|
when(mFactory.dashboardFeatureProvider.getPriorityGroup(pref2)).thenReturn(2);
|
||||||
|
|
||||||
|
mDecoration.onDrawOver(mCanvas, mRecyclerView, null /* state */);
|
||||||
|
|
||||||
|
verify(mDrawable).draw(mCanvas);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void drawOver_samePriorityGroup_doNotDrawDivider() {
|
||||||
|
when(mRecyclerView.getAdapter()).thenReturn(mAdapter);
|
||||||
|
when(mRecyclerView.getChildCount()).thenReturn(2);
|
||||||
|
when(mRecyclerView.getChildAdapterPosition(any(View.class)))
|
||||||
|
.thenReturn(0)
|
||||||
|
.thenReturn(1);
|
||||||
|
when(mAdapter.getItem(0)).thenReturn(pref1);
|
||||||
|
when(mAdapter.getItem(1)).thenReturn(pref2);
|
||||||
|
|
||||||
|
mDecoration.onDrawOver(mCanvas, mRecyclerView, null /* state */);
|
||||||
|
|
||||||
|
verify(mDrawable, never()).draw(mCanvas);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user