Search in sources :

Example 6 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android-job by evernote.

the class XmlUtils method readThisValueXml.

private static final Object readThisValueXml(XmlPullParser parser, String[] name, ReadMapCallback callback) throws XmlPullParserException, java.io.IOException {
    final String valueName = parser.getAttributeValue(null, "name");
    final String tagName = parser.getName();
    //System.out.println("Reading this value tag: " + tagName + ", name=" + valueName);
    Object res;
    if (tagName.equals("null")) {
        res = null;
    } else if (tagName.equals("string")) {
        String value = "";
        int eventType;
        while ((eventType = parser.next()) != parser.END_DOCUMENT) {
            if (eventType == parser.END_TAG) {
                if (parser.getName().equals("string")) {
                    name[0] = valueName;
                    //System.out.println("Returning value for " + valueName + ": " + value);
                    return value;
                }
                throw new XmlPullParserException("Unexpected end tag in <string>: " + parser.getName());
            } else if (eventType == parser.TEXT) {
                value += parser.getText();
            } else if (eventType == parser.START_TAG) {
                throw new XmlPullParserException("Unexpected start tag in <string>: " + parser.getName());
            }
        }
        throw new XmlPullParserException("Unexpected end of document in <string>");
    } else if ((res = readThisPrimitiveValueXml(parser, tagName)) != null) {
    // all work already done by readThisPrimitiveValueXml
    } else if (tagName.equals("int-array")) {
        res = readThisIntArrayXml(parser, "int-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("long-array")) {
        res = readThisLongArrayXml(parser, "long-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("double-array")) {
        res = readThisDoubleArrayXml(parser, "double-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("string-array")) {
        res = readThisStringArrayXml(parser, "string-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("map")) {
        parser.next();
        res = readThisMapXml(parser, "map", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("list")) {
        parser.next();
        res = readThisListXml(parser, "list", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("set")) {
        parser.next();
        res = readThisSetXml(parser, "set", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (callback != null) {
        res = callback.readThisUnknownObjectXml(parser, tagName);
        name[0] = valueName;
        return res;
    } else {
        throw new XmlPullParserException("Unknown tag: " + tagName);
    }
    // Skip through to end tag.
    int eventType;
    while ((eventType = parser.next()) != parser.END_DOCUMENT) {
        if (eventType == parser.END_TAG) {
            if (parser.getName().equals(tagName)) {
                name[0] = valueName;
                //System.out.println("Returning value for " + valueName + ": " + res);
                return res;
            }
            throw new XmlPullParserException("Unexpected end tag in <" + tagName + ">: " + parser.getName());
        } else if (eventType == parser.TEXT) {
            throw new XmlPullParserException("Unexpected text in <" + tagName + ">: " + parser.getName());
        } else if (eventType == parser.START_TAG) {
            throw new XmlPullParserException("Unexpected start tag in <" + tagName + ">: " + parser.getName());
        }
    }
    throw new XmlPullParserException("Unexpected end of document in <" + tagName + ">");
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 7 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android_frameworks_base by ParanoidAndroid.

the class AliasActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    XmlResourceParser parser = null;
    try {
        ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
        parser = ai.loadXmlMetaData(getPackageManager(), ALIAS_META_DATA);
        if (parser == null) {
            throw new RuntimeException("Alias requires a meta-data field " + ALIAS_META_DATA);
        }
        Intent intent = parseAlias(parser);
        if (intent == null) {
            throw new RuntimeException("No <intent> tag found in alias description");
        }
        startActivity(intent);
        finish();
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException("Error parsing alias", e);
    } catch (XmlPullParserException e) {
        throw new RuntimeException("Error parsing alias", e);
    } catch (IOException e) {
        throw new RuntimeException("Error parsing alias", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) XmlResourceParser(android.content.res.XmlResourceParser) PackageManager(android.content.pm.PackageManager) Intent(android.content.Intent) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 8 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android_frameworks_base by ParanoidAndroid.

the class SearchableInfo method getActivityMetaData.

/**
     * Get the metadata for a given activity
     * 
     * @param context runtime context
     * @param xml XML parser for reading attributes
     * @param cName The component name of the searchable activity
     * 
     * @result A completely constructed SearchableInfo, or null if insufficient XML data for it
     */
private static SearchableInfo getActivityMetaData(Context context, XmlPullParser xml, final ComponentName cName) {
    SearchableInfo result = null;
    Context activityContext = createActivityContext(context, cName);
    if (activityContext == null)
        return null;
    // forward through the file until it's reading the tag of interest.
    try {
        int tagType = xml.next();
        while (tagType != XmlPullParser.END_DOCUMENT) {
            if (tagType == XmlPullParser.START_TAG) {
                if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE)) {
                    AttributeSet attr = Xml.asAttributeSet(xml);
                    if (attr != null) {
                        try {
                            result = new SearchableInfo(activityContext, attr, cName);
                        } catch (IllegalArgumentException ex) {
                            Log.w(LOG_TAG, "Invalid searchable metadata for " + cName.flattenToShortString() + ": " + ex.getMessage());
                            return null;
                        }
                    }
                } else if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE_ACTION_KEY)) {
                    if (result == null) {
                        // Can't process an embedded element if we haven't seen the enclosing
                        return null;
                    }
                    AttributeSet attr = Xml.asAttributeSet(xml);
                    if (attr != null) {
                        try {
                            result.addActionKey(new ActionKeyInfo(activityContext, attr));
                        } catch (IllegalArgumentException ex) {
                            Log.w(LOG_TAG, "Invalid action key for " + cName.flattenToShortString() + ": " + ex.getMessage());
                            return null;
                        }
                    }
                }
            }
            tagType = xml.next();
        }
    } catch (XmlPullParserException e) {
        Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
        return null;
    } catch (IOException e) {
        Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
        return null;
    }
    return result;
}
Also used : Context(android.content.Context) AttributeSet(android.util.AttributeSet) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 9 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android_frameworks_base by ParanoidAndroid.

the class SharedPreferencesImpl method loadFromDiskLocked.

private void loadFromDiskLocked() {
    if (mLoaded) {
        return;
    }
    if (mBackupFile.exists()) {
        mFile.delete();
        mBackupFile.renameTo(mFile);
    }
    // Debugging
    if (mFile.exists() && !mFile.canRead()) {
        Log.w(TAG, "Attempt to read preferences file " + mFile + " without permission");
    }
    Map map = null;
    StructStat stat = null;
    try {
        stat = Libcore.os.stat(mFile.getPath());
        if (mFile.canRead()) {
            BufferedInputStream str = null;
            try {
                str = new BufferedInputStream(new FileInputStream(mFile), 16 * 1024);
                map = XmlUtils.readMapXml(str);
            } catch (XmlPullParserException e) {
                Log.w(TAG, "getSharedPreferences", e);
            } catch (FileNotFoundException e) {
                Log.w(TAG, "getSharedPreferences", e);
            } catch (IOException e) {
                Log.w(TAG, "getSharedPreferences", e);
            } finally {
                IoUtils.closeQuietly(str);
            }
        }
    } catch (ErrnoException e) {
    }
    mLoaded = true;
    if (map != null) {
        mMap = map;
        mStatTimestamp = stat.st_mtime;
        mStatSize = stat.st_size;
    } else {
        mMap = new HashMap<String, Object>();
    }
    notifyAll();
}
Also used : StructStat(libcore.io.StructStat) ErrnoException(libcore.io.ErrnoException) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) FileInputStream(java.io.FileInputStream)

Example 10 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android_frameworks_base by ParanoidAndroid.

the class RegisteredServicesCache method generateServicesMap.

/**
     * Populate {@link UserServices#services} by scanning installed packages for
     * given {@link UserHandle}.
     */
private void generateServicesMap(int userId) {
    if (DEBUG) {
        Slog.d(TAG, "generateServicesMap() for " + userId);
    }
    final PackageManager pm = mContext.getPackageManager();
    final ArrayList<ServiceInfo<V>> serviceInfos = new ArrayList<ServiceInfo<V>>();
    final List<ResolveInfo> resolveInfos = pm.queryIntentServicesAsUser(new Intent(mInterfaceName), PackageManager.GET_META_DATA, userId);
    for (ResolveInfo resolveInfo : resolveInfos) {
        try {
            ServiceInfo<V> info = parseServiceInfo(resolveInfo);
            if (info == null) {
                Log.w(TAG, "Unable to load service info " + resolveInfo.toString());
                continue;
            }
            serviceInfos.add(info);
        } catch (XmlPullParserException e) {
            Log.w(TAG, "Unable to load service info " + resolveInfo.toString(), e);
        } catch (IOException e) {
            Log.w(TAG, "Unable to load service info " + resolveInfo.toString(), e);
        }
    }
    synchronized (mServicesLock) {
        final UserServices<V> user = findOrCreateUserLocked(userId);
        final boolean firstScan = user.services == null;
        if (firstScan) {
            user.services = Maps.newHashMap();
        } else {
            user.services.clear();
        }
        StringBuilder changes = new StringBuilder();
        boolean changed = false;
        for (ServiceInfo<V> info : serviceInfos) {
            // four cases:
            // - doesn't exist yet
            //   - add, notify user that it was added
            // - exists and the UID is the same
            //   - replace, don't notify user
            // - exists, the UID is different, and the new one is not a system package
            //   - ignore
            // - exists, the UID is different, and the new one is a system package
            //   - add, notify user that it was added
            Integer previousUid = user.persistentServices.get(info.type);
            if (previousUid == null) {
                if (DEBUG) {
                    changes.append("  New service added: ").append(info).append("\n");
                }
                changed = true;
                user.services.put(info.type, info);
                user.persistentServices.put(info.type, info.uid);
                if (!(mPersistentServicesFileDidNotExist && firstScan)) {
                    notifyListener(info.type, userId, false);
                }
            } else if (previousUid == info.uid) {
                if (DEBUG) {
                    changes.append("  Existing service (nop): ").append(info).append("\n");
                }
                user.services.put(info.type, info);
            } else if (inSystemImage(info.uid) || !containsTypeAndUid(serviceInfos, info.type, previousUid)) {
                if (DEBUG) {
                    if (inSystemImage(info.uid)) {
                        changes.append("  System service replacing existing: ").append(info).append("\n");
                    } else {
                        changes.append("  Existing service replacing a removed service: ").append(info).append("\n");
                    }
                }
                changed = true;
                user.services.put(info.type, info);
                user.persistentServices.put(info.type, info.uid);
                notifyListener(info.type, userId, false);
            } else {
                // ignore
                if (DEBUG) {
                    changes.append("  Existing service with new uid ignored: ").append(info).append("\n");
                }
            }
        }
        ArrayList<V> toBeRemoved = Lists.newArrayList();
        for (V v1 : user.persistentServices.keySet()) {
            if (!containsType(serviceInfos, v1)) {
                toBeRemoved.add(v1);
            }
        }
        for (V v1 : toBeRemoved) {
            if (DEBUG) {
                changes.append("  Service removed: ").append(v1).append("\n");
            }
            changed = true;
            user.persistentServices.remove(v1);
            notifyListener(v1, userId, true);
        }
        if (DEBUG) {
            if (changes.length() > 0) {
                Log.d(TAG, "generateServicesMap(" + mInterfaceName + "): " + serviceInfos.size() + " services:\n" + changes);
            } else {
                Log.d(TAG, "generateServicesMap(" + mInterfaceName + "): " + serviceInfos.size() + " services unchanged");
            }
        }
        if (changed) {
            writePersistentServicesLocked();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Intent(android.content.Intent) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1062 IOException (java.io.IOException)630 XmlPullParser (org.xmlpull.v1.XmlPullParser)437 FileNotFoundException (java.io.FileNotFoundException)187 XmlResourceParser (android.content.res.XmlResourceParser)185 FileInputStream (java.io.FileInputStream)182 AttributeSet (android.util.AttributeSet)159 TypedArray (android.content.res.TypedArray)156 Resources (android.content.res.Resources)101 File (java.io.File)101 ArrayList (java.util.ArrayList)97 PackageManager (android.content.pm.PackageManager)62 ComponentName (android.content.ComponentName)57 InputStream (java.io.InputStream)56 HashMap (java.util.HashMap)56 Intent (android.content.Intent)54 XmlSerializer (org.xmlpull.v1.XmlSerializer)52 AtomicFile (android.util.AtomicFile)50 BufferedInputStream (java.io.BufferedInputStream)44 TypedValue (android.util.TypedValue)43