Search in sources :

Example 26 with XmlResourceParser

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

the class PowerProfile method readPowerValuesFromXml.

private void readPowerValuesFromXml(Context context) {
    int id = com.android.internal.R.xml.power_profile;
    final Resources resources = context.getResources();
    XmlResourceParser parser = resources.getXml(id);
    boolean parsingArray = false;
    ArrayList<Double> array = new ArrayList<Double>();
    String arrayName = null;
    try {
        XmlUtils.beginDocument(parser, TAG_DEVICE);
        while (true) {
            XmlUtils.nextElement(parser);
            String element = parser.getName();
            if (element == null)
                break;
            if (parsingArray && !element.equals(TAG_ARRAYITEM)) {
                // Finish array
                sPowerMap.put(arrayName, array.toArray(new Double[array.size()]));
                parsingArray = false;
            }
            if (element.equals(TAG_ARRAY)) {
                parsingArray = true;
                array.clear();
                arrayName = parser.getAttributeValue(null, ATTR_NAME);
            } else if (element.equals(TAG_ITEM) || element.equals(TAG_ARRAYITEM)) {
                String name = null;
                if (!parsingArray)
                    name = parser.getAttributeValue(null, ATTR_NAME);
                if (parser.next() == XmlPullParser.TEXT) {
                    String power = parser.getText();
                    double value = 0;
                    try {
                        value = Double.valueOf(power);
                    } catch (NumberFormatException nfe) {
                    }
                    if (element.equals(TAG_ITEM)) {
                        sPowerMap.put(name, value);
                    } else if (parsingArray) {
                        array.add(value);
                    }
                }
            }
        }
        if (parsingArray) {
            sPowerMap.put(arrayName, array.toArray(new Double[array.size()]));
        }
    } catch (XmlPullParserException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        parser.close();
    }
    // Now collect other config variables.
    int[] configResIds = new int[] { com.android.internal.R.integer.config_bluetooth_idle_cur_ma, com.android.internal.R.integer.config_bluetooth_rx_cur_ma, com.android.internal.R.integer.config_bluetooth_tx_cur_ma, com.android.internal.R.integer.config_bluetooth_operating_voltage_mv, com.android.internal.R.integer.config_wifi_idle_receive_cur_ma, com.android.internal.R.integer.config_wifi_active_rx_cur_ma, com.android.internal.R.integer.config_wifi_tx_cur_ma, com.android.internal.R.integer.config_wifi_operating_voltage_mv };
    String[] configResIdKeys = new String[] { POWER_BLUETOOTH_CONTROLLER_IDLE, POWER_BLUETOOTH_CONTROLLER_RX, POWER_BLUETOOTH_CONTROLLER_TX, POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE, POWER_WIFI_CONTROLLER_IDLE, POWER_WIFI_CONTROLLER_RX, POWER_WIFI_CONTROLLER_TX, POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE };
    for (int i = 0; i < configResIds.length; i++) {
        String key = configResIdKeys[i];
        // value in config.xml
        if ((sPowerMap.containsKey(key) && (Double) sPowerMap.get(key) > 0)) {
            continue;
        }
        int value = resources.getInteger(configResIds[i]);
        if (value > 0) {
            sPowerMap.put(key, (double) value);
        }
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) ArrayList(java.util.ArrayList) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources)

Example 27 with XmlResourceParser

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

the class MetaDataTest method checkMetaData.

private void checkMetaData(ComponentName cn, PackageItemInfo ci) throws IOException, XmlPullParserException {
    assertNotNull("Unable to find component " + cn, ci);
    Bundle md = ci.metaData;
    assertNotNull("No meta data found", md);
    assertEquals("foo", md.getString("com.android.frameworks.coretests.string"));
    assertTrue(md.getBoolean("com.android.frameworks.coretests.boolean"));
    assertEquals(100, md.getInt("com.android.frameworks.coretests.integer"));
    assertEquals(0xff000000, md.getInt("com.android.frameworks.coretests.color"));
    assertEquals((double) 1001, Math.floor(md.getFloat("com.android.frameworks.coretests.float") * 10 + .5));
    assertEquals(R.xml.metadata, md.getInt("com.android.frameworks.coretests.reference"));
    XmlResourceParser xml = ci.loadXmlMetaData(mContext.getPackageManager(), "com.android.frameworks.coretests.reference");
    assertNotNull(xml);
    int type;
    while ((type = xml.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
    }
    assertEquals(XmlPullParser.START_TAG, type);
    assertEquals("thedata", xml.getName());
    // method 1: direct access
    final String rawAttr = xml.getAttributeValue(null, "rawText");
    assertEquals("some raw text", rawAttr);
    // method 2: direct access of typed value
    final int rawColorIntAttr = xml.getAttributeIntValue(null, "rawColor", 0);
    assertEquals(0xffffff00, rawColorIntAttr);
    final String rawColorStrAttr = xml.getAttributeValue(null, "rawColor");
    assertEquals("#ffffff00", rawColorStrAttr);
    // method 2: direct access of resource attribute
    final String nameSpace = "http://schemas.android.com/apk/res/android";
    final int colorIntAttr = xml.getAttributeIntValue(nameSpace, "color", 0);
    assertEquals(0xffff0000, colorIntAttr);
    final String colorStrAttr = xml.getAttributeValue(nameSpace, "color");
    assertEquals("#ffff0000", colorStrAttr);
    // method 3: styled access (borrowing an attr from view system here)
    TypedArray a = mContext.obtainStyledAttributes(xml, android.R.styleable.TextView);
    String styledAttr = a.getString(android.R.styleable.TextView_text);
    assertEquals("text", styledAttr);
    a.recycle();
    xml.close();
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) Bundle(android.os.Bundle) TypedArray(android.content.res.TypedArray)

Example 28 with XmlResourceParser

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

the class AudioService method loadTouchSoundAssets.

private void loadTouchSoundAssets() {
    XmlResourceParser parser = null;
    // only load assets once.
    if (!SOUND_EFFECT_FILES.isEmpty()) {
        return;
    }
    loadTouchSoundAssetDefaults();
    try {
        parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets);
        XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS);
        String version = parser.getAttributeValue(null, ATTR_VERSION);
        boolean inTouchSoundsGroup = false;
        if (ASSET_FILE_VERSION.equals(version)) {
            while (true) {
                XmlUtils.nextElement(parser);
                String element = parser.getName();
                if (element == null) {
                    break;
                }
                if (element.equals(TAG_GROUP)) {
                    String name = parser.getAttributeValue(null, ATTR_GROUP_NAME);
                    if (GROUP_TOUCH_SOUNDS.equals(name)) {
                        inTouchSoundsGroup = true;
                        break;
                    }
                }
            }
            while (inTouchSoundsGroup) {
                XmlUtils.nextElement(parser);
                String element = parser.getName();
                if (element == null) {
                    break;
                }
                if (element.equals(TAG_ASSET)) {
                    String id = parser.getAttributeValue(null, ATTR_ASSET_ID);
                    String file = parser.getAttributeValue(null, ATTR_ASSET_FILE);
                    int fx;
                    try {
                        Field field = AudioManager.class.getField(id);
                        fx = field.getInt(null);
                    } catch (Exception e) {
                        Log.w(TAG, "Invalid touch sound ID: " + id);
                        continue;
                    }
                    int i = SOUND_EFFECT_FILES.indexOf(file);
                    if (i == -1) {
                        i = SOUND_EFFECT_FILES.size();
                        SOUND_EFFECT_FILES.add(file);
                    }
                    SOUND_EFFECT_FILES_MAP[fx][0] = i;
                } else {
                    break;
                }
            }
        }
    } catch (Resources.NotFoundException e) {
        Log.w(TAG, "audio assets file not found", e);
    } catch (XmlPullParserException e) {
        Log.w(TAG, "XML parser exception reading touch sound assets", e);
    } catch (IOException e) {
        Log.w(TAG, "I/O exception reading touch sound assets", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
Also used : Field(java.lang.reflect.Field) XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) IOException(java.io.IOException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) AndroidRuntimeException(android.util.AndroidRuntimeException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ActivityNotFoundException(android.content.ActivityNotFoundException) NoSuchElementException(java.util.NoSuchElementException)

Example 29 with XmlResourceParser

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

the class VendorConfig method readVendorConfigs.

/**
     * Read the vendor configuration file.
     *
     * @param context The content issuing the read
     *
     * @return An map pointing from vendor name to config
     *
     * @throws IOException
     * @throws XmlPullParserException
     */
@NonNull
private static ArrayMap<String, VendorConfig> readVendorConfigs(@NonNull final Context context) throws IOException, XmlPullParserException {
    try (XmlResourceParser parser = context.getResources().getXml(R.xml.vendorconfigs)) {
        // Skip header
        int parsingEvent;
        do {
            parsingEvent = parser.next();
        } while (parsingEvent != XmlResourceParser.START_TAG);
        ArrayList<VendorConfig> configs = readTagList(parser, VENDORS_TAG, VENDOR_TAG, new TagReader<VendorConfig>() {

            public VendorConfig readTag(XmlPullParser parser, String tagName) throws XmlPullParserException, IOException {
                return readVendorConfig(context, parser, tagName);
            }
        });
        ArrayMap<String, VendorConfig> configMap = new ArrayMap<>(configs.size());
        final int numConfigs = configs.size();
        for (int i = 0; i < numConfigs; i++) {
            VendorConfig config = configs.get(i);
            configMap.put(config.name, config);
        }
        return configMap;
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) ArrayMap(android.util.ArrayMap) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) NonNull(android.annotation.NonNull)

Example 30 with XmlResourceParser

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

the class PackageParser method parseSplitApk.

private void parseSplitApk(Package pkg, int splitIndex, AssetManager assets, int flags) throws PackageParserException {
    final String apkPath = pkg.splitCodePaths[splitIndex];
    mParseError = PackageManager.INSTALL_SUCCEEDED;
    mArchiveSourcePath = apkPath;
    if (DEBUG_JAR)
        Slog.d(TAG, "Scanning split APK: " + apkPath);
    final int cookie = loadApkIntoAssetManager(assets, apkPath, flags);
    Resources res = null;
    XmlResourceParser parser = null;
    try {
        res = new Resources(assets, mMetrics, null);
        assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Build.VERSION.RESOURCES_SDK_INT);
        parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
        final String[] outError = new String[1];
        pkg = parseSplitApk(pkg, res, parser, flags, splitIndex, outError);
        if (pkg == null) {
            throw new PackageParserException(mParseError, apkPath + " (at " + parser.getPositionDescription() + "): " + outError[0]);
        }
    } catch (PackageParserException e) {
        throw e;
    } catch (Exception e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION, "Failed to read manifest from " + apkPath, e);
    } finally {
        IoUtils.closeQuietly(parser);
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) Resources(android.content.res.Resources) GeneralSecurityException(java.security.GeneralSecurityException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) 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