Merge "Remove PreferenceXmlParserUtils deprecated methods" into main

This commit is contained in:
Chaohui Wang
2023-08-07 03:13:42 +00:00
committed by Android (Google) Code Review
5 changed files with 16 additions and 332 deletions

View File

@@ -17,7 +17,6 @@
package com.android.settings.core;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.XmlRes;
import android.content.Context;
import android.content.res.TypedArray;
@@ -26,11 +25,9 @@ import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.util.Xml;
import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
@@ -50,8 +47,7 @@ import java.util.List;
public class PreferenceXmlParserUtils {
private static final String TAG = "PreferenceXmlParserUtil";
@VisibleForTesting
static final String PREF_SCREEN_TAG = "PreferenceScreen";
public static final String PREF_SCREEN_TAG = "PreferenceScreen";
private static final List<String> SUPPORTED_PREF_TYPES = Arrays.asList(
"Preference", "PreferenceCategory", "PreferenceScreen",
"com.android.settings.widget.WorkOnlyCategory");
@@ -71,7 +67,9 @@ public class PreferenceXmlParserUtils {
MetadataFlag.FLAG_NEED_PREF_TITLE,
MetadataFlag.FLAG_NEED_PREF_SUMMARY,
MetadataFlag.FLAG_NEED_PREF_ICON,
MetadataFlag.FLAG_NEED_KEYWORDS,
MetadataFlag.FLAG_NEED_SEARCHABLE,
MetadataFlag.FLAG_NEED_PREF_APPEND,
MetadataFlag.FLAG_UNAVAILABLE_SLICE_SUBTITLE,
MetadataFlag.FLAG_FOR_WORK,
MetadataFlag.FLAG_NEED_HIGHLIGHTABLE_MENU_KEY,
@@ -109,70 +107,6 @@ public class PreferenceXmlParserUtils {
public static final String METADATA_HIGHLIGHTABLE_MENU_KEY = "highlightable_menu_key";
public static final String METADATA_USER_RESTRICTION = "userRestriction";
private static final String ENTRIES_SEPARATOR = "|";
/**
* Call {@link #extractMetadata(Context, int, int)} with {@link #METADATA_KEY} instead.
*/
@Deprecated
public static String getDataKey(Context context, AttributeSet attrs) {
return getStringData(context, attrs,
com.android.internal.R.styleable.Preference,
com.android.internal.R.styleable.Preference_key);
}
/**
* Call {@link #extractMetadata(Context, int, int)} with {@link #METADATA_TITLE} instead.
*/
@Deprecated
public static String getDataTitle(Context context, AttributeSet attrs) {
return getStringData(context, attrs,
com.android.internal.R.styleable.Preference,
com.android.internal.R.styleable.Preference_title);
}
/**
* Call {@link #extractMetadata(Context, int, int)} with {@link #METADATA_SUMMARY} instead.
*/
@Deprecated
public static String getDataSummary(Context context, AttributeSet attrs) {
return getStringData(context, attrs,
com.android.internal.R.styleable.Preference,
com.android.internal.R.styleable.Preference_summary);
}
public static String getDataSummaryOn(Context context, AttributeSet attrs) {
return getStringData(context, attrs,
com.android.internal.R.styleable.CheckBoxPreference,
com.android.internal.R.styleable.CheckBoxPreference_summaryOn);
}
public static String getDataSummaryOff(Context context, AttributeSet attrs) {
return getStringData(context, attrs,
com.android.internal.R.styleable.CheckBoxPreference,
com.android.internal.R.styleable.CheckBoxPreference_summaryOff);
}
public static String getDataEntries(Context context, AttributeSet attrs) {
return getDataEntries(context, attrs,
com.android.internal.R.styleable.ListPreference,
com.android.internal.R.styleable.ListPreference_entries);
}
public static String getDataKeywords(Context context, AttributeSet attrs) {
return getStringData(context, attrs, R.styleable.Preference,
R.styleable.Preference_keywords);
}
/**
* Call {@link #extractMetadata(Context, int, int)} with {@link #METADATA_CONTROLLER} instead.
*/
@Deprecated
public static String getController(Context context, AttributeSet attrs) {
return getStringData(context, attrs, R.styleable.Preference,
R.styleable.Preference_controller);
}
/**
* Extracts metadata from preference xml and put them into a {@link Bundle}.
*
@@ -276,45 +210,10 @@ public class PreferenceXmlParserUtils {
return metadata;
}
/**
* Call {@link #extractMetadata(Context, int, int)} with a {@link MetadataFlag} instead.
*/
@Deprecated
@Nullable
private static String getStringData(Context context, AttributeSet set, int[] attrs, int resId) {
final TypedArray ta = context.obtainStyledAttributes(set, attrs);
String data = ta.getString(resId);
ta.recycle();
return data;
}
private static boolean hasFlag(int flags, @MetadataFlag int flag) {
return (flags & flag) != 0;
}
private static String getDataEntries(Context context, AttributeSet set, int[] attrs,
int resId) {
final TypedArray sa = context.obtainStyledAttributes(set, attrs);
final TypedValue tv = sa.peekValue(resId);
sa.recycle();
String[] data = null;
if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) {
if (tv.resourceId != 0) {
data = context.getResources().getStringArray(tv.resourceId);
}
}
final int count = (data == null) ? 0 : data.length;
if (count == 0) {
return null;
}
final StringBuilder result = new StringBuilder();
for (int n = 0; n < count; n++) {
result.append(data[n]);
result.append(ENTRIES_SEPARATOR);
}
return result.toString();
}
private static String getKey(TypedArray styledAttributes) {
return styledAttributes.getString(com.android.internal.R.styleable.Preference_key);
}

View File

@@ -19,10 +19,12 @@ package com.android.settings.slices;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_CONTROLLER;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_ICON;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_PREF_TYPE;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_SUMMARY;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_TITLE;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_UNAVAILABLE_SLICE_SUBTITLE;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_USER_RESTRICTION;
import static com.android.settings.core.PreferenceXmlParserUtils.PREF_SCREEN_TAG;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.settings.SettingsEnums;
@@ -33,17 +35,15 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.net.Uri;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.provider.SettingsSlicesContract;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;
import android.view.accessibility.AccessibilityManager;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
@@ -59,7 +59,6 @@ import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.search.Indexable.SearchIndexProvider;
import com.android.settingslib.search.SearchIndexableData;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
@@ -80,8 +79,6 @@ class SliceDataConverter {
private static final String TAG = "SliceDataConverter";
private static final String NODE_NAME_PREFERENCE_SCREEN = "PreferenceScreen";
private final MetricsFeatureProvider mMetricsFeatureProvider;
private Context mContext;
@@ -155,36 +152,18 @@ class SliceDataConverter {
}
private List<SliceData> getSliceDataFromXML(int xmlResId, String fragmentName) {
XmlResourceParser parser = null;
final List<SliceData> xmlSliceData = new ArrayList<>();
String controllerClassName = "";
@NonNull String screenTitle = "";
try {
parser = mContext.getResources().getXml(xmlResId);
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& type != XmlPullParser.START_TAG) {
// Parse next until start tag is found
}
String nodeName = parser.getName();
if (!NODE_NAME_PREFERENCE_SCREEN.equals(nodeName)) {
throw new RuntimeException(
"XML document must start with <PreferenceScreen> tag; found"
+ nodeName + " at " + parser.getPositionDescription());
}
final AttributeSet attrs = Xml.asAttributeSet(parser);
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.
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
xmlResId,
MetadataFlag.FLAG_NEED_KEY
MetadataFlag.FLAG_INCLUDE_PREF_SCREEN
| MetadataFlag.FLAG_NEED_KEY
| MetadataFlag.FLAG_NEED_PREF_CONTROLLER
| MetadataFlag.FLAG_NEED_PREF_TYPE
| MetadataFlag.FLAG_NEED_PREF_TITLE
@@ -194,6 +173,13 @@ class SliceDataConverter {
| MetadataFlag.FLAG_NEED_USER_RESTRICTION);
for (Bundle bundle : metadata) {
final String title = bundle.getString(METADATA_TITLE);
if (PREF_SCREEN_TAG.equals(bundle.getString(METADATA_PREF_TYPE))) {
if (title != null) {
screenTitle = title;
}
continue;
}
// TODO (b/67996923) Non-controller Slices should become intent-only slices.
// Note that without a controller, dynamic summaries are impossible.
controllerClassName = bundle.getString(METADATA_CONTROLLER);
@@ -211,7 +197,6 @@ class SliceDataConverter {
|| controller instanceof RingerModeAffectedVolumePreferenceController)) {
continue;
}
final String title = bundle.getString(METADATA_TITLE);
final String summary = bundle.getString(METADATA_SUMMARY);
final int iconResId = bundle.getInt(METADATA_ICON);
@@ -261,8 +246,6 @@ class SliceDataConverter {
SettingsEnums.PAGE_UNKNOWN,
fragmentName + "_" + controllerClassName,
1);
} finally {
if (parser != null) parser.close();
}
return xmlSliceData;
}