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

@@ -97,13 +97,5 @@ public class AppAndNotificationDashboardFragment extends DashboardFragment {
Context context) {
return buildPreferenceControllers(context, null, null /* host */);
}
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
keys.add((new SpecialAppAccessPreferenceController(context))
.getPreferenceKey());
return keys;
}
};
}

View File

@@ -101,16 +101,6 @@ public class DefaultAppSettings extends DashboardFragment {
return Arrays.asList(sir);
}
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
keys.add(KEY_ASSIST_VOICE_INPUT);
// TODO (b/38230148) Remove these keys when we can differentiate work results
keys.add(DefaultWorkPhonePreferenceController.KEY);
keys.add(DefaultWorkBrowserPreferenceController.KEY);
return keys;
}
@Override
public List<AbstractPreferenceController> createPreferenceControllers(
Context context) {

View File

@@ -40,7 +40,7 @@ public class BackupSettingsActivityPreferenceController extends BasePreferenceCo
@Override
public int getAvailabilityStatus() {
return mUm.isAdminUser()
? AVAILABLE
? AVAILABLE_UNSEARCHABLE
: UNSUPPORTED_ON_DEVICE;
}

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);
}

View File

@@ -21,6 +21,7 @@ import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.provider.Settings;
@@ -30,7 +31,7 @@ final class AutofillDeveloperSettingsObserver extends ContentObserver {
private final ContentResolver mResolver;
public AutofillDeveloperSettingsObserver(Context context, Runnable changeCallback) {
super(new Handler());
super(new Handler(Looper.getMainLooper()));
mResolver = context.getContentResolver();
mChangeCallback = changeCallback;

View File

@@ -47,6 +47,9 @@ public class SwipeUpPreferenceController extends GesturePreferenceController {
final ComponentName recentsComponentName = ComponentName.unflattenFromString(
context.getString(R.string.config_recentsComponentName));
if (recentsComponentName == null) {
return false;
}
final Intent quickStepIntent = new Intent(ACTION_QUICKSTEP)
.setPackage(recentsComponentName.getPackageName());
if (context.getPackageManager().resolveService(quickStepIntent,

View File

@@ -108,7 +108,7 @@ public class LanguageAndInputSettings extends DashboardFragment {
// Pointer and Tts
final TtsPreferenceController ttsPreferenceController =
new TtsPreferenceController(context, new TtsEngines(context));
new TtsPreferenceController(context, KEY_TEXT_TO_SPEECH);
controllers.add(ttsPreferenceController);
final PointerSpeedController pointerController = new PointerSpeedController(context);
controllers.add(pointerController);
@@ -180,7 +180,6 @@ public class LanguageAndInputSettings extends DashboardFragment {
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
// Duplicates in summary and details pages.
keys.add(KEY_TEXT_TO_SPEECH);
keys.add(KEY_PHYSICAL_KEYBOARD);
return keys;
}

View File

@@ -19,31 +19,26 @@ package com.android.settings.language;
import android.content.Context;
import android.speech.tts.TtsEngines;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.core.BasePreferenceController;
public class TtsPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin {
public class TtsPreferenceController extends BasePreferenceController {
private static final String KEY_VOICE_CATEGORY = "voice_category";
private static final String KEY_TTS_SETTINGS = "tts_settings_summary";
@VisibleForTesting
TtsEngines mTtsEngines;
private final TtsEngines mTtsEngines;
public TtsPreferenceController(Context context, TtsEngines ttsEngines) {
super(context);
mTtsEngines = ttsEngines;
public TtsPreferenceController(Context context, String key) {
super(context, key);
mTtsEngines = new TtsEngines(context);
}
@Override
public boolean isAvailable() {
public int getAvailabilityStatus() {
return !mTtsEngines.getEngines().isEmpty() &&
mContext.getResources().getBoolean(R.bool.config_show_tts_settings_summary);
}
@Override
public String getPreferenceKey() {
return KEY_TTS_SETTINGS;
mContext.getResources().getBoolean(R.bool.config_show_tts_settings_summary)
? AVAILABLE_UNSEARCHABLE
: CONDITIONALLY_UNAVAILABLE;
}
}

View File

@@ -16,14 +16,21 @@
package com.android.settings.search;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_SEARCHABLE;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag
.FLAG_INCLUDE_PREF_SCREEN;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_KEY;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_SEARCHABLE;
import android.annotation.XmlRes;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;
import androidx.annotation.CallSuper;
import androidx.annotation.VisibleForTesting;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerListHelper;
@@ -31,16 +38,12 @@ import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.core.PreferenceXmlParserUtils;
import com.android.settingslib.core.AbstractPreferenceController;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.CallSuper;
import androidx.annotation.VisibleForTesting;
/**
* A basic SearchIndexProvider that returns no data to index.
*/
@@ -66,11 +69,12 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
public List<String> getNonIndexableKeys(Context context) {
if (!isPageSearchEnabled(context)) {
// Entire page should be suppressed, mark all keys from this page as non-indexable.
return getNonIndexableKeysFromXml(context);
return getNonIndexableKeysFromXml(context, true /* suppressAllPage */);
}
final List<String> nonIndexableKeys = new ArrayList<>();
nonIndexableKeys.addAll(getNonIndexableKeysFromXml(context, false /* suppressAllPage */));
final List<AbstractPreferenceController> controllers = getPreferenceControllers(context);
if (controllers != null && !controllers.isEmpty()) {
final List<String> nonIndexableKeys = new ArrayList<>();
for (AbstractPreferenceController controller : controllers) {
if (controller instanceof PreferenceControllerMixin) {
((PreferenceControllerMixin) controller)
@@ -85,10 +89,8 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
nonIndexableKeys.add(controller.getPreferenceKey());
}
}
return nonIndexableKeys;
} else {
return new ArrayList<>();
}
return nonIndexableKeys;
}
@Override
@@ -131,7 +133,11 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
return true;
}
private List<String> getNonIndexableKeysFromXml(Context context) {
/**
* Get all non-indexable keys from xml. If {@param suppressAllPage} is set, all keys are
* considered non-indexable. Otherwise, only keys with searchable="false" are included.
*/
private List<String> getNonIndexableKeysFromXml(Context context, boolean suppressAllPage) {
final List<SearchIndexableResource> resources = getXmlResourcesToIndex(
context, true /* not used*/);
if (resources == null || resources.isEmpty()) {
@@ -139,27 +145,32 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
}
final List<String> nonIndexableKeys = new ArrayList<>();
for (SearchIndexableResource res : resources) {
nonIndexableKeys.addAll(getNonIndexableKeysFromXml(context, res.xmlResId));
nonIndexableKeys.addAll(
getNonIndexableKeysFromXml(context, res.xmlResId, suppressAllPage));
}
return nonIndexableKeys;
}
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
public List<String> getNonIndexableKeysFromXml(Context context, @XmlRes int xmlResId) {
final List<String> nonIndexableKeys = new ArrayList<>();
final XmlResourceParser parser = context.getResources().getXml(xmlResId);
final AttributeSet attrs = Xml.asAttributeSet(parser);
public List<String> getNonIndexableKeysFromXml(Context context, @XmlRes int xmlResId,
boolean suppressAllPage) {
return getKeysFromXml(context, xmlResId, suppressAllPage);
}
private List<String> getKeysFromXml(Context context, @XmlRes int xmlResId,
boolean suppressAllPage) {
final List<String> keys = new ArrayList<>();
try {
while (parser.next() != XmlPullParser.END_DOCUMENT) {
final String key = PreferenceXmlParserUtils.getDataKey(context, attrs);
if (!TextUtils.isEmpty(key)) {
nonIndexableKeys.add(key);
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(context,
xmlResId, FLAG_NEED_KEY | FLAG_INCLUDE_PREF_SCREEN | FLAG_NEED_SEARCHABLE);
for (Bundle bundle : metadata) {
if (suppressAllPage || !bundle.getBoolean(METADATA_SEARCHABLE, true)) {
keys.add(bundle.getString(METADATA_KEY));
}
}
} catch (IOException | XmlPullParserException e) {
Log.w(TAG, "Error parsing non-indexable from xml " + xmlResId);
}
return nonIndexableKeys;
return keys;
}
}

View File

@@ -101,8 +101,6 @@ public class SystemDashboardFragment extends DashboardFragment {
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
keys.add((new BackupSettingsActivityPreferenceController(
context).getPreferenceKey()));
keys.add(KEY_RESET);
return keys;
}

View File

@@ -49,6 +49,13 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.SimpleAdapter;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.util.UserIcons;
import com.android.internal.widget.LockPatternUtils;
@@ -76,13 +83,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
/**
* Screen that manages the list of users on the device.
* Guest user is an always visible entry, even if the guest is not currently
@@ -635,8 +635,8 @@ public class UserSettings extends SettingsPreferenceFragment
AlertDialog.Builder builder = new AlertDialog.Builder(context);
SimpleAdapter adapter = new SimpleAdapter(builder.getContext(),
data, R.layout.two_line_list_item,
new String[] {KEY_TITLE, KEY_SUMMARY},
new int[] {R.id.title, R.id.summary});
new String[]{KEY_TITLE, KEY_SUMMARY},
new int[]{R.id.title, R.id.summary});
builder.setTitle(R.string.user_add_user_type_title);
builder.setAdapter(adapter,
new DialogInterface.OnClickListener() {
@@ -1238,8 +1238,10 @@ public class UserSettings extends SettingsPreferenceFragment
}
@Override
public List<String> getNonIndexableKeysFromXml(Context context, int xmlResId) {
final List<String> niks = super.getNonIndexableKeysFromXml(context, xmlResId);
public List<String> getNonIndexableKeysFromXml(Context context, int xmlResId,
boolean suppressAllPage) {
final List<String> niks = super.getNonIndexableKeysFromXml(context, xmlResId,
suppressAllPage);
new AddUserWhenLockedPreferenceController(context, KEY_ADD_USER_WHEN_LOCKED)
.updateNonIndexableKeys(niks);
new AutoSyncDataPreferenceController(context, null /* parent */)