Java Code Examples for android.content.pm.activityinfo#loadXmlMetaData()

The following examples show how to use android.content.pm.activityinfo#loadXmlMetaData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchableInfo.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 */
public static SearchableInfo getActivityMetaData(Context context, ActivityInfo activityInfo,
        int userId) {
    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;
}
 
Example 2
Source File: DevicePluginXmlUtil.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * コンポーネントのActivityInfoを取得する.
 *
 * @param context コンテキスト
 * @param packageName package name
 * @return コンポーネントのActivityInfo
 */
private static ActivityInfo getReceiverInfo(final Context context, final String packageName) {
    try {
        PackageManager pkgMgr = context.getPackageManager();
        PackageInfo pkg = pkgMgr.getPackageInfo(packageName, PackageManager.GET_RECEIVERS);
        if (pkg != null) {
            ActivityInfo[] receivers = pkg.receivers;
            if (receivers != null) {
                for (int i = 0; i < receivers.length; i++) {
                    String pkgName = receivers[i].packageName;
                    String className = receivers[i].name;
                    ComponentName component = new ComponentName(pkgName, className);
                    ActivityInfo receiverInfo = pkgMgr.getReceiverInfo(component, PackageManager.GET_META_DATA);
                    if (receiverInfo.metaData != null) {
                        Object xmlData = receiverInfo.metaData.get(PLUGIN_META_DATA);
                        if (xmlData instanceof Integer) {
                            XmlResourceParser xrp = receiverInfo.loadXmlMetaData(pkgMgr, PLUGIN_META_DATA);
                            if (xrp != null) {
                                return receiverInfo;
                            }
                        }
                    }
                }
            }
        }
        return null;
    } catch (NameNotFoundException e) {
        return null;
    }
}
 
Example 3
Source File: ShortcutService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Nullable
XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
    return activityInfo.loadXmlMetaData(mContext.getPackageManager(), key);
}
 
Example 4
Source File: PreferenceManager.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Inflates a preference hierarchy from the preference hierarchies of
 * {@link Activity Activities} that match the given {@link Intent}. An
 * {@link Activity} defines its preference hierarchy with meta-data using
 * the {@link #METADATA_KEY_PREFERENCES} key.
 * <p>
 * If a preference hierarchy is given, the new preference hierarchies will
 * be merged in.
 *
 * @param queryIntent The intent to match activities.
 * @param rootPreferences Optional existing hierarchy to merge the new
 *            hierarchies into.
 * @return The root hierarchy (if one was not provided, the new hierarchy's
 *         root).
 */
PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences) {
    final List<ResolveInfo> activities = queryIntentActivities(queryIntent);
    final HashSet<String> inflatedRes = new HashSet<String>();

    for (int i = activities.size() - 1; i >= 0; i--) {
        final ActivityInfo activityInfo = activities.get(i).activityInfo;
        final Bundle metaData = activityInfo.metaData;

        if ((metaData == null) || !metaData.containsKey(METADATA_KEY_PREFERENCES)) {
            continue;
        }

        // Need to concat the package with res ID since the same res ID
        // can be re-used across contexts
        final String uniqueResId = activityInfo.packageName + ":"
                + activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES);

        if (!inflatedRes.contains(uniqueResId)) {
            inflatedRes.add(uniqueResId);

            final Context context;
            try {
                context = mContext.createPackageContext(activityInfo.packageName, 0);
            } catch (NameNotFoundException e) {
                Log.w(TAG, "Could not create context for " + activityInfo.packageName + ": "
                        + Log.getStackTraceString(e));
                continue;
            }

            final PreferenceInflater inflater = new PreferenceInflater(context, this);
            final XmlResourceParser parser = activityInfo.loadXmlMetaData(context
                    .getPackageManager(), METADATA_KEY_PREFERENCES);
            rootPreferences = (PreferenceScreen) inflater
                    .inflate(parser, rootPreferences, true);
            parser.close();
        }
    }

    rootPreferences.onAttachedToHierarchy(this);

    return rootPreferences;
}
 
Example 5
Source File: PreferenceManager.java    From ticdesign with Apache License 2.0 4 votes vote down vote up
/**
 * Inflates a preference hierarchy from the preference hierarchies of
 * {@link Activity Activities} that match the given {@link Intent}. An
 * {@link Activity} defines its preference hierarchy with meta-data using
 * the {@link #METADATA_KEY_PREFERENCES} key.
 * <p>
 * If a preference hierarchy is given, the new preference hierarchies will
 * be merged in.
 *
 * @param queryIntent The intent to match activities.
 * @param rootPreferences Optional existing hierarchy to merge the new
 *            hierarchies into.
 * @return The root hierarchy (if one was not provided, the new hierarchy's
 *         root).
 */
PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences) {
    final List<ResolveInfo> activities = queryIntentActivities(queryIntent);
    final HashSet<String> inflatedRes = new HashSet<String>();

    for (int i = activities.size() - 1; i >= 0; i--) {
        final ActivityInfo activityInfo = activities.get(i).activityInfo;
        final Bundle metaData = activityInfo.metaData;

        if ((metaData == null) || !metaData.containsKey(METADATA_KEY_PREFERENCES)) {
            continue;
        }

        // Need to concat the package with res ID since the same res ID
        // can be re-used across contexts
        final String uniqueResId = activityInfo.packageName + ":"
                + activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES);

        if (!inflatedRes.contains(uniqueResId)) {
            inflatedRes.add(uniqueResId);

            final Context context;
            try {
                context = mContext.createPackageContext(activityInfo.packageName, 0);
            } catch (NameNotFoundException e) {
                Log.w(TAG, "Could not create context for " + activityInfo.packageName + ": "
                    + Log.getStackTraceString(e));
                continue;
            }

            final PreferenceInflater inflater = new PreferenceInflater(context, this);
            final XmlResourceParser parser = activityInfo.loadXmlMetaData(context
                    .getPackageManager(), METADATA_KEY_PREFERENCES);
            rootPreferences = (PreferenceScreen) inflater
                    .inflate(parser, rootPreferences, true);
            parser.close();
        }
    }

    rootPreferences.onAttachedToHierarchy(this);

    return rootPreferences;
}
 
Example 6
Source File: PreferenceManager.java    From droidel with Apache License 2.0 4 votes vote down vote up
/**
   * Inflates a preference hierarchy from the preference hierarchies of
   * {@link Activity Activities} that match the given {@link Intent}. An
   * {@link Activity} defines its preference hierarchy with meta-data using
   * the {@link #METADATA_KEY_PREFERENCES} key.
   * <p>
   * If a preference hierarchy is given, the new preference hierarchies will
   * be merged in.
   * 
   * @param queryIntent The intent to match activities.
   * @param rootPreferences Optional existing hierarchy to merge the new
   *            hierarchies into.
   * @return The root hierarchy (if one was not provided, the new hierarchy's
   *         root).
   */
  PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences) {
      final List<ResolveInfo> activities = queryIntentActivities(queryIntent);
      final HashSet<String> inflatedRes = new HashSet<String>();

      for (int i = activities.size() - 1; i >= 0; i--) {
          final ActivityInfo activityInfo = activities.get(i).activityInfo;
          final Bundle metaData = activityInfo.metaData;

          if ((metaData == null) || !metaData.containsKey(METADATA_KEY_PREFERENCES)) {
              continue;
          }

          // Need to concat the package with res ID since the same res ID
          // can be re-used across contexts
          final String uniqueResId = activityInfo.packageName + ":"
                  + activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES);
          
          if (!inflatedRes.contains(uniqueResId)) {
              inflatedRes.add(uniqueResId);

              final Context context;
              try {
                  context = mContext.createPackageContext(activityInfo.packageName, 0);
              } catch (NameNotFoundException e) {
                  Log.w(TAG, "Could not create context for " + activityInfo.packageName + ": "
                      + Log.getStackTraceString(e));
                  continue;
              }
              
              final PreferenceInflater inflater = new PreferenceInflater(context, this);
              final XmlResourceParser parser = activityInfo.loadXmlMetaData(context
                      .getPackageManager(), METADATA_KEY_PREFERENCES);
              //rootPreferences = (PreferenceScreen) inflater
//.inflate(parser, rootPreferences, true);
// TODO: could call out to stubs that returned actual
// values parsed from the XML if we really wanted to here
rootPreferences = new PreferenceScreen(context, null);
              //parser.close();
          }
      }

      rootPreferences.onAttachedToHierarchy(this);
      
      return rootPreferences;
  }
 
Example 7
Source File: PreferenceManager.java    From PreferenceFragment with Apache License 2.0 4 votes vote down vote up
/**
 * Inflates a preference hierarchy from the preference hierarchies of
 * {@link Activity Activities} that match the given {@link Intent}. An
 * {@link Activity} defines its preference hierarchy with meta-data using
 * the {@link #METADATA_KEY_PREFERENCES} key.
 * <p>
 * If a preference hierarchy is given, the new preference hierarchies will
 * be merged in.
 * 
 * @param queryIntent The intent to match activities.
 * @param rootPreferences Optional existing hierarchy to merge the new
 *            hierarchies into.
 * @return The root hierarchy (if one was not provided, the new hierarchy's
 *         root).
 */
PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences) {
    final List<ResolveInfo> activities = queryIntentActivities(queryIntent);
    final HashSet<String> inflatedRes = new HashSet<String>();

    for (int i = activities.size() - 1; i >= 0; i--) {
        final ActivityInfo activityInfo = activities.get(i).activityInfo;
        final Bundle metaData = activityInfo.metaData;

        if ((metaData == null) || !metaData.containsKey(METADATA_KEY_PREFERENCES)) {
            continue;
        }

        // Need to concat the package with res ID since the same res ID
        // can be re-used across contexts
        final String uniqueResId = activityInfo.packageName + ":"
                + activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES);
        
        if (!inflatedRes.contains(uniqueResId)) {
            inflatedRes.add(uniqueResId);

            final Context context;
            try {
                context = mContext.createPackageContext(activityInfo.packageName, 0);
            } catch (NameNotFoundException e) {
                Log.w(TAG, "Could not create context for " + activityInfo.packageName + ": "
                    + Log.getStackTraceString(e));
                continue;
            }
            
            final PreferenceInflater inflater = new PreferenceInflater(context, this);
            final XmlResourceParser parser = activityInfo.loadXmlMetaData(context
                    .getPackageManager(), METADATA_KEY_PREFERENCES);
            rootPreferences = (PreferenceScreen) inflater
                    .inflate(parser, rootPreferences, true);
            parser.close();
        }
    }

    rootPreferences.onAttachedToHierarchy(this);
    
    return rootPreferences;
}