Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:(android): Background handling improvements #10451

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions apps/automated/src/ui/button/button-tests-native.android.ts
@@ -1,4 +1,5 @@
import { Color, Button, Utils, CoreTypes } from '@nativescript/core';
import { AndroidHelper } from '@nativescript/core/ui/core/view';

export function getNativeText(button: Button): string {
return button.android.getText();
Expand All @@ -19,15 +20,7 @@ export function getNativeColor(button: Button): Color {
}

export function getNativeBackgroundColor(button: Button): Color {
let bg = <any>button.android.getBackground();
if (bg instanceof org.nativescript.widgets.BorderDrawable) {
return new Color(bg.getBackgroundColor());
} else if (bg instanceof android.graphics.drawable.ColorDrawable) {
console.log(bg);
return new Color(bg.getColor());
} else {
return new Color(bg.backgroundColor);
}
return AndroidHelper.getDrawableColor(button.android.getBackground());
}

export function getNativeTextAlignment(button: Button): string {
Expand Down
15 changes: 4 additions & 11 deletions apps/automated/src/ui/label/label-tests-native.android.ts
@@ -1,6 +1,6 @@
import * as labelModule from '@nativescript/core/ui/label';
import { CoreTypes } from '@nativescript/core';
import * as colorModule from '@nativescript/core/color';
import { Color, CoreTypes } from '@nativescript/core';
import { AndroidHelper } from '@nativescript/core/ui/core/view';

export function getNativeTextAlignment(label: labelModule.Label): string {
let gravity = label.android.getGravity();
Expand All @@ -20,13 +20,6 @@ export function getNativeTextAlignment(label: labelModule.Label): string {
return 'unexpected value';
}

export function getNativeBackgroundColor(label: labelModule.Label): colorModule.Color {
let bg = <any>label.android.getBackground();
if (bg instanceof org.nativescript.widgets.BorderDrawable) {
return new colorModule.Color(bg.getBackgroundColor());
} else if (bg instanceof android.graphics.drawable.ColorDrawable) {
return new colorModule.Color(bg.getColor());
} else {
return new colorModule.Color(bg.backgroundColor);
}
export function getNativeBackgroundColor(label: labelModule.Label): Color {
return AndroidHelper.getDrawableColor(label.android.getBackground());
}
@@ -1,5 +1,6 @@
import * as segmentedBarModule from '@nativescript/core/ui/segmented-bar';
import { Color } from '@nativescript/core';
import { AndroidHelper } from '@nativescript/core/ui/core/view';

export function getNativeTabWidget(bar: segmentedBarModule.SegmentedBar): android.widget.TabWidget {
return (<android.widget.TabHost>bar.android).getTabWidget();
Expand Down Expand Up @@ -44,7 +45,7 @@ export var checkBackgroundColorUpdatedAfterItemSelected = function (bar: segment
const item = bar.items[i];
const textView = item?.nativeViewProtected;

const newDrawable = tryCloneDrawable(view.getBackground(), view.getResources());
const newDrawable = AndroidHelper.getCopyOrDrawable(view.getBackground(), view.getResources());
newDrawable.setColorFilter(new android.graphics.Paint(bar.selectedBackgroundColor.android).getColorFilter());

if (bar.selectedIndex == i) {
Expand All @@ -68,16 +69,5 @@ export var checkBackgroundColorUpdatedAfterItemSelected = function (bar: segment
}
}

function tryCloneDrawable(value: android.graphics.drawable.Drawable, resources: android.content.res.Resources): android.graphics.drawable.Drawable {
if (value) {
const constantState = value.getConstantState();
if (constantState) {
return constantState.newDrawable(resources);
}
}

return value;
}

return isValid === 0;
};
@@ -1,4 +1,5 @@
import { TextField, Color, Utils, CoreTypes } from '@nativescript/core';
import { AndroidHelper } from '@nativescript/core/ui/core/view';

export function getNativeText(textField: TextField): string {
return textField.android.getText().toString();
Expand Down Expand Up @@ -29,15 +30,7 @@ export function getNativePlaceholderColor(textField: TextField): Color {
}

export function getNativeBackgroundColor(textField: TextField): Color {
let bg = <any>textField.android.getBackground();
if (bg instanceof org.nativescript.widgets.BorderDrawable) {
return new Color(bg.getBackgroundColor());
} else if (bg instanceof android.graphics.drawable.ColorDrawable) {
console.log(bg);
return new Color(bg.getColor());
} else {
return new Color(bg.backgroundColor);
}
return AndroidHelper.getDrawableColor(textField.android.getBackground());
}

export function getNativeTextAlignment(textField: TextField): string {
Expand Down
@@ -1,4 +1,5 @@
import { TextView, Color, Utils, CoreTypes } from '@nativescript/core';
import { AndroidHelper } from '@nativescript/core/ui/core/view';

export function getNativeText(textView: TextView): string {
return textView.android.getText().toString();
Expand Down Expand Up @@ -27,14 +28,7 @@ export function getNativeColor(textView: TextView): Color {
}

export function getNativeBackgroundColor(textView: TextView): Color {
let bg = <any>textView.android.getBackground();
if (bg instanceof org.nativescript.widgets.BorderDrawable) {
return new Color(bg.getBackgroundColor());
} else if (bg instanceof android.graphics.drawable.ColorDrawable) {
return new Color(bg.getColor());
} else {
return new Color(bg.backgroundColor);
}
return AndroidHelper.getDrawableColor(textView.android.getBackground());
}

export function getNativeTextAlignment(textView: TextView): string {
Expand Down
29 changes: 2 additions & 27 deletions packages/core/ui/action-bar/index.android.ts
@@ -1,6 +1,6 @@
import { AndroidActionItemSettings, AndroidActionBarSettings as AndroidActionBarSettingsDefinition, ActionItem as ActionItemDefinition } from '.';
import { ActionItemBase, ActionBarBase, isVisible, flatProperty, traceMissingIcon, androidContentInsetLeftProperty, androidContentInsetRightProperty } from './action-bar-common';
import { View } from '../core/view';
import { AndroidHelper, View } from '../core/view';
import { Color } from '../../color';
import { layout, RESOURCE_PREFIX, isFontIconURI } from '../../utils';
import { colorProperty } from '../styling/style-properties';
Expand All @@ -9,6 +9,7 @@ import { Application } from '../../application';
import { isAccessibilityServiceEnabled, updateContentDescription } from '../../accessibility';
import type { Background } from '../styling/background';
import { SDK_VERSION } from '../../utils/constants';
import { NativeScriptAndroidView } from '../utils';

export * from './action-bar-common';

Expand Down Expand Up @@ -216,32 +217,6 @@ export class ActionBar extends ActionBarBase {
this._updateNavigationButton();
}

public _applyBackground(background: Background, isBorderDrawable, onlyColor: boolean, backgroundDrawable: any) {
const nativeView = this.nativeViewProtected;
if (backgroundDrawable && onlyColor && SDK_VERSION >= 21) {
if (isBorderDrawable && (<any>nativeView)._cachedDrawable) {
backgroundDrawable = (<any>nativeView)._cachedDrawable;
// we need to duplicate the drawable or we lose the "default" cached drawable
const constantState = backgroundDrawable.getConstantState();
if (constantState) {
try {
backgroundDrawable = constantState.newDrawable(nativeView.getResources());
// eslint-disable-next-line no-empty
} catch {}
}
nativeView.setBackground(backgroundDrawable);
}

const backgroundColor = ((<any>backgroundDrawable).backgroundColor = background.color.android);
backgroundDrawable.mutate();
backgroundDrawable.setColorFilter(backgroundColor, android.graphics.PorterDuff.Mode.SRC_IN);
backgroundDrawable.invalidateSelf(); // Make sure the drawable is invalidated. Android forgets to invalidate it in some cases: toolbar
(<any>backgroundDrawable).backgroundColor = backgroundColor;
} else {
super._applyBackground(background, isBorderDrawable, onlyColor, backgroundDrawable);
}
}

public _onAndroidItemSelected(itemId: number): boolean {
// Handle home button
if (this.navigationButton && itemId === R_ID_HOME) {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/ui/activity-indicator/index.android.ts
Expand Up @@ -2,6 +2,7 @@ import { ActivityIndicatorBase, busyProperty } from './activity-indicator-common
import { CoreTypes } from '../../core-types';
import { Color } from '../../color';
import { colorProperty, visibilityProperty } from '../styling/style-properties';
import { AndroidHelper } from '../core/view';

export * from './activity-indicator-common';

Expand Down Expand Up @@ -51,9 +52,9 @@ export class ActivityIndicator extends ActivityIndicatorBase {
const color = value instanceof Color ? value.android : value;
const drawable = this.nativeViewProtected.getIndeterminateDrawable().mutate();
if (color) {
drawable.setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
AndroidHelper.setDrawableColor(color, drawable);
} else {
drawable.clearColorFilter();
AndroidHelper.clearDrawableColor(drawable);
}
}
}
30 changes: 2 additions & 28 deletions packages/core/ui/button/index.android.ts
@@ -1,14 +1,14 @@
import { ButtonBase } from './button-common';
import { PseudoClassHandler } from '../core/view';
import { AndroidHelper, PseudoClassHandler } from '../core/view';
import { paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty, Length, zIndexProperty, minWidthProperty, minHeightProperty } from '../styling/style-properties';
import { textAlignmentProperty } from '../text-base';
import { CoreTypes } from '../../core-types';
import { profile } from '../../profiling';
import { TouchGestureEventData, GestureTypes, TouchAction } from '../gestures';
import { Device } from '../../platform';
import { SDK_VERSION } from '../../utils/constants';
import lazy from '../../utils/lazy';
import type { Background } from '../styling/background';
import { NativeScriptAndroidView } from '../utils';

export * from './button-common';

Expand Down Expand Up @@ -50,32 +50,6 @@ export class Button extends ButtonBase {
private _stateListAnimator: any;
private _highlightedHandler: (args: TouchGestureEventData) => void;

public _applyBackground(background: Background, isBorderDrawable, onlyColor: boolean, backgroundDrawable: any) {
const nativeView = this.nativeViewProtected;
if (backgroundDrawable && onlyColor) {
if (isBorderDrawable && (<any>nativeView)._cachedDrawable) {
backgroundDrawable = (<any>nativeView)._cachedDrawable;
// we need to duplicate the drawable or we lose the "default" cached drawable
const constantState = backgroundDrawable.getConstantState();
if (constantState) {
try {
backgroundDrawable = constantState.newDrawable(nativeView.getResources());
// eslint-disable-next-line no-empty
} catch {}
}
nativeView.setBackground(backgroundDrawable);
}

const backgroundColor = ((<any>backgroundDrawable).backgroundColor = background.color.android);
backgroundDrawable.mutate();
backgroundDrawable.setColorFilter(backgroundColor, android.graphics.PorterDuff.Mode.SRC_IN);
backgroundDrawable.invalidateSelf(); // Make sure the drawable is invalidated. Android forgets to invalidate it in some cases: toolbar
(<any>backgroundDrawable).backgroundColor = backgroundColor;
} else {
super._applyBackground(background, isBorderDrawable, onlyColor, backgroundDrawable);
}
}

@profile
public createNativeView() {
if (!AndroidButton) {
Expand Down