Android Question ExactSize does not work properly?

Filippo

Expert
Licensed User
Hi,

I use this code to determine if a device is suitable for my app.
The suitable devices should have at least a display of 5" inches.

A customer tells me that "Huawei p20 mate pro", with 6.4" inch display, is excluded from my app, how can it be?

B4X:
    If Not(mBBL.IsTablet) Then
       log("Device not compatible with the app")
   End If

Public Sub IsTablet As Boolean
    If ExactSize > 4.69 Then ' 4.69 = 5 Zoll ?
        Return True
    Else
        Return False
    End If
End Sub

Public Sub ExactSize As Double
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    r.Target = r.RunMethod("getDisplayMetrics")
    Dim xdpi As Double = r.GetField("xdpi")
    Dim ydpi As Double = r.GetField("ydpi")
    'Return Sqrt(Power(100%x / xdpi, 2) + Power(100%y / ydpi, 2))

    Dim lv As LayoutValues
    lv = GetDeviceLayoutValues
    Dim size As Double  = Sqrt(Power(lv.Width / xdpi, 2) + Power(lv.Height / ydpi, 2))
    'Log("ExactSize=" & size)
    Return size
End Sub
 

Filippo

Expert
Licensed User
I used to use the function "GetDeviceLayoutValues.ApproximateScreenSize", at some point I changed that function and it worked well with my test devices as well; no other customer has also complained, only now one with a "Huawei p20 mate pro" now reports a mistake.
Well, maybe I should use the function "GetDeviceLayoutValues.ApproximateScreenSize" again.
Thanks Erel
 

Filippo

Expert
Licensed User
Why does the exact size matter? If there is a minimum size for the layout then it makes more sense to use GetDeviceLayoutValues.ApproximateScreenSize.
The value you are measuring doesn't include the soft buttons bar. So it is a bit smaller. It is also possible that xdpi / ydpi doesn't return accurate values.
The 2 routines deliver 2 very different results, what is correct?
I am a little confused.

B4X:
Sub Activity_Resume
If Not(IsTablet) Then
       log("Device not compatible with the app")
End If
...
End Sub

Public Sub IsTablet As Boolean
    Log("ExactSize= " & ExactSize)
    Log("ApproximateScreenSize= " & ApproximateScreenSize)
    Log(" ")
    
    If ExactSize > 4.69 Then ' 4.69 = 5 Zoll ?
        '7'' or 10'' tablet
        Return True
    Else
        'phone
        Return False
    End If
End Sub

Private Sub ApproximateScreenSize As Double
    'Log("ApproximateScreenSize=" & lv.ApproximateScreenSize)
    Return GetDeviceLayoutValues.ApproximateScreenSize
End Sub

Public Sub ExactSize As Double
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    r.Target = r.RunMethod("getDisplayMetrics")
    Dim xdpi As Double = r.GetField("xdpi")
    Dim ydpi As Double = r.GetField("ydpi")
    'Return Sqrt(Power(100%x / xdpi, 2) + Power(100%y / ydpi, 2))

    Dim lv As LayoutValues
    lv = GetDeviceLayoutValues
    Dim size As Double  = Sqrt(Power(lv.Width / xdpi, 2) + Power(lv.Height / ydpi, 2))
    'Log("ExactSize=" & size)
    Return size
End Sub
Tested with Samsung S4 5" inch display.
** Activity (main) Resume **
ExactSize= 4.987339037643987
ApproximateScreenSize= 4.589389937671455
Tested with HTC-one-A9 5" inch display.
** Activity (main) Resume **
ExactSize= 4.6953648028813415
ApproximateScreenSize= 4.330415684434925
Tested with Samsung-A5(2017) 5.2" inch display.
** Activity (main) Resume **
ExactSize= 5.2137667559923155
ApproximateScreenSize= 4.589389937671455
Tested with Lenovo-Tab8 8" inch display.
** Activity (main) Resume **
ExactSize= 7.838617423472672
ApproximateScreenSize= 6.921163197035597
 

agraham

Expert
Licensed User
It seems LayoutValues.ApproximateScreenSize is calculated from LayoutValues.Width, LayoutValues.Height and LayoutValues.Scale
B4X:
   Math.sqrt(Math.pow(Width / Scale, 2) + Math.pow(Height / Scale, 2) / 160
I've tried this on all six of my Android devices and the Width and Height values appear to be accurate for the display area but the Scale value, which is supposed to be relative to 160dpi, appears to be an oddly rounded approximation. On a 6.9 inch Xiaomi Mi Max 3 the scale is reported as 2.75 whereas the actual scale is 2.19. This gives an ApproximateScreenSize of 5.22 which is obviously far too small. I can't see where the Scale values comes from but it seems to be totally unreliable as a real world value as on all six of my devices it is way off the actual dpi value of the screen. In one case nearly half of the actual value!

I've tried your ExactSize calculation on three of my devices and the dpis returned by getDisplayMetrics seem accurate and so therefore is the ExactSize calculation.

I guess that your customer's Huawei p20 mate pro must be either, as Erel suggested, be returning the wrong dpi values in getDisplayMetrics or the wrong width and height in GetDeviceLayoutValues.

Theoretically the most accurate way of getting the display size should be using the dpi, width and height values values from getDisplayMetrics which are documented to be the exact values.

B4X:
    Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   r.Target = r.RunMethod("getDisplayMetrics")
   Dim xdpi As Double = r.GetField("xdpi")
   Dim ydpi As Double = r.GetField("ydpi")
   Dim xpix As Int = r.GetField("widthPixels")
   Dim ypix As Int = r.GetField("heightPixels")
   Log("DisplayMetrics xdpi x ydpi = " & xdpi & " x " & ydpi)
   Log("DisplayMetrics width x height = " & xpix & " x " & ypix)
   Dim size As Double  = Sqrt(Power(xpix / xdpi, 2) + Power(ypix / ydpi, 2))
   Log("ExactSize = " & size)
 

Filippo

Expert
Licensed User
Hi @agraham ,

many thanks for the answer.

What would you do at my place now?
At the procedure change something or leave as is, with the error that customers with a "Huawei p20 mate pro" my app can not use?
 

agraham

Expert
Licensed User
I would try using just the values from getDisplayMetrics as in my last code fragment above. If you can't get hold of a Huawei P20 Mate Pro you could show a message box saying the app is not compatible and include the values from LayoutValues and DisplayMetrics so they could tell you what they are and you could see why it fails.
 

Filippo

Expert
Licensed User
I would try using just the values from getDisplayMetrics as in my last code fragment above. If you can't get hold of a Huawei P20 Mate Pro you could show a message box saying the app is not compatible and include the values from LayoutValues and DisplayMetrics so they could tell you what they are and you could see why it fails.
Yes, that would be a possibility.

You first need to explain the reason for checking this value.
The reason is actually simple, I did not want the layout to be used on devices with a display smaller than 5" inches.
But I have now tried using a 4.7" device and the layout does not look so bad.
I now remove this check from the app and all is fine.

Thanks again to all. :)
 
Top