Improve the robustness of updateNonIndexableKeys

- Isolate preference controller crash in the same page
  Current if one search item throw exception, entire page's search will
  broken, can isolate issue by controller to improve.

- Expose preference controller crash in debuggable build
  Current the search index exception is silently ignored, expose the
  exception in debuggable builds to expose issue earlier.

- Change default to hide items instead of shown when crash

Fix: 352455031
Fix: 352455432
Fix: 352455540
Flag: EXEMPT bug fix
Test: manual - Settings Search
Change-Id: I2cdbd58543be8fe6617e42d9c02789791186f53b
This commit is contained in:
Chaohui Wang
2024-07-31 14:58:35 +08:00
parent 86e5406c46
commit a6d7b8c9d9
2 changed files with 48 additions and 22 deletions

View File

@@ -21,9 +21,11 @@ import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_SEARCH
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_INCLUDE_PREF_SCREEN; import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_INCLUDE_PREF_SCREEN;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_KEY; import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_KEY;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_SEARCHABLE; import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_SEARCHABLE;
import static com.android.settings.search.SettingsSearchIndexablesProvider.SYSPROP_CRASH_ON_ERROR;
import android.annotation.XmlRes; import android.annotation.XmlRes;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.SearchIndexableResource; import android.provider.SearchIndexableResource;
import android.util.Log; import android.util.Log;
@@ -131,24 +133,48 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
return nonIndexableKeys; return nonIndexableKeys;
} }
nonIndexableKeys.addAll(getNonIndexableKeysFromXml(context, false /* suppressAllPage */)); nonIndexableKeys.addAll(getNonIndexableKeysFromXml(context, false /* suppressAllPage */));
updateNonIndexableKeysFromControllers(context, nonIndexableKeys);
return nonIndexableKeys;
}
private void updateNonIndexableKeysFromControllers(
Context context, List<String> nonIndexableKeys) {
final List<AbstractPreferenceController> controllers = getPreferenceControllers(context); final List<AbstractPreferenceController> controllers = getPreferenceControllers(context);
if (controllers != null && !controllers.isEmpty()) { if (controllers != null) {
for (AbstractPreferenceController controller : controllers) { for (AbstractPreferenceController controller : controllers) {
if (controller instanceof PreferenceControllerMixin) { updateNonIndexableKeysFromController(nonIndexableKeys, controller);
((PreferenceControllerMixin) controller)
.updateNonIndexableKeys(nonIndexableKeys);
} else if (controller instanceof BasePreferenceController) {
((BasePreferenceController) controller).updateNonIndexableKeys(
nonIndexableKeys);
} else {
Log.e(TAG, controller.getClass().getName()
+ " must implement " + PreferenceControllerMixin.class.getName()
+ " treating the key non-indexable");
nonIndexableKeys.add(controller.getPreferenceKey());
}
} }
} }
return nonIndexableKeys; }
private static void updateNonIndexableKeysFromController(
List<String> nonIndexableKeys, AbstractPreferenceController controller) {
try {
if (controller instanceof PreferenceControllerMixin controllerMixin) {
controllerMixin.updateNonIndexableKeys(nonIndexableKeys);
} else if (controller instanceof BasePreferenceController basePreferenceController) {
basePreferenceController.updateNonIndexableKeys(nonIndexableKeys);
} else {
Log.e(TAG, controller.getClass().getName()
+ " must implement " + PreferenceControllerMixin.class.getName()
+ " treating the key non-indexable");
nonIndexableKeys.add(controller.getPreferenceKey());
}
} catch (Exception e) {
String msg = "Error trying to get non-indexable keys from: " + controller;
// Catch a generic crash. In the absence of the catch, the background thread will
// silently fail anyway, so we aren't losing information by catching the exception.
// We crash on debuggable build or when the system property exists, so that we can test
// if crashes need to be fixed.
// The gain is that if there is a crash in a specific controller, we don't lose all
// non-indexable keys, but we can still find specific crashes in development.
if (Build.IS_DEBUGGABLE || System.getProperty(SYSPROP_CRASH_ON_ERROR) != null) {
throw new RuntimeException(msg, e);
}
Log.e(TAG, msg, e);
// When there is an error, treat the key as non-indexable.
nonIndexableKeys.add(controller.getPreferenceKey());
}
} }
public List<AbstractPreferenceController> getPreferenceControllers(Context context) { public List<AbstractPreferenceController> getPreferenceControllers(Context context) {

View File

@@ -50,6 +50,7 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.MatrixCursor; import android.database.MatrixCursor;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.provider.SearchIndexableResource; import android.provider.SearchIndexableResource;
import android.provider.SearchIndexablesContract; import android.provider.SearchIndexablesContract;
import android.provider.SearchIndexablesProvider; import android.provider.SearchIndexablesProvider;
@@ -283,17 +284,16 @@ public class SettingsSearchIndexablesProvider extends SearchIndexablesProvider {
try { try {
providerNonIndexableKeys = provider.getNonIndexableKeys(context); providerNonIndexableKeys = provider.getNonIndexableKeys(context);
} catch (Exception e) { } catch (Exception e) {
String msg = "Error trying to get non-indexable keys from: "
+ bundle.getTargetClass().getName();
// Catch a generic crash. In the absence of the catch, the background thread will // Catch a generic crash. In the absence of the catch, the background thread will
// silently fail anyway, so we aren't losing information by catching the exception. // silently fail anyway, so we aren't losing information by catching the exception.
// We crash when the system property exists so that we can test if crashes need to // We crash on debuggable build or when the system property exists, so that we can
// be fixed. // test if crashes need to be fixed.
// The gain is that if there is a crash in a specific controller, we don't lose all if (Build.IS_DEBUGGABLE || System.getProperty(SYSPROP_CRASH_ON_ERROR) != null) {
// non-indexable keys, but we can still find specific crashes in development. throw new RuntimeException(msg, e);
if (System.getProperty(SYSPROP_CRASH_ON_ERROR) != null) {
throw new RuntimeException(e);
} }
Log.e(TAG, "Error trying to get non-indexable keys from: " Log.e(TAG, msg, e);
+ bundle.getTargetClass().getName(), e);
continue; continue;
} }