Search in sources :

Example 46 with XmlPullParser

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

the class Owners method readLegacyOwnerFileLocked.

private boolean readLegacyOwnerFileLocked(File file) {
    if (!file.exists()) {
        // Already migrated or the device has no owners.
        return false;
    }
    try {
        InputStream input = new AtomicFile(file).openRead();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(input, StandardCharsets.UTF_8.name());
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type != XmlPullParser.START_TAG) {
                continue;
            }
            String tag = parser.getName();
            if (tag.equals(TAG_DEVICE_OWNER)) {
                String name = parser.getAttributeValue(null, ATTR_NAME);
                String packageName = parser.getAttributeValue(null, ATTR_PACKAGE);
                mDeviceOwner = new OwnerInfo(name, packageName, /* userRestrictionsMigrated =*/
                false, /* remoteBugreportUri =*/
                null, /* remoteBugreportHash =*/
                null);
                mDeviceOwnerUserId = UserHandle.USER_SYSTEM;
            } else if (tag.equals(TAG_DEVICE_INITIALIZER)) {
            // Deprecated tag
            } else if (tag.equals(TAG_PROFILE_OWNER)) {
                String profileOwnerPackageName = parser.getAttributeValue(null, ATTR_PACKAGE);
                String profileOwnerName = parser.getAttributeValue(null, ATTR_NAME);
                String profileOwnerComponentStr = parser.getAttributeValue(null, ATTR_COMPONENT_NAME);
                int userId = Integer.parseInt(parser.getAttributeValue(null, ATTR_USERID));
                OwnerInfo profileOwnerInfo = null;
                if (profileOwnerComponentStr != null) {
                    ComponentName admin = ComponentName.unflattenFromString(profileOwnerComponentStr);
                    if (admin != null) {
                        profileOwnerInfo = new OwnerInfo(profileOwnerName, admin, /* userRestrictionsMigrated =*/
                        false, null, null);
                    } else {
                        // This shouldn't happen but switch from package name -> component name
                        // might have written bad device owner files. b/17652534
                        Slog.e(TAG, "Error parsing device-owner file. Bad component name " + profileOwnerComponentStr);
                    }
                }
                if (profileOwnerInfo == null) {
                    profileOwnerInfo = new OwnerInfo(profileOwnerName, profileOwnerPackageName, /* userRestrictionsMigrated =*/
                    false, /* remoteBugreportUri =*/
                    null, /* remoteBugreportHash =*/
                    null);
                }
                mProfileOwners.put(userId, profileOwnerInfo);
            } else if (TAG_SYSTEM_UPDATE_POLICY.equals(tag)) {
                mSystemUpdatePolicy = SystemUpdatePolicy.restoreFromXml(parser);
            } else {
                throw new XmlPullParserException("Unexpected tag in device owner file: " + tag);
            }
        }
        input.close();
    } catch (XmlPullParserException | IOException e) {
        Slog.e(TAG, "Error parsing device-owner file", e);
    }
    return true;
}
Also used : AtomicFile(android.util.AtomicFile) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) ComponentName(android.content.ComponentName) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 47 with XmlPullParser

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

the class DevicePolicyManagerService method loadSettingsLocked.

private void loadSettingsLocked(DevicePolicyData policy, int userHandle) {
    JournaledFile journal = makeJournaledFile(userHandle);
    FileInputStream stream = null;
    File file = journal.chooseForRead();
    try {
        stream = new FileInputStream(file);
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(stream, StandardCharsets.UTF_8.name());
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        }
        String tag = parser.getName();
        if (!"policies".equals(tag)) {
            throw new XmlPullParserException("Settings do not start with policies tag: found " + tag);
        }
        // Extract the permission provider component name if available
        String permissionProvider = parser.getAttributeValue(null, ATTR_PERMISSION_PROVIDER);
        if (permissionProvider != null) {
            policy.mRestrictionsProvider = ComponentName.unflattenFromString(permissionProvider);
        }
        String userSetupComplete = parser.getAttributeValue(null, ATTR_SETUP_COMPLETE);
        if (userSetupComplete != null && Boolean.toString(true).equals(userSetupComplete)) {
            policy.mUserSetupComplete = true;
        }
        String paired = parser.getAttributeValue(null, ATTR_DEVICE_PAIRED);
        if (paired != null && Boolean.toString(true).equals(paired)) {
            policy.mPaired = true;
        }
        String deviceProvisioningConfigApplied = parser.getAttributeValue(null, ATTR_DEVICE_PROVISIONING_CONFIG_APPLIED);
        if (deviceProvisioningConfigApplied != null && Boolean.toString(true).equals(deviceProvisioningConfigApplied)) {
            policy.mDeviceProvisioningConfigApplied = true;
        }
        String provisioningState = parser.getAttributeValue(null, ATTR_PROVISIONING_STATE);
        if (!TextUtils.isEmpty(provisioningState)) {
            policy.mUserProvisioningState = Integer.parseInt(provisioningState);
        }
        String permissionPolicy = parser.getAttributeValue(null, ATTR_PERMISSION_POLICY);
        if (!TextUtils.isEmpty(permissionPolicy)) {
            policy.mPermissionPolicy = Integer.parseInt(permissionPolicy);
        }
        policy.mDelegatedCertInstallerPackage = parser.getAttributeValue(null, ATTR_DELEGATED_CERT_INSTALLER);
        policy.mApplicationRestrictionsManagingPackage = parser.getAttributeValue(null, ATTR_APPLICATION_RESTRICTIONS_MANAGER);
        type = parser.next();
        int outerDepth = parser.getDepth();
        policy.mLockTaskPackages.clear();
        policy.mAdminList.clear();
        policy.mAdminMap.clear();
        policy.mAffiliationIds.clear();
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                continue;
            }
            tag = parser.getName();
            if ("admin".equals(tag)) {
                String name = parser.getAttributeValue(null, "name");
                try {
                    DeviceAdminInfo dai = findAdmin(ComponentName.unflattenFromString(name), userHandle, /* throwForMissionPermission= */
                    false);
                    if (VERBOSE_LOG && (UserHandle.getUserId(dai.getActivityInfo().applicationInfo.uid) != userHandle)) {
                        Slog.w(LOG_TAG, "findAdmin returned an incorrect uid " + dai.getActivityInfo().applicationInfo.uid + " for user " + userHandle);
                    }
                    if (dai != null) {
                        ActiveAdmin ap = new ActiveAdmin(dai, /* parent */
                        false);
                        ap.readFromXml(parser);
                        policy.mAdminMap.put(ap.info.getComponent(), ap);
                    }
                } catch (RuntimeException e) {
                    Slog.w(LOG_TAG, "Failed loading admin " + name, e);
                }
            } else if ("failed-password-attempts".equals(tag)) {
                policy.mFailedPasswordAttempts = Integer.parseInt(parser.getAttributeValue(null, "value"));
            } else if ("password-owner".equals(tag)) {
                policy.mPasswordOwner = Integer.parseInt(parser.getAttributeValue(null, "value"));
            } else if ("active-password".equals(tag)) {
                policy.mActivePasswordQuality = Integer.parseInt(parser.getAttributeValue(null, "quality"));
                policy.mActivePasswordLength = Integer.parseInt(parser.getAttributeValue(null, "length"));
                policy.mActivePasswordUpperCase = Integer.parseInt(parser.getAttributeValue(null, "uppercase"));
                policy.mActivePasswordLowerCase = Integer.parseInt(parser.getAttributeValue(null, "lowercase"));
                policy.mActivePasswordLetters = Integer.parseInt(parser.getAttributeValue(null, "letters"));
                policy.mActivePasswordNumeric = Integer.parseInt(parser.getAttributeValue(null, "numeric"));
                policy.mActivePasswordSymbols = Integer.parseInt(parser.getAttributeValue(null, "symbols"));
                policy.mActivePasswordNonLetter = Integer.parseInt(parser.getAttributeValue(null, "nonletter"));
            } else if (TAG_ACCEPTED_CA_CERTIFICATES.equals(tag)) {
                policy.mAcceptedCaCertificates.add(parser.getAttributeValue(null, ATTR_NAME));
            } else if (TAG_LOCK_TASK_COMPONENTS.equals(tag)) {
                policy.mLockTaskPackages.add(parser.getAttributeValue(null, "name"));
            } else if (TAG_STATUS_BAR.equals(tag)) {
                policy.mStatusBarDisabled = Boolean.parseBoolean(parser.getAttributeValue(null, ATTR_DISABLED));
            } else if (DO_NOT_ASK_CREDENTIALS_ON_BOOT_XML.equals(tag)) {
                policy.doNotAskCredentialsOnBoot = true;
            } else if (TAG_AFFILIATION_ID.equals(tag)) {
                policy.mAffiliationIds.add(parser.getAttributeValue(null, "id"));
            } else if (TAG_ADMIN_BROADCAST_PENDING.equals(tag)) {
                String pending = parser.getAttributeValue(null, ATTR_VALUE);
                policy.mAdminBroadcastPending = Boolean.toString(true).equals(pending);
            } else if (TAG_INITIALIZATION_BUNDLE.equals(tag)) {
                policy.mInitBundle = PersistableBundle.restoreFromXml(parser);
            } else {
                Slog.w(LOG_TAG, "Unknown tag: " + tag);
                XmlUtils.skipCurrentTag(parser);
            }
        }
    } catch (FileNotFoundException e) {
    // Don't be noisy, this is normal if we haven't defined any policies.
    } catch (NullPointerException | NumberFormatException | XmlPullParserException | IOException | IndexOutOfBoundsException e) {
        Slog.w(LOG_TAG, "failed parsing " + file, e);
    }
    try {
        if (stream != null) {
            stream.close();
        }
    } catch (IOException e) {
    // Ignore
    }
    // Generate a list of admins from the admin map
    policy.mAdminList.addAll(policy.mAdminMap.values());
    // Validate that what we stored for the password quality matches
    // sufficiently what is currently set.  Note that this is only
    // a sanity check in case the two get out of sync; this should
    // never normally happen.
    final long identity = mInjector.binderClearCallingIdentity();
    try {
        int actualPasswordQuality = mLockPatternUtils.getActivePasswordQuality(userHandle);
        if (actualPasswordQuality < policy.mActivePasswordQuality) {
            Slog.w(LOG_TAG, "Active password quality 0x" + Integer.toHexString(policy.mActivePasswordQuality) + " does not match actual quality 0x" + Integer.toHexString(actualPasswordQuality));
            policy.mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
            policy.mActivePasswordLength = 0;
            policy.mActivePasswordUpperCase = 0;
            policy.mActivePasswordLowerCase = 0;
            policy.mActivePasswordLetters = 0;
            policy.mActivePasswordNumeric = 0;
            policy.mActivePasswordSymbols = 0;
            policy.mActivePasswordNonLetter = 0;
        }
    } finally {
        mInjector.binderRestoreCallingIdentity(identity);
    }
    validatePasswordOwnerLocked(policy);
    updateMaximumTimeToLockLocked(userHandle);
    updateLockTaskPackagesLocked(policy.mLockTaskPackages, userHandle);
    if (policy.mStatusBarDisabled) {
        setStatusBarDisabledInternal(policy.mStatusBarDisabled, userHandle);
    }
}
Also used : JournaledFile(com.android.internal.util.JournaledFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) FileNotFoundException(java.io.FileNotFoundException) ParcelableString(com.android.internal.util.ParcelableString) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DeviceAdminInfo(android.app.admin.DeviceAdminInfo) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) File(java.io.File) JournaledFile(com.android.internal.util.JournaledFile)

Example 48 with XmlPullParser

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

the class DisplaySettings method readSettingsLocked.

public void readSettingsLocked() {
    FileInputStream stream;
    try {
        stream = mFile.openRead();
    } catch (FileNotFoundException e) {
        Slog.i(TAG, "No existing display settings " + mFile.getBaseFile() + "; starting empty");
        return;
    }
    boolean success = false;
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(stream, StandardCharsets.UTF_8.name());
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
        // Do nothing.
        }
        if (type != XmlPullParser.START_TAG) {
            throw new IllegalStateException("no start tag found");
        }
        int outerDepth = parser.getDepth();
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                continue;
            }
            String tagName = parser.getName();
            if (tagName.equals("display")) {
                readDisplay(parser);
            } else {
                Slog.w(TAG, "Unknown element under <display-settings>: " + parser.getName());
                XmlUtils.skipCurrentTag(parser);
            }
        }
        success = true;
    } catch (IllegalStateException e) {
        Slog.w(TAG, "Failed parsing " + e);
    } catch (NullPointerException e) {
        Slog.w(TAG, "Failed parsing " + e);
    } catch (NumberFormatException e) {
        Slog.w(TAG, "Failed parsing " + e);
    } catch (XmlPullParserException e) {
        Slog.w(TAG, "Failed parsing " + e);
    } catch (IOException e) {
        Slog.w(TAG, "Failed parsing " + e);
    } catch (IndexOutOfBoundsException e) {
        Slog.w(TAG, "Failed parsing " + e);
    } finally {
        if (!success) {
            mEntries.clear();
        }
        try {
            stream.close();
        } catch (IOException e) {
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 49 with XmlPullParser

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

the class UsbSettingsManager method upgradeSingleUserLocked.

/**
     * Upgrade any single-user settings from {@link #sSingleUserSettingsFile}.
     * Should only by called by owner.
     */
private void upgradeSingleUserLocked() {
    if (sSingleUserSettingsFile.exists()) {
        mDevicePreferenceMap.clear();
        mAccessoryPreferenceMap.clear();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(sSingleUserSettingsFile);
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(fis, StandardCharsets.UTF_8.name());
            XmlUtils.nextElement(parser);
            while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
                final String tagName = parser.getName();
                if ("preference".equals(tagName)) {
                    readPreference(parser);
                } else {
                    XmlUtils.nextElement(parser);
                }
            }
        } catch (IOException e) {
            Log.wtf(TAG, "Failed to read single-user settings", e);
        } catch (XmlPullParserException e) {
            Log.wtf(TAG, "Failed to read single-user settings", e);
        } finally {
            IoUtils.closeQuietly(fis);
        }
        writeSettingsLocked();
        // Success or failure, we delete single-user file
        sSingleUserSettingsFile.delete();
    }
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 50 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project androidannotations by androidannotations.

the class SetXmlSerializer method deserialize.

public static Set<String> deserialize(String data) {
    Set<String> stringSet = new TreeSet<>();
    XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setInput(new StringReader(data));
        parser.next();
        parser.require(XmlPullParser.START_TAG, NAMESPACE, SET_TAG);
        while (parser.next() != XmlPullParser.END_TAG) {
            parser.require(XmlPullParser.START_TAG, NAMESPACE, STRING_TAG);
            parser.next();
            parser.require(XmlPullParser.TEXT, null, null);
            stringSet.add(parser.getText());
            parser.next();
            parser.require(XmlPullParser.END_TAG, null, STRING_TAG);
        }
    } catch (XmlPullParserException e) {
        Log.w("getStringSet", e);
        return null;
    } catch (IOException e) {
        Log.w("getStringSet", e);
        return null;
    }
    return stringSet;
}
Also used : TreeSet(java.util.TreeSet) XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

XmlPullParser (org.xmlpull.v1.XmlPullParser)673 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)623 IOException (java.io.IOException)377 FileInputStream (java.io.FileInputStream)185 FileNotFoundException (java.io.FileNotFoundException)185 File (java.io.File)107 ArrayList (java.util.ArrayList)77 StringReader (java.io.StringReader)65 AttributeSet (android.util.AttributeSet)61 Test (org.junit.Test)57 TypedArray (android.content.res.TypedArray)56 InputStream (java.io.InputStream)48 AtomicFile (android.util.AtomicFile)47 HashMap (java.util.HashMap)45 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)39 FileReader (java.io.FileReader)36 BufferedInputStream (java.io.BufferedInputStream)30 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)30 RemoteException (android.os.RemoteException)28 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)28