diff --git a/src/com/android/settings/SettingsPreferenceFragment.java b/src/com/android/settings/SettingsPreferenceFragment.java index 07ba28f0418..80e57060431 100644 --- a/src/com/android/settings/SettingsPreferenceFragment.java +++ b/src/com/android/settings/SettingsPreferenceFragment.java @@ -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(); diff --git a/src/com/android/settings/dashboard/DashboardFragment.java b/src/com/android/settings/dashboard/DashboardFragment.java index 0d3d5b000f7..d1211953d68 100644 --- a/src/com/android/settings/dashboard/DashboardFragment.java +++ b/src/com/android/settings/dashboard/DashboardFragment.java @@ -319,6 +319,13 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment return false; } + /** + * Get current PreferenceController(s) + */ + protected Collection> getPreferenceControllers() { + return mPreferenceControllers.values(); + } + /** * Update state of each preference managed by PreferenceController. */ diff --git a/src/com/android/settings/network/telephony/MobileNetworkSettings.java b/src/com/android/settings/network/telephony/MobileNetworkSettings.java index a4b32e65c3d..199564dd4d2 100644 --- a/src/com/android/settings/network/telephony/MobileNetworkSettings.java +++ b/src/com/android/settings/network/telephony/MobileNetworkSettings.java @@ -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 mHiddenControllerList; + public MobileNetworkSettings() { super(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS); } @@ -113,12 +118,12 @@ public class MobileNetworkSettings extends RestrictedDashboardFragment { MobileNetworkUtils.getSearchableSubscriptionId(context)); Log.i(LOG_TAG, "display subId: " + mSubId); - if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { - return Arrays.asList( - new DataUsageSummaryPreferenceController(getActivity(), getSettingsLifecycle(), - this, mSubId)); + if (!SubscriptionManager.isValidSubscriptionId(mSubId)) { + return Arrays.asList(); } - return Arrays.asList(); + return Arrays.asList( + new DataUsageSummaryPreferenceController(getActivity(), getSettingsLifecycle(), + this, mSubId)); } @Override @@ -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(); + + final PreferenceScreen screen = getPreferenceScreen(); + final Collection> 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);