How to calculate Android screen aspect ratio mathematically

android, aspect-ratio, screen

Solution

...property seems to have only two values - long and notlong. I am trying to reverse engineer the logic being used by Android to classify a device as having one of the two aspect ratios.

For the record, there's no need to reverse engineer, just see https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/content/res/Configuration.java

        // Is this a long screen?
        if (((longSizeDp*3)/5) >= (shortSizeDp-1)) {
            // Anything wider than WVGA (5:3) is considering to be long.
            screenLayoutLong = true;
        } else {
            screenLayoutLong = false;
        }

So basically Android takes screen sizes in DP and the result is:

- long - screen with aspect ratio > 1.667 (5:3) - i.e. > WVGA

- notlong - screen with aspect ratio <= 1.667 (5:3) - i.e. <= WVGA

Example - Nexus 4 - 384 x 640 dp (5:3): long edge in dp: 640 short edge in dp: 384

Maths: 640 * 3 / 5 = 384 384 >= (384 - 1) -> false -> notlong

Problem

One of the device screen properties that Android lets an app query is it's aspect ratio. In the examples that I have seen this property seems to have only two values - long and notlong. I am trying to reverse engineer the logic being used by Android to classify a device as having one of the two aspect ratios. To get some official data to work with, I referred to the values provided by the device definitions included in the AVD Manager tool in Android Studio, and combined that with my own calculations: The column "Published Ratio" shows the value extracted from the AVD Manager. Based on these results, I am failing to understand how Nexus 5 and 6 are considered notlong while Galaxy S4 and Galaxy Nexus are considered long.

Original source