Add progressive disclsoure
- Add a ProgressiveDisclosureMixin that contains all logic for collapse preference list when it's too long - Refactored PreferenceController's updateState to take a preference instead of PreferenceScreen, because with progressive disclosure the preference can either be in screen or the mixin. DashboardFragment is responsible finding the preference before passing it to controller. Bug: 32255863 Test: RunSettingsRoboTests Change-Id: I6713abd61c954ce12732902e5b3ca4d4c0b1563e
This commit is contained in:
@@ -52,12 +52,14 @@ import java.util.Set;
|
||||
public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
implements SettingsDrawerActivity.CategoryListener, Indexable,
|
||||
SummaryLoader.SummaryConsumer {
|
||||
private static final String TAG = "DashboardFragment";
|
||||
|
||||
private final Map<Class, PreferenceController> mPreferenceControllers =
|
||||
new ArrayMap<>();
|
||||
private final Set<String> mDashboardTilePrefKeys = new ArraySet<>();
|
||||
private DashboardDividerDecoration mDividerDecoration;
|
||||
|
||||
protected ProgressiveDisclosureMixin mProgressiveDisclosureMixin;
|
||||
protected DashboardFeatureProvider mDashboardFeatureProvider;
|
||||
private boolean mListeningToCategoryChange;
|
||||
private SummaryLoader mSummaryLoader;
|
||||
@@ -67,6 +69,8 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
super.onAttach(context);
|
||||
mDashboardFeatureProvider =
|
||||
FeatureFactory.getFactory(context).getDashboardFeatureProvider(context);
|
||||
mProgressiveDisclosureMixin = new ProgressiveDisclosureMixin(context, this);
|
||||
getLifecycle().addObserver(mProgressiveDisclosureMixin);
|
||||
|
||||
final List<PreferenceController> controllers = getPreferenceControllers(context);
|
||||
if (controllers == null) {
|
||||
@@ -160,6 +164,18 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Preference findPreference(CharSequence key) {
|
||||
Preference preference = super.findPreference(key);
|
||||
if (preference == null && mProgressiveDisclosureMixin != null) {
|
||||
preference = mProgressiveDisclosureMixin.findPreference(key);
|
||||
}
|
||||
if (preference == null) {
|
||||
Log.d(TAG, "Cannot find preference with key " + key);
|
||||
}
|
||||
return preference;
|
||||
}
|
||||
|
||||
protected <T extends PreferenceController> T getPreferenceController(Class<T> clazz) {
|
||||
PreferenceController controller = mPreferenceControllers.get(clazz);
|
||||
return (T) controller;
|
||||
@@ -259,7 +275,19 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
// (larger value has higher priority). However pref order defines smaller value has
|
||||
// higher priority.
|
||||
pref.setOrder(-tile.priority);
|
||||
screen.addPreference(pref);
|
||||
|
||||
// Either add to screen, or to collapsed list.
|
||||
if (mProgressiveDisclosureMixin.isCollapsed()) {
|
||||
// Already collapsed, add to collapsed list.
|
||||
mProgressiveDisclosureMixin.addToCollapsedList(pref);
|
||||
} else if (mProgressiveDisclosureMixin.shouldCollapse(screen)) {
|
||||
// About to have too many tiles on scree, collapse and add pref to collapsed list.
|
||||
mProgressiveDisclosureMixin.collapse(screen);
|
||||
mProgressiveDisclosureMixin.addToCollapsedList(pref);
|
||||
} else {
|
||||
// No need to collapse, add to screen directly.
|
||||
screen.addPreference(pref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,9 +306,15 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
*/
|
||||
private void updatePreferenceStates() {
|
||||
Collection<PreferenceController> controllers = mPreferenceControllers.values();
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
for (PreferenceController controller : controllers) {
|
||||
controller.updateState(screen);
|
||||
final String key = controller.getPreferenceKey();
|
||||
|
||||
final Preference preference = findPreference(key);
|
||||
if (preference == null) {
|
||||
Log.d(TAG, "Cannot find preference with key " + key);
|
||||
continue;
|
||||
}
|
||||
controller.updateState(preference);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,15 +336,20 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
*/
|
||||
private void refreshAllPreferences(final String TAG) {
|
||||
// First remove old preferences.
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
if (screen != null) {
|
||||
screen.removeAll();
|
||||
if (getPreferenceScreen() != null) {
|
||||
// Intentionally do not cache PreferenceScreen because it will be recreated later.
|
||||
getPreferenceScreen().removeAll();
|
||||
}
|
||||
|
||||
// Add resource based tiles.
|
||||
displayResourceTiles();
|
||||
|
||||
refreshDashboardTiles(TAG);
|
||||
|
||||
if (!mProgressiveDisclosureMixin.isCollapsed()
|
||||
&& mProgressiveDisclosureMixin.shouldCollapse(getPreferenceScreen())) {
|
||||
mProgressiveDisclosureMixin.collapse(getPreferenceScreen());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -319,10 +358,13 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
|
||||
private void refreshDashboardTiles(final String TAG) {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
for (String key : mDashboardTilePrefKeys) {
|
||||
// Remove tiles from screen
|
||||
final Preference pref = screen.findPreference(key);
|
||||
if (pref != null) {
|
||||
screen.removePreference(pref);
|
||||
}
|
||||
// Also remove tile from collapsed set
|
||||
mProgressiveDisclosureMixin.removePreference(screen, key);
|
||||
}
|
||||
mDashboardTilePrefKeys.clear();
|
||||
displayDashboardTiles(TAG, getPreferenceScreen());
|
||||
|
||||
53
src/com/android/settings/dashboard/ExpandPreference.java
Normal file
53
src/com/android/settings/dashboard/ExpandPreference.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.support.v7.preference.Preference;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
public class ExpandPreference extends Preference {
|
||||
|
||||
public ExpandPreference(Context context, AttributeSet attrs,
|
||||
int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
init();
|
||||
}
|
||||
|
||||
public ExpandPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
public ExpandPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public ExpandPreference(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
setLayoutResource(R.layout.expand_preference);
|
||||
setTitle(R.string.wifi_more);
|
||||
setOrder(999);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* 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.os.Bundle;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settings.core.lifecycle.events.OnCreate;
|
||||
import com.android.settings.core.lifecycle.events.OnSaveInstanceState;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ProgressiveDisclosureMixin implements Preference.OnPreferenceClickListener,
|
||||
LifecycleObserver, OnCreate, OnSaveInstanceState {
|
||||
|
||||
private static final String TAG = "ProgressiveDisclosure";
|
||||
private static final String STATE_USER_EXPANDED = "state_user_expanded";
|
||||
private static final int DEFAULT_TILE_LIMIT = 3;
|
||||
|
||||
private int mTileLimit = DEFAULT_TILE_LIMIT;
|
||||
|
||||
private final DashboardFeatureProvider mDashboardFeatureProvider;
|
||||
private final List<Preference> collapsedPrefs = new ArrayList<>();
|
||||
private final ExpandPreference mExpandButton;
|
||||
private final PreferenceFragment mFragment;
|
||||
|
||||
private boolean mUserExpanded;
|
||||
|
||||
public ProgressiveDisclosureMixin(Context context, PreferenceFragment fragment) {
|
||||
mFragment = fragment;
|
||||
mExpandButton = new ExpandPreference(context);
|
||||
mExpandButton.setOnPreferenceClickListener(this);
|
||||
mDashboardFeatureProvider = FeatureFactory.getFactory(context)
|
||||
.getDashboardFeatureProvider(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
if (savedInstanceState != null) {
|
||||
mUserExpanded = savedInstanceState.getBoolean(STATE_USER_EXPANDED, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
outState.putBoolean(STATE_USER_EXPANDED, mUserExpanded);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
if (preference instanceof ExpandPreference) {
|
||||
final PreferenceScreen screen = mFragment.getPreferenceScreen();
|
||||
if (screen != null) {
|
||||
screen.removePreference(preference);
|
||||
for (Preference pref : collapsedPrefs) {
|
||||
screen.addPreference(pref);
|
||||
}
|
||||
mUserExpanded = true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the threshold to start collapsing preferences when there are too many.
|
||||
*/
|
||||
public void setTileLimit(int limit) {
|
||||
mTileLimit = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the controller is in collapsed state.
|
||||
*/
|
||||
public boolean isCollapsed() {
|
||||
return !collapsedPrefs.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the screen should be collapsed.
|
||||
*/
|
||||
public boolean shouldCollapse(PreferenceScreen screen) {
|
||||
return mDashboardFeatureProvider.isEnabled() && screen.getPreferenceCount() >= mTileLimit
|
||||
&& !mUserExpanded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collapse extra preferences and show a "More" button
|
||||
*/
|
||||
public void collapse(PreferenceScreen screen) {
|
||||
final int itemCount = screen.getPreferenceCount();
|
||||
if (!shouldCollapse(screen)) {
|
||||
return;
|
||||
}
|
||||
if (!collapsedPrefs.isEmpty()) {
|
||||
Log.w(TAG, "collapsed list should ALWAYS BE EMPTY before collapsing!");
|
||||
}
|
||||
|
||||
for (int i = itemCount - 1; i >= mTileLimit; i--) {
|
||||
final Preference preference = screen.getPreference(i);
|
||||
addToCollapsedList(preference);
|
||||
screen.removePreference(preference);
|
||||
}
|
||||
screen.addPreference(mExpandButton);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add preference to collapsed list.
|
||||
*/
|
||||
public void addToCollapsedList(Preference preference) {
|
||||
collapsedPrefs.add(preference);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove preference from collapsed list. If the preference is not in list, do nothing.
|
||||
*/
|
||||
public void removePreference(PreferenceScreen screen, String key) {
|
||||
if (!isCollapsed()) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < collapsedPrefs.size(); i++) {
|
||||
final Preference pref = collapsedPrefs.get(i);
|
||||
if (TextUtils.equals(key, pref.getKey())) {
|
||||
collapsedPrefs.remove(pref);
|
||||
if (collapsedPrefs.isEmpty()) {
|
||||
// Removed last element, remove expand button too.
|
||||
screen.removePreference(mExpandButton);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find whether a preference is in collapsed list.
|
||||
*/
|
||||
public Preference findPreference(CharSequence key) {
|
||||
for (int i = 0; i < collapsedPrefs.size(); i++) {
|
||||
final Preference pref = collapsedPrefs.get(i);
|
||||
if (TextUtils.equals(key, pref.getKey())) {
|
||||
return pref;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user