Code example for Intent

Methods: setComponent, resolveTypeIfNeeded

0
     * Checks whether the global search provider with a given 
     * component name is installed on the system or not. This deals with 
     * cases such as the removal of an installed provider. 
     */ 
    private boolean isInstalled(ComponentName globalSearch) {
        Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
        intent.setComponent(globalSearch);
 
        List<ResolveInfo> activities = queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (activities != null && !activities.isEmpty()) {
            return true; 
        } 
 
        return false; 
    } 
 
    private static final Comparator<ResolveInfo> GLOBAL_SEARCH_RANKER =
            new Comparator<ResolveInfo>() { 
        @Override 
        public int compare(ResolveInfo lhs, ResolveInfo rhs) { 
            if (lhs == rhs) { 
                return 0; 
            } 
            boolean lhsSystem = isSystemApp(lhs); 
            boolean rhsSystem = isSystemApp(rhs); 
 
            if (lhsSystem && !rhsSystem) { 
                return -1; 
            } else if (rhsSystem && !lhsSystem) { 
                return 1; 
            } else { 
                // Either both system engines, or both non system 
                // engines. 
                // 
                // Note, this isn't a typo. Higher priority numbers imply 
                // higher priority, but are "lower" in the sort order. 
                return rhs.priority - lhs.priority; 
            } 
        } 
    }; 
 
    /** 
     * @return true iff. the resolve info corresponds to a system application. 
     */ 
    private static final boolean isSystemApp(ResolveInfo res) {
        return (res.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
    } 
 
    /** 
     * Returns the highest ranked search provider as per the 
     * ranking defined in {@link #getGlobalSearchActivities()}. 
     */ 
    private ComponentName getDefaultGlobalSearchProvider(List<ResolveInfo> providerList) {
        if (providerList != null && !providerList.isEmpty()) {
            ActivityInfo ai = providerList.get(0).activityInfo;
            return new ComponentName(ai.packageName, ai.name);
        } 
 
        Log.w(LOG_TAG, "No global search activity found");
        return null; 
    } 
 
    private String getGlobalSearchProviderSetting() {
        return Settings.Secure.getString(mContext.getContentResolver(),
                Settings.Secure.SEARCH_GLOBAL_SEARCH_ACTIVITY); 
    } 
 
    /** 
     * Finds the web search activity. 
     * 
     * Only looks in the package of the global search activity. 
     */ 
    private ComponentName findWebSearchActivity(ComponentName globalSearchActivity) {
        if (globalSearchActivity == null) {
            return null; 
        } 
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.setPackage(globalSearchActivity.getPackageName());
        List<ResolveInfo> activities =
                queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
 
        if (activities != null && !activities.isEmpty()) {
            ActivityInfo ai = activities.get(0).activityInfo;
            // TODO: do some sanity checks here? 
            return new ComponentName(ai.packageName, ai.name);
        } 
        Log.w(LOG_TAG, "No web search activity found");
        return null; 
    } 
 
    private List<ResolveInfo> queryIntentActivities(Intent intent, int flags) {
        List<ResolveInfo> activities = null;
        try { 
            activities =
                    mPm.queryIntentActivities(intent,
                    intent.resolveTypeIfNeeded(mContext.getContentResolver()),
                    flags, mUserId);
        } catch (RemoteException re) {
            // Local call 
        } 
        return activities;