From 1ac5a012539fda7081fc2646cf330759a456f38b Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Tue, 20 Feb 2018 17:43:28 -0800 Subject: [PATCH] Move xmlParserUtils to core This class is currently used by both search and slice, in the future will be used by DashboardFragment to build controller list. So the scope of this class is beyond search now. Test: rerun robotests Change-Id: If43ebca065aac31ad24f95a94bfe5be784109605 --- .../PreferenceXmlParserUtils.java} | 5 ++- .../search/BaseSearchIndexProvider.java | 3 +- .../search/indexing/IndexDataConverter.java | 33 +++++++++---------- .../settings/slices/SliceDataConverter.java | 14 ++++---- .../core/XmlControllerAttributeTest.java | 3 +- .../settings/search/XmlParserUtilTest.java | 25 +++++++------- .../slices/SliceControllerInXmlTest.java | 4 +-- .../settings/testutils/XmlTestUtils.java | 4 +-- .../settings/core/UniquePreferenceTest.java | 3 +- 9 files changed, 46 insertions(+), 48 deletions(-) rename src/com/android/settings/{search/XmlParserUtils.java => core/PreferenceXmlParserUtils.java} (98%) diff --git a/src/com/android/settings/search/XmlParserUtils.java b/src/com/android/settings/core/PreferenceXmlParserUtils.java similarity index 98% rename from src/com/android/settings/search/XmlParserUtils.java rename to src/com/android/settings/core/PreferenceXmlParserUtils.java index 27c5cd36593..4d6e84069d9 100644 --- a/src/com/android/settings/search/XmlParserUtils.java +++ b/src/com/android/settings/core/PreferenceXmlParserUtils.java @@ -12,10 +12,9 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ -package com.android.settings.search; +package com.android.settings.core; import android.annotation.Nullable; import android.content.Context; @@ -27,7 +26,7 @@ import com.android.settings.R; /** * Utility class to parse elements of XML preferences */ -public class XmlParserUtils { +public class PreferenceXmlParserUtils { private static final String ENTRIES_SEPARATOR = "|"; diff --git a/src/com/android/settings/search/BaseSearchIndexProvider.java b/src/com/android/settings/search/BaseSearchIndexProvider.java index 57233009993..f75aa85a1fb 100644 --- a/src/com/android/settings/search/BaseSearchIndexProvider.java +++ b/src/com/android/settings/search/BaseSearchIndexProvider.java @@ -29,6 +29,7 @@ import android.util.Xml; import com.android.settings.core.BasePreferenceController; import com.android.settings.core.PreferenceControllerMixin; +import com.android.settings.core.PreferenceXmlParserUtils; import com.android.settingslib.core.AbstractPreferenceController; import org.xmlpull.v1.XmlPullParser; @@ -119,7 +120,7 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider { final AttributeSet attrs = Xml.asAttributeSet(parser); try { while (parser.next() != XmlPullParser.END_DOCUMENT) { - final String key = XmlParserUtils.getDataKey(context, attrs); + final String key = PreferenceXmlParserUtils.getDataKey(context, attrs); if (!TextUtils.isEmpty(key)) { nonIndexableKeys.add(key); } diff --git a/src/com/android/settings/search/indexing/IndexDataConverter.java b/src/com/android/settings/search/indexing/IndexDataConverter.java index c40a46658ed..5d20704b4a8 100644 --- a/src/com/android/settings/search/indexing/IndexDataConverter.java +++ b/src/com/android/settings/search/indexing/IndexDataConverter.java @@ -29,11 +29,10 @@ import android.util.AttributeSet; import android.util.Log; import android.util.Xml; -import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.search.DatabaseIndexingUtils; +import com.android.settings.core.PreferenceXmlParserUtils; import com.android.settings.search.ResultPayload; import com.android.settings.search.SearchIndexableRaw; -import com.android.settings.search.XmlParserUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -161,8 +160,8 @@ public class IndexDataConverter { final int outerDepth = parser.getDepth(); final AttributeSet attrs = Xml.asAttributeSet(parser); - final String screenTitle = XmlParserUtils.getDataTitle(context, attrs); - String key = XmlParserUtils.getDataKey(context, attrs); + final String screenTitle = PreferenceXmlParserUtils.getDataTitle(context, attrs); + String key = PreferenceXmlParserUtils.getDataKey(context, attrs); String title; String headerTitle; @@ -186,9 +185,9 @@ public class IndexDataConverter { .getPayloadKeyMap(fragmentName, context); } - headerTitle = XmlParserUtils.getDataTitle(context, attrs); - headerSummary = XmlParserUtils.getDataSummary(context, attrs); - headerKeywords = XmlParserUtils.getDataKeywords(context, attrs); + headerTitle = PreferenceXmlParserUtils.getDataTitle(context, attrs); + headerSummary = PreferenceXmlParserUtils.getDataSummary(context, attrs); + headerKeywords = PreferenceXmlParserUtils.getDataKeywords(context, attrs); enabled = !nonIndexableKeys.contains(key); // TODO: Set payload type for header results @@ -217,11 +216,11 @@ public class IndexDataConverter { nodeName = parser.getName(); - title = XmlParserUtils.getDataTitle(context, attrs); - key = XmlParserUtils.getDataKey(context, attrs); + title = PreferenceXmlParserUtils.getDataTitle(context, attrs); + key = PreferenceXmlParserUtils.getDataKey(context, attrs); enabled = !nonIndexableKeys.contains(key); - keywords = XmlParserUtils.getDataKeywords(context, attrs); - iconResId = XmlParserUtils.getDataIcon(context, attrs); + keywords = PreferenceXmlParserUtils.getDataKeywords(context, attrs); + iconResId = PreferenceXmlParserUtils.getDataIcon(context, attrs); if (isHeaderUnique && TextUtils.equals(headerTitle, title)) { isHeaderUnique = false; @@ -241,17 +240,17 @@ public class IndexDataConverter { .setUserId(-1 /* default user id */); if (!nodeName.equals(NODE_NAME_CHECK_BOX_PREFERENCE)) { - summary = XmlParserUtils.getDataSummary(context, attrs); + summary = PreferenceXmlParserUtils.getDataSummary(context, attrs); String entries = null; if (nodeName.endsWith(NODE_NAME_LIST_PREFERENCE)) { - entries = XmlParserUtils.getDataEntries(context, attrs); + entries = PreferenceXmlParserUtils.getDataEntries(context, attrs); } // TODO (b/62254931) index primitives instead of payload payload = controllerUriMap.get(key); - childFragment = XmlParserUtils.getDataChildFragment(context, attrs); + childFragment = PreferenceXmlParserUtils.getDataChildFragment(context, attrs); builder.setSummaryOn(summary) .setEntries(entries) @@ -263,11 +262,11 @@ public class IndexDataConverter { // TODO (b/33577327) We removed summary off here. We should check if we can // merge this 'else' section with the one above. Put a break point to // investigate. - String summaryOn = XmlParserUtils.getDataSummaryOn(context, attrs); - String summaryOff = XmlParserUtils.getDataSummaryOff(context, attrs); + String summaryOn = PreferenceXmlParserUtils.getDataSummaryOn(context, attrs); + String summaryOff = PreferenceXmlParserUtils.getDataSummaryOff(context, attrs); if (TextUtils.isEmpty(summaryOn) && TextUtils.isEmpty(summaryOff)) { - summaryOn = XmlParserUtils.getDataSummary(context, attrs); + summaryOn = PreferenceXmlParserUtils.getDataSummary(context, attrs); } builder.setSummaryOn(summaryOn); diff --git a/src/com/android/settings/slices/SliceDataConverter.java b/src/com/android/settings/slices/SliceDataConverter.java index e5a21e40c1a..a023b160a79 100644 --- a/src/com/android/settings/slices/SliceDataConverter.java +++ b/src/com/android/settings/slices/SliceDataConverter.java @@ -30,7 +30,7 @@ import com.android.settings.dashboard.DashboardFragment; import com.android.settings.overlay.FeatureFactory; import com.android.settings.search.DatabaseIndexingUtils; import com.android.settings.search.Indexable.SearchIndexProvider; -import com.android.settings.search.XmlParserUtils; +import com.android.settings.core.PreferenceXmlParserUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -151,7 +151,7 @@ class SliceDataConverter { final int outerDepth = parser.getDepth(); final AttributeSet attrs = Xml.asAttributeSet(parser); - final String screenTitle = XmlParserUtils.getDataTitle(mContext, attrs); + final String screenTitle = PreferenceXmlParserUtils.getDataTitle(mContext, attrs); // TODO (b/67996923) Investigate if we need headers for Slices, since they never // correspond to an actual setting. @@ -168,15 +168,15 @@ class SliceDataConverter { // TODO (b/67996923) This will not work if preferences have nested intents: // // - controllerClassName = XmlParserUtils.getController(mContext, attrs); + controllerClassName = PreferenceXmlParserUtils.getController(mContext, attrs); if (TextUtils.isEmpty(controllerClassName)) { continue; } - title = XmlParserUtils.getDataTitle(mContext, attrs); - key = XmlParserUtils.getDataKey(mContext, attrs); - iconResId = XmlParserUtils.getDataIcon(mContext, attrs); - summary = XmlParserUtils.getDataSummary(mContext, attrs); + title = PreferenceXmlParserUtils.getDataTitle(mContext, attrs); + key = PreferenceXmlParserUtils.getDataKey(mContext, attrs); + iconResId = PreferenceXmlParserUtils.getDataIcon(mContext, attrs); + summary = PreferenceXmlParserUtils.getDataSummary(mContext, attrs); xmlSlice = new SliceData.Builder() .setKey(key) diff --git a/tests/robotests/src/com/android/settings/core/XmlControllerAttributeTest.java b/tests/robotests/src/com/android/settings/core/XmlControllerAttributeTest.java index e69b97ef0c0..4939cca57f3 100644 --- a/tests/robotests/src/com/android/settings/core/XmlControllerAttributeTest.java +++ b/tests/robotests/src/com/android/settings/core/XmlControllerAttributeTest.java @@ -17,7 +17,6 @@ import com.android.settings.search.DatabaseIndexingUtils; import com.android.settings.search.Indexable; import com.android.settings.search.SearchFeatureProvider; import com.android.settings.search.SearchFeatureProviderImpl; -import com.android.settings.search.XmlParserUtils; import com.android.settings.security.SecuritySettings; import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.SettingsRobolectricTestRunner; @@ -183,7 +182,7 @@ public class XmlControllerAttributeTest { continue; } - controllerClassName = XmlParserUtils.getController(mContext, attrs); + controllerClassName = PreferenceXmlParserUtils.getController(mContext, attrs); // If controller is not indexed, then it is not compatible with if (!TextUtils.isEmpty(controllerClassName)) { xmlControllers.add(controllerClassName); diff --git a/tests/robotests/src/com/android/settings/search/XmlParserUtilTest.java b/tests/robotests/src/com/android/settings/search/XmlParserUtilTest.java index af7a4628886..2d1a232fa47 100644 --- a/tests/robotests/src/com/android/settings/search/XmlParserUtilTest.java +++ b/tests/robotests/src/com/android/settings/search/XmlParserUtilTest.java @@ -26,6 +26,7 @@ import android.util.Xml; import com.android.settings.R; import com.android.settings.TestConfig; +import com.android.settings.core.PreferenceXmlParserUtils; import com.android.settings.testutils.SettingsRobolectricTestRunner; import org.junit.Before; @@ -58,7 +59,7 @@ public class XmlParserUtilTest { XmlResourceParser parser = getChildByType(R.xml.display_settings, "com.android.settings.TimeoutListPreference"); final AttributeSet attrs = Xml.asAttributeSet(parser); - String title = XmlParserUtils.getDataTitle(mContext, attrs); + String title = PreferenceXmlParserUtils.getDataTitle(mContext, attrs); String expTitle = mContext.getString(R.string.screen_timeout); assertThat(title).isEqualTo(expTitle); } @@ -67,7 +68,7 @@ public class XmlParserUtilTest { public void testDataKeywordsValid_ReturnsPreferenceKeywords() { XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings); final AttributeSet attrs = Xml.asAttributeSet(parser); - String keywords = XmlParserUtils.getDataKeywords(mContext, attrs); + String keywords = PreferenceXmlParserUtils.getDataKeywords(mContext, attrs); String expKeywords = mContext.getString(R.string.keywords_display); assertThat(keywords).isEqualTo(expKeywords); } @@ -77,7 +78,7 @@ public class XmlParserUtilTest { XmlResourceParser parser = getChildByType(R.xml.display_settings, "com.android.settings.TimeoutListPreference"); final AttributeSet attrs = Xml.asAttributeSet(parser); - String key = XmlParserUtils.getDataKey(mContext, attrs); + String key = PreferenceXmlParserUtils.getDataKey(mContext, attrs); String expKey = "screen_timeout"; assertThat(key).isEqualTo(expKey); } @@ -87,7 +88,7 @@ public class XmlParserUtilTest { XmlResourceParser parser = getChildByType(R.xml.display_settings, "com.android.settings.TimeoutListPreference"); final AttributeSet attrs = Xml.asAttributeSet(parser); - String summary = XmlParserUtils.getDataSummary(mContext, attrs); + String summary = PreferenceXmlParserUtils.getDataSummary(mContext, attrs); String expSummary = mContext.getString(R.string.summary_placeholder); assertThat(summary).isEqualTo(expSummary); @@ -99,9 +100,9 @@ public class XmlParserUtilTest { XmlResourceParser parser = getChildByType(R.xml.display_settings, "CheckBoxPreference"); final AttributeSet attrs = Xml.asAttributeSet(parser); - assertThat(XmlParserUtils.getDataSummaryOn(mContext, attrs)) + assertThat(PreferenceXmlParserUtils.getDataSummaryOn(mContext, attrs)) .isEqualTo("summary_on"); - assertThat(XmlParserUtils.getDataSummaryOff(mContext, attrs)) + assertThat(PreferenceXmlParserUtils.getDataSummaryOff(mContext, attrs)) .isEqualTo("summary_off"); } @@ -110,7 +111,7 @@ public class XmlParserUtilTest { public void testDataEntriesValid_ReturnsPreferenceEntries() { XmlResourceParser parser = getChildByType(R.xml.display_settings, "ListPreference"); final AttributeSet attrs = Xml.asAttributeSet(parser); - String entries = XmlParserUtils.getDataEntries(mContext, attrs); + String entries = PreferenceXmlParserUtils.getDataEntries(mContext, attrs); String[] expEntries = mContext.getResources() .getStringArray(R.array.app_install_location_entries); for (int i = 0; i < expEntries.length; i++) { @@ -124,7 +125,7 @@ public class XmlParserUtilTest { public void testDataKeyInvalid_ReturnsNull() { XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings); final AttributeSet attrs = Xml.asAttributeSet(parser); - String key = XmlParserUtils.getDataKey(mContext, attrs); + String key = PreferenceXmlParserUtils.getDataKey(mContext, attrs); assertThat(key).isNull(); } @@ -134,7 +135,7 @@ public class XmlParserUtilTest { XmlResourceParser parser = getChildByType(R.xml.about_legal, "Preference"); final AttributeSet attrs = Xml.asAttributeSet(parser); - String controller = XmlParserUtils.getController(mContext, attrs); + String controller = PreferenceXmlParserUtils.getController(mContext, attrs); assertThat(controller).isEqualTo("mind_flayer"); } @@ -142,7 +143,7 @@ public class XmlParserUtilTest { public void testDataSummaryInvalid_ReturnsNull() { XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings); final AttributeSet attrs = Xml.asAttributeSet(parser); - String summary = XmlParserUtils.getDataSummary(mContext, attrs); + String summary = PreferenceXmlParserUtils.getDataSummary(mContext, attrs); assertThat(summary).isNull(); } @@ -150,7 +151,7 @@ public class XmlParserUtilTest { public void testDataSummaryOffInvalid_ReturnsNull() { XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings); final AttributeSet attrs = Xml.asAttributeSet(parser); - String summaryOff = XmlParserUtils.getDataSummaryOff(mContext, attrs); + String summaryOff = PreferenceXmlParserUtils.getDataSummaryOff(mContext, attrs); assertThat(summaryOff).isNull(); } @@ -158,7 +159,7 @@ public class XmlParserUtilTest { public void testDataEntriesInvalid_ReturnsNull() { XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings); final AttributeSet attrs = Xml.asAttributeSet(parser); - String entries = XmlParserUtils.getDataEntries(mContext, attrs); + String entries = PreferenceXmlParserUtils.getDataEntries(mContext, attrs); assertThat(entries).isNull(); } diff --git a/tests/robotests/src/com/android/settings/slices/SliceControllerInXmlTest.java b/tests/robotests/src/com/android/settings/slices/SliceControllerInXmlTest.java index 66ed459cb3f..9c079a7e87b 100644 --- a/tests/robotests/src/com/android/settings/slices/SliceControllerInXmlTest.java +++ b/tests/robotests/src/com/android/settings/slices/SliceControllerInXmlTest.java @@ -38,7 +38,7 @@ import com.android.settings.search.DatabaseIndexingUtils; import com.android.settings.search.Indexable; import com.android.settings.search.SearchFeatureProvider; import com.android.settings.search.SearchFeatureProviderImpl; -import com.android.settings.search.XmlParserUtils; +import com.android.settings.core.PreferenceXmlParserUtils; import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.SettingsRobolectricTestRunner; @@ -111,7 +111,7 @@ public class SliceControllerInXmlTest { if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } - controllerClassName = XmlParserUtils.getController(mContext, attrs); + controllerClassName = PreferenceXmlParserUtils.getController(mContext, attrs); if (!TextUtils.isEmpty(controllerClassName)) { mXmlDeclaredControllers.add(controllerClassName); diff --git a/tests/robotests/src/com/android/settings/testutils/XmlTestUtils.java b/tests/robotests/src/com/android/settings/testutils/XmlTestUtils.java index 4f5600226fb..2b27649a926 100644 --- a/tests/robotests/src/com/android/settings/testutils/XmlTestUtils.java +++ b/tests/robotests/src/com/android/settings/testutils/XmlTestUtils.java @@ -7,7 +7,7 @@ import android.content.Context; import android.text.TextUtils; import android.util.AttributeSet; import android.util.Xml; -import com.android.settings.search.XmlParserUtils; +import com.android.settings.core.PreferenceXmlParserUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -35,7 +35,7 @@ public class XmlTestUtils { try { while (parser.next() != XmlPullParser.END_DOCUMENT) { try { - key = XmlParserUtils.getDataKey(context, attrs); + key = PreferenceXmlParserUtils.getDataKey(context, attrs); if (!TextUtils.isEmpty(key)) { keys.add(key); } diff --git a/tests/unit/src/com/android/settings/core/UniquePreferenceTest.java b/tests/unit/src/com/android/settings/core/UniquePreferenceTest.java index dd275913677..e2087098230 100644 --- a/tests/unit/src/com/android/settings/core/UniquePreferenceTest.java +++ b/tests/unit/src/com/android/settings/core/UniquePreferenceTest.java @@ -36,7 +36,6 @@ import com.android.settings.search.DatabaseIndexingUtils; import com.android.settings.search.Indexable; import com.android.settings.search.SearchIndexableRaw; import com.android.settings.search.SearchIndexableResources; -import com.android.settings.search.XmlParserUtils; import org.junit.Before; import org.junit.Test; @@ -203,7 +202,7 @@ public class UniquePreferenceTest { continue; } final AttributeSet attrs = Xml.asAttributeSet(parser); - final String key = XmlParserUtils.getDataKey(mContext, attrs); + final String key = PreferenceXmlParserUtils.getDataKey(mContext, attrs); if (TextUtils.isEmpty(key)) { Log.e(TAG, "Every preference must have an key; found null key" + " in " + className