Merge "Change to use the expand button in support lib."

This commit is contained in:
TreeHugger Robot
2017-10-27 21:56:23 +00:00
committed by Android (Google) Code Review
30 changed files with 85 additions and 1473 deletions

View File

@@ -65,16 +65,9 @@ public class DisplaySettings extends DashboardFragment {
return TAG;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mProgressiveDisclosureMixin.setTileLimit(4);
}
@Override
protected int getPreferenceScreenResId() {
return mProgressiveDisclosureMixin.isEnabled()
? R.xml.display_settings : R.xml.display_settings_new_advance_button;
return R.xml.display_settings;
}
@Override

View File

@@ -58,7 +58,6 @@ import com.android.internal.widget.LockPatternUtils;
import com.android.settings.TrustAgentUtils.TrustAgentComponentInfo;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settings.dashboard.DashboardFeatureProvider;
import com.android.settings.dashboard.ProgressiveDisclosureMixin;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.enterprise.EnterprisePrivacyPreferenceController;
import com.android.settings.enterprise.ManageDeviceAdminPreferenceController;
@@ -275,11 +274,7 @@ public class SecuritySettings extends SettingsPreferenceFragment
if (root != null) {
root.removeAll();
}
if (ProgressiveDisclosureMixin.isV2Enabled()) {
addPreferencesFromResource(R.xml.security_settings_advanced_button);
} else {
addPreferencesFromResource(R.xml.security_settings);
}
addPreferencesFromResource(R.xml.security_settings);
root = getPreferenceScreen();
// Add category for security status

View File

@@ -47,12 +47,6 @@ public class AppAndNotificationDashboardFragment extends DashboardFragment {
return TAG;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mProgressiveDisclosureMixin.setTileLimit(4);
}
@Override
protected int getHelpResource() {
return R.string.help_url_apps_and_notifications;
@@ -60,8 +54,7 @@ public class AppAndNotificationDashboardFragment extends DashboardFragment {
@Override
protected int getPreferenceScreenResId() {
return mProgressiveDisclosureMixin.isEnabled()
? R.xml.app_and_notification : R.xml.app_and_notification_new_advance_button;
return R.xml.app_and_notification;
}
@Override

View File

@@ -17,7 +17,6 @@ package com.android.settings.dashboard;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import com.android.settingslib.drawer.DashboardCategory;
@@ -79,12 +78,6 @@ public interface DashboardFeatureProvider {
void bindPreferenceToTile(Activity activity, int sourceMetricsCategory, Preference pref,
Tile tile, String key, int baseOrder);
/**
* Returns a {@link ProgressiveDisclosureMixin} for specified fragment.
*/
ProgressiveDisclosureMixin getProgressiveDisclosureMixin(Context context,
DashboardFragment fragment, Bundle args);
/**
* Returns additional intent filter action for dashboard tiles
*/

View File

@@ -188,16 +188,6 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
}
}
@Override
public ProgressiveDisclosureMixin getProgressiveDisclosureMixin(Context context,
DashboardFragment fragment, Bundle args) {
boolean keepExpanded = false;
if (args != null) {
keepExpanded = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY) != null;
}
return new ProgressiveDisclosureMixin(context, fragment, keepExpanded);
}
@Override
public String getExtraIntentAction() {
return null;

View File

@@ -58,7 +58,6 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
new ArrayMap<>();
private final Set<String> mDashboardTilePrefKeys = new ArraySet<>();
protected ProgressiveDisclosureMixin mProgressiveDisclosureMixin;
protected DashboardFeatureProvider mDashboardFeatureProvider;
private DashboardTilePlaceholderPreferenceController mPlaceholderPreferenceController;
private boolean mListeningToCategoryChange;
@@ -69,9 +68,6 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
super.onAttach(context);
mDashboardFeatureProvider =
FeatureFactory.getFactory(context).getDashboardFeatureProvider(context);
mProgressiveDisclosureMixin = mDashboardFeatureProvider
.getProgressiveDisclosureMixin(context, this, getArguments());
getLifecycle().addObserver(mProgressiveDisclosureMixin);
List<AbstractPreferenceController> controllers = getPreferenceControllers(context);
if (controllers == null) {
@@ -140,8 +136,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
@Override
public void notifySummaryChanged(Tile tile) {
final String key = mDashboardFeatureProvider.getDashboardKeyForTile(tile);
final Preference pref = mProgressiveDisclosureMixin.findPreference(
getPreferenceScreen(), key);
final Preference pref = getPreferenceScreen().findPreference(key);
if (pref == null) {
Log.d(getLogTag(),
String.format("Can't find pref by key %s, skipping update summary %s/%s",
@@ -267,7 +262,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
}
final String key = controller.getPreferenceKey();
final Preference preference = mProgressiveDisclosureMixin.findPreference(screen, key);
final Preference preference = screen.findPreference(key);
if (preference == null) {
Log.d(TAG, String.format("Cannot find preference with key %s in Controller %s",
key, controller.getClass().getSimpleName()));
@@ -290,7 +285,6 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
// Add resource based tiles.
displayResourceTiles();
mProgressiveDisclosureMixin.collapse(getPreferenceScreen());
refreshDashboardTiles(TAG);
}
@@ -342,8 +336,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
}
if (mDashboardTilePrefKeys.contains(key)) {
// Have the key already, will rebind.
final Preference preference = mProgressiveDisclosureMixin.findPreference(
screen, key);
final Preference preference = screen.findPreference(key);
mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), getMetricsCategory(),
preference, tile, key, mPlaceholderPreferenceController.getOrder());
} else {
@@ -351,7 +344,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
final Preference pref = new Preference(getPrefContext());
mDashboardFeatureProvider.bindPreferenceToTile(getActivity(), getMetricsCategory(),
pref, tile, key, mPlaceholderPreferenceController.getOrder());
mProgressiveDisclosureMixin.addPreference(screen, pref);
screen.addPreference(pref);
mDashboardTilePrefKeys.add(key);
}
remove.remove(key);
@@ -359,7 +352,10 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
// Finally remove tiles that are gone.
for (String key : remove) {
mDashboardTilePrefKeys.remove(key);
mProgressiveDisclosureMixin.removePreference(screen, key);
final Preference preference = screen.findPreference(key);
if (preference != null) {
screen.removePreference(preference);
}
}
mSummaryLoader.setListening(true);
}

View File

@@ -1,61 +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.dashboard;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
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);
setIcon(R.drawable.ic_arrow_down_24dp);
setTitle(R.string.advanced_section_header);
setOrder(999);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
holder.setDividerAllowedAbove(false);
}
}

View File

@@ -1,282 +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.dashboard;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.VisibleForTesting;
import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import android.util.FeatureFlagUtils;
import android.util.Log;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.instrumentation.Instrumentable;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnCreate;
import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ProgressiveDisclosureMixin implements Preference.OnPreferenceClickListener,
LifecycleObserver, OnCreate, OnSaveInstanceState {
@VisibleForTesting
static final String FEATURE_FLAG_NEW_ADVANCE_BUTTON = "new_settings_advance_button";
private static final String TAG = "ProgressiveDisclosure";
private static final String STATE_USER_EXPANDED = "state_user_expanded";
private static final int DEFAULT_TILE_LIMIT = 300;
private final Context mContext;
// Collapsed preference sorted by order.
private final List<Preference> mCollapsedPrefs = new ArrayList<>();
private final MetricsFeatureProvider mMetricsFeatureProvider;
private final PreferenceFragment mFragment;
private /* final */ ExpandPreference mExpandButton;
private int mTileLimit = DEFAULT_TILE_LIMIT;
private boolean mUserExpanded;
public ProgressiveDisclosureMixin(Context context,
PreferenceFragment fragment, boolean keepExpanded) {
mContext = context;
mFragment = fragment;
mExpandButton = new ExpandPreference(context);
mExpandButton.setOnPreferenceClickListener(this);
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
mUserExpanded = keepExpanded;
}
@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 : mCollapsedPrefs) {
screen.addPreference(pref);
}
mCollapsedPrefs.clear();
mUserExpanded = true;
final int metricsCategory;
if (mFragment instanceof Instrumentable) {
metricsCategory = ((Instrumentable) mFragment).getMetricsCategory();
} else {
metricsCategory = MetricsProto.MetricsEvent.VIEW_UNKNOWN;
}
mMetricsFeatureProvider.actionWithSource(mContext, metricsCategory,
MetricsProto.MetricsEvent.ACTION_SETTINGS_ADVANCED_BUTTON_EXPAND);
}
}
return false;
}
public boolean isEnabled() {
return !isV2Enabled();
}
public static boolean isV2Enabled() {
return FeatureFlagUtils.isEnabled(FEATURE_FLAG_NEW_ADVANCE_BUTTON);
}
/**
* Sets the threshold to start collapsing preferences when there are too many.
*/
public void setTileLimit(int limit) {
if (isEnabled()) {
mTileLimit = limit;
}
}
/**
* Whether the controller is in collapsed state.
*/
public boolean isCollapsed() {
return !mCollapsedPrefs.isEmpty();
}
/**
* Whether the screen should be collapsed.
*/
public boolean shouldCollapse(PreferenceScreen screen) {
return !mUserExpanded && screen.getPreferenceCount() > mTileLimit;
}
/**
* Collapse extra preferences and show a "More" button
*/
public void collapse(PreferenceScreen screen) {
final int itemCount = screen.getPreferenceCount();
if (!shouldCollapse(screen)) {
return;
}
if (!mCollapsedPrefs.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);
}
/**
* Adds preference to screen. If there are too many preference on screen, adds it to
* collapsed list instead.
*/
public void addPreference(PreferenceScreen screen, Preference pref) {
// Either add to screen, or to collapsed list.
if (isCollapsed()) {
// insert the preference to right position.
final int lastPreferenceIndex = screen.getPreferenceCount() - 2;
if (lastPreferenceIndex >= 0) {
final Preference lastPreference = screen.getPreference(lastPreferenceIndex);
if (lastPreference.getOrder() > pref.getOrder()) {
// insert to screen and move the last pref to collapsed list.
screen.removePreference(lastPreference);
screen.addPreference(pref);
addToCollapsedList(lastPreference);
} else {
// Insert to collapsed list.
addToCollapsedList(pref);
}
} else {
// Couldn't find last preference on screen, just add to collapsed list.
addToCollapsedList(pref);
}
} else if (shouldCollapse(screen)) {
// About to have too many tiles on scree, collapse and add pref to collapsed list.
screen.addPreference(pref);
collapse(screen);
} else {
// No need to collapse, add to screen directly.
screen.addPreference(pref);
}
}
/**
* Removes preference. If the preference is on screen, remove it from screen. If the
* preference is in collapsed list, remove it from list.
*/
public void removePreference(PreferenceScreen screen, String key) {
// Try removing from screen.
final Preference preference = screen.findPreference(key);
if (preference != null) {
screen.removePreference(preference);
return;
}
// Didn't find on screen, try removing from collapsed list.
for (int i = 0; i < mCollapsedPrefs.size(); i++) {
final Preference pref = mCollapsedPrefs.get(i);
if (TextUtils.equals(key, pref.getKey())) {
mCollapsedPrefs.remove(pref);
if (mCollapsedPrefs.isEmpty()) {
// Removed last element, remove expand button too.
screen.removePreference(mExpandButton);
} else {
updateExpandButtonSummary();
}
return;
}
}
}
/**
* Finds preference by key, either from screen or from collapsed list.
*/
public Preference findPreference(PreferenceScreen screen, CharSequence key) {
Preference preference = screen.findPreference(key);
if (preference != null) {
return preference;
}
for (int i = 0; i < mCollapsedPrefs.size(); i++) {
final Preference pref = mCollapsedPrefs.get(i);
if (TextUtils.equals(key, pref.getKey())) {
return pref;
}
if (pref instanceof PreferenceGroup) {
final Preference returnedPreference = ((PreferenceGroup) pref).findPreference(key);
if (returnedPreference != null) {
return returnedPreference;
}
}
}
Log.d(TAG, "Cannot find preference with key " + key);
return null;
}
/**
* Add preference to collapsed list.
*/
@VisibleForTesting
void addToCollapsedList(Preference preference) {
// Insert preference based on it's order.
int insertionIndex = Collections.binarySearch(mCollapsedPrefs, preference);
if (insertionIndex < 0) {
insertionIndex = insertionIndex * -1 - 1;
}
mCollapsedPrefs.add(insertionIndex, preference);
updateExpandButtonSummary();
}
@VisibleForTesting
List<Preference> getCollapsedPrefs() {
return mCollapsedPrefs;
}
@VisibleForTesting
void updateExpandButtonSummary() {
final int size = mCollapsedPrefs.size();
if (size == 0) {
mExpandButton.setSummary(null);
} else if (size == 1) {
mExpandButton.setSummary(mCollapsedPrefs.get(0).getTitle());
} else {
CharSequence summary = mCollapsedPrefs.get(0).getTitle();
for (int i = 1; i < size; i++) {
final CharSequence nextSummary = mCollapsedPrefs.get(i).getTitle();
if (!TextUtils.isEmpty(nextSummary)) {
summary = mContext.getString(R.string.join_many_items_middle, summary,
nextSummary);
}
}
mExpandButton.setSummary(summary);
}
}
}

View File

@@ -64,13 +64,6 @@ public class LanguageAndInputSettings extends DashboardFragment {
return TAG;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mProgressiveDisclosureMixin.setTileLimit(2);
}
@Override
public void onResume() {
super.onResume();
@@ -86,8 +79,7 @@ public class LanguageAndInputSettings extends DashboardFragment {
@Override
protected int getPreferenceScreenResId() {
return mProgressiveDisclosureMixin.isEnabled()
? R.xml.language_and_input : R.xml.language_and_input_new_advance_button;
return R.xml.language_and_input;
}
@Override

View File

@@ -54,12 +54,6 @@ public class SoundSettings extends DashboardFragment {
private RingtonePreference mRequestPreference;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mProgressiveDisclosureMixin.setTileLimit(1);
}
@Override
public int getMetricsCategory() {
return MetricsEvent.SOUND;
@@ -109,8 +103,7 @@ public class SoundSettings extends DashboardFragment {
@Override
protected int getPreferenceScreenResId() {
return mProgressiveDisclosureMixin.isEnabled()
? R.xml.sound_settings : R.xml.sound_settings_new_advance_button;
return R.xml.sound_settings;
}
@Override

View File

@@ -23,6 +23,7 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkScoreManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -60,8 +61,8 @@ public class ConfigureWifiSettings extends DashboardFragment {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
int tileLimit = 1;
if (mWifiWakeupPreferenceController.isAvailable()) {
tileLimit++;
@@ -69,7 +70,7 @@ public class ConfigureWifiSettings extends DashboardFragment {
if (mUseOpenWifiPreferenceController.isAvailable()) {
tileLimit++;
}
mProgressiveDisclosureMixin.setTileLimit(tileLimit);
getPreferenceScreen().setInitialExpandedChildrenCount(tileLimit);
}
@Override