Skip to content

Commit

Permalink
fix(android): null check in DisplayCapsProxy (#13422)
Browse files Browse the repository at this point in the history
* fix(android): null check in DispalyCapsProxy

* add note to apidocs

* beautify
  • Loading branch information
m1ga committed Jul 9, 2022
1 parent e5c8391 commit d60baec
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public DisplayCapsProxy()

private Display getDisplay()
{
if (TiApplication.getAppRootOrCurrentActivity() == null) {
return null;
}
if (softDisplay == null || softDisplay.get() == null) {
// we only need the window manager so it doesn't matter if the root or current activity is used
// for accessing it
Expand All @@ -43,8 +46,12 @@ public int getPlatformWidth()
{
synchronized (dm)
{
getDisplay().getMetrics(dm);
return dm.widthPixels;
if (getDisplay() != null) {
getDisplay().getMetrics(dm);
return dm.widthPixels;
} else {
return 0;
}
}
}

Expand All @@ -53,8 +60,12 @@ public int getPlatformHeight()
{
synchronized (dm)
{
getDisplay().getMetrics(dm);
return dm.heightPixels;
if (getDisplay() != null) {
getDisplay().getMetrics(dm);
return dm.heightPixels;
} else {
return 0;
}
}
}

Expand All @@ -63,22 +74,26 @@ public String getDensity()
{
synchronized (dm)
{
getDisplay().getMetrics(dm);
int dpi = dm.densityDpi;
if (dpi >= DisplayMetrics.DENSITY_560) {
return "xxxhigh";
} else if (dpi >= DisplayMetrics.DENSITY_400) {
return "xxhigh";
} else if (dpi >= DisplayMetrics.DENSITY_280) {
return "xhigh";
} else if (dpi >= DisplayMetrics.DENSITY_HIGH) {
return "high";
} else if (dpi >= DisplayMetrics.DENSITY_TV) {
return "tvdpi";
} else if (dpi >= DisplayMetrics.DENSITY_MEDIUM) {
return "medium";
if (getDisplay() != null) {
getDisplay().getMetrics(dm);
int dpi = dm.densityDpi;
if (dpi >= DisplayMetrics.DENSITY_560) {
return "xxxhigh";
} else if (dpi >= DisplayMetrics.DENSITY_400) {
return "xxhigh";
} else if (dpi >= DisplayMetrics.DENSITY_280) {
return "xhigh";
} else if (dpi >= DisplayMetrics.DENSITY_HIGH) {
return "high";
} else if (dpi >= DisplayMetrics.DENSITY_TV) {
return "tvdpi";
} else if (dpi >= DisplayMetrics.DENSITY_MEDIUM) {
return "medium";
}
return "low";
} else {
return "low";
}
return "low";
}
}

Expand All @@ -87,8 +102,12 @@ public float getDpi()
{
synchronized (dm)
{
getDisplay().getMetrics(dm);
return dm.densityDpi;
if (getDisplay() != null) {
getDisplay().getMetrics(dm);
return dm.densityDpi;
} else {
return 0;
}
}
}

Expand All @@ -97,8 +116,12 @@ public float getXdpi()
{
synchronized (dm)
{
getDisplay().getMetrics(dm);
return dm.xdpi;
if (getDisplay() != null) {
getDisplay().getMetrics(dm);
return dm.xdpi;
} else {
return 0;
}
}
}

Expand All @@ -107,8 +130,12 @@ public float getYdpi()
{
synchronized (dm)
{
getDisplay().getMetrics(dm);
return dm.ydpi;
if (getDisplay() != null) {
getDisplay().getMetrics(dm);
return dm.ydpi;
} else {
return 0;
}
}
}

Expand All @@ -117,8 +144,12 @@ public float getLogicalDensityFactor()
{
synchronized (dm)
{
getDisplay().getMetrics(dm);
return dm.density;
if (getDisplay() != null) {
getDisplay().getMetrics(dm);
return dm.density;
} else {
return 0;
}
}
}

Expand Down
107 changes: 55 additions & 52 deletions apidoc/Titanium/Platform/DisplayCaps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ name: Titanium.Platform.DisplayCaps
summary: |
The Display Caps object returned by the <Titanium.Platform.displayCaps> property.
description: |
On iPhone and iPod devices with retina display, the `density` property is `high` and the `dpi`
On iPhone and iPod devices with retina display, the `density` property is `high` and the `dpi`
property is `320`. For other iPhone and iPod devices, `density` is `medium` and `dpi` is `160`.
On iPad devices with retina display, the `density` property is `high` and the `dpi`
On iPad devices with retina display, the `density` property is `high` and the `dpi`
property is `260`. For other iPad devices, `density` is `medium` and `dpi` is `130`.
Note that Xcode versions prior to 4.3.1 do not have the correct tools to detect the iPad 3
Note that Xcode versions prior to 4.3.1 do not have the correct tools to detect the iPad 3
retina display. Apps must be built with Xcode 4.3.1 or later to detect iPad 3 retina display.
Note that the `displayCaps` property begins with a lowercase letter, which differentiates it
from the `DisplayCaps` object that it returns. Refer to the example to understand how it should
Note that the `displayCaps` property begins with a lowercase letter, which differentiates it
from the `DisplayCaps` object that it returns. Refer to the example to understand how it should
be called.
On Android you have to make sure that the Activity is created before you fetch the displayCaps
values. Use the window `onOpen` event or `win.activity.onCreate = () => {};`.
extends: Titanium.Proxy
since: "0.8"
createable: false
Expand All @@ -23,25 +26,25 @@ properties:
summary: Logical density of the display.
type: String
permission: read-only

- name: dpi
summary: Display density expressed as dots-per-inch.
type: Number
permission: read-only

- name: logicalDensityFactor
summary: |
Logical density of the display, as a scaling factor for the Density Independent Pixel (dip)
Logical density of the display, as a scaling factor for the Density Independent Pixel (dip)
unit.
description: |
One dip is one pixel on a 160dpi display, approximately, with a 240x320, 1.5"x2" display
providing a baseline. For example, for a 160dpi display, this value will be 1, and for 120dpi,
providing a baseline. For example, for a 160dpi display, this value will be 1, and for 120dpi,
it will be .75.
This value does not precisely follow the real display size, as given by `xdpi` and `ydpi`,
but instead is used to scale the size of the overall UI in steps based on changes in the
display dpi. For example, a 240x320 screen will have a density of 1, whether its width is
1.8" or 1.3". However, if the resolution is increased to 320x480 but the display remains
This value does not precisely follow the real display size, as given by `xdpi` and `ydpi`,
but instead is used to scale the size of the overall UI in steps based on changes in the
display dpi. For example, a 240x320 screen will have a density of 1, whether its width is
1.8" or 1.3". However, if the resolution is increased to 320x480 but the display remains
1.5"x2" then the density would be increased to about 1.5.
On iOS devices, this property returns 1, 2 and 3 for &#64;1x, &#64;2x and &#64;3x respectively.
Expand All @@ -53,23 +56,23 @@ properties:

- name: platformHeight
summary: |
Absolute height of the display in relation to UI orientation. Measured in platform-specific
Absolute height of the display in relation to UI orientation. Measured in platform-specific
units; pixels on Android and density-independent pixels (dip) on iOS.
description: |
This property depends on the orientation of the UI, rather than the physical orientation of
the device. While these may often be one in the same, it is not necessarily the case when
orientation is restricted by <Titanium.UI.Window.orientationModes>. See example for
This property depends on the orientation of the UI, rather than the physical orientation of
the device. While these may often be one in the same, it is not necessarily the case when
orientation is restricted by <Titanium.UI.Window.orientationModes>. See example for
clarification.
type: Number
permission: read-only
examples:
- title: Platform Width and Height
example: |
Create a window and limit it to portrait mode. Start the app and observe that the platform
width and height is output to the log. Change the physical orientation of the device and
click the window to test again. Note that the logged information relates to the UI rather
Create a window and limit it to portrait mode. Start the app and observe that the platform
width and height is output to the log. Change the physical orientation of the device and
click the window to test again. Note that the logged information relates to the UI rather
than the physical orientation of the device.
``` js
var win = Ti.UI.createWindow({
title: 'Click window to output UI dimensions',
Expand All @@ -78,47 +81,47 @@ properties:
fullscreen: false,
exitOnClose: true
});
var label = Ti.UI.createLabel({
text:'Hello world'
});
win.add(label);
function logDisplayCaps(){
Ti.API.info('Width x Height: ' + Ti.Platform.displayCaps.platformWidth + ' x ' + Ti.Platform.displayCaps.platformHeight);
}
win.addEventListener('click',function(e){
// as the UI orientation is restricted by orientationModes, this will only ever
// output the dimensions of the UI in portrait mode. For example, 480 x 800 for the
// as the UI orientation is restricted by orientationModes, this will only ever
// output the dimensions of the UI in portrait mode. For example, 480 x 800 for the
// Android device tested
logDisplayCaps();
});
win.open();
logDisplayCaps();
```
- name: platformWidth
summary: |
Absolute width of the display in relation to UI orientation. Measured in platform-specific
Absolute width of the display in relation to UI orientation. Measured in platform-specific
units; pixels on Android and density-independent pixels (dip) on iOS.
description: |
This property depends on the orientation of the UI, rather than the physical orientation of
the device. While these may often be one in the same, it is not necessarily the case when
orientation is restricted by <Titanium.UI.Window.orientationModes>. See example for
This property depends on the orientation of the UI, rather than the physical orientation of
the device. While these may often be one in the same, it is not necessarily the case when
orientation is restricted by <Titanium.UI.Window.orientationModes>. See example for
clarification.
type: Number
permission: read-only
examples:
- title: Platform Width and Height
example: |
Create a window and limit it to portrait mode. Start the app and observe that the platform
width and height is output to the log. Change the physical orientation of the device and
click the window to test again. Note that the logged information relates to the UI rather
Create a window and limit it to portrait mode. Start the app and observe that the platform
width and height is output to the log. Change the physical orientation of the device and
click the window to test again. Note that the logged information relates to the UI rather
than the physical orientation of the device.
``` js
var win = Ti.UI.createWindow({
title: 'Click window to output UI dimensions',
Expand All @@ -127,46 +130,46 @@ properties:
fullscreen: false,
exitOnClose: true
});
var label = Ti.UI.createLabel({
text:'Hello world'
});
win.add(label);
function logDisplayCaps(){
Ti.API.info('Width x Height: ' + Ti.Platform.displayCaps.platformWidth + ' x ' + Ti.Platform.displayCaps.platformHeight);
}
win.addEventListener('click',function(e){
// as the UI orientation is restricted by orientationModes, this will only ever
// output the dimensions of the UI in portrait mode. For example, 480 x 800 for the
// as the UI orientation is restricted by orientationModes, this will only ever
// output the dimensions of the UI in portrait mode. For example, 480 x 800 for the
// Android device tested
logDisplayCaps();
});
win.open();
logDisplayCaps();
logDisplayCaps();
```
- name: xdpi
summary: Physical pixels per inch of the display in the X dimension.
type: Number
platforms: [android]
permission: read-only

- name: ydpi
summary: Physical pixels per inch of the display in the Y dimension.
type: Number
platforms: [android]
permission: read-only

examples:
- title: System Display Information
example: |
Output the display properties to the system log.
``` js
``` js
Ti.API.info('Ti.Platform.displayCaps.density: ' + Ti.Platform.displayCaps.density);
Ti.API.info('Ti.Platform.displayCaps.dpi: ' + Ti.Platform.displayCaps.dpi);
Ti.API.info('Ti.Platform.displayCaps.platformHeight: ' + Ti.Platform.displayCaps.platformHeight);
Expand All @@ -178,4 +181,4 @@ examples:
Ti.API.info('Ti.Platform.displayCaps.xdpi: ' + Ti.Platform.displayCaps.xdpi);
Ti.API.info('Ti.Platform.displayCaps.ydpi: ' + Ti.Platform.displayCaps.ydpi);
}
```
```

0 comments on commit d60baec

Please sign in to comment.