Java Code Examples for com.android.internal.util.XmlUtils#beginDocument()

The following examples show how to use com.android.internal.util.XmlUtils#beginDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistentDataStore.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void loadFromXml(XmlPullParser parser)
        throws IOException, XmlPullParserException {
    XmlUtils.beginDocument(parser, TAG_DISPLAY_MANAGER_STATE);
    final int outerDepth = parser.getDepth();
    while (XmlUtils.nextElementWithin(parser, outerDepth)) {
        if (parser.getName().equals(TAG_REMEMBERED_WIFI_DISPLAYS)) {
            loadRememberedWifiDisplaysFromXml(parser);
        }
        if (parser.getName().equals(TAG_DISPLAY_STATES)) {
            loadDisplaysFromXml(parser);
        }
        if (parser.getName().equals(TAG_STABLE_DEVICE_VALUES)) {
            mStableDeviceValues.loadFromXml(parser);
        }
        if (parser.getName().equals(TAG_BRIGHTNESS_CONFIGURATIONS)) {
            mBrightnessConfigurations.loadFromXml(parser);
        }
    }
}
 
Example 2
Source File: PersistentDataStore.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void loadFromXml(XmlPullParser parser)
        throws IOException, XmlPullParserException {
    XmlUtils.beginDocument(parser, TAG_TV_INPUT_MANAGER_STATE);
    final int outerDepth = parser.getDepth();
    while (XmlUtils.nextElementWithin(parser, outerDepth)) {
        if (parser.getName().equals(TAG_BLOCKED_RATINGS)) {
            loadBlockedRatingsFromXml(parser);
        } else if (parser.getName().equals(TAG_PARENTAL_CONTROLS)) {
            String enabled = parser.getAttributeValue(null, ATTR_ENABLED);
            if (TextUtils.isEmpty(enabled)) {
                throw new XmlPullParserException(
                        "Missing " + ATTR_ENABLED + " attribute on " + TAG_PARENTAL_CONTROLS);
            }
            mParentalControlsEnabled = Boolean.parseBoolean(enabled);
        }
    }
}
 
Example 3
Source File: OverlayManagerSettings.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public static void restore(@NonNull final ArrayList<SettingsItem> table,
        @NonNull final InputStream is) throws IOException, XmlPullParserException {

    try (InputStreamReader reader = new InputStreamReader(is)) {
        table.clear();
        final XmlPullParser parser = Xml.newPullParser();
        parser.setInput(reader);
        XmlUtils.beginDocument(parser, TAG_OVERLAYS);
        int version = XmlUtils.readIntAttribute(parser, ATTR_VERSION);
        if (version != CURRENT_VERSION) {
            upgrade(version);
        }
        int depth = parser.getDepth();

        while (XmlUtils.nextElementWithin(parser, depth)) {
            switch (parser.getName()) {
                case TAG_ITEM:
                    final SettingsItem item = restoreRow(parser, depth + 1);
                    table.add(item);
                    break;
            }
        }
    }
}
 
Example 4
Source File: WatchlistSettings.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void reloadSettings() {
    if (!mXmlFile.exists()) {
        // No settings config
        return;
    }
    try (FileInputStream stream = mXmlFile.openRead()){
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(stream, StandardCharsets.UTF_8.name());
        XmlUtils.beginDocument(parser, "network-watchlist-settings");
        final int outerDepth = parser.getDepth();
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if (parser.getName().equals("secret-key")) {
                mPrivacySecretKey = parseSecretKey(parser);
            }
        }
        Slog.i(TAG, "Reload watchlist settings done");
    } catch (IllegalStateException | NullPointerException | NumberFormatException |
            XmlPullParserException | IOException | IndexOutOfBoundsException e) {
        Slog.e(TAG, "Failed parsing xml", e);
    }
}
 
Example 5
Source File: PersistentDataStore.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void loadFromXml(XmlPullParser parser)
        throws IOException, XmlPullParserException {
    XmlUtils.beginDocument(parser, "input-manager-state");
    final int outerDepth = parser.getDepth();
    while (XmlUtils.nextElementWithin(parser, outerDepth)) {
        if (parser.getName().equals("input-devices")) {
            loadInputDevicesFromXml(parser);
        }
    }
}
 
Example 6
Source File: XmlConfigSource.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private NetworkSecurityConfig.Builder parseDebugOverridesResource()
        throws IOException, XmlPullParserException, ParserException {
    Resources resources = mContext.getResources();
    String packageName = resources.getResourcePackageName(mResourceId);
    String entryName = resources.getResourceEntryName(mResourceId);
    int resId = resources.getIdentifier(entryName + "_debug", "xml", packageName);
    // No debug-overrides resource was found, nothing to parse.
    if (resId == 0) {
        return null;
    }
    NetworkSecurityConfig.Builder debugConfigBuilder = null;
    // Parse debug-overrides out of the _debug resource.
    try (XmlResourceParser parser = resources.getXml(resId)) {
        XmlUtils.beginDocument(parser, "network-security-config");
        int outerDepth = parser.getDepth();
        boolean seenDebugOverrides = false;
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if ("debug-overrides".equals(parser.getName())) {
                if (seenDebugOverrides) {
                    throw new ParserException(parser, "Only one debug-overrides allowed");
                }
                if (mDebugBuild) {
                    debugConfigBuilder =
                            parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
                } else {
                    XmlUtils.skipCurrentTag(parser);
                }
                seenDebugOverrides = true;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }
    }

    return debugConfigBuilder;
}
 
Example 7
Source File: XmlConfigSource.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void parseNetworkSecurityConfig(XmlResourceParser parser)
        throws IOException, XmlPullParserException, ParserException {
    Set<String> seenDomains = new ArraySet<>();
    List<Pair<NetworkSecurityConfig.Builder, Set<Domain>>> builders = new ArrayList<>();
    NetworkSecurityConfig.Builder baseConfigBuilder = null;
    NetworkSecurityConfig.Builder debugConfigBuilder = null;
    boolean seenDebugOverrides = false;
    boolean seenBaseConfig = false;

    XmlUtils.beginDocument(parser, "network-security-config");
    int outerDepth = parser.getDepth();
    while (XmlUtils.nextElementWithin(parser, outerDepth)) {
        if ("base-config".equals(parser.getName())) {
            if (seenBaseConfig) {
                throw new ParserException(parser, "Only one base-config allowed");
            }
            seenBaseConfig = true;
            baseConfigBuilder =
                    parseConfigEntry(parser, seenDomains, null, CONFIG_BASE).get(0).first;
        } else if ("domain-config".equals(parser.getName())) {
            builders.addAll(
                    parseConfigEntry(parser, seenDomains, baseConfigBuilder, CONFIG_DOMAIN));
        } else if ("debug-overrides".equals(parser.getName())) {
            if (seenDebugOverrides) {
                throw new ParserException(parser, "Only one debug-overrides allowed");
            }
            if (mDebugBuild) {
                debugConfigBuilder =
                        parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
            seenDebugOverrides = true;
        } else {
            XmlUtils.skipCurrentTag(parser);
        }
    }
    // If debug is true and there was no debug-overrides in the file check for an extra
    // _debug resource.
    if (mDebugBuild && debugConfigBuilder == null) {
        debugConfigBuilder = parseDebugOverridesResource();
    }

    // Use the platform default as the parent of the base config for any values not provided
    // there. If there is no base config use the platform default.
    NetworkSecurityConfig.Builder platformDefaultBuilder =
            NetworkSecurityConfig.getDefaultBuilder(mApplicationInfo);
    addDebugAnchorsIfNeeded(debugConfigBuilder, platformDefaultBuilder);
    if (baseConfigBuilder != null) {
        baseConfigBuilder.setParent(platformDefaultBuilder);
        addDebugAnchorsIfNeeded(debugConfigBuilder, baseConfigBuilder);
    } else {
        baseConfigBuilder = platformDefaultBuilder;
    }
    // Build the per-domain config mapping.
    Set<Pair<Domain, NetworkSecurityConfig>> configs = new ArraySet<>();

    for (Pair<NetworkSecurityConfig.Builder, Set<Domain>> entry : builders) {
        NetworkSecurityConfig.Builder builder = entry.first;
        Set<Domain> domains = entry.second;
        // Set the parent of configs that do not have a parent to the base-config. This can
        // happen if the base-config comes after a domain-config in the file.
        // Note that this is safe with regards to children because of the order that
        // parseConfigEntry returns builders, the parent is always before the children. The
        // children builders will not have build called until _after_ their parents have their
        // parent set so everything is consistent.
        if (builder.getParent() == null) {
            builder.setParent(baseConfigBuilder);
        }
        addDebugAnchorsIfNeeded(debugConfigBuilder, builder);
        NetworkSecurityConfig config = builder.build();
        for (Domain domain : domains) {
            configs.add(new Pair<>(domain, config));
        }
    }
    mDefaultConfig = baseConfigBuilder.build();
    mDomainMap = configs;
}
 
Example 8
Source File: PointerIcon.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
    final XmlResourceParser parser = resources.getXml(resourceId);
    final int bitmapRes;
    final float hotSpotX;
    final float hotSpotY;
    try {
        XmlUtils.beginDocument(parser, "pointer-icon");

        final TypedArray a = resources.obtainAttributes(
                parser, com.android.internal.R.styleable.PointerIcon);
        bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
        hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
        hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
        a.recycle();
    } catch (Exception ex) {
        throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex);
    } finally {
        parser.close();
    }

    if (bitmapRes == 0) {
        throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute.");
    }

    Drawable drawable;
    if (context == null) {
        drawable = resources.getDrawable(bitmapRes);
    } else {
        drawable = context.getDrawable(bitmapRes);
    }
    if (drawable instanceof AnimationDrawable) {
        // Extract animation frame bitmaps.
        final AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
        final int frames = animationDrawable.getNumberOfFrames();
        drawable = animationDrawable.getFrame(0);
        if (frames == 1) {
            Log.w(TAG, "Animation icon with single frame -- simply treating the first "
                    + "frame as a normal bitmap icon.");
        } else {
            // Assumes they have the exact duration.
            mDurationPerFrame = animationDrawable.getDuration(0);
            mBitmapFrames = new Bitmap[frames - 1];
            final int width = drawable.getIntrinsicWidth();
            final int height = drawable.getIntrinsicHeight();
            for (int i = 1; i < frames; ++i) {
                Drawable drawableFrame = animationDrawable.getFrame(i);
                if (!(drawableFrame instanceof BitmapDrawable)) {
                    throw new IllegalArgumentException("Frame of an animated pointer icon "
                            + "must refer to a bitmap drawable.");
                }
                if (drawableFrame.getIntrinsicWidth() != width ||
                    drawableFrame.getIntrinsicHeight() != height) {
                    throw new IllegalArgumentException("The bitmap size of " + i + "-th frame "
                            + "is different. All frames should have the exact same size and "
                            + "share the same hotspot.");
                }
                BitmapDrawable bitmapDrawableFrame = (BitmapDrawable) drawableFrame;
                mBitmapFrames[i - 1] = getBitmapFromDrawable(bitmapDrawableFrame);
            }
        }
    }
    if (!(drawable instanceof BitmapDrawable)) {
        throw new IllegalArgumentException("<pointer-icon> bitmap attribute must "
                + "refer to a bitmap drawable.");
    }

    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    final Bitmap bitmap = getBitmapFromDrawable(bitmapDrawable);
    validateHotSpot(bitmap, hotSpotX, hotSpotY);
    // Set the properties now that we have successfully loaded the icon.
    mBitmap = bitmap;
    mHotSpotX = hotSpotX;
    mHotSpotY = hotSpotY;
}
 
Example 9
Source File: XmlConfigSource.java    From cwac-netsecurity with Apache License 2.0 4 votes vote down vote up
private void parseNetworkSecurityConfig(XmlResourceParser parser)
        throws IOException, XmlPullParserException, ParserException {
    Set<String> seenDomains = new HashSet<>();
    List<Pair<NetworkSecurityConfig.Builder, Set<Domain>>> builders = new ArrayList<>();
    NetworkSecurityConfig.Builder baseConfigBuilder = null;
    NetworkSecurityConfig.Builder debugConfigBuilder = null;
    boolean seenDebugOverrides = false;
    boolean seenBaseConfig = false;

    XmlUtils.beginDocument(parser, "network-security-config");
    int outerDepth = parser.getDepth();
    while (XmlUtils.nextElementWithin(parser, outerDepth)) {
        if ("base-config".equals(parser.getName())) {
            if (seenBaseConfig) {
                throw new ParserException(parser, "Only one base-config allowed");
            }
            seenBaseConfig = true;
            baseConfigBuilder =
                    parseConfigEntry(parser, seenDomains, null, CONFIG_BASE).get(0).first;
        } else if ("domain-config".equals(parser.getName())) {
            builders.addAll(
                    parseConfigEntry(parser, seenDomains, baseConfigBuilder, CONFIG_DOMAIN));
        } else if ("debug-overrides".equals(parser.getName())) {
            if (seenDebugOverrides) {
                throw new ParserException(parser, "Only one debug-overrides allowed");
            }
            if (mDebugBuild) {
                debugConfigBuilder =
                        parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
            seenDebugOverrides = true;
        } else {
            XmlUtils.skipCurrentTag(parser);
        }
    }
    // If debug is true and there was no debug-overrides in the file check for an extra
    // _debug resource.
    if (mDebugBuild && debugConfigBuilder == null) {
        debugConfigBuilder = parseDebugOverridesResource();
    }

    // Use the platform default as the parent of the base config for any values not provided
    // there. If there is no base config use the platform default.
    NetworkSecurityConfig.Builder platformDefaultBuilder =
            NetworkSecurityConfig.getDefaultBuilder(mTargetSdkVersion);
    addDebugAnchorsIfNeeded(debugConfigBuilder, platformDefaultBuilder);
    if (baseConfigBuilder != null) {
        baseConfigBuilder.setParent(platformDefaultBuilder);
        addDebugAnchorsIfNeeded(debugConfigBuilder, baseConfigBuilder);
    } else {
        baseConfigBuilder = platformDefaultBuilder;
    }
    // Build the per-domain config mapping.
    Set<Pair<Domain, NetworkSecurityConfig>> configs = new HashSet<>();

    for (Pair<NetworkSecurityConfig.Builder, Set<Domain>> entry : builders) {
        NetworkSecurityConfig.Builder builder = entry.first;
        Set<Domain> domains = entry.second;
        // Set the parent of configs that do not have a parent to the base-config. This can
        // happen if the base-config comes after a domain-config in the file.
        // Note that this is safe with regards to children because of the order that
        // parseConfigEntry returns builders, the parent is always before the children. The
        // children builders will not have build called until _after_ their parents have their
        // parent set so everything is consistent.
        if (builder.getParent() == null) {
            builder.setParent(baseConfigBuilder);
        }
        addDebugAnchorsIfNeeded(debugConfigBuilder, builder);
        NetworkSecurityConfig config = builder.build();
        for (Domain domain : domains) {
            configs.add(new Pair<>(domain, config));
        }
    }
    mDefaultConfig = baseConfigBuilder.build();
    mDomainMap = configs;
}
 
Example 10
Source File: XmlConfigSource.java    From cwac-netsecurity with Apache License 2.0 4 votes vote down vote up
private NetworkSecurityConfig.Builder parseDebugOverridesResource()
        throws IOException, XmlPullParserException, ParserException {
    Resources resources = mContext.getResources();
    String packageName = resources.getResourcePackageName(mResourceId);
    String entryName = resources.getResourceEntryName(mResourceId);
    int resId = resources.getIdentifier(entryName + "_debug", "xml", packageName);
    // No debug-overrides resource was found, nothing to parse.
    if (resId == 0) {
        return null;
    }
    NetworkSecurityConfig.Builder debugConfigBuilder = null;
    // Parse debug-overrides out of the _debug resource.
    XmlResourceParser parser=null;

    try {
        parser = resources.getXml(resId);

        XmlUtils.beginDocument(parser, "network-security-config");
        int outerDepth = parser.getDepth();
        boolean seenDebugOverrides = false;
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if ("debug-overrides".equals(parser.getName())) {
                if (seenDebugOverrides) {
                    throw new ParserException(parser, "Only one debug-overrides allowed");
                }
                if (mDebugBuild) {
                    debugConfigBuilder =
                            parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
                } else {
                    XmlUtils.skipCurrentTag(parser);
                }
                seenDebugOverrides = true;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }
    }
    finally {
        if (parser!=null) parser.close();
    }

    return debugConfigBuilder;
}