Skip to content

Commit

Permalink
feat(android): statusBarHeight parity and cutoutSize (#13733)
Browse files Browse the repository at this point in the history
* test

* feat(android): statusBarHeight parity and cutoutSize

* remove empty line

* apidocs

* missing gitignore
  • Loading branch information
m1ga committed Jan 28, 2023
1 parent 22d26f8 commit 82e07f7
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
64 changes: 64 additions & 0 deletions android/modules/ui/src/java/ti/modules/titanium/ui/UIModule.java
Expand Up @@ -26,16 +26,22 @@
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.InputType;
import android.text.util.Linkify;
import android.view.DisplayCutout;
import android.view.View;
import android.view.Window;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;

import java.util.List;

@Kroll.module
public class UIModule extends KrollModule implements TiApplication.ConfigurationChangedListener
{
Expand Down Expand Up @@ -571,6 +577,64 @@ public int getUserInterfaceStyle()
return getUserInterfaceStyle(TiApplication.getInstance().getResources().getConfiguration());
}

@Kroll.getProperty
public int statusBarHeight()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Window w = TiApplication.getAppCurrentActivity().getWindow();
if (w == null) return 0;
View dv = w.getDecorView();
if (dv == null) return 0;
if (dv.getRootWindowInsets() == null) return 0;
DisplayCutout dpc = dv.getRootWindowInsets().getDisplayCutout();
if (dpc == null) return 0;
List<Rect> rects = dpc.getBoundingRects();
if (rects.size() > 0) {
int h = dpc.getBoundingRects().get(0).height();
return (int) new TiDimension(h, TiDimension.TYPE_HEIGHT).getAsDefault(dv);
}
}
return 0;
}

@Kroll.getProperty
public KrollDict getCutoutSize()
{
KrollDict returnValue = new KrollDict();
returnValue.put("top", 0);
returnValue.put("left", 0);
returnValue.put("height", 0);
returnValue.put("width", 0);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Window w = TiApplication.getAppCurrentActivity().getWindow();
if (w == null) return returnValue;
View dv = w.getDecorView();
if (dv == null) return returnValue;
if (dv.getRootWindowInsets() == null) return returnValue;
DisplayCutout dpc = dv.getRootWindowInsets().getDisplayCutout();
if (dpc == null) return returnValue;
List<Rect> rects = dpc.getBoundingRects();

int cutouts = rects.size();
int[] result = new int[cutouts * 4];
int index = 0;

if (rects.size() > 0) {
int dh = dpc.getBoundingRects().get(0).height();
int dw = dpc.getBoundingRects().get(0).width();
int dt = dpc.getBoundingRects().get(0).top;
int dl = dpc.getBoundingRects().get(0).left;

returnValue.put("left", (int) new TiDimension(dl, TiDimension.TYPE_LEFT).getAsDefault(dv));
returnValue.put("top", (int) new TiDimension(dt, TiDimension.TYPE_TOP).getAsDefault(dv));
returnValue.put("width", (int) new TiDimension(dw, TiDimension.TYPE_WIDTH).getAsDefault(dv));
returnValue.put("height", (int) new TiDimension(dh, TiDimension.TYPE_HEIGHT).getAsDefault(dv));
}
}
return returnValue;
}

@Override
public String getApiName()
{
Expand Down
31 changes: 31 additions & 0 deletions apidoc/Titanium/UI/UI.yml
Expand Up @@ -2807,6 +2807,15 @@ properties:
osver: {ios: {min: "13.0"}}
since: "9.1.0"

- name: cutoutSize
summary: Returns the position and shape of the Android notch. Read more about the notch [here](https://developer.android.com/develop/ui/views/layout/display-cutout).
description: |
Using Android 9+ it will return the bounding box of the Android notch. It will return `top`,`left, `width` and `height`.
type: CutoutSize
permission: read-only
platforms: [android]
since: "12.1.0"

- name: statusBarHeight
summary: The total height of the status bar including safe area padding.
description: |
Expand Down Expand Up @@ -2856,3 +2865,25 @@ examples:
win.add(table);
win.open();
```
---
name: CutoutSize
summary: Dictionary object of parameters for the <Titanium.UI.cutoutSize>.
since: "12.1.0"
platforms: [android]
properties:
- name: left
type: Number
summary: The left margin of the cutout.

- name: right
type: Number
summary: The left margin of the cutout.

- name: width
type: Number
summary: The width of the cutout

- name: height
type: Number
summary: The height of the cutout

0 comments on commit 82e07f7

Please sign in to comment.