Declare "searchable" attribute for preferences.

Now we can easily mark a preference nonIndexable in xml instead of
adding key into searchIndexProvider.

Bug: 112608186
Test: robotests
Change-Id: I0ff16d44bb7b6ad148d3d35f09ca0da0163f73f4
This commit is contained in:
Fan Zhang
2018-08-14 16:25:54 -07:00
parent 097cfa7251
commit a79c377fbc
28 changed files with 201 additions and 148 deletions

View File

@@ -274,6 +274,10 @@ public abstract class BasePreferenceController extends AbstractPreferenceControl
Log.w(TAG, "Skipping updateNonIndexableKeys due to empty key " + toString());
return;
}
if (keys.contains(key)) {
Log.w(TAG, "Skipping updateNonIndexableKeys, key already in list. " + toString());
return;
}
keys.add(key);
}
}

View File

@@ -42,7 +42,12 @@ public interface PreferenceControllerMixin {
final String key = ((AbstractPreferenceController) this).getPreferenceKey();
if (TextUtils.isEmpty(key)) {
Log.w(TAG,
"Skipping updateNonIndexableKeys due to empty key " + this.toString());
"Skipping updateNonIndexableKeys due to empty key " + toString());
return;
}
if (keys.contains(key)) {
Log.w(TAG, "Skipping updateNonIndexableKeys, key already in list. "
+ toString());
return;
}
keys.add(key);

View File

@@ -29,6 +29,9 @@ 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;
import org.xmlpull.v1.XmlPullParser;
@@ -41,9 +44,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;
/**
* Utility class to parse elements of XML preferences
*/
@@ -53,7 +53,8 @@ public class PreferenceXmlParserUtils {
@VisibleForTesting
static final String PREF_SCREEN_TAG = "PreferenceScreen";
private static final List<String> SUPPORTED_PREF_TYPES = Arrays.asList(
"Preference", "PreferenceCategory", "PreferenceScreen");
"Preference", "PreferenceCategory", "PreferenceScreen",
"com.android.settings.widget.WorkOnlyCategory");
/**
* Flag definition to indicate which metadata should be extracted when
@@ -67,7 +68,8 @@ public class PreferenceXmlParserUtils {
MetadataFlag.FLAG_NEED_PREF_CONTROLLER,
MetadataFlag.FLAG_NEED_PREF_TITLE,
MetadataFlag.FLAG_NEED_PREF_SUMMARY,
MetadataFlag.FLAG_NEED_PREF_ICON})
MetadataFlag.FLAG_NEED_PREF_ICON,
MetadataFlag.FLAG_NEED_SEARCHABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface MetadataFlag {
int FLAG_INCLUDE_PREF_SCREEN = 1;
@@ -79,6 +81,7 @@ public class PreferenceXmlParserUtils {
int FLAG_NEED_PREF_ICON = 1 << 6;
int FLAG_NEED_PLATFORM_SLICE_FLAG = 1 << 7;
int FLAG_NEED_KEYWORDS = 1 << 8;
int FLAG_NEED_SEARCHABLE = 1 << 9;
}
public static final String METADATA_PREF_TYPE = "type";
@@ -89,6 +92,7 @@ public class PreferenceXmlParserUtils {
public static final String METADATA_ICON = "icon";
public static final String METADATA_PLATFORM_SLICE_FLAG = "platform_slice";
public static final String METADATA_KEYWORDS = "keywords";
public static final String METADATA_SEARCHABLE = "searchable";
private static final String ENTRIES_SEPARATOR = "|";
@@ -154,18 +158,6 @@ public class PreferenceXmlParserUtils {
R.styleable.Preference_controller);
}
/**
* Call {@link #extractMetadata(Context, int, int)} with {@link #METADATA_ICON} instead.
*/
@Deprecated
public static int getDataIcon(Context context, AttributeSet attrs) {
final TypedArray ta = context.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.Preference);
final int dataIcon = ta.getResourceId(com.android.internal.R.styleable.Icon_icon, 0);
ta.recycle();
return dataIcon;
}
/**
* Extracts metadata from preference xml and put them into a {@link Bundle}.
*
@@ -232,6 +224,10 @@ public class PreferenceXmlParserUtils {
if (hasFlag(flags, MetadataFlag.FLAG_NEED_KEYWORDS)) {
preferenceMetadata.putString(METADATA_KEYWORDS, getKeywords(preferenceAttributes));
}
if (hasFlag(flags, MetadataFlag.FLAG_NEED_SEARCHABLE)) {
preferenceMetadata.putBoolean(METADATA_SEARCHABLE,
isSearchable(preferenceAttributes));
}
metadata.add(preferenceMetadata);
preferenceAttributes.recycle();
@@ -312,6 +308,10 @@ public class PreferenceXmlParserUtils {
return styledAttributes.getBoolean(R.styleable.Preference_platform_slice, false /* def */);
}
private static boolean isSearchable(TypedArray styledAttributes) {
return styledAttributes.getBoolean(R.styleable.Preference_searchable, true /* default */);
}
private static String getKeywords(TypedArray styleAttributes) {
return styleAttributes.getString(R.styleable.Preference_keywords);
}