Search in sources :

Example 31 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

the class SearchableInfo method getActivityMetaData.

/**
     * 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;
}
Also used : Context(android.content.Context) XmlResourceParser(android.content.res.XmlResourceParser) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName)

Example 32 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

the class RegisteredServicesCache method parseServiceInfo.

@VisibleForTesting
protected ServiceInfo<V> parseServiceInfo(ResolveInfo service) throws XmlPullParserException, IOException {
    android.content.pm.ServiceInfo si = service.serviceInfo;
    ComponentName componentName = new ComponentName(si.packageName, si.name);
    PackageManager pm = mContext.getPackageManager();
    XmlResourceParser parser = null;
    try {
        parser = si.loadXmlMetaData(pm, mMetaDataName);
        if (parser == null) {
            throw new XmlPullParserException("No " + mMetaDataName + " meta-data");
        }
        AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        }
        String nodeName = parser.getName();
        if (!mAttributesName.equals(nodeName)) {
            throw new XmlPullParserException("Meta-data does not start with " + mAttributesName + " tag");
        }
        V v = parseServiceAttributes(pm.getResourcesForApplication(si.applicationInfo), si.packageName, attrs);
        if (v == null) {
            return null;
        }
        final android.content.pm.ServiceInfo serviceInfo = service.serviceInfo;
        return new ServiceInfo<V>(v, serviceInfo, componentName);
    } catch (NameNotFoundException e) {
        throw new XmlPullParserException("Unable to load resources for pacakge " + si.packageName);
    } finally {
        if (parser != null)
            parser.close();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) AttributeSet(android.util.AttributeSet) ComponentName(android.content.ComponentName) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 33 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

the class MidiService method addPackageDeviceServer.

private void addPackageDeviceServer(ServiceInfo serviceInfo) {
    XmlResourceParser parser = null;
    try {
        parser = serviceInfo.loadXmlMetaData(mPackageManager, MidiDeviceService.SERVICE_INTERFACE);
        if (parser == null)
            return;
        // ignore virtual device servers that do not require the correct permission
        if (!android.Manifest.permission.BIND_MIDI_DEVICE_SERVICE.equals(serviceInfo.permission)) {
            Log.w(TAG, "Skipping MIDI device service " + serviceInfo.packageName + ": it does not require the permission " + android.Manifest.permission.BIND_MIDI_DEVICE_SERVICE);
            return;
        }
        Bundle properties = null;
        int numInputPorts = 0;
        int numOutputPorts = 0;
        boolean isPrivate = false;
        ArrayList<String> inputPortNames = new ArrayList<String>();
        ArrayList<String> outputPortNames = new ArrayList<String>();
        while (true) {
            int eventType = parser.next();
            if (eventType == XmlPullParser.END_DOCUMENT) {
                break;
            } else if (eventType == XmlPullParser.START_TAG) {
                String tagName = parser.getName();
                if ("device".equals(tagName)) {
                    if (properties != null) {
                        Log.w(TAG, "nested <device> elements in metadata for " + serviceInfo.packageName);
                        continue;
                    }
                    properties = new Bundle();
                    properties.putParcelable(MidiDeviceInfo.PROPERTY_SERVICE_INFO, serviceInfo);
                    numInputPorts = 0;
                    numOutputPorts = 0;
                    isPrivate = false;
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        String name = parser.getAttributeName(i);
                        String value = parser.getAttributeValue(i);
                        if ("private".equals(name)) {
                            isPrivate = "true".equals(value);
                        } else {
                            properties.putString(name, value);
                        }
                    }
                } else if ("input-port".equals(tagName)) {
                    if (properties == null) {
                        Log.w(TAG, "<input-port> outside of <device> in metadata for " + serviceInfo.packageName);
                        continue;
                    }
                    numInputPorts++;
                    String portName = null;
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        String name = parser.getAttributeName(i);
                        String value = parser.getAttributeValue(i);
                        if ("name".equals(name)) {
                            portName = value;
                            break;
                        }
                    }
                    inputPortNames.add(portName);
                } else if ("output-port".equals(tagName)) {
                    if (properties == null) {
                        Log.w(TAG, "<output-port> outside of <device> in metadata for " + serviceInfo.packageName);
                        continue;
                    }
                    numOutputPorts++;
                    String portName = null;
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        String name = parser.getAttributeName(i);
                        String value = parser.getAttributeValue(i);
                        if ("name".equals(name)) {
                            portName = value;
                            break;
                        }
                    }
                    outputPortNames.add(portName);
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                String tagName = parser.getName();
                if ("device".equals(tagName)) {
                    if (properties != null) {
                        if (numInputPorts == 0 && numOutputPorts == 0) {
                            Log.w(TAG, "<device> with no ports in metadata for " + serviceInfo.packageName);
                            continue;
                        }
                        int uid;
                        try {
                            ApplicationInfo appInfo = mPackageManager.getApplicationInfo(serviceInfo.packageName, 0);
                            uid = appInfo.uid;
                        } catch (PackageManager.NameNotFoundException e) {
                            Log.e(TAG, "could not fetch ApplicationInfo for " + serviceInfo.packageName);
                            continue;
                        }
                        synchronized (mDevicesByInfo) {
                            addDeviceLocked(MidiDeviceInfo.TYPE_VIRTUAL, numInputPorts, numOutputPorts, inputPortNames.toArray(EMPTY_STRING_ARRAY), outputPortNames.toArray(EMPTY_STRING_ARRAY), properties, null, serviceInfo, isPrivate, uid);
                        }
                        // setting properties to null signals that we are no longer
                        // processing a <device>
                        properties = null;
                        inputPortNames.clear();
                        outputPortNames.clear();
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.w(TAG, "Unable to load component info " + serviceInfo.toString(), e);
    } finally {
        if (parser != null)
            parser.close();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) PackageManager(android.content.pm.PackageManager) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) RemoteException(android.os.RemoteException)

Example 34 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

the class OverlayBaseTest method testAppXml.

public void testAppXml() throws Throwable {
    int expected = getExpected(0, 1, 2);
    int actual = -1;
    XmlResourceParser parser = mResources.getXml(R.xml.integer);
    int type = parser.getEventType();
    while (type != XmlResourceParser.END_DOCUMENT && actual == -1) {
        if (type == XmlResourceParser.START_TAG && "integer".equals(parser.getName())) {
            AttributeSet as = Xml.asAttributeSet(parser);
            actual = as.getAttributeIntValue(null, "value", -1);
        }
        type = parser.next();
    }
    parser.close();
    assertEquals(expected, actual);
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet)

Example 35 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

the class XmlConfigSource method ensureInitialized.

private void ensureInitialized() {
    synchronized (mLock) {
        if (mInitialized) {
            return;
        }
        try (XmlResourceParser parser = mContext.getResources().getXml(mResourceId)) {
            parseNetworkSecurityConfig(parser);
            mContext = null;
            mInitialized = true;
        } catch (Resources.NotFoundException | XmlPullParserException | IOException | ParserException e) {
            throw new RuntimeException("Failed to parse XML configuration from " + mContext.getResources().getResourceEntryName(mResourceId), e);
        }
    }
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

XmlResourceParser (android.content.res.XmlResourceParser)508 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)267 IOException (java.io.IOException)247 AttributeSet (android.util.AttributeSet)241 Resources (android.content.res.Resources)146 TypedArray (android.content.res.TypedArray)97 Test (org.junit.Test)82 PackageManager (android.content.pm.PackageManager)68 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)68 ArrayList (java.util.ArrayList)52 ComponentName (android.content.ComponentName)49 Bundle (android.os.Bundle)44 ActivityInfo (android.content.pm.ActivityInfo)43 AssetManager (android.content.res.AssetManager)32 RemoteException (android.os.RemoteException)31 TypedValue (android.util.TypedValue)29 ResolveInfo (android.content.pm.ResolveInfo)27 Intent (android.content.Intent)25 ApplicationInfo (android.content.pm.ApplicationInfo)25 Context (android.content.Context)22