Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1b9bda3b authored by Bonian Chen's avatar Bonian Chen
Browse files

[Settings] Avoid rendering hidden preferences

Under dashboards with expand button, preferences are renderred even
not been expanded.
Avoid from rendering un-expanded preference can improve the performance
of dashboard display.

Bug: 141833767
Test: manual
Change-Id: I00c6f827a0b7b7cec6a6fd8c809b94ca1dce88bb
parent d4e2f78d
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -258,6 +258,18 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
        return 0;
    }

    /**
     * Whether preference is allowing to be displayed to the user.
     *
     * @param preference to check if it can be displayed to the user (not hidding in expand area).
     * @return {@code true} when preference is allowing to be displayed to the user.
     * {@code false} when preference is hidden in expand area and not been displayed to the user.
     */
    protected boolean isPreferenceExpanded(Preference preference) {
        return ((mAdapter == null)
                || (mAdapter.getPreferenceAdapterPosition(preference) != RecyclerView.NO_POSITION));
    }

    protected void onDataSetChanged() {
        highlightPreferenceIfNeeded();
        updateEmptyView();
+7 −0
Original line number Diff line number Diff line
@@ -319,6 +319,13 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
        return false;
    }

    /**
     * Get current PreferenceController(s)
     */
    protected Collection<List<AbstractPreferenceController>> getPreferenceControllers() {
        return mPreferenceControllers.values();
    }

    /**
     * Update state of each preference managed by PreferenceController.
     */
+56 −7
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.view.MenuItem;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.dashboard.RestrictedDashboardFragment;
@@ -47,7 +48,9 @@ import com.android.settings.widget.PreferenceCategoryController;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
@@ -72,6 +75,8 @@ public class MobileNetworkSettings extends RestrictedDashboardFragment {
    private UserManager mUserManager;
    private String mClickedPrefKey;

    private List<AbstractPreferenceController> mHiddenControllerList;

    public MobileNetworkSettings() {
        super(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
    }
@@ -113,13 +118,13 @@ public class MobileNetworkSettings extends RestrictedDashboardFragment {
                MobileNetworkUtils.getSearchableSubscriptionId(context));
        Log.i(LOG_TAG, "display subId: " + mSubId);

        if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
            return Arrays.asList();
        }
        return Arrays.asList(
                new DataUsageSummaryPreferenceController(getActivity(), getSettingsLifecycle(),
                        this, mSubId));
    }
        return Arrays.asList();
    }

    @Override
    public void onAttach(Context context) {
@@ -187,6 +192,50 @@ public class MobileNetworkSettings extends RestrictedDashboardFragment {
        onRestoreInstance(icicle);
    }

    @Override
    public void onExpandButtonClick() {
        final PreferenceScreen screen = getPreferenceScreen();
        mHiddenControllerList.stream()
                .filter(controller -> controller.isAvailable())
                .forEach(controller -> {
                    final String key = controller.getPreferenceKey();
                    final Preference preference = screen.findPreference(key);
                    controller.updateState(preference);
                });
        super.onExpandButtonClick();
    }

    /*
     * Replace design within {@link DashboardFragment#updatePreferenceStates()}
     */
    @Override
    protected void updatePreferenceStates() {
        mHiddenControllerList = new ArrayList<AbstractPreferenceController>();

        final PreferenceScreen screen = getPreferenceScreen();
        final Collection<List<AbstractPreferenceController>> controllerLists =
                getPreferenceControllers();
        controllerLists.stream().flatMap(Collection::stream)
                .forEach(controller -> {
                    final String key = controller.getPreferenceKey();
                    if (TextUtils.isEmpty(key)) {
                        return;
                    }
                    final Preference preference = screen.findPreference(key);
                    if (preference == null) {
                        return;
                    }
                    if (!isPreferenceExpanded(preference)) {
                        mHiddenControllerList.add(controller);
                        return;
                    }
                    if (!controller.isAvailable()) {
                        return;
                    }
                    controller.updateState(preference);
                });
    }

    @VisibleForTesting
    void onRestoreInstance(Bundle icicle) {
        if (icicle != null) {
@@ -238,7 +287,7 @@ public class MobileNetworkSettings extends RestrictedDashboardFragment {

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        if (SubscriptionManager.isValidSubscriptionId(mSubId)) {
            final MenuItem item = menu.add(Menu.NONE, R.id.edit_sim_name, Menu.NONE,
                    R.string.mobile_network_sim_name);
            item.setIcon(com.android.internal.R.drawable.ic_mode_edit);
@@ -249,7 +298,7 @@ public class MobileNetworkSettings extends RestrictedDashboardFragment {

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        if (SubscriptionManager.isValidSubscriptionId(mSubId)) {
            if (menuItem.getItemId() == R.id.edit_sim_name) {
                RenameMobileNetworkDialogFragment.newInstance(mSubId).show(
                        getFragmentManager(), RenameMobileNetworkDialogFragment.TAG);