Search in sources :

Example 6 with ScreenSize

use of com.android.resources.ScreenSize in project android_frameworks_base by ParanoidAndroid.

the class RenderAction method getConfiguration.

private Configuration getConfiguration() {
    Configuration config = new Configuration();
    HardwareConfig hardwareConfig = mParams.getHardwareConfig();
    ScreenSize screenSize = hardwareConfig.getScreenSize();
    if (screenSize != null) {
        switch(screenSize) {
            case SMALL:
                config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_SMALL;
                break;
            case NORMAL:
                config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_NORMAL;
                break;
            case LARGE:
                config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_LARGE;
                break;
            case XLARGE:
                config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_XLARGE;
                break;
        }
    }
    Density density = hardwareConfig.getDensity();
    if (density == null) {
        density = Density.MEDIUM;
    }
    config.screenWidthDp = hardwareConfig.getScreenWidth() / density.getDpiValue();
    config.screenHeightDp = hardwareConfig.getScreenHeight() / density.getDpiValue();
    if (config.screenHeightDp < config.screenWidthDp) {
        config.smallestScreenWidthDp = config.screenHeightDp;
    } else {
        config.smallestScreenWidthDp = config.screenWidthDp;
    }
    config.densityDpi = density.getDpiValue();
    // never run in compat mode:
    config.compatScreenWidthDp = config.screenWidthDp;
    config.compatScreenHeightDp = config.screenHeightDp;
    ScreenOrientation orientation = hardwareConfig.getOrientation();
    if (orientation != null) {
        switch(orientation) {
            case PORTRAIT:
                config.orientation = Configuration.ORIENTATION_PORTRAIT;
                break;
            case LANDSCAPE:
                config.orientation = Configuration.ORIENTATION_LANDSCAPE;
                break;
            case SQUARE:
                config.orientation = Configuration.ORIENTATION_SQUARE;
                break;
        }
    } else {
        config.orientation = Configuration.ORIENTATION_UNDEFINED;
    }
    return config;
}
Also used : ScreenOrientation(com.android.resources.ScreenOrientation) HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) Configuration(android.content.res.Configuration) ScreenSize(com.android.resources.ScreenSize) Density(com.android.resources.Density)

Example 7 with ScreenSize

use of com.android.resources.ScreenSize in project android_frameworks_base by AOSPA.

the class RenderAction method getConfiguration.

// VisibleForTesting
public static Configuration getConfiguration(RenderParams params) {
    Configuration config = new Configuration();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    ScreenSize screenSize = hardwareConfig.getScreenSize();
    if (screenSize != null) {
        switch(screenSize) {
            case SMALL:
                config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_SMALL;
                break;
            case NORMAL:
                config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_NORMAL;
                break;
            case LARGE:
                config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_LARGE;
                break;
            case XLARGE:
                config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_XLARGE;
                break;
        }
    }
    Density density = hardwareConfig.getDensity();
    if (density == null) {
        density = Density.MEDIUM;
    }
    config.screenWidthDp = hardwareConfig.getScreenWidth() / density.getDpiValue();
    config.screenHeightDp = hardwareConfig.getScreenHeight() / density.getDpiValue();
    if (config.screenHeightDp < config.screenWidthDp) {
        //noinspection SuspiciousNameCombination
        config.smallestScreenWidthDp = config.screenHeightDp;
    } else {
        config.smallestScreenWidthDp = config.screenWidthDp;
    }
    config.densityDpi = density.getDpiValue();
    // never run in compat mode:
    config.compatScreenWidthDp = config.screenWidthDp;
    config.compatScreenHeightDp = config.screenHeightDp;
    ScreenOrientation orientation = hardwareConfig.getOrientation();
    if (orientation != null) {
        switch(orientation) {
            case PORTRAIT:
                config.orientation = Configuration.ORIENTATION_PORTRAIT;
                break;
            case LANDSCAPE:
                config.orientation = Configuration.ORIENTATION_LANDSCAPE;
                break;
            case SQUARE:
                //noinspection deprecation
                config.orientation = Configuration.ORIENTATION_SQUARE;
                break;
        }
    } else {
        config.orientation = Configuration.ORIENTATION_UNDEFINED;
    }
    ScreenRound roundness = hardwareConfig.getScreenRoundness();
    if (roundness != null) {
        switch(roundness) {
            case ROUND:
                config.screenLayout |= Configuration.SCREENLAYOUT_ROUND_YES;
                break;
            case NOTROUND:
                config.screenLayout |= Configuration.SCREENLAYOUT_ROUND_NO;
        }
    } else {
        config.screenLayout |= Configuration.SCREENLAYOUT_ROUND_UNDEFINED;
    }
    String locale = params.getLocale();
    if (locale != null && !locale.isEmpty())
        config.locale = new Locale(locale);
    return config;
}
Also used : ScreenOrientation(com.android.resources.ScreenOrientation) Locale(java.util.Locale) HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) Configuration(android.content.res.Configuration) ScreenSize(com.android.resources.ScreenSize) Density(com.android.resources.Density) ScreenRound(com.android.resources.ScreenRound)

Example 8 with ScreenSize

use of com.android.resources.ScreenSize in project android by JetBrains.

the class DeviceDefinitionPreview method paintComponent.

@Override
protected void paintComponent(Graphics g) {
    GraphicsUtil.setupAntialiasing(g);
    GraphicsUtil.setupAAPainting(g);
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(JBColor.background());
    g2d.fillRect(0, 0, getWidth(), getHeight());
    g2d.setColor(JBColor.foreground());
    g2d.setFont(STANDARD_FONT);
    if (myDeviceData.name().get().equals(DO_NOT_DISPLAY)) {
        FontMetrics metrics = g2d.getFontMetrics();
        g2d.drawString(NO_DEVICE_SELECTED, (getWidth() - metrics.stringWidth(NO_DEVICE_SELECTED)) / 2, (getHeight() - metrics.getHeight()) / 2);
        return;
    }
    boolean isCircular = myDeviceData.isWear().get() && myDeviceData.isScreenRound().get();
    // Paint our icon
    Icon icon = getIcon(myDeviceData);
    icon.paintIcon(this, g, PADDING / 2, PADDING / 2);
    // Paint the device name
    g2d.setFont(TITLE_FONT);
    FontMetrics metrics = g.getFontMetrics(TITLE_FONT);
    g2d.drawString(myDeviceData.name().get(), JBUI.scale(50), PADDING + metrics.getHeight() / 2);
    g2d.drawLine(0, JBUI.scale(50), getWidth(), JBUI.scale(50));
    // Paint the device outline with dimensions labelled
    Dimension screenSize = getScaledDimension();
    Dimension pixelScreenSize = myDeviceData.getDeviceScreenDimension();
    if (screenSize != null) {
        if (screenSize.getHeight() <= 0) {
            screenSize.height = 1;
        }
        if (screenSize.getWidth() <= 0) {
            screenSize.width = 1;
        }
        RoundRectangle2D roundRect = new RoundRectangle2D.Double(PADDING, JBUI.scale(100), screenSize.width, screenSize.height, JBUI.scale(10), JBUI.scale(10));
        g2d.setStroke(new BasicStroke(DIMENSION_LINE_WIDTH));
        g2d.setColor(OUR_GRAY);
        g2d.setFont(FIGURE_FONT);
        metrics = g2d.getFontMetrics(FIGURE_FONT);
        int stringHeight = metrics.getHeight() - metrics.getDescent();
        // Paint the width dimension
        String widthString = Integer.toString(pixelScreenSize.width) + "px";
        int widthLineY = JBUI.scale(95) - (metrics.getHeight() - metrics.getDescent()) / 2;
        g2d.drawLine(PADDING, widthLineY, round(PADDING + screenSize.width), widthLineY);
        // Erase the part of the line that the text overlays
        g2d.setColor(JBColor.background());
        int widthStringWidth = metrics.stringWidth(widthString);
        int widthTextX = round(PADDING + (screenSize.width - widthStringWidth) / 2);
        g2d.drawLine(widthTextX - FIGURE_PADDING, widthLineY, widthTextX + widthStringWidth + FIGURE_PADDING, widthLineY);
        // Paint the width text
        g2d.setColor(JBColor.foreground());
        g2d.drawString(widthString, widthTextX, JBUI.scale(95));
        // Paint the height dimension
        g2d.setColor(OUR_GRAY);
        String heightString = Integer.toString(pixelScreenSize.height) + "px";
        int heightLineX = round(PADDING + screenSize.width + JBUI.scale(15));
        g2d.drawLine(heightLineX, JBUI.scale(100), heightLineX, round(JBUI.scale(100) + screenSize.height));
        // Erase the part of the line that the text overlays
        g2d.setColor(JBColor.background());
        int heightTextY = round(JBUI.scale(100) + (screenSize.height + stringHeight) / 2);
        g2d.drawLine(heightLineX, heightTextY + FIGURE_PADDING, heightLineX, heightTextY - stringHeight - FIGURE_PADDING);
        // Paint the height text
        g2d.setColor(JBColor.foreground());
        g2d.drawString(heightString, heightLineX - JBUI.scale(10), heightTextY);
        // Paint the diagonal dimension
        g2d.setColor(OUR_GRAY);
        String diagString = FORMAT.format(myDeviceData.diagonalScreenSize().get());
        int diagTextX = round(PADDING + (screenSize.width - metrics.stringWidth(diagString)) / 2);
        int diagTextY = round(JBUI.scale(100) + (screenSize.height + stringHeight) / 2);
        double chin = (double) myDeviceData.screenChinSize().get();
        chin *= screenSize.getWidth() / myDeviceData.getDeviceScreenDimension().getWidth();
        Line2D diagLine = new Line2D.Double(PADDING, JBUI.scale(100) + screenSize.height + chin, PADDING + screenSize.width, JBUI.scale(100));
        if (isCircular) {
            // Move the endpoints of the line to within the circle. Each endpoint must move towards the center axis of the circle by
            // 0.5 * (l - l/sqrt(2)) where l is the diameter of the circle.
            double dist = 0.5 * (screenSize.width - screenSize.width / Math.sqrt(2));
            diagLine.setLine(diagLine.getX1() + dist, diagLine.getY1() - dist, diagLine.getX2() - dist, diagLine.getY2() + dist);
        }
        g2d.draw(diagLine);
        // Erase the part of the line that the text overlays
        g2d.setColor(JBColor.background());
        Rectangle erasureRect = new Rectangle(diagTextX - FIGURE_PADDING, diagTextY - stringHeight - FIGURE_PADDING, metrics.stringWidth(diagString) + FIGURE_PADDING * 2, stringHeight + FIGURE_PADDING * 2);
        g2d.fill(erasureRect);
        // Paint the diagonal text
        g2d.setColor(JBColor.foreground());
        g2d.drawString(diagString, diagTextX, diagTextY);
        // Finally, paint the outline
        g2d.setStroke(new BasicStroke(OUTLINE_LINE_WIDTH));
        g2d.setColor(JBColor.foreground());
        if (isCircular) {
            double x = roundRect.getX();
            double y = roundRect.getY();
            Ellipse2D circle = new Ellipse2D.Double(x, y, screenSize.width, screenSize.height + chin);
            g2d.draw(circle);
            if (chin > 0) {
                erasureRect = new Rectangle((int) x, (int) (y + screenSize.height + OUTLINE_LINE_WIDTH / 2 + 1), screenSize.width, (int) chin + OUTLINE_LINE_WIDTH / 2 + 1);
                g2d.setColor(JBColor.background());
                g2d.fill(erasureRect);
                g2d.setColor(JBColor.foreground());
                double halfChinWidth = Math.sqrt(chin * (screenSize.width - chin)) - OUTLINE_LINE_WIDTH / 2;
                int chinX = (int) (x + screenSize.width / 2 - halfChinWidth);
                g2d.drawLine(chinX, (int) (y + screenSize.height), (int) (chinX + halfChinWidth * 2), (int) (y + screenSize.height));
            }
        } else {
            g2d.draw(roundRect);
        }
        // Paint the details. If it's a portrait phone, then paint to the right of the rect.
        // If it's a landscape tablet/tv, paint below.
        g2d.setFont(STANDARD_FONT);
        metrics = g2d.getFontMetrics(STANDARD_FONT);
        stringHeight = metrics.getHeight();
        int infoSegmentX;
        int infoSegmentY;
        if (myDeviceData.getDefaultDeviceOrientation().equals(ScreenOrientation.PORTRAIT)) {
            infoSegmentX = round(PADDING + screenSize.width + metrics.stringWidth(heightString) + PADDING);
            infoSegmentY = JBUI.scale(100);
        } else {
            infoSegmentX = PADDING;
            infoSegmentY = round(JBUI.scale(100) + screenSize.height + PADDING);
        }
        infoSegmentY += stringHeight;
        ScreenSize size = AvdScreenData.getScreenSize(myDeviceData.diagonalScreenSize().get());
        g2d.drawString("Size:      " + size.getResourceValue(), infoSegmentX, infoSegmentY);
        infoSegmentY += stringHeight;
        ScreenRatio ratio = AvdScreenData.getScreenRatio(myDeviceData.screenResolutionWidth().get(), myDeviceData.screenResolutionHeight().get());
        g2d.drawString("Ratio:    " + ratio.getResourceValue(), infoSegmentX, infoSegmentY);
        infoSegmentY += stringHeight;
        Density pixelDensity = myDeviceData.density().get();
        if (pixelDensity == Density.NODPI) {
            // We need to calculate the density
            pixelDensity = AvdScreenData.getScreenDensity(myDeviceData.isTv().get(), myDeviceData.screenDpi().get(), myDeviceData.screenResolutionHeight().get());
        }
        g2d.drawString("Density: " + pixelDensity.getResourceValue(), infoSegmentX, infoSegmentY);
    }
}
Also used : RoundRectangle2D(java.awt.geom.RoundRectangle2D) ScreenRatio(com.android.resources.ScreenRatio) Line2D(java.awt.geom.Line2D) Density(com.android.resources.Density) Ellipse2D(java.awt.geom.Ellipse2D) ScreenSize(com.android.resources.ScreenSize)

Aggregations

Density (com.android.resources.Density)8 ScreenSize (com.android.resources.ScreenSize)8 Configuration (android.content.res.Configuration)6 HardwareConfig (com.android.ide.common.rendering.api.HardwareConfig)6 ScreenOrientation (com.android.resources.ScreenOrientation)6 ScreenRound (com.android.resources.ScreenRound)5 Locale (java.util.Locale)5 ScreenRatio (com.android.resources.ScreenRatio)1 Storage (com.android.sdklib.devices.Storage)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Line2D (java.awt.geom.Line2D)1 RoundRectangle2D (java.awt.geom.RoundRectangle2D)1 NotNull (org.jetbrains.annotations.NotNull)1