From a7c152816d4d644091756d4909d799a63d54550d Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Fri, 2 Aug 2024 13:35:42 +0800 Subject: [PATCH] Speed up BasePreferenceController.updateNonIndexableKeys By avoid unnecessary calls to getAvailabilityStatus(), which also avoid unnecessary crash potential. - When key already marked searchable="false" in xml, no need to check getAvailabilityStatus() again. - When getAvailabilityStatus() return AVAILABLE, no need to call it again to check it not equals to AVAILABLE_UNSEARCHABLE. Bug: 352455031 Flag: EXEMPT refactor Test: redo search index - by changing locale Change-Id: Ic0c43b9bcd5974907b3a7d4aba73d4c7203f8af4 --- .../core/BasePreferenceController.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/com/android/settings/core/BasePreferenceController.java b/src/com/android/settings/core/BasePreferenceController.java index 5763d3b93ef..3f91fb70ab4 100644 --- a/src/com/android/settings/core/BasePreferenceController.java +++ b/src/com/android/settings/core/BasePreferenceController.java @@ -263,6 +263,16 @@ public abstract class BasePreferenceController extends AbstractPreferenceControl || availabilityStatus == DISABLED_DEPENDENT_SETTING); } + private boolean isAvailableForSearch() { + if (mIsForWork && mWorkProfileUser == null) { + return false; + } + + final int availabilityStatus = getAvailabilityStatus(); + return (availabilityStatus == AVAILABLE + || availabilityStatus == DISABLED_DEPENDENT_SETTING); + } + /** * @return {@code false} if the setting is not applicable to the device. This covers both * settings which were only introduced in future versions of android, or settings that have @@ -303,18 +313,12 @@ public abstract class BasePreferenceController extends AbstractPreferenceControl * Called by SearchIndexProvider#getNonIndexableKeys */ public void updateNonIndexableKeys(List keys) { - final boolean shouldSuppressFromSearch = !isAvailable() - || getAvailabilityStatus() == AVAILABLE_UNSEARCHABLE; - if (shouldSuppressFromSearch) { - final String key = getPreferenceKey(); - if (TextUtils.isEmpty(key)) { - Log.w(TAG, "Skipping updateNonIndexableKeys due to empty key " + toString()); - return; - } - if (keys.contains(key)) { - Log.w(TAG, "Skipping updateNonIndexableKeys, key already in list. " + toString()); - return; - } + final String key = getPreferenceKey(); + if (TextUtils.isEmpty(key)) { + Log.w(TAG, "Skipping updateNonIndexableKeys due to empty key " + this); + return; + } + if (!keys.contains(key) && !isAvailableForSearch()) { keys.add(key); } }