Merge "Instantiate pref controllers from xml if it's defined."
This commit is contained in:
committed by
Android (Google) Code Review
commit
8e6d5ad27e
@@ -116,6 +116,9 @@ public abstract class BasePreferenceController extends AbstractPreferenceControl
|
||||
public BasePreferenceController(Context context, String preferenceKey) {
|
||||
super(context);
|
||||
mPreferenceKey = preferenceKey;
|
||||
if (TextUtils.isEmpty(mPreferenceKey)) {
|
||||
throw new IllegalArgumentException("Preference key must be set");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* 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.core;
|
||||
|
||||
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_CONTROLLER;
|
||||
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.XmlRes;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* Helper to load {@link BasePreferenceController} lists from Xml.
|
||||
*/
|
||||
public class PreferenceControllerListHelper {
|
||||
|
||||
private static final String TAG = "PrefCtrlListCreator";
|
||||
|
||||
/**
|
||||
* Instantiates a list of controller based on xml definition.
|
||||
*/
|
||||
@NonNull
|
||||
public static List<BasePreferenceController> getPreferenceControllersFromXml(Context context,
|
||||
@XmlRes int xmlResId) {
|
||||
final List<BasePreferenceController> controllers = new ArrayList<>();
|
||||
List<Bundle> preferenceMetadata;
|
||||
try {
|
||||
preferenceMetadata = PreferenceXmlParserUtils.extractMetadata(context, xmlResId);
|
||||
} catch (IOException | XmlPullParserException e) {
|
||||
Log.e(TAG, "Failed to parse preference xml for getting controllers");
|
||||
return controllers;
|
||||
}
|
||||
|
||||
for (Bundle metadata : preferenceMetadata) {
|
||||
final String controllerName = metadata.getString(METADATA_CONTROLLER);
|
||||
if (TextUtils.isEmpty(controllerName)) {
|
||||
continue;
|
||||
}
|
||||
BasePreferenceController controller;
|
||||
try {
|
||||
controller = BasePreferenceController.createInstance(context, controllerName);
|
||||
} catch (IllegalStateException e) {
|
||||
final String key = metadata.getString(METADATA_KEY);
|
||||
if (TextUtils.isEmpty(key)) {
|
||||
Log.w(TAG, "Controller requires key but it's not defined in xml: "
|
||||
+ controllerName);
|
||||
continue;
|
||||
}
|
||||
Log.d(TAG, "Could not find Context-only controller for pref: " + key);
|
||||
controller = BasePreferenceController.createInstance(context, controllerName, key);
|
||||
}
|
||||
controllers.add(controller);
|
||||
}
|
||||
return controllers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a sub list of {@link AbstractPreferenceController} to only contain controller that
|
||||
* doesn't exist in filter.
|
||||
*
|
||||
* @param filter The filter. This list will be unchanged.
|
||||
* @param input This list will be filtered into a sublist and element is kept
|
||||
* IFF the controller key is not used by anything from {@param filter}.
|
||||
*/
|
||||
@NonNull
|
||||
public static List<BasePreferenceController> filterControllers(
|
||||
@NonNull List<BasePreferenceController> input,
|
||||
List<AbstractPreferenceController> filter) {
|
||||
if (input == null || filter == null) {
|
||||
return input;
|
||||
}
|
||||
final Set<String> keys = new TreeSet<>();
|
||||
final List<BasePreferenceController> filteredList = new ArrayList<>();
|
||||
for (AbstractPreferenceController controller : filter) {
|
||||
final String key = controller.getPreferenceKey();
|
||||
if (key != null) {
|
||||
keys.add(key);
|
||||
}
|
||||
}
|
||||
for (BasePreferenceController controller : input) {
|
||||
if (keys.contains(controller.getPreferenceKey())) {
|
||||
Log.w(TAG, controller.getPreferenceKey() + " already has a controller");
|
||||
continue;
|
||||
}
|
||||
filteredList.add(controller);
|
||||
}
|
||||
return filteredList;
|
||||
}
|
||||
|
||||
}
|
@@ -16,18 +16,40 @@
|
||||
|
||||
package com.android.settings.core;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.content.res.XmlResourceParser;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.util.TypedValue;
|
||||
import android.util.Xml;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Utility class to parse elements of XML preferences
|
||||
*/
|
||||
public class PreferenceXmlParserUtils {
|
||||
|
||||
private static final String TAG = "PreferenceXmlParserUtil";
|
||||
|
||||
private static final List<String> SUPPORTED_PREF_TYPES = Arrays.asList(
|
||||
"Preference", "PreferenceCategory", "PreferenceScreen");
|
||||
|
||||
public static final String METADATA_KEY = "key";
|
||||
public static final String METADATA_CONTROLLER = "controller";
|
||||
|
||||
private static final String ENTRIES_SEPARATOR = "|";
|
||||
|
||||
public static String getDataKey(Context context, AttributeSet attrs) {
|
||||
@@ -82,6 +104,47 @@ public class PreferenceXmlParserUtils {
|
||||
return dataIcon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts metadata from preference xml and put them into a {@link Bundle}.
|
||||
*
|
||||
* TODO(zhfan): Similar logic exists in {@link SliceBuilderUtils} and
|
||||
* {@link UniquePreferenceTest}. Need refactoring to consolidate them all.
|
||||
*/
|
||||
@NonNull
|
||||
public static List<Bundle> extractMetadata(Context context, int xmlResId)
|
||||
throws IOException, XmlPullParserException {
|
||||
final List<Bundle> metadata = new ArrayList<>();
|
||||
if (xmlResId <= 0) {
|
||||
Log.d(TAG, xmlResId + " is invalid.");
|
||||
return metadata;
|
||||
}
|
||||
final XmlResourceParser parser = context.getResources().getXml(xmlResId);
|
||||
|
||||
int type;
|
||||
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
|
||||
&& type != XmlPullParser.START_TAG) {
|
||||
// Parse next until start tag is found
|
||||
}
|
||||
final int outerDepth = parser.getDepth();
|
||||
|
||||
do {
|
||||
if (type != XmlPullParser.START_TAG) {
|
||||
continue;
|
||||
}
|
||||
final String nodeName = parser.getName();
|
||||
if (!SUPPORTED_PREF_TYPES.contains(nodeName) && !nodeName.endsWith("Preference")) {
|
||||
continue;
|
||||
}
|
||||
final Bundle preferenceMetadata = new Bundle();
|
||||
final AttributeSet attrs = Xml.asAttributeSet(parser);
|
||||
preferenceMetadata.putString(METADATA_KEY, getDataKey(context, attrs));
|
||||
preferenceMetadata.putString(METADATA_CONTROLLER, getController(context, attrs));
|
||||
metadata.add(preferenceMetadata);
|
||||
} while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
|
||||
&& (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth));
|
||||
return metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fragment name if this preference launches a child fragment.
|
||||
*/
|
||||
@@ -98,7 +161,8 @@ public class PreferenceXmlParserUtils {
|
||||
return data;
|
||||
}
|
||||
|
||||
private static String getDataEntries(Context context, AttributeSet set, int[] attrs, int resId) {
|
||||
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();
|
||||
@@ -108,7 +172,7 @@ public class PreferenceXmlParserUtils {
|
||||
data = context.getResources().getStringArray(tv.resourceId);
|
||||
}
|
||||
}
|
||||
final int count = (data == null ) ? 0 : data.length;
|
||||
final int count = (data == null) ? 0 : data.length;
|
||||
if (count == 0) {
|
||||
return null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user