FileDocCategorySizeDatePackage
SearchableInfo.javaAPI DocAndroid 5.1 API33872Thu Mar 12 22:22:10 GMT 2015android.app

SearchableInfo

public final class SearchableInfo extends Object implements android.os.Parcelable
Searchability meta-data for an activity. Only applications that search other applications should need to use this class. See Searchable Configuration for more information about declaring searchability meta-data for your application.
see
SearchManager#getSearchableInfo(ComponentName)
see
SearchManager#getSearchablesInGlobalSearch()

Fields Summary
private static final boolean
DBG
private static final String
LOG_TAG
private static final String
MD_LABEL_SEARCHABLE
private static final String
MD_XML_ELEMENT_SEARCHABLE
private static final String
MD_XML_ELEMENT_SEARCHABLE_ACTION_KEY
private static final int
SEARCH_MODE_BADGE_LABEL
private static final int
SEARCH_MODE_BADGE_ICON
private static final int
SEARCH_MODE_QUERY_REWRITE_FROM_DATA
private static final int
SEARCH_MODE_QUERY_REWRITE_FROM_TEXT
private final int
mLabelId
private final android.content.ComponentName
mSearchActivity
private final int
mHintId
private final int
mSearchMode
private final int
mIconId
private final int
mSearchButtonText
private final int
mSearchInputType
private final int
mSearchImeOptions
private final boolean
mIncludeInGlobalSearch
private final boolean
mQueryAfterZeroResults
private final boolean
mAutoUrlDetect
private final int
mSettingsDescriptionId
private final String
mSuggestAuthority
private final String
mSuggestPath
private final String
mSuggestSelection
private final String
mSuggestIntentAction
private final String
mSuggestIntentData
private final int
mSuggestThreshold
private HashMap
mActionKeys
private final String
mSuggestProviderPackage
private static final int
VOICE_SEARCH_SHOW_BUTTON
private static final int
VOICE_SEARCH_LAUNCH_WEB_SEARCH
private static final int
VOICE_SEARCH_LAUNCH_RECOGNIZER
private final int
mVoiceSearchMode
private final int
mVoiceLanguageModeId
private final int
mVoicePromptTextId
private final int
mVoiceLanguageId
private final int
mVoiceMaxResults
public static final Parcelable.Creator
CREATOR
Support for parcelable and aidl operations.
Constructors Summary
private SearchableInfo(android.content.Context activityContext, android.util.AttributeSet attr, android.content.ComponentName cName)
Constructor Given a ComponentName, get the searchability info and build a local copy of it. Use the factory, not this.

param
activityContext runtime context for the activity that the searchable info is about.
param
attr The attribute set we found in the XML file, contains the values that are used to construct the object.
param
cName The component name of the searchable activity
throws
IllegalArgumentException if the searchability info is invalid or insufficient

        mSearchActivity = cName;
        
        TypedArray a = activityContext.obtainStyledAttributes(attr,
                com.android.internal.R.styleable.Searchable);
        mSearchMode = a.getInt(com.android.internal.R.styleable.Searchable_searchMode, 0);
        mLabelId = a.getResourceId(com.android.internal.R.styleable.Searchable_label, 0);
        mHintId = a.getResourceId(com.android.internal.R.styleable.Searchable_hint, 0);
        mIconId = a.getResourceId(com.android.internal.R.styleable.Searchable_icon, 0);
        mSearchButtonText = a.getResourceId(
                com.android.internal.R.styleable.Searchable_searchButtonText, 0);
        mSearchInputType = a.getInt(com.android.internal.R.styleable.Searchable_inputType, 
                InputType.TYPE_CLASS_TEXT |
                InputType.TYPE_TEXT_VARIATION_NORMAL);
        mSearchImeOptions = a.getInt(com.android.internal.R.styleable.Searchable_imeOptions, 
                EditorInfo.IME_ACTION_GO);
        mIncludeInGlobalSearch = a.getBoolean(
                com.android.internal.R.styleable.Searchable_includeInGlobalSearch, false);
        mQueryAfterZeroResults = a.getBoolean(
                com.android.internal.R.styleable.Searchable_queryAfterZeroResults, false);
        mAutoUrlDetect = a.getBoolean(
                com.android.internal.R.styleable.Searchable_autoUrlDetect, false);

        mSettingsDescriptionId = a.getResourceId(
                com.android.internal.R.styleable.Searchable_searchSettingsDescription, 0);
        mSuggestAuthority = a.getString(
                com.android.internal.R.styleable.Searchable_searchSuggestAuthority);
        mSuggestPath = a.getString(
                com.android.internal.R.styleable.Searchable_searchSuggestPath);
        mSuggestSelection = a.getString(
                com.android.internal.R.styleable.Searchable_searchSuggestSelection);
        mSuggestIntentAction = a.getString(
                com.android.internal.R.styleable.Searchable_searchSuggestIntentAction);
        mSuggestIntentData = a.getString(
                com.android.internal.R.styleable.Searchable_searchSuggestIntentData);
        mSuggestThreshold = a.getInt(
                com.android.internal.R.styleable.Searchable_searchSuggestThreshold, 0);

        mVoiceSearchMode = 
            a.getInt(com.android.internal.R.styleable.Searchable_voiceSearchMode, 0);
        // TODO this didn't work - came back zero from YouTube
        mVoiceLanguageModeId = 
            a.getResourceId(com.android.internal.R.styleable.Searchable_voiceLanguageModel, 0);
        mVoicePromptTextId = 
            a.getResourceId(com.android.internal.R.styleable.Searchable_voicePromptText, 0);
        mVoiceLanguageId = 
            a.getResourceId(com.android.internal.R.styleable.Searchable_voiceLanguage, 0);
        mVoiceMaxResults = 
            a.getInt(com.android.internal.R.styleable.Searchable_voiceMaxResults, 0);

        a.recycle();

        // get package info for suggestions provider (if any)
        String suggestProviderPackage = null;
        if (mSuggestAuthority != null) {
            PackageManager pm = activityContext.getPackageManager();
            ProviderInfo pi = pm.resolveContentProvider(mSuggestAuthority, 0);
            if (pi != null) {
                suggestProviderPackage = pi.packageName;
            }
        }
        mSuggestProviderPackage = suggestProviderPackage;

        // for now, implement some form of rules - minimal data
        if (mLabelId == 0) {
            throw new IllegalArgumentException("Search label must be a resource reference.");
        }
    
SearchableInfo(android.os.Parcel in)
Instantiates a new SearchableInfo from the data in a Parcel that was previously written with {@link #writeToParcel(Parcel, int)}.

param
in The Parcel containing the previously written SearchableInfo, positioned at the location in the buffer where it was written.


                                               
      
        mLabelId = in.readInt();
        mSearchActivity = ComponentName.readFromParcel(in);
        mHintId = in.readInt();
        mSearchMode = in.readInt();
        mIconId = in.readInt();
        mSearchButtonText = in.readInt();
        mSearchInputType = in.readInt();
        mSearchImeOptions = in.readInt();
        mIncludeInGlobalSearch = in.readInt() != 0;
        mQueryAfterZeroResults = in.readInt() != 0;
        mAutoUrlDetect = in.readInt() != 0;
        
        mSettingsDescriptionId = in.readInt();
        mSuggestAuthority = in.readString();
        mSuggestPath = in.readString();
        mSuggestSelection = in.readString();
        mSuggestIntentAction = in.readString();
        mSuggestIntentData = in.readString();
        mSuggestThreshold = in.readInt();

        for (int count = in.readInt(); count > 0; count--) {
            addActionKey(new ActionKeyInfo(in));
        }

        mSuggestProviderPackage = in.readString();
        
        mVoiceSearchMode = in.readInt();
        mVoiceLanguageModeId = in.readInt();
        mVoicePromptTextId = in.readInt();
        mVoiceLanguageId = in.readInt();
        mVoiceMaxResults = in.readInt();
    
Methods Summary
private voidaddActionKey(android.app.SearchableInfo$ActionKeyInfo keyInfo)

        if (mActionKeys == null) {
            mActionKeys = new HashMap<Integer,ActionKeyInfo>();
        }
        mActionKeys.put(keyInfo.getKeyCode(), keyInfo);
    
public booleanautoUrlDetect()
Checks whether this searchable activity has auto URL detection turned on.

see
android.R.styleable#Searchable_autoUrlDetect

        return mAutoUrlDetect;
    
private static android.content.ContextcreateActivityContext(android.content.Context context, android.content.ComponentName activity)
Creates a context for another activity.

        Context theirContext = null;
        try {
            theirContext = context.createPackageContext(activity.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(LOG_TAG, "Package not found " + activity.getPackageName());
        } catch (java.lang.SecurityException e) {
            Log.e(LOG_TAG, "Can't make context for " + activity.getPackageName(), e);
        }
        
        return theirContext;
    
public intdescribeContents()

        return 0;
    
public android.app.SearchableInfo$ActionKeyInfofindActionKey(int keyCode)
If any action keys were defined for this searchable activity, look up and return.

param
keyCode The key that was pressed
return
Returns the action key info, or {@code null} if none defined.
hide
ActionKeyInfo is hidden

        if (mActionKeys == null) {
            return null;
        }
        return mActionKeys.get(keyCode);
    
public android.content.ContextgetActivityContext(android.content.Context context)
Get the context for the searchable activity.

param
context You need to supply a context to start with
return
Returns a context related to the searchable activity
hide

        return createActivityContext(context, mSearchActivity);
    
public static android.app.SearchableInfogetActivityMetaData(android.content.Context context, android.content.pm.ActivityInfo activityInfo, int userId)
Gets search information for the given activity.

param
context Context to use for reading activity resources.
param
activityInfo Activity to get search information from.
return
Search information about the given activity, or {@code null} if the activity has no or invalid searchability meta-data.
hide
For use by SearchManagerService.

        Context userContext = null;
        try {
            userContext = context.createPackageContextAsUser("system", 0,
                new UserHandle(userId));
        } catch (NameNotFoundException nnfe) {
            Log.e(LOG_TAG, "Couldn't create package context for user " + userId);
            return null;
        }
        // for each component, try to find metadata
        XmlResourceParser xml = 
                activityInfo.loadXmlMetaData(userContext.getPackageManager(), MD_LABEL_SEARCHABLE);
        if (xml == null) {
            return null;
        }
        ComponentName cName = new ComponentName(activityInfo.packageName, activityInfo.name);
        
        SearchableInfo searchable = getActivityMetaData(userContext, xml, cName);
        xml.close();
        
        if (DBG) {
            if (searchable != null) {
                Log.d(LOG_TAG, "Checked " + activityInfo.name
                        + ",label=" + searchable.getLabelId()
                        + ",icon=" + searchable.getIconId()
                        + ",suggestAuthority=" + searchable.getSuggestAuthority()
                        + ",target=" + searchable.getSearchActivity().getClassName()
                        + ",global=" + searchable.shouldIncludeInGlobalSearch()
                        + ",settingsDescription=" + searchable.getSettingsDescriptionId()
                        + ",threshold=" + searchable.getSuggestThreshold());
            } else {
                Log.d(LOG_TAG, "Checked " + activityInfo.name + ", no searchable meta-data");
            }
        }
        return searchable;
    
private static android.app.SearchableInfogetActivityMetaData(android.content.Context context, org.xmlpull.v1.XmlPullParser xml, android.content.ComponentName cName)
Get the metadata for a given activity

param
context runtime context
param
xml XML parser for reading attributes
param
cName The component name of the searchable activity
result
A completely constructed SearchableInfo, or null if insufficient XML data for it

        SearchableInfo result = null;
        Context activityContext = createActivityContext(context, cName);
        if (activityContext == null) return null;

        // in order to use the attributes mechanism, we have to walk the parser
        // forward through the file until it's reading the tag of interest.
        try {
            int tagType = xml.next();
            while (tagType != XmlPullParser.END_DOCUMENT) {
                if (tagType == XmlPullParser.START_TAG) {
                    if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE)) {
                        AttributeSet attr = Xml.asAttributeSet(xml);
                        if (attr != null) {
                            try {
                                result = new SearchableInfo(activityContext, attr, cName);
                            } catch (IllegalArgumentException ex) {
                                Log.w(LOG_TAG, "Invalid searchable metadata for " +
                                        cName.flattenToShortString() + ": " + ex.getMessage());
                                return null;
                            }
                        }
                    } else if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE_ACTION_KEY)) {
                        if (result == null) {
                            // Can't process an embedded element if we haven't seen the enclosing
                            return null;
                        }
                        AttributeSet attr = Xml.asAttributeSet(xml);
                        if (attr != null) {
                            try {
                                result.addActionKey(new ActionKeyInfo(activityContext, attr));
                            } catch (IllegalArgumentException ex) {
                                Log.w(LOG_TAG, "Invalid action key for " +
                                        cName.flattenToShortString() + ": " + ex.getMessage());
                                return null;
                            }
                        }
                    }
                }
                tagType = xml.next();
            }
        } catch (XmlPullParserException e) {
            Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
            return null;
        } catch (IOException e) {
            Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
            return null;
        }
        
        return result;
    
public intgetHintId()
Gets the resource id of the hint text. This must be read using the searchable Activity's resources.

return
A resource id, or {@code 0} if no hint was specified.
see
android.R.styleable#Searchable_hint

        return mHintId;
    
public intgetIconId()
Gets the icon id specified by the Searchable_icon meta-data entry. This must be read using the searchable Activity's resources.

return
A resource id, or {@code 0} if no icon was specified.
see
android.R.styleable#Searchable_icon
hide
deprecated functionality

        return mIconId;
    
public intgetImeOptions()
Gets the input method options specified in the searchable attributes. This will default to {@link EditorInfo#IME_ACTION_GO} if not specified (which is appropriate for a search box).

return
the input type
see
android.R.styleable#Searchable_imeOptions

        return mSearchImeOptions;
    
public intgetInputType()
Gets the input type as specified in the searchable attributes. This will default to {@link InputType#TYPE_CLASS_TEXT} if not specified (which is appropriate for free text input).

return
the input type
see
android.R.styleable#Searchable_inputType

        return mSearchInputType;
    
public intgetLabelId()
Gets the "label" (user-visible name) of this searchable context. This must be read using the searchable Activity's resources.

return
A resource id, or {@code 0} if no label was specified.
see
android.R.styleable#Searchable_label
hide
deprecated functionality

        return mLabelId;
    
public android.content.ContextgetProviderContext(android.content.Context context, android.content.Context activityContext)
Get the context for the suggestions provider.

param
context You need to supply a context to start with
param
activityContext If we can determine that the provider and the activity are the same, we'll just return this one.
return
Returns a context related to the suggestion provider
hide

        Context theirContext = null;
        if (mSearchActivity.getPackageName().equals(mSuggestProviderPackage)) {
            return activityContext;
        }
        if (mSuggestProviderPackage != null) {
            try {
                theirContext = context.createPackageContext(mSuggestProviderPackage, 0);
            } catch (PackageManager.NameNotFoundException e) {
                // unexpected, but we deal with this by null-checking theirContext
            } catch (java.lang.SecurityException e) {
                // unexpected, but we deal with this by null-checking theirContext
            }
        }
        return theirContext;
    
public android.content.ComponentNamegetSearchActivity()
Gets the component name of the searchable activity.

return
A component name, never {@code null}.

        return mSearchActivity;
    
public intgetSearchButtonText()
Gets the resource id of replacement text for the "Search" button.

return
A resource id, or {@code 0} if no replacement text was specified.
see
android.R.styleable#Searchable_searchButtonText
hide
This feature is deprecated, no need to add it to the API.

        return mSearchButtonText;
    
public intgetSettingsDescriptionId()
Gets the resource id of the description string to use for this source in system search settings, or {@code 0} if none has been specified.

see
android.R.styleable#Searchable_searchSettingsDescription

        return mSettingsDescriptionId;
    
public java.lang.StringgetSuggestAuthority()
Gets the search suggestion content provider authority.

return
The search suggestions authority, or {@code null} if not set.
see
android.R.styleable#Searchable_searchSuggestAuthority

           // voiceMaxResults

                             
       
        return mSuggestAuthority;
    
public java.lang.StringgetSuggestIntentAction()
Gets the optional intent action for use with these suggestions. This is useful if all intents will have the same action (e.g. {@link android.content.Intent#ACTION_VIEW}) This can be overriden in any given suggestion using the column {@link SearchManager#SUGGEST_COLUMN_INTENT_ACTION}.

return
The default intent action, or {@code null} if not set.
see
android.R.styleable#Searchable_searchSuggestIntentAction

        return mSuggestIntentAction;
    
public java.lang.StringgetSuggestIntentData()
Gets the optional intent data for use with these suggestions. This is useful if all intents will have similar data URIs, but you'll likely need to provide a specific ID as well via the column {@link SearchManager#SUGGEST_COLUMN_INTENT_DATA_ID}, which will be appended to the intent data URI. This can be overriden in any given suggestion using the column {@link SearchManager#SUGGEST_COLUMN_INTENT_DATA}.

return
The default intent data, or {@code null} if not set.
see
android.R.styleable#Searchable_searchSuggestIntentData

        return mSuggestIntentData;
    
public java.lang.StringgetSuggestPackage()
Gets the name of the package where the suggestion provider lives, or {@code null}.

        return mSuggestProviderPackage;
    
public java.lang.StringgetSuggestPath()
Gets the content provider path for obtaining search suggestions.

return
The suggestion path, or {@code null} if not set.
see
android.R.styleable#Searchable_searchSuggestPath

        return mSuggestPath;
    
public java.lang.StringgetSuggestSelection()
Gets the selection for obtaining search suggestions.

see
android.R.styleable#Searchable_searchSuggestSelection

        return mSuggestSelection;
    
public intgetSuggestThreshold()
Gets the suggestion threshold.

return
The suggestion threshold, or {@code 0} if not set.
see
android.R.styleable#Searchable_searchSuggestThreshold

        return mSuggestThreshold;
    
public intgetVoiceLanguageId()
Gets the resource id of the spoken language to recognize in voice search.

return
A resource id, or {@code 0} if no language was specified.
see
android.R.styleable#Searchable_voiceLanguage

        return mVoiceLanguageId;
    
public intgetVoiceLanguageModeId()
Gets the resource id of the voice search language model string.

return
A resource id, or {@code 0} if no language model was specified.
see
android.R.styleable#Searchable_voiceLanguageModel

        return mVoiceLanguageModeId;
    
public intgetVoiceMaxResults()
The maximum number of voice recognition results to return.

return
the max results count, if specified in the searchable activity's metadata, or {@code 0} if not specified.
see
android.R.styleable#Searchable_voiceMaxResults

        return mVoiceMaxResults;
    
public intgetVoicePromptTextId()
Gets the resource id of the voice prompt text string.

return
A resource id, or {@code 0} if no voice prompt text was specified.
see
android.R.styleable#Searchable_voicePromptText

        return mVoicePromptTextId;
    
public booleangetVoiceSearchEnabled()
Checks if the searchable activity wants the voice search button to be shown.

see
android.R.styleable#Searchable_voiceSearchMode

        return 0 != (mVoiceSearchMode & VOICE_SEARCH_SHOW_BUTTON);
    
public booleangetVoiceSearchLaunchRecognizer()
Checks if voice search should start in-app search.

see
android.R.styleable#Searchable_voiceSearchMode

        return 0 != (mVoiceSearchMode & VOICE_SEARCH_LAUNCH_RECOGNIZER);
    
public booleangetVoiceSearchLaunchWebSearch()
Checks if voice search should start web search.

see
android.R.styleable#Searchable_voiceSearchMode

        return 0 != (mVoiceSearchMode & VOICE_SEARCH_LAUNCH_WEB_SEARCH);
    
public booleanqueryAfterZeroResults()
Checks whether this searchable activity should be queried for suggestions if a prefix of the query has returned no results.

see
android.R.styleable#Searchable_queryAfterZeroResults

        return mQueryAfterZeroResults;
    
public booleanshouldIncludeInGlobalSearch()
Checks whether the searchable should be included in global search.

return
The value of the {@link android.R.styleable#Searchable_includeInGlobalSearch} attribute, or {@code false} if the attribute is not set.
see
android.R.styleable#Searchable_includeInGlobalSearch

        return mIncludeInGlobalSearch;
    
public booleanshouldRewriteQueryFromData()
Checks whether the text in the query field should come from the suggestion intent data.

see
android.R.styleable#Searchable_searchMode

        return 0 != (mSearchMode & SEARCH_MODE_QUERY_REWRITE_FROM_DATA);
    
public booleanshouldRewriteQueryFromText()
Checks whether the text in the query field should come from the suggestion title.

see
android.R.styleable#Searchable_searchMode

        return 0 != (mSearchMode & SEARCH_MODE_QUERY_REWRITE_FROM_TEXT);
    
public booleanuseBadgeIcon()
Checks whether the badge should be an icon.

see
android.R.styleable#Searchable_searchMode
hide
This feature is deprecated, no need to add it to the API.

        return (0 != (mSearchMode & SEARCH_MODE_BADGE_ICON)) && (mIconId != 0);
    
public booleanuseBadgeLabel()
Checks whether the badge should be a text label.

see
android.R.styleable#Searchable_searchMode
hide
This feature is deprecated, no need to add it to the API.

        return 0 != (mSearchMode & SEARCH_MODE_BADGE_LABEL);
    
public voidwriteToParcel(android.os.Parcel dest, int flags)

        dest.writeInt(mLabelId);
        mSearchActivity.writeToParcel(dest, flags);
        dest.writeInt(mHintId);
        dest.writeInt(mSearchMode);
        dest.writeInt(mIconId);
        dest.writeInt(mSearchButtonText);
        dest.writeInt(mSearchInputType);
        dest.writeInt(mSearchImeOptions);
        dest.writeInt(mIncludeInGlobalSearch ? 1 : 0);
        dest.writeInt(mQueryAfterZeroResults ? 1 : 0);
        dest.writeInt(mAutoUrlDetect ? 1 : 0);
        
        dest.writeInt(mSettingsDescriptionId);
        dest.writeString(mSuggestAuthority);
        dest.writeString(mSuggestPath);
        dest.writeString(mSuggestSelection);
        dest.writeString(mSuggestIntentAction);
        dest.writeString(mSuggestIntentData);
        dest.writeInt(mSuggestThreshold);

        if (mActionKeys == null) {
            dest.writeInt(0);
        } else {
            dest.writeInt(mActionKeys.size());
            for (ActionKeyInfo actionKey : mActionKeys.values()) {
                actionKey.writeToParcel(dest, flags);
            }
        }

        dest.writeString(mSuggestProviderPackage);

        dest.writeInt(mVoiceSearchMode);
        dest.writeInt(mVoiceLanguageModeId);
        dest.writeInt(mVoicePromptTextId);
        dest.writeInt(mVoiceLanguageId);
        dest.writeInt(mVoiceMaxResults);