Recursively remove preference from screen.

The preference framework does not remove pref recursively. So when a
preference is nested in preference hierarchy, it's not safe to simply
call screen.removePreference(). We need to first find its parent and
remove pref from its parent.

Change-Id: Ic7fefa498ed71a8877d862845ddcc2d6d6034a55
Fix: 38507066
Test: make RunSettingsRoboTests
This commit is contained in:
Fan Zhang
2017-05-24 11:19:52 -07:00
parent a935b3c030
commit e84407f5c3
2 changed files with 134 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.XmlRes;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
@@ -423,11 +424,28 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
return -1;
}
protected void removePreference(String key) {
Preference pref = findPreference(key);
if (pref != null) {
getPreferenceScreen().removePreference(pref);
protected boolean removePreference(String key) {
return removePreference(getPreferenceScreen(), key);
}
@VisibleForTesting
boolean removePreference(PreferenceGroup group, String key) {
final int preferenceCount = group.getPreferenceCount();
for (int i = 0; i < preferenceCount; i++) {
final Preference preference = group.getPreference(i);
final String curKey = preference.getKey();
if (TextUtils.equals(curKey, key)) {
return group.removePreference(preference);
}
if (preference instanceof PreferenceGroup) {
if (removePreference((PreferenceGroup) preference, key)) {
return true;
}
}
}
return false;
}
/**