Search in sources :

Example 61 with XmlResourceParser

use of android.content.res.XmlResourceParser in project XobotOS by xamarin.

the class PreferenceManager method inflateFromIntent.

/**
     * 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;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Context(android.content.Context) ActivityInfo(android.content.pm.ActivityInfo) XmlResourceParser(android.content.res.XmlResourceParser) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) Bundle(android.os.Bundle) HashSet(java.util.HashSet)

Example 62 with XmlResourceParser

use of android.content.res.XmlResourceParser in project XobotOS by xamarin.

the class PowerProfile method readPowerValuesFromXml.

private void readPowerValuesFromXml(Context context) {
    int id = com.android.internal.R.xml.power_profile;
    XmlResourceParser parser = context.getResources().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();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) ArrayList(java.util.ArrayList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 63 with XmlResourceParser

use of android.content.res.XmlResourceParser in project XobotOS by xamarin.

the class AutoText method init.

private void init(Resources r) {
    XmlResourceParser parser = r.getXml(com.android.internal.R.xml.autotext);
    StringBuilder right = new StringBuilder(RIGHT);
    mTrie = new char[DEFAULT];
    mTrie[TRIE_ROOT] = TRIE_NULL;
    mTrieUsed = TRIE_ROOT + 1;
    try {
        XmlUtils.beginDocument(parser, "words");
        String odest = "";
        char ooff = 0;
        while (true) {
            XmlUtils.nextElement(parser);
            String element = parser.getName();
            if (element == null || !(element.equals("word"))) {
                break;
            }
            String src = parser.getAttributeValue(null, "src");
            if (parser.next() == XmlPullParser.TEXT) {
                String dest = parser.getText();
                char off;
                if (dest.equals(odest)) {
                    off = ooff;
                } else {
                    off = (char) right.length();
                    right.append((char) dest.length());
                    right.append(dest);
                }
                add(src, off);
            }
        }
        // Don't let Resources cache a copy of all these strings.
        r.flushLayoutCache();
    } catch (XmlPullParserException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        parser.close();
    }
    mText = right.toString();
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 64 with XmlResourceParser

use of android.content.res.XmlResourceParser in project XobotOS by xamarin.

the class TextView method setInputExtras.

/**
     * Set the extra input data of the text, which is the
     * {@link EditorInfo#extras TextBoxAttribute.extras}
     * Bundle that will be filled in when creating an input connection.  The
     * given integer is the resource ID of an XML resource holding an
     * {@link android.R.styleable#InputExtras &lt;input-extras&gt;} XML tree.
     *
     * @see #getInputExtras(boolean) 
     * @see EditorInfo#extras
     * @attr ref android.R.styleable#TextView_editorExtras
     */
public void setInputExtras(int xmlResId) throws XmlPullParserException, IOException {
    XmlResourceParser parser = getResources().getXml(xmlResId);
    if (mInputContentType == null)
        mInputContentType = new InputContentType();
    mInputContentType.extras = new Bundle();
    getResources().parseBundleExtras(parser, mInputContentType.extras);
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) Bundle(android.os.Bundle)

Example 65 with XmlResourceParser

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

the class ZoneGetter method readTimezonesToDisplay.

private static List<String> readTimezonesToDisplay(Context context) {
    List<String> olsonIds = new ArrayList<String>();
    try (XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones)) {
        while (xrp.next() != XmlResourceParser.START_TAG) {
            continue;
        }
        xrp.next();
        while (xrp.getEventType() != XmlResourceParser.END_TAG) {
            while (xrp.getEventType() != XmlResourceParser.START_TAG) {
                if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
                    return olsonIds;
                }
                xrp.next();
            }
            if (xrp.getName().equals(XMLTAG_TIMEZONE)) {
                String olsonId = xrp.getAttributeValue(0);
                olsonIds.add(olsonId);
            }
            while (xrp.getEventType() != XmlResourceParser.END_TAG) {
                xrp.next();
            }
            xrp.next();
        }
    } catch (XmlPullParserException xppe) {
        Log.e(TAG, "Ill-formatted timezones.xml file");
    } catch (java.io.IOException ioe) {
        Log.e(TAG, "Unable to read timezones.xml file");
    }
    return olsonIds;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) ArrayList(java.util.ArrayList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

XmlResourceParser (android.content.res.XmlResourceParser)332 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)211 IOException (java.io.IOException)203 Resources (android.content.res.Resources)114 AttributeSet (android.util.AttributeSet)113 TypedArray (android.content.res.TypedArray)80 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)64 PackageManager (android.content.pm.PackageManager)45 ActivityInfo (android.content.pm.ActivityInfo)43 Bundle (android.os.Bundle)42 ComponentName (android.content.ComponentName)41 ArrayList (java.util.ArrayList)30 TypedValue (android.util.TypedValue)29 AssetManager (android.content.res.AssetManager)28 RemoteException (android.os.RemoteException)28 Intent (android.content.Intent)22 ApplicationInfo (android.content.pm.ApplicationInfo)18 InflateException (android.view.InflateException)18 FileNotFoundException (java.io.FileNotFoundException)18 NotFoundException (android.content.res.Resources.NotFoundException)17