Search in sources :

Example 36 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project platform_frameworks_base by android.

the class AccountManagerBackupHelper method restoreAccountAccessPermissions.

public void restoreAccountAccessPermissions(byte[] data, int userId) {
    try {
        ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(dataStream, StandardCharsets.UTF_8.name());
        PackageManager packageManager = mAccountManagerService.mContext.getPackageManager();
        final int permissionsOuterDepth = parser.getDepth();
        while (XmlUtils.nextElementWithin(parser, permissionsOuterDepth)) {
            if (!TAG_PERMISSIONS.equals(parser.getName())) {
                continue;
            }
            final int permissionOuterDepth = parser.getDepth();
            while (XmlUtils.nextElementWithin(parser, permissionOuterDepth)) {
                if (!TAG_PERMISSION.equals(parser.getName())) {
                    continue;
                }
                String accountDigest = parser.getAttributeValue(null, ATTR_ACCOUNT_SHA_256);
                if (TextUtils.isEmpty(accountDigest)) {
                    XmlUtils.skipCurrentTag(parser);
                }
                String packageName = parser.getAttributeValue(null, ATTR_PACKAGE);
                if (TextUtils.isEmpty(packageName)) {
                    XmlUtils.skipCurrentTag(parser);
                }
                String digest = parser.getAttributeValue(null, ATTR_DIGEST);
                if (TextUtils.isEmpty(digest)) {
                    XmlUtils.skipCurrentTag(parser);
                }
                PendingAppPermission pendingAppPermission = new PendingAppPermission(accountDigest, packageName, digest, userId);
                if (!pendingAppPermission.apply(packageManager)) {
                    synchronized (mLock) {
                        // Start watching before add pending to avoid a missed signal
                        if (mRestorePackageMonitor == null) {
                            mRestorePackageMonitor = new RestorePackageMonitor();
                            mRestorePackageMonitor.register(mAccountManagerService.mContext, mAccountManagerService.mMessageHandler.getLooper(), true);
                        }
                        if (mRestorePendingAppPermissions == null) {
                            mRestorePendingAppPermissions = new ArrayList<>();
                        }
                        mRestorePendingAppPermissions.add(pendingAppPermission);
                    }
                }
            }
        }
        // Make sure we eventually prune the in-memory pending restores
        synchronized (mLock) {
            mRestoreCancelCommand = new CancelRestoreCommand();
        }
        mAccountManagerService.mMessageHandler.postDelayed(mRestoreCancelCommand, PENDING_RESTORE_TIMEOUT_MILLIS);
    } catch (XmlPullParserException | IOException e) {
        Log.e(TAG, "Error restoring app permissions", e);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) ByteArrayInputStream(java.io.ByteArrayInputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 37 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project platform_frameworks_base by android.

the class TextServicesManagerService method buildSpellCheckerMapLocked.

private static void buildSpellCheckerMapLocked(Context context, ArrayList<SpellCheckerInfo> list, HashMap<String, SpellCheckerInfo> map, TextServicesSettings settings) {
    list.clear();
    map.clear();
    final PackageManager pm = context.getPackageManager();
    // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
    // behavior of PackageManager is exactly what we want.  It by default picks up appropriate
    // services depending on the unlock state for the specified user.
    final List<ResolveInfo> services = pm.queryIntentServicesAsUser(new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA, settings.getCurrentUserId());
    final int N = services.size();
    for (int i = 0; i < N; ++i) {
        final ResolveInfo ri = services.get(i);
        final ServiceInfo si = ri.serviceInfo;
        final ComponentName compName = new ComponentName(si.packageName, si.name);
        if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) {
            Slog.w(TAG, "Skipping text service " + compName + ": it does not require the permission " + android.Manifest.permission.BIND_TEXT_SERVICE);
            continue;
        }
        if (DBG)
            Slog.d(TAG, "Add: " + compName);
        try {
            final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri);
            if (sci.getSubtypeCount() <= 0) {
                Slog.w(TAG, "Skipping text service " + compName + ": it does not contain subtypes.");
                continue;
            }
            list.add(sci);
            map.put(sci.getId(), sci);
        } catch (XmlPullParserException e) {
            Slog.w(TAG, "Unable to load the spell checker " + compName, e);
        } catch (IOException e) {
            Slog.w(TAG, "Unable to load the spell checker " + compName, e);
        }
    }
    if (DBG) {
        Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size());
    }
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ServiceInfo(android.content.pm.ServiceInfo) PackageManager(android.content.pm.PackageManager) Intent(android.content.Intent) ComponentName(android.content.ComponentName) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) SpellCheckerInfo(android.view.textservice.SpellCheckerInfo)

Example 38 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project platform_frameworks_base by android.

the class TimeUtils method getTimeZones.

/**
     * Returns the time zones for the country, which is the code
     * attribute of the timezone element in time_zones_by_country.xml. Do not modify.
     *
     * @param country is a two character country code.
     * @return TimeZone list, maybe empty but never null. Do not modify.
     * @hide
     */
public static ArrayList<TimeZone> getTimeZones(String country) {
    synchronized (sLastLockObj) {
        if ((country != null) && country.equals(sLastCountry)) {
            if (DBG)
                Log.d(TAG, "getTimeZones(" + country + "): return cached version");
            return sLastZones;
        }
    }
    ArrayList<TimeZone> tzs = new ArrayList<TimeZone>();
    if (country == null) {
        if (DBG)
            Log.d(TAG, "getTimeZones(null): return empty list");
        return tzs;
    }
    Resources r = Resources.getSystem();
    XmlResourceParser parser = r.getXml(com.android.internal.R.xml.time_zones_by_country);
    try {
        XmlUtils.beginDocument(parser, "timezones");
        while (true) {
            XmlUtils.nextElement(parser);
            String element = parser.getName();
            if (element == null || !(element.equals("timezone"))) {
                break;
            }
            String code = parser.getAttributeValue(null, "code");
            if (country.equals(code)) {
                if (parser.next() == XmlPullParser.TEXT) {
                    String zoneIdString = parser.getText();
                    TimeZone tz = TimeZone.getTimeZone(zoneIdString);
                    if (tz.getID().startsWith("GMT") == false) {
                        // tz.getID doesn't start not "GMT" so its valid
                        tzs.add(tz);
                        if (DBG) {
                            Log.d(TAG, "getTimeZone('" + country + "'): found tz.getID==" + ((tz != null) ? tz.getID() : "<no tz>"));
                        }
                    }
                }
            }
        }
    } catch (XmlPullParserException e) {
        Log.e(TAG, "Got xml parser exception getTimeZone('" + country + "'): e=", e);
    } catch (IOException e) {
        Log.e(TAG, "Got IO exception getTimeZone('" + country + "'): e=", e);
    } finally {
        parser.close();
    }
    synchronized (sLastLockObj) {
        // Cache the last result;
        sLastZones = tzs;
        sLastCountry = country;
        return sLastZones;
    }
}
Also used : TimeZone(java.util.TimeZone) XmlResourceParser(android.content.res.XmlResourceParser) ArrayList(java.util.ArrayList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) IOException(java.io.IOException)

Example 39 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project platform_frameworks_base by android.

the class Xml method newPullParser.

/**
     * Returns a new pull parser with namespace support.
     */
public static XmlPullParser newPullParser() {
    try {
        KXmlParser parser = new KXmlParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true);
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        return parser;
    } catch (XmlPullParserException e) {
        throw new AssertionError();
    }
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 40 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project AsmackService by rtreffer.

the class FeatureNegotiationEngine method open.

/*
     * From RFC 3920-bis-13#page-26
     * 4.2.7. Flow Chart
     * 
     *                         +------------+
     *                         |  open TCP  |
     *                         | connection |
     *                         +------------+
     *                               |
     *                               | <------------ open() starts here
     *                               |
     *                               v
     *                        +---------------+
     *                        | send initial  |<-------------------------+
     *                        | stream header |                          ^
     *                        +---------------+                          |
     *                               |                                   |
     *                               v                                   |
     *                       +------------------+                        |
     *                       | receive response |                        |
     *                       | stream header    |                        |
     *                       +------------------+                        |
     *                               |                                   |
     *                               v                                   |
     *                        +----------------+                         |
     *                        | receive stream |                         |
     *    +------------------>| features       |                         |
     *    ^                   +----------------+                         |
     *    |                          |                                   |
     *    |                          v                                   |
     *    |       +<-----------------+                                   |
     *    |       |                                                      |
     *    |    {empty?} ----> {all voluntary?} ----> {some mandatory?}   |
     *    |       |      no          |          no         |             |
     *    |       | yes              | yes                 | yes         |
     *    |       |                  v                     v             |
     *    |       |           +---------------+    +----------------+    |
     *    |       |           | MAY negotiate |    | MUST negotiate |    |
     *    |       |           | any or none   |    | one feature    |    |
     *    |       |           +---------------+    +----------------+    |
     *    |       |                  |                     |             |
     *    |       v                  v                     |             |
     *    |   +----------+      +-----------+              |             |
     *    |   | process  |<-----| negotiate |              |             |
     *    |   | complete |  no  | a feature |              |             |
     *    |   +----------+      +-----------+              |             |
     *    |                          |                     |             |
     *    |                     yes  |                     |             |
     *    |                          v                     v             |
     *    |                          +--------->+<---------+             |
     *    |                                     |                        |
     *    |                                     v                        |
     *    +<-------------------------- {restart mandatory?} ------------>+
     *                   no                                     yes
     * 
     * The "open" method starts directly after opening the TCP streams,
     * negotiates the connection and returns true if the xmpp stream is ready
     * for a bind.
     * 
     * The usual way to bind is
     * if (streamEngine.open(account)) {
     *     String resource = streamEngine.bind(account.getResource);
     * }
     * 
     * Interresting and available features that require restarts:
     * - SASL
     * - TLS
     * - Compression
     */
/**
     * <p>Open a connection for a given account. This will run the full
     * negotiation with the following precedence:
     * <ol>
     *     <li>TLS (if available)</li>
     *     <li>Compression (if available)</li>
     *     <li>SASL</li>
     * <ol></p>
     *
     * <p><b>Note:</b> Servers should not offer compression befor SASL is
     * completed. This is not violated by the rule, mobile devices love xml
     * compression, thus a higher preference. Everything will work as expected
     * when compression is offered after SASL.</p>
     *
     * <p>This method requires a call to bind (if you wish to bind) afterwards.
     * </p>
     * 
     * @param account XmppAccount The account used for negotiation.
     * @throws XmppException In case of an error.
     */
public void open(XmppAccount account) throws XmppException {
    boolean rerun = true;
    boolean canBind = false;
    while (rerun) {
        try {
            rerun = false;
            xmppOutput.open(XMPPUtils.getDomain(account.getJid()), null);
            xmppInput.readOpening();
            Node features = null;
            do {
                Node stanza = xmppInput.nextStanza().getDocumentNode();
                if (XMLUtils.isInstance(stanza, "http://etherx.jabber.org/streams", "features")) {
                    features = stanza;
                }
            } while (features == null);
            // check basic stream features
            rosterVersioningSupported |= XMLUtils.hasChild(features, "urn:xmpp:features:rosterver", "ver");
            sessionsSupported |= XMLUtils.hasChild(features, "urn:ietf:params:xml:ns:xmpp-session", "session");
            canBind |= XMLUtils.hasChild(features, "urn:ietf:params:xml:ns:xmpp-bind", "bind");
            hasTLS = XMLUtils.hasChild(features, "urn:ietf:params:xml:ns:xmpp-tls", "starttls");
            Node compression = XMLUtils.getFirstChild(features, "http://jabber.org/features/compress", "compression");
            if (compression != null) {
                NodeList methods = compression.getChildNodes();
                for (int i = 0, l = methods.getLength(); i < l; i++) {
                    Node method = methods.item(i);
                    if (method.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    if (!"method".equals(method.getNodeName())) {
                        continue;
                    }
                    String methodName = method.getFirstChild().getNodeValue();
                    methodName = methodName.trim();
                    compressionSupported |= "zlib".equals(methodName);
                }
            }
            Node saslMechanisms = XMLUtils.getFirstChild(features, "urn:ietf:params:xml:ns:xmpp-sasl", "mechanisms");
            SASLSupported |= saslMechanisms != null;
            if (hasTLS && !secure) {
                // enable tls
                xmppOutput.sendUnchecked("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
                boolean startTLS = XMLUtils.isInstance(xmppInput.nextStanza().getDocumentNode(), "urn:ietf:params:xml:ns:xmpp-tls", "proceed");
                if (startTLS) {
                    startTLS();
                    secure = true;
                    rerun = true;
                    continue;
                }
            }
            if (compressionSupported && !compressed && ZLibOutputStream.SUPPORTED) {
                startCompress();
                rerun = true;
                continue;
            }
            if (SASLSupported && !authenticated) {
                if (saslLogin(saslMechanisms, account)) {
                    authenticated = true;
                    rerun = true;
                    continue;
                }
            }
        } catch (IllegalArgumentException e) {
            throw new XmppMalformedException("Can't negotiate features", e);
        } catch (IllegalStateException e) {
            throw new XmppMalformedException("Can't negotiate features", e);
        } catch (IOException e) {
            throw new XmppTransportException("Can't negotiate features", e);
        } catch (XmlPullParserException e) {
            throw new XmppMalformedException("Can't negotiate features", e);
        } catch (NoSuchAlgorithmException e) {
            // Should never happen - TLS not available?
            throw new XmppTransportException("Can't enable tls", e);
        } catch (KeyManagementException e) {
            throw new XmppTransportException("Can't trust server", e);
        }
    }
    if (!canBind) {
        throw new XmppTransportException("Couldn't reach bind state.");
    }
}
Also used : Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) XmppTransportException(com.googlecode.asmack.connection.XmppTransportException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) XmppMalformedException(com.googlecode.asmack.XmppMalformedException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1071 IOException (java.io.IOException)630 XmlPullParser (org.xmlpull.v1.XmlPullParser)440 FileNotFoundException (java.io.FileNotFoundException)187 XmlResourceParser (android.content.res.XmlResourceParser)186 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)99 PackageManager (android.content.pm.PackageManager)62 HashMap (java.util.HashMap)58 ComponentName (android.content.ComponentName)57 InputStream (java.io.InputStream)57 Intent (android.content.Intent)54 XmlSerializer (org.xmlpull.v1.XmlSerializer)54 AtomicFile (android.util.AtomicFile)50 BufferedInputStream (java.io.BufferedInputStream)44 TypedValue (android.util.TypedValue)43