perf(core): android sdk check (#10084)

This commit is contained in:
farfromrefuge
2022-11-12 18:09:45 +00:00
committed by GitHub
parent e687e9d756
commit abd722cd3d
22 changed files with 69 additions and 90 deletions

View File

@ -73,7 +73,7 @@ function ensureStateListener(): SharedA11YObservable {
}); });
accessibilityManager.addAccessibilityStateChangeListener(accessibilityStateChangeListener); accessibilityManager.addAccessibilityStateChangeListener(accessibilityStateChangeListener);
if (android.os.Build.VERSION.SDK_INT >= 19) { if (Utils.SDK_VERSION >= 19) {
touchExplorationStateChangeListener = new android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener({ touchExplorationStateChangeListener = new android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener({
onTouchExplorationStateChanged(enabled) { onTouchExplorationStateChanged(enabled) {
updateAccessibilityState(); updateAccessibilityState();

View File

@ -1,5 +1,6 @@
import * as Application from '../application'; import * as Application from '../application';
import { Trace } from '../trace'; import { Trace } from '../trace';
import { SDK_VERSION } from '../utils';
import type { View } from '../ui/core/view'; import type { View } from '../ui/core/view';
import { GestureTypes } from '../ui/gestures'; import { GestureTypes } from '../ui/gestures';
import { notifyAccessibilityFocusState } from './accessibility-common'; import { notifyAccessibilityFocusState } from './accessibility-common';
@ -52,7 +53,7 @@ function accessibilityEventHelper(view: Partial<View>, eventType: number) {
* Android API >= 26 handles accessibility tap-events by converting them to TYPE_VIEW_CLICKED * Android API >= 26 handles accessibility tap-events by converting them to TYPE_VIEW_CLICKED
* These aren't triggered for custom tap events in NativeScript. * These aren't triggered for custom tap events in NativeScript.
*/ */
if (android.os.Build.VERSION.SDK_INT >= 26) { if (SDK_VERSION >= 26) {
// Find all tap gestures and trigger them. // Find all tap gestures and trigger them.
for (const tapGesture of view.getGestureObservers(GestureTypes.tap) ?? []) { for (const tapGesture of view.getGestureObservers(GestureTypes.tap) ?? []) {
tapGesture.callback({ tapGesture.callback({
@ -169,7 +170,7 @@ function ensureNativeClasses() {
if (accessibilityRole) { if (accessibilityRole) {
const androidClassName = RoleTypeMap.get(accessibilityRole); const androidClassName = RoleTypeMap.get(accessibilityRole);
if (androidClassName) { if (androidClassName) {
const oldClassName = info.getClassName() || (android.os.Build.VERSION.SDK_INT >= 28 && host.getAccessibilityClassName()) || null; const oldClassName = info.getClassName() || (SDK_VERSION >= 28 && host.getAccessibilityClassName()) || null;
info.setClassName(androidClassName); info.setClassName(androidClassName);
if (Trace.isEnabled()) { if (Trace.isEnabled()) {
@ -189,7 +190,7 @@ function ensureNativeClasses() {
info.setClickable(true); info.setClickable(true);
} }
if (android.os.Build.VERSION.SDK_INT >= 28) { if (SDK_VERSION >= 28) {
if (accessibilityRole === AccessibilityRole.Header) { if (accessibilityRole === AccessibilityRole.Header) {
if (Trace.isEnabled()) { if (Trace.isEnabled()) {
Trace.write(`onInitializeAccessibilityNodeInfo ${view} - set heading role=${accessibilityRole}`, Trace.categories.Accessibility); Trace.write(`onInitializeAccessibilityNodeInfo ${view} - set heading role=${accessibilityRole}`, Trace.categories.Accessibility);

View File

@ -1,4 +1,5 @@
import { android as androidApp, getNativeApplication } from '../application'; import { android as androidApp, getNativeApplication } from '../application';
import { SDK_VERSION } from '../utils';
export enum connectionType { export enum connectionType {
none = 0, none = 0,
@ -69,7 +70,7 @@ function parseNetworkCapabilities(capabilities?: android.net.NetworkCapabilities
} }
export function getConnectionType(): number { export function getConnectionType(): number {
if (android.os.Build.VERSION.SDK_INT >= 28) { if (SDK_VERSION >= 28) {
return getNetworkCapabilities(); return getNetworkCapabilities();
} else { } else {
const activeNetworkInfo = getActiveNetworkInfo(); const activeNetworkInfo = getActiveNetworkInfo();
@ -117,7 +118,7 @@ let networkCallback;
let notifyCallback; let notifyCallback;
export function startMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void { export function startMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void {
if (android.os.Build.VERSION.SDK_INT >= 28) { if (SDK_VERSION >= 28) {
const manager = getConnectivityManager(); const manager = getConnectivityManager();
if (manager) { if (manager) {
notifyCallback = (network: android.net.Network, networkCapabilities: android.net.NetworkCapabilities) => { notifyCallback = (network: android.net.Network, networkCapabilities: android.net.NetworkCapabilities) => {
@ -162,7 +163,7 @@ export function startMonitoring(connectionTypeChangedCallback: (newConnectionTyp
} }
export function stopMonitoring(): void { export function stopMonitoring(): void {
if (android.os.Build.VERSION.SDK_INT >= 28) { if (SDK_VERSION >= 28) {
// @ts-ignore // @ts-ignore
const manager = getConnectivityManager(); const manager = getConnectivityManager();
if (manager && callback) { if (manager && callback) {

View File

@ -1,5 +1,6 @@
import { IFileSystemAccess, FileSystemAccess, FileSystemAccess29 } from './file-system-access'; import { IFileSystemAccess, FileSystemAccess, FileSystemAccess29 } from './file-system-access';
import { Device } from '../platform'; import { Device } from '../platform';
import { SDK_VERSION } from '../utils';
// The FileSystemAccess implementation, used through all the APIs. // The FileSystemAccess implementation, used through all the APIs.
let fileAccess: IFileSystemAccess; let fileAccess: IFileSystemAccess;
@ -9,7 +10,7 @@ let fileAccess: IFileSystemAccess;
*/ */
export function getFileAccess(): IFileSystemAccess { export function getFileAccess(): IFileSystemAccess {
if (!fileAccess) { if (!fileAccess) {
if (global.isAndroid && parseInt(Device.sdkVersion) >= 29) { if (global.isAndroid && SDK_VERSION >= 29) {
fileAccess = new FileSystemAccess29(); fileAccess = new FileSystemAccess29();
} else { } else {
fileAccess = new FileSystemAccess(); fileAccess = new FileSystemAccess();

View File

@ -1,19 +1,17 @@
import * as definition from './fps-native'; import * as definition from './fps-native';
import { Device } from '../platform'; import { SDK_VERSION } from '../utils';
export class FPSCallback implements definition.FPSCallback { export class FPSCallback implements definition.FPSCallback {
private impl: android.view.Choreographer.FrameCallback | ((nanos: number) => void); private impl: android.view.Choreographer.FrameCallback | ((nanos: number) => void);
private onFrame: (currentTimeMillis: number) => void; private onFrame: (currentTimeMillis: number) => void;
public running: boolean; public running: boolean;
sdkVersion: number;
nativeFramesSupported: boolean; nativeFramesSupported: boolean;
constructor(onFrame: (currentTimeMillis: number) => void) { constructor(onFrame: (currentTimeMillis: number) => void) {
this.running = false; this.running = false;
this.onFrame = onFrame; this.onFrame = onFrame;
this.sdkVersion = parseInt(Device.sdkVersion); this.nativeFramesSupported = SDK_VERSION >= 24 && this._isNativeFramesSupported();
this.nativeFramesSupported = this.sdkVersion >= 24 && this._isNativeFramesSupported();
if (this.nativeFramesSupported) { if (this.nativeFramesSupported) {
this.impl = (nanos: number) => { this.impl = (nanos: number) => {

View File

@ -1,5 +1,6 @@
/* tslint:disable:class-name */ /* tslint:disable:class-name */
import { getNativeApplication, on, orientationChangedEvent, android as AndroidApplication } from '../application'; import { getNativeApplication, on, orientationChangedEvent, android as AndroidApplication } from '../application';
import { SDK_VERSION } from '../utils';
const MIN_TABLET_PIXELS = 600; const MIN_TABLET_PIXELS = 600;
@ -129,7 +130,7 @@ class DeviceRef {
get language(): string { get language(): string {
let defaultNativeLocale; let defaultNativeLocale;
if (android.os.Build.VERSION.SDK_INT >= 24) { if (SDK_VERSION >= 24) {
defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().getLocales().get(0); defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().getLocales().get(0);
} else { } else {
defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().locale; defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().locale;
@ -139,7 +140,7 @@ class DeviceRef {
get region(): string { get region(): string {
let defaultNativeLocale; let defaultNativeLocale;
if (android.os.Build.VERSION.SDK_INT >= 24) { if (SDK_VERSION >= 24) {
defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().getLocales().get(0); defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().getLocales().get(0);
} else { } else {
defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().locale; defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().locale;

View File

@ -8,8 +8,7 @@ import { ImageSource } from '../../image-source';
import * as application from '../../application'; import * as application from '../../application';
import { isAccessibilityServiceEnabled, updateContentDescription } from '../../accessibility'; import { isAccessibilityServiceEnabled, updateContentDescription } from '../../accessibility';
import type { Background } from '../styling/background'; import type { Background } from '../styling/background';
import { Device } from '../../platform'; import { SDK_VERSION } from '../../utils';
import lazy from '../../utils/lazy';
export * from './action-bar-common'; export * from './action-bar-common';
@ -17,8 +16,6 @@ const R_ID_HOME = 0x0102002c;
const ACTION_ITEM_ID_OFFSET = 10000; const ACTION_ITEM_ID_OFFSET = 10000;
const DEFAULT_ELEVATION = 4; const DEFAULT_ELEVATION = 4;
const sdkVersion = lazy(() => parseInt(Device.sdkVersion));
let AppCompatTextView; let AppCompatTextView;
let actionItemIdGenerator = ACTION_ITEM_ID_OFFSET; let actionItemIdGenerator = ACTION_ITEM_ID_OFFSET;
function generateItemId(): number { function generateItemId(): number {
@ -64,8 +61,6 @@ function initializeMenuItemClickListener(): void {
return; return;
} }
apiLevel = sdkVersion();
AppCompatTextView = androidx.appcompat.widget.AppCompatTextView; AppCompatTextView = androidx.appcompat.widget.AppCompatTextView;
@NativeClass @NativeClass
@ -88,8 +83,6 @@ function initializeMenuItemClickListener(): void {
appResources = application.android.context.getResources(); appResources = application.android.context.getResources();
} }
let apiLevel: number;
export class ActionItem extends ActionItemBase { export class ActionItem extends ActionItemBase {
private _androidPosition: AndroidActionItemSettings = { private _androidPosition: AndroidActionItemSettings = {
position: 'actionBar', position: 'actionBar',
@ -223,7 +216,7 @@ export class ActionBar extends ActionBarBase {
public _applyBackground(background: Background, isBorderDrawable, onlyColor: boolean, backgroundDrawable: any) { public _applyBackground(background: Background, isBorderDrawable, onlyColor: boolean, backgroundDrawable: any) {
const nativeView = this.nativeViewProtected; const nativeView = this.nativeViewProtected;
if (backgroundDrawable && onlyColor && sdkVersion() >= 21) { if (backgroundDrawable && onlyColor && SDK_VERSION >= 21) {
if (isBorderDrawable && (<any>nativeView)._cachedDrawable) { if (isBorderDrawable && (<any>nativeView)._cachedDrawable) {
backgroundDrawable = (<any>nativeView)._cachedDrawable; backgroundDrawable = (<any>nativeView)._cachedDrawable;
// we need to duplicate the drawable or we lose the "default" cached drawable // we need to duplicate the drawable or we lose the "default" cached drawable
@ -472,13 +465,13 @@ export class ActionBar extends ActionBarBase {
} }
[androidContentInsetLeftProperty.setNative]() { [androidContentInsetLeftProperty.setNative]() {
if (apiLevel >= 21) { if (SDK_VERSION >= 21) {
this.nativeViewProtected.setContentInsetsAbsolute(this.effectiveContentInsetLeft, this.effectiveContentInsetRight); this.nativeViewProtected.setContentInsetsAbsolute(this.effectiveContentInsetLeft, this.effectiveContentInsetRight);
} }
} }
[androidContentInsetRightProperty.setNative]() { [androidContentInsetRightProperty.setNative]() {
if (apiLevel >= 21) { if (SDK_VERSION >= 21) {
this.nativeViewProtected.setContentInsetsAbsolute(this.effectiveContentInsetLeft, this.effectiveContentInsetRight); this.nativeViewProtected.setContentInsetsAbsolute(this.effectiveContentInsetLeft, this.effectiveContentInsetRight);
} }
} }
@ -493,9 +486,9 @@ export class ActionBar extends ActionBarBase {
return; return;
} }
const originalFocusableState = android.os.Build.VERSION.SDK_INT >= 26 && nativeView.getFocusable(); const originalFocusableState = SDK_VERSION >= 26 && nativeView.getFocusable();
const originalImportantForAccessibility = nativeView.getImportantForAccessibility(); const originalImportantForAccessibility = nativeView.getImportantForAccessibility();
const originalIsAccessibilityHeading = android.os.Build.VERSION.SDK_INT >= 28 && nativeView.isAccessibilityHeading(); const originalIsAccessibilityHeading = SDK_VERSION >= 28 && nativeView.isAccessibilityHeading();
try { try {
nativeView.setFocusable(false); nativeView.setFocusable(false);
@ -513,7 +506,7 @@ export class ActionBar extends ActionBarBase {
childView.setFocusable(true); childView.setFocusable(true);
if (childView instanceof androidx.appcompat.widget.AppCompatTextView) { if (childView instanceof androidx.appcompat.widget.AppCompatTextView) {
announceView = childView; announceView = childView;
if (android.os.Build.VERSION.SDK_INT >= 28) { if (SDK_VERSION >= 28) {
announceView.setAccessibilityHeading(true); announceView.setAccessibilityHeading(true);
} }
} }
@ -538,11 +531,11 @@ export class ActionBar extends ActionBarBase {
return; return;
} }
if (android.os.Build.VERSION.SDK_INT >= 28) { if (SDK_VERSION >= 28) {
nativeView.setAccessibilityHeading(originalIsAccessibilityHeading); nativeView.setAccessibilityHeading(originalIsAccessibilityHeading);
} }
if (android.os.Build.VERSION.SDK_INT >= 26) { if (SDK_VERSION >= 26) {
localNativeView.setFocusable(originalFocusableState); localNativeView.setFocusable(originalFocusableState);
} }

View File

@ -7,7 +7,7 @@ import { AnimationBase, Properties, CubicBezierAnimationCurve } from './animatio
import { Color } from '../../color'; import { Color } from '../../color';
import { Trace } from '../../trace'; import { Trace } from '../../trace';
import { opacityProperty, backgroundColorProperty, rotateProperty, rotateXProperty, rotateYProperty, translateXProperty, translateYProperty, scaleXProperty, scaleYProperty, heightProperty, widthProperty, PercentLength } from '../styling/style-properties'; import { opacityProperty, backgroundColorProperty, rotateProperty, rotateXProperty, rotateYProperty, translateXProperty, translateYProperty, scaleXProperty, scaleYProperty, heightProperty, widthProperty, PercentLength } from '../styling/style-properties';
import { layout } from '../../utils'; import { SDK_VERSION, layout } from '../../utils';
import { Device, Screen } from '../../platform'; import { Device, Screen } from '../../platform';
import lazy from '../../utils/lazy'; import lazy from '../../utils/lazy';
@ -207,8 +207,8 @@ export class Animation extends AnimationBase {
return _resolveAnimationCurve(curve); return _resolveAnimationCurve(curve);
} }
private _play(): void { protected _play(): void {
if (Device.sdkVersion <= '23') { if (SDK_VERSION <= 23) {
this._animatorSet = new android.animation.AnimatorSet(); this._animatorSet = new android.animation.AnimatorSet();
this._animatorSet.addListener(this._animatorListener); this._animatorSet.addListener(this._animatorListener);
} }

View File

@ -6,19 +6,17 @@ import { CoreTypes } from '../../core-types';
import { profile } from '../../profiling'; import { profile } from '../../profiling';
import { TouchGestureEventData, GestureTypes, TouchAction } from '../gestures'; import { TouchGestureEventData, GestureTypes, TouchAction } from '../gestures';
import { Device } from '../../platform'; import { Device } from '../../platform';
import { SDK_VERSION } from '../../utils';
import lazy from '../../utils/lazy'; import lazy from '../../utils/lazy';
import type { Background } from '../styling/background'; import type { Background } from '../styling/background';
export * from './button-common'; export * from './button-common';
const sdkVersion = lazy(() => parseInt(Device.sdkVersion));
interface ClickListener { interface ClickListener {
new (owner: Button): android.view.View.OnClickListener; new (owner: Button): android.view.View.OnClickListener;
} }
let ClickListener: ClickListener; let ClickListener: ClickListener;
let APILEVEL: number;
let AndroidButton: typeof android.widget.Button; let AndroidButton: typeof android.widget.Button;
function initializeClickListener(): void { function initializeClickListener(): void {
@ -49,13 +47,6 @@ function initializeClickListener(): void {
export class Button extends ButtonBase { export class Button extends ButtonBase {
nativeViewProtected: android.widget.Button; nativeViewProtected: android.widget.Button;
constructor() {
super();
if (!APILEVEL) {
APILEVEL = android.os.Build.VERSION.SDK_INT;
}
}
private _stateListAnimator: any; private _stateListAnimator: any;
private _highlightedHandler: (args: TouchGestureEventData) => void; private _highlightedHandler: (args: TouchGestureEventData) => void;
@ -113,7 +104,7 @@ export class Button extends ButtonBase {
public resetNativeView(): void { public resetNativeView(): void {
super.resetNativeView(); super.resetNativeView();
if (this._stateListAnimator && APILEVEL >= 21) { if (this._stateListAnimator && SDK_VERSION >= 21) {
(<any>this.nativeViewProtected).setStateListAnimator(this._stateListAnimator); (<any>this.nativeViewProtected).setStateListAnimator(this._stateListAnimator);
this._stateListAnimator = undefined; this._stateListAnimator = undefined;
} }
@ -182,8 +173,7 @@ export class Button extends ButtonBase {
} }
[zIndexProperty.setNative](value: number) { [zIndexProperty.setNative](value: number) {
// API >= 21 if (SDK_VERSION >= 21) {
if (APILEVEL >= 21) {
const nativeView = this.nativeViewProtected; const nativeView = this.nativeViewProtected;
if (!this._stateListAnimator) { if (!this._stateListAnimator) {
this._stateListAnimator = (<any>nativeView).getStateListAnimator(); this._stateListAnimator = (<any>nativeView).getStateListAnimator();
@ -201,7 +191,7 @@ export class Button extends ButtonBase {
} }
protected getDefaultElevation(): number { protected getDefaultElevation(): number {
if (sdkVersion() < 21) { if (SDK_VERSION < 21) {
return 0; return 0;
} }
@ -212,7 +202,7 @@ export class Button extends ButtonBase {
} }
protected getDefaultDynamicElevationOffset(): number { protected getDefaultDynamicElevationOffset(): number {
if (sdkVersion() < 21) { if (SDK_VERSION < 21) {
return 0; return 0;
} }

View File

@ -50,8 +50,6 @@ const GRAVITY_FILL_HORIZONTAL = 7; // android.view.Gravity.FILL_HORIZONTAL
const GRAVITY_CENTER_VERTICAL = 16; // android.view.Gravity.CENTER_VERTICAL const GRAVITY_CENTER_VERTICAL = 16; // android.view.Gravity.CENTER_VERTICAL
const GRAVITY_FILL_VERTICAL = 112; // android.view.Gravity.FILL_VERTICAL const GRAVITY_FILL_VERTICAL = 112; // android.view.Gravity.FILL_VERTICAL
const sdkVersion = lazy(() => parseInt(Device.sdkVersion));
const modalMap = new Map<number, DialogOptions>(); const modalMap = new Map<number, DialogOptions>();
let TouchListener: TouchListener; let TouchListener: TouchListener;
@ -848,7 +846,7 @@ export class View extends ViewCommon {
this.accessibilityRole = value; this.accessibilityRole = value;
updateAccessibilityProperties(this); updateAccessibilityProperties(this);
if (android.os.Build.VERSION.SDK_INT >= 28) { if (Utils.SDK_VERSION >= 28) {
this.nativeViewProtected?.setAccessibilityHeading(value === AccessibilityRole.Header); this.nativeViewProtected?.setAccessibilityHeading(value === AccessibilityRole.Header);
} }
} }
@ -906,7 +904,7 @@ export class View extends ViewCommon {
return this.getDefaultElevation(); return this.getDefaultElevation();
} }
[androidElevationProperty.setNative](value: number) { [androidElevationProperty.setNative](value: number) {
if (sdkVersion() < 21) { if (Utils.SDK_VERSION < 21) {
return; return;
} }
@ -917,7 +915,7 @@ export class View extends ViewCommon {
return this.getDefaultDynamicElevationOffset(); return this.getDefaultDynamicElevationOffset();
} }
[androidDynamicElevationOffsetProperty.setNative](value: number) { [androidDynamicElevationOffsetProperty.setNative](value: number) {
if (sdkVersion() < 21) { if (Utils.SDK_VERSION < 21) {
return; return;
} }
@ -925,7 +923,7 @@ export class View extends ViewCommon {
} }
protected getDefaultElevation(): number { protected getDefaultElevation(): number {
if (sdkVersion() < 21) { if (Utils.SDK_VERSION < 21) {
return 0; return 0;
} }

View File

@ -1,15 +1,13 @@
import { EditableTextBase as EditableTextBaseCommon, autofillTypeProperty, keyboardTypeProperty, returnKeyTypeProperty, editableProperty, autocapitalizationTypeProperty, autocorrectProperty, hintProperty, placeholderColorProperty, maxLengthProperty } from './editable-text-base-common'; import { EditableTextBase as EditableTextBaseCommon, autofillTypeProperty, keyboardTypeProperty, returnKeyTypeProperty, editableProperty, autocapitalizationTypeProperty, autocorrectProperty, hintProperty, placeholderColorProperty, maxLengthProperty } from './editable-text-base-common';
import { textTransformProperty, textProperty, resetSymbol } from '../text-base'; import { textTransformProperty, textProperty, resetSymbol } from '../text-base';
import { Color } from '../../color'; import { Color } from '../../color';
import { ad } from '../../utils'; import { SDK_VERSION, ad } from '../../utils';
import { CoreTypes } from '../../core-types'; import { CoreTypes } from '../../core-types';
import { Device } from '../../platform'; import { Device } from '../../platform';
import lazy from '../../utils/lazy'; import lazy from '../../utils/lazy';
export * from './editable-text-base-common'; export * from './editable-text-base-common';
const sdkVersion = lazy(() => parseInt(Device.sdkVersion));
//https://github.com/NativeScript/NativeScript/issues/2942 //https://github.com/NativeScript/NativeScript/issues/2942
export let dismissKeyboardTimeoutId: NodeJS.Timer; export let dismissKeyboardTimeoutId: NodeJS.Timer;
@ -300,7 +298,7 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
} }
[autofillTypeProperty.setNative](value: CoreTypes.AutofillType) { [autofillTypeProperty.setNative](value: CoreTypes.AutofillType) {
if (sdkVersion() < 26) { if (SDK_VERSION < 26) {
return; return;
} }
let newOptions; let newOptions;

View File

@ -1,4 +1,5 @@
import { Color } from '../../color'; import { Color } from '../../color';
import { SDK_VERSION } from '../../utils';
import { Font } from '../styling/font'; import { Font } from '../styling/font';
import { colorProperty, fontSizeProperty, fontInternalProperty } from '../styling/style-properties'; import { colorProperty, fontSizeProperty, fontInternalProperty } from '../styling/style-properties';
import { HtmlViewBase, htmlProperty, selectableProperty, linkColorProperty } from './html-view-common'; import { HtmlViewBase, htmlProperty, selectableProperty, linkColorProperty } from './html-view-common';
@ -41,8 +42,7 @@ export class HtmlView extends HtmlViewBase {
mask = 0; mask = 0;
} }
this.nativeViewProtected.setAutoLinkMask(mask); this.nativeViewProtected.setAutoLinkMask(mask);
const apiLevel = android.os.Build.VERSION.SDK_INT; if (SDK_VERSION >= 24) {
if (apiLevel >= 24) {
this.nativeViewProtected.setText(<any>android.text.Html.fromHtml(value, android.text.Html.FROM_HTML_MODE_LEGACY)); this.nativeViewProtected.setText(<any>android.text.Html.fromHtml(value, android.text.Html.FROM_HTML_MODE_LEGACY));
} else { } else {
this.nativeViewProtected.setText(<any>android.text.Html.fromHtml(value)); this.nativeViewProtected.setText(<any>android.text.Html.fromHtml(value));

View File

@ -6,8 +6,6 @@ import lazy from '../../utils/lazy';
export * from './list-picker-common'; export * from './list-picker-common';
const sdkVersion = lazy(() => parseInt(Device.sdkVersion));
interface Formatter { interface Formatter {
new (owner: ListPicker): android.widget.NumberPicker.Formatter; new (owner: ListPicker): android.widget.NumberPicker.Formatter;
} }

View File

@ -4,7 +4,7 @@ import { View } from '../core/view';
import { Color } from '../../color'; import { Color } from '../../color';
import { ActionBar } from '../action-bar'; import { ActionBar } from '../action-bar';
import { GridLayout } from '../layouts/grid-layout'; import { GridLayout } from '../layouts/grid-layout';
import { Device } from '../../platform'; import { SDK_VERSION } from '../../utils';
import { profile } from '../../profiling'; import { profile } from '../../profiling';
import { AndroidAccessibilityEvent, getLastFocusedViewOnPage, isAccessibilityServiceEnabled } from '../../accessibility'; import { AndroidAccessibilityEvent, getLastFocusedViewOnPage, isAccessibilityServiceEnabled } from '../../accessibility';
@ -91,7 +91,7 @@ export class Page extends PageBase {
color: number; color: number;
systemUiVisibility: number; systemUiVisibility: number;
} { } {
if (Device.sdkVersion >= '21') { if (SDK_VERSION >= 21) {
const window = this.getClosestWindow(); const window = this.getClosestWindow();
const decorView = window.getDecorView(); const decorView = window.getDecorView();
@ -104,7 +104,7 @@ export class Page extends PageBase {
return null; return null;
} }
[statusBarStyleProperty.setNative](value: 'dark' | 'light' | { color: number; systemUiVisibility: number }) { [statusBarStyleProperty.setNative](value: 'dark' | 'light' | { color: number; systemUiVisibility: number }) {
if (Device.sdkVersion >= '21') { if (SDK_VERSION >= 21) {
const window = this.getClosestWindow(); const window = this.getClosestWindow();
const decorView = window.getDecorView(); const decorView = window.getDecorView();
@ -122,7 +122,7 @@ export class Page extends PageBase {
} }
[androidStatusBarBackgroundProperty.getDefault](): number { [androidStatusBarBackgroundProperty.getDefault](): number {
if (Device.sdkVersion >= '21') { if (SDK_VERSION >= 21) {
const window = this.getClosestWindow(); const window = this.getClosestWindow();
return (<any>window).getStatusBarColor(); return (<any>window).getStatusBarColor();
} }
@ -130,7 +130,7 @@ export class Page extends PageBase {
return null; return null;
} }
[androidStatusBarBackgroundProperty.setNative](value: number | Color) { [androidStatusBarBackgroundProperty.setNative](value: number | Color) {
if (Device.sdkVersion >= '21') { if (SDK_VERSION >= 21) {
const window = this.getClosestWindow(); const window = this.getClosestWindow();
const color = value instanceof Color ? value.android : value; const color = value instanceof Color ? value.android : value;
(<any>window).setStatusBarColor(color); (<any>window).setStatusBarColor(color);

View File

@ -3,7 +3,7 @@ import { SegmentedBarItemBase, SegmentedBarBase, selectedIndexProperty, itemsPro
import { isEnabledProperty } from '../core/view'; import { isEnabledProperty } from '../core/view';
import { colorProperty, fontInternalProperty, fontSizeProperty } from '../styling/style-properties'; import { colorProperty, fontInternalProperty, fontSizeProperty } from '../styling/style-properties';
import { Color } from '../../color'; import { Color } from '../../color';
import { layout } from '../../utils'; import { SDK_VERSION, layout } from '../../utils';
export * from './segmented-bar-common'; export * from './segmented-bar-common';
@ -24,7 +24,6 @@ interface TabHost {
new (context: android.content.Context, attrs: android.util.AttributeSet): android.widget.TabHost; new (context: android.content.Context, attrs: android.util.AttributeSet): android.widget.TabHost;
} }
let apiLevel: number;
let selectedIndicatorThickness: number; let selectedIndicatorThickness: number;
let TabHost: TabHost; let TabHost: TabHost;
@ -37,9 +36,8 @@ function initializeNativeClasses(): void {
return; return;
} }
apiLevel = android.os.Build.VERSION.SDK_INT;
// Indicator thickness for material - 2dip. For pre-material - 5dip. // Indicator thickness for material - 2dip. For pre-material - 5dip.
selectedIndicatorThickness = layout.toDevicePixels(apiLevel >= 21 ? 2 : 5); selectedIndicatorThickness = layout.toDevicePixels(SDK_VERSION >= 21 ? 2 : 5);
@NativeClass @NativeClass
@Interfaces([android.widget.TabHost.OnTabChangeListener]) @Interfaces([android.widget.TabHost.OnTabChangeListener])
@ -164,7 +162,7 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
if (value instanceof Color) { if (value instanceof Color) {
const color = value.android; const color = value.android;
const backgroundDrawable = viewGroup.getBackground(); const backgroundDrawable = viewGroup.getBackground();
if (apiLevel > 21 && backgroundDrawable) { if (SDK_VERSION > 21 && backgroundDrawable) {
const newDrawable = tryCloneDrawable(backgroundDrawable, nativeView.getResources()); const newDrawable = tryCloneDrawable(backgroundDrawable, nativeView.getResources());
newDrawable.setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN); newDrawable.setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
viewGroup.setBackground(newDrawable); viewGroup.setBackground(newDrawable);

View File

@ -1,5 +1,6 @@
import { Font as FontBase, parseFontFamily, genericFontFamilies, FontStyleType, FontWeight, FontWeightType } from './font-common'; import { Font as FontBase, parseFontFamily, genericFontFamilies, FontStyleType, FontWeight, FontWeightType } from './font-common';
import { Trace } from '../../trace'; import { Trace } from '../../trace';
import { SDK_VERSION } from '../../utils';
import * as application from '../../application'; import * as application from '../../application';
import * as fs from '../../file-system'; import * as fs from '../../file-system';
@ -144,10 +145,10 @@ function getFontWeightSuffix(fontWeight: FontWeightType): string {
} }
switch (fontWeight) { switch (fontWeight) {
case FontWeight.THIN: case FontWeight.THIN:
return android.os.Build.VERSION.SDK_INT >= 16 ? '-thin' : ''; return SDK_VERSION >= 16 ? '-thin' : '';
case FontWeight.EXTRA_LIGHT: case FontWeight.EXTRA_LIGHT:
case FontWeight.LIGHT: case FontWeight.LIGHT:
return android.os.Build.VERSION.SDK_INT >= 16 ? '-light' : ''; return SDK_VERSION >= 16 ? '-light' : '';
case FontWeight.NORMAL: case FontWeight.NORMAL:
case '400': case '400':
case undefined: case undefined:
@ -155,13 +156,13 @@ function getFontWeightSuffix(fontWeight: FontWeightType): string {
return ''; return '';
case FontWeight.MEDIUM: case FontWeight.MEDIUM:
case FontWeight.SEMI_BOLD: case FontWeight.SEMI_BOLD:
return android.os.Build.VERSION.SDK_INT >= 21 ? '-medium' : ''; return SDK_VERSION >= 21 ? '-medium' : '';
case FontWeight.BOLD: case FontWeight.BOLD:
case '700': case '700':
case FontWeight.EXTRA_BOLD: case FontWeight.EXTRA_BOLD:
return ''; return '';
case FontWeight.BLACK: case FontWeight.BLACK:
return android.os.Build.VERSION.SDK_INT >= 21 ? '-black' : ''; return SDK_VERSION >= 21 ? '-black' : '';
default: default:
throw new Error(`Invalid font weight: "${fontWeight}"`); throw new Error(`Invalid font weight: "${fontWeight}"`);
} }

View File

@ -11,7 +11,7 @@ import { colorProperty, fontSizeProperty, fontInternalProperty, paddingLeftPrope
import { FormattedString } from './formatted-string'; import { FormattedString } from './formatted-string';
import { Span } from './span'; import { Span } from './span';
import { CoreTypes } from '../../core-types'; import { CoreTypes } from '../../core-types';
import { layout } from '../../utils'; import { SDK_VERSION, layout } from '../../utils';
import { isString, isNullOrUndefined } from '../../utils/types'; import { isString, isNullOrUndefined } from '../../utils/types';
import { accessibilityIdentifierProperty } from '../../accessibility/accessibility-properties'; import { accessibilityIdentifierProperty } from '../../accessibility/accessibility-properties';
import * as Utils from '../../utils'; import * as Utils from '../../utils';
@ -298,7 +298,7 @@ export class TextBase extends TextBaseCommon {
this.nativeTextViewProtected.setGravity(android.view.Gravity.START | verticalGravity); this.nativeTextViewProtected.setGravity(android.view.Gravity.START | verticalGravity);
break; break;
} }
if (android.os.Build.VERSION.SDK_INT >= 26) { if (SDK_VERSION >= 26) {
if (value === 'justify') { if (value === 'justify') {
this.nativeTextViewProtected.setJustificationMode(android.text.Layout.JUSTIFICATION_MODE_INTER_WORD); this.nativeTextViewProtected.setJustificationMode(android.text.Layout.JUSTIFICATION_MODE_INTER_WORD);
} else { } else {

View File

@ -1,4 +1,5 @@
import { TimePickerBase, getValidTime, timeProperty, hourProperty, minuteProperty } from './time-picker-common'; import { TimePickerBase, getValidTime, timeProperty, hourProperty, minuteProperty } from './time-picker-common';
import { SDK_VERSION } from '../../utils';
export * from './time-picker-common'; export * from './time-picker-common';
@ -13,8 +14,6 @@ function initializeTimeChangedListener(): void {
return; return;
} }
apiLevel = android.os.Build.VERSION.SDK_INT;
@NativeClass @NativeClass
@Interfaces([android.widget.TimePicker.OnTimeChangedListener]) @Interfaces([android.widget.TimePicker.OnTimeChangedListener])
class TimeChangedListenerImpl extends java.lang.Object implements android.widget.TimePicker.OnTimeChangedListener { class TimeChangedListenerImpl extends java.lang.Object implements android.widget.TimePicker.OnTimeChangedListener {
@ -38,8 +37,6 @@ function initializeTimeChangedListener(): void {
TimeChangedListener = TimeChangedListenerImpl; TimeChangedListener = TimeChangedListenerImpl;
} }
let apiLevel: number;
export class TimePicker extends TimePickerBase { export class TimePicker extends TimePickerBase {
nativeViewProtected: android.widget.TimePicker; nativeViewProtected: android.widget.TimePicker;
updatingNativeValue: boolean; updatingNativeValue: boolean;
@ -69,7 +66,7 @@ export class TimePicker extends TimePickerBase {
[minuteProperty.setNative](value: number) { [minuteProperty.setNative](value: number) {
this.updatingNativeValue = true; this.updatingNativeValue = true;
try { try {
if (apiLevel >= 23) { if (SDK_VERSION >= 23) {
(<any>this.nativeViewProtected).setMinute(value); (<any>this.nativeViewProtected).setMinute(value);
} else { } else {
this.nativeViewProtected.setCurrentMinute(new java.lang.Integer(value)); this.nativeViewProtected.setCurrentMinute(new java.lang.Integer(value));
@ -82,7 +79,7 @@ export class TimePicker extends TimePickerBase {
[hourProperty.setNative](value: number) { [hourProperty.setNative](value: number) {
this.updatingNativeValue = true; this.updatingNativeValue = true;
try { try {
if (apiLevel >= 23) { if (SDK_VERSION >= 23) {
(<any>this.nativeViewProtected).setHour(value); (<any>this.nativeViewProtected).setHour(value);
} else { } else {
this.nativeViewProtected.setCurrentHour(new java.lang.Integer(value)); this.nativeViewProtected.setCurrentHour(new java.lang.Integer(value));

View File

@ -1,5 +1,5 @@
import { ad } from './native-helper'; import { ad } from './native-helper';
import { Device } from '../platform'; import { SDK_VERSION } from '../utils';
import { FileSystemAccess } from '../file-system/file-system-access'; import { FileSystemAccess } from '../file-system/file-system-access';
import { Trace } from '../trace'; import { Trace } from '../trace';
@ -114,16 +114,15 @@ Applications cannot access internal storage of other application on Android (see
chooserIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK); chooserIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
// Android SDK <28 only requires starting the chooser Intent straight forwardly // Android SDK <28 only requires starting the chooser Intent straight forwardly
const sdkVersion = parseInt(Device.sdkVersion, 10); if (SDK_VERSION < MIN_URI_SHARE_RESTRICTED_APK_VERSION) {
if (sdkVersion && sdkVersion < MIN_URI_SHARE_RESTRICTED_APK_VERSION) { Trace.write(`detected sdk version ${SDK_VERSION} (< ${MIN_URI_SHARE_RESTRICTED_APK_VERSION}), using simple openFile`, Trace.categories.Debug);
Trace.write(`detected sdk version ${sdkVersion} (< ${MIN_URI_SHARE_RESTRICTED_APK_VERSION}), using simple openFile`, Trace.categories.Debug);
intent.setDataAndType(android.net.Uri.fromFile(new java.io.File(filePath)), mimeType); intent.setDataAndType(android.net.Uri.fromFile(new java.io.File(filePath)), mimeType);
context.startActivity(chooserIntent); context.startActivity(chooserIntent);
return true; return true;
} }
Trace.write(`detected sdk version ${sdkVersion} (>= ${MIN_URI_SHARE_RESTRICTED_APK_VERSION}), using URI openFile`, Trace.categories.Debug); Trace.write(`detected sdk version ${SDK_VERSION} (>= ${MIN_URI_SHARE_RESTRICTED_APK_VERSION}), using URI openFile`, Trace.categories.Debug);
// Android SDK 24+ introduced file system permissions changes that disallow // Android SDK 24+ introduced file system permissions changes that disallow
// exposing URIs between applications // exposing URIs between applications

View File

@ -9,6 +9,8 @@ export * from './native-helper';
export const RESOURCE_PREFIX: string; export const RESOURCE_PREFIX: string;
export const FILE_PREFIX: string; export const FILE_PREFIX: string;
export const SDK_VERSION: number;
//@private //@private
/** /**
* Used by various android event listener implementations. * Used by various android event listener implementations.

View File

@ -12,7 +12,7 @@ export function makeMeasureSpec(size: number, mode: number): number {
if (sdkVersion === undefined) { if (sdkVersion === undefined) {
// check whether the old layout is needed // check whether the old layout is needed
sdkVersion = ad.getApplicationContext().getApplicationInfo().targetSdkVersion; sdkVersion = ad.getApplicationContext().getApplicationInfo().targetSdkVersion;
useOldMeasureSpec = sdkVersion <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; useOldMeasureSpec = sdkVersion <= 17;
} }
if (useOldMeasureSpec) { if (useOldMeasureSpec) {

View File

@ -4,6 +4,7 @@ import { sanitizeModuleName } from '../ui/builder/module-name-sanitizer';
import * as layout from './layout-helper'; import * as layout from './layout-helper';
import { GC } from './index'; import { GC } from './index';
import { Device } from '../platform';
export { layout }; export { layout };
export * from './mainthread-helper'; export * from './mainthread-helper';
@ -191,3 +192,5 @@ export function queueGC(delay = 900, useThrottle?: boolean) {
debouncedGC.get(delay)(); debouncedGC.get(delay)();
} }
} }
export const SDK_VERSION = parseInt(Device.sdkVersion, 10);