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

The following examples show how to use android.content.pm.ServiceInfo#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: XmlModel.java    From opentasks with Apache License 2.0 6 votes vote down vote up
private XmlResourceParser getParser()
{
    try
    {
        PackageInfo info = mPackageManager.getPackageInfo(mPackageName, PackageManager.GET_SERVICES | PackageManager.GET_META_DATA);
        ServiceInfo[] sinfo = info.services;

        XmlResourceParser parser;
        for (ServiceInfo i : sinfo)
        {
            parser = i.loadXmlMetaData(mPackageManager, METADATA_TASKS);
            if (parser != null)
            {
                return parser;
            }
        }
    }
    catch (NameNotFoundException e)
    {
    }
    return null;
}
 
Example 2
Source File: RecognitionServiceManager.java    From speechutils with Apache License 2.0 5 votes vote down vote up
public static String getSettingsActivity(Context context, ServiceInfo si)
        throws XmlPullParserException, IOException {
    PackageManager pm = context.getPackageManager();
    XmlResourceParser parser = null;
    try {
        parser = si.loadXmlMetaData(pm, RecognitionService.SERVICE_META_DATA);
        if (parser == null) {
            throw new XmlPullParserException("No " + RecognitionService.SERVICE_META_DATA + " meta-data");
        }

        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                && type != XmlPullParser.START_TAG) {
        }

        String nodeName = parser.getName();
        if (!"recognition-service".equals(nodeName)) {
            throw new XmlPullParserException(
                    "Meta-data does not start with recognition-service tag");
        }

        return parser.getAttributeValue("http://schemas.android.com/apk/res/android",
                "settingsActivity");
    } finally {
        if (parser != null) parser.close();
    }
}
 
Example 3
Source File: DevicePluginXmlUtil.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 指定されたパッケージ名からサービスのコンポーネント情報を取得します.
 *
 * @param context コンテキスト
 * @param packageName パッケージ名
 * @return コンポーネント情報
 */
private static ServiceInfo getServiceInfo(final Context context, final String packageName) {
    try {
        PackageManager pkgMgr = context.getPackageManager();
        PackageInfo pkg = pkgMgr.getPackageInfo(packageName, PackageManager.GET_SERVICES);
        if (pkg != null) {
            ServiceInfo[] services = pkg.services;
            if (services != null) {
                for (int i = 0; i < services.length; i++) {
                    String pkgName = services[i].packageName;
                    String className = services[i].name;
                    ComponentName component = new ComponentName(pkgName, className);
                    ServiceInfo serviceInfo = pkgMgr.getServiceInfo(component, PackageManager.GET_META_DATA);
                    if (serviceInfo.metaData != null) {
                        Object xmlData = serviceInfo.metaData.get(PLUGIN_META_DATA);
                        if (xmlData instanceof Integer) {
                            XmlResourceParser xrp = serviceInfo.loadXmlMetaData(pkgMgr, PLUGIN_META_DATA);
                            if (xrp != null) {
                                return serviceInfo;
                            }
                        }
                    }
                }
            }
        }
        return null;
    } catch (NameNotFoundException e) {
        return null;
    }
}
 
Example 4
Source File: SpellCheckerInfo.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 * @hide
 */
public SpellCheckerInfo(Context context, ResolveInfo service)
        throws XmlPullParserException, IOException {
    mService = service;
    ServiceInfo si = service.serviceInfo;
    mId = new ComponentName(si.packageName, si.name).flattenToShortString();

    final PackageManager pm = context.getPackageManager();
    int label = 0;
    String settingsActivityComponent = null;

    XmlResourceParser parser = null;
    try {
        parser = si.loadXmlMetaData(pm, SpellCheckerSession.SERVICE_META_DATA);
        if (parser == null) {
            throw new XmlPullParserException("No "
                    + SpellCheckerSession.SERVICE_META_DATA + " meta-data");
        }

        final Resources res = pm.getResourcesForApplication(si.applicationInfo);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
                && type != XmlPullParser.START_TAG) {
        }

        final String nodeName = parser.getName();
        if (!"spell-checker".equals(nodeName)) {
            throw new XmlPullParserException(
                    "Meta-data does not start with spell-checker tag");
        }

        TypedArray sa = res.obtainAttributes(attrs,
                com.android.internal.R.styleable.SpellChecker);
        label = sa.getResourceId(com.android.internal.R.styleable.SpellChecker_label, 0);
        settingsActivityComponent = sa.getString(
                com.android.internal.R.styleable.SpellChecker_settingsActivity);
        sa.recycle();

        final int depth = parser.getDepth();
        // Parse all subtypes
        while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
                && type != XmlPullParser.END_DOCUMENT) {
            if (type == XmlPullParser.START_TAG) {
                final String subtypeNodeName = parser.getName();
                if (!"subtype".equals(subtypeNodeName)) {
                    throw new XmlPullParserException(
                            "Meta-data in spell-checker does not start with subtype tag");
                }
                final TypedArray a = res.obtainAttributes(
                        attrs, com.android.internal.R.styleable.SpellChecker_Subtype);
                SpellCheckerSubtype subtype = new SpellCheckerSubtype(
                        a.getResourceId(com.android.internal.R.styleable
                                .SpellChecker_Subtype_label, 0),
                        a.getString(com.android.internal.R.styleable
                                .SpellChecker_Subtype_subtypeLocale),
                        a.getString(com.android.internal.R.styleable
                                .SpellChecker_Subtype_languageTag),
                        a.getString(com.android.internal.R.styleable
                                .SpellChecker_Subtype_subtypeExtraValue),
                        a.getInt(com.android.internal.R.styleable
                                .SpellChecker_Subtype_subtypeId, 0));
                mSubtypes.add(subtype);
            }
        }
    } catch (Exception e) {
        Slog.e(TAG, "Caught exception: " + e);
        throw new XmlPullParserException(
                "Unable to create context for: " + si.packageName);
    } finally {
        if (parser != null) parser.close();
    }
    mLabel = label;
    mSettingsActivityName = settingsActivityComponent;
}
 
Example 5
Source File: WallpaperInfo.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 * 
 * @param context The Context in which we are parsing the wallpaper.
 * @param service The ResolveInfo returned from the package manager about
 * this wallpaper's component.
 */
public WallpaperInfo(Context context, ResolveInfo service)
        throws XmlPullParserException, IOException {
    mService = service;
    ServiceInfo si = service.serviceInfo;
    
    final PackageManager pm = context.getPackageManager();
    XmlResourceParser parser = null;
    try {
        parser = si.loadXmlMetaData(pm, WallpaperService.SERVICE_META_DATA);
        if (parser == null) {
            throw new XmlPullParserException("No "
                    + WallpaperService.SERVICE_META_DATA + " meta-data");
        }
    
        Resources res = pm.getResourcesForApplication(si.applicationInfo);
        
        AttributeSet attrs = Xml.asAttributeSet(parser);
        
        int type;
        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
                && type != XmlPullParser.START_TAG) {
        }
        
        String nodeName = parser.getName();
        if (!"wallpaper".equals(nodeName)) {
            throw new XmlPullParserException(
                    "Meta-data does not start with wallpaper tag");
        }
        
        TypedArray sa = res.obtainAttributes(attrs,
                com.android.internal.R.styleable.Wallpaper);
        mSettingsActivityName = sa.getString(
                com.android.internal.R.styleable.Wallpaper_settingsActivity);

        mThumbnailResource = sa.getResourceId(
                com.android.internal.R.styleable.Wallpaper_thumbnail,
                -1);
        mAuthorResource = sa.getResourceId(
                com.android.internal.R.styleable.Wallpaper_author,
                -1);
        mDescriptionResource = sa.getResourceId(
                com.android.internal.R.styleable.Wallpaper_description,
                -1);
        mContextUriResource = sa.getResourceId(
                com.android.internal.R.styleable.Wallpaper_contextUri,
                -1);
        mContextDescriptionResource = sa.getResourceId(
                com.android.internal.R.styleable.Wallpaper_contextDescription,
                -1);
        mShowMetadataInPreview = sa.getBoolean(
                com.android.internal.R.styleable.Wallpaper_showMetadataInPreview,
                false);
        mSupportsAmbientMode = sa.getBoolean(
                com.android.internal.R.styleable.Wallpaper_supportsAmbientMode,
                false);

        sa.recycle();
    } catch (NameNotFoundException e) {
        throw new XmlPullParserException(
                "Unable to create context for: " + si.packageName);
    } finally {
        if (parser != null) parser.close();
    }
}