mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00
chore: remove critical circular dependencies (#8114)
* chore: remove critical circular dependencies * chore: fix tslint errors * chore: remove platform specific types from interfaces * chore: update unit tests polyfills * fix: incorrect null check * chore: update api.md file * test: improve test case * chore: apply comments * test: avoid page style leaks in tests
This commit is contained in:

committed by
Alexander Vakrilov

parent
5b647bd809
commit
0ffc790d82
@ -1,224 +1,17 @@
|
||||
import { ad } from "./native-helper";
|
||||
import { device } from "../platform";
|
||||
import { FileSystemAccess } from "../file-system/file-system-access";
|
||||
import {
|
||||
write as traceWrite,
|
||||
categories as traceCategories,
|
||||
messageType as traceMessageType,
|
||||
} from "../trace";
|
||||
|
||||
import { layoutCommon } from "./utils-common";
|
||||
|
||||
export { ad };
|
||||
export * from "./utils-common";
|
||||
|
||||
import { getNativeApplication, android as androidApp } from "../application";
|
||||
import { device } from "../platform";
|
||||
import { FileSystemAccess } from "../file-system/file-system-access";
|
||||
|
||||
const MIN_URI_SHARE_RESTRICTED_APK_VERSION = 24;
|
||||
|
||||
export module layout {
|
||||
let density: number;
|
||||
|
||||
// cache the MeasureSpec constants here, to prevent extensive marshaling calls to and from Java
|
||||
// TODO: While this boosts the performance it is error-prone in case Google changes these constants
|
||||
const MODE_SHIFT = 30;
|
||||
const MODE_MASK = 0x3 << MODE_SHIFT;
|
||||
let sdkVersion: number;
|
||||
let useOldMeasureSpec = false;
|
||||
|
||||
export function makeMeasureSpec(size: number, mode: number): number {
|
||||
if (sdkVersion === undefined) {
|
||||
// check whether the old layout is needed
|
||||
sdkVersion = ad.getApplicationContext().getApplicationInfo().targetSdkVersion;
|
||||
useOldMeasureSpec = sdkVersion <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
|
||||
}
|
||||
|
||||
if (useOldMeasureSpec) {
|
||||
return size + mode;
|
||||
}
|
||||
|
||||
return (size & ~MODE_MASK) | (mode & MODE_MASK);
|
||||
}
|
||||
|
||||
export function getDisplayDensity(): number {
|
||||
if (density === undefined) {
|
||||
density = ad.getResources().getDisplayMetrics().density;
|
||||
}
|
||||
|
||||
return density;
|
||||
}
|
||||
|
||||
export function toDevicePixels(value: number): number {
|
||||
return value * getDisplayDensity();
|
||||
}
|
||||
|
||||
export function toDeviceIndependentPixels(value: number): number {
|
||||
return value / getDisplayDensity();
|
||||
}
|
||||
|
||||
export function measureNativeView(nativeView: any /* android.view.View */, width: number, widthMode: number, height: number, heightMode: number): { width: number, height: number } {
|
||||
const view = <android.view.View>nativeView;
|
||||
view.measure(makeMeasureSpec(width, widthMode), makeMeasureSpec(height, heightMode));
|
||||
|
||||
return {
|
||||
width: view.getMeasuredWidth(),
|
||||
height: view.getMeasuredHeight()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(webpack-workflow): Export all methods from layoutCommon
|
||||
// Think of a cleaner way to do that
|
||||
Object.assign(layout, layoutCommon);
|
||||
|
||||
// We are using "ad" here to avoid namespace collision with the global android object
|
||||
export module ad {
|
||||
|
||||
let application: android.app.Application;
|
||||
let applicationContext: android.content.Context;
|
||||
let contextResources: android.content.res.Resources;
|
||||
let packageName: string;
|
||||
export function getApplicationContext() {
|
||||
if (!applicationContext) {
|
||||
applicationContext = getApplication().getApplicationContext();
|
||||
}
|
||||
|
||||
return applicationContext;
|
||||
}
|
||||
export function getApplication() {
|
||||
if (!application) {
|
||||
application = (<android.app.Application>getNativeApplication());
|
||||
}
|
||||
|
||||
return application;
|
||||
}
|
||||
export function getResources() {
|
||||
if (!contextResources) {
|
||||
contextResources = getApplication().getResources();
|
||||
}
|
||||
|
||||
return contextResources;
|
||||
}
|
||||
function getPackageName() {
|
||||
if (!packageName) {
|
||||
packageName = getApplicationContext().getPackageName();
|
||||
}
|
||||
|
||||
return packageName;
|
||||
}
|
||||
|
||||
let inputMethodManager: android.view.inputmethod.InputMethodManager;
|
||||
export function getInputMethodManager(): android.view.inputmethod.InputMethodManager {
|
||||
if (!inputMethodManager) {
|
||||
inputMethodManager = <android.view.inputmethod.InputMethodManager>getApplicationContext().getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
|
||||
}
|
||||
|
||||
return inputMethodManager;
|
||||
}
|
||||
|
||||
export function showSoftInput(nativeView: android.view.View): void {
|
||||
const inputManager = getInputMethodManager();
|
||||
if (inputManager && nativeView instanceof android.view.View) {
|
||||
inputManager.showSoftInput(nativeView, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
|
||||
}
|
||||
}
|
||||
|
||||
export function dismissSoftInput(nativeView?: android.view.View): void {
|
||||
const inputManager = getInputMethodManager();
|
||||
let windowToken: android.os.IBinder;
|
||||
|
||||
if (nativeView instanceof android.view.View) {
|
||||
windowToken = nativeView.getWindowToken();
|
||||
} else if (androidApp.foregroundActivity instanceof androidx.appcompat.app.AppCompatActivity) {
|
||||
const decorView = androidApp.foregroundActivity.getWindow().getDecorView();
|
||||
windowToken = decorView ? decorView.getWindowToken() : null;
|
||||
}
|
||||
|
||||
if (inputManager && windowToken) {
|
||||
inputManager.hideSoftInputFromWindow(windowToken, 0);
|
||||
}
|
||||
}
|
||||
|
||||
export module collections {
|
||||
export function stringArrayToStringSet(str: string[]): java.util.HashSet<string> {
|
||||
const hashSet = new java.util.HashSet<string>();
|
||||
if (str !== undefined) {
|
||||
for (let element in str) {
|
||||
hashSet.add("" + str[element]);
|
||||
}
|
||||
}
|
||||
|
||||
return hashSet;
|
||||
}
|
||||
|
||||
export function stringSetToStringArray(stringSet: any): string[] {
|
||||
const arr = [];
|
||||
if (stringSet !== undefined) {
|
||||
const it = stringSet.iterator();
|
||||
while (it.hasNext()) {
|
||||
const element = "" + it.next();
|
||||
arr.push(element);
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
export module resources {
|
||||
let attr;
|
||||
const attrCache = new Map<string, number>();
|
||||
|
||||
export function getDrawableId(name) {
|
||||
return getId(":drawable/" + name);
|
||||
}
|
||||
|
||||
export function getStringId(name) {
|
||||
return getId(":string/" + name);
|
||||
}
|
||||
|
||||
export function getId(name: string): number {
|
||||
const resources = getResources();
|
||||
const packageName = getPackageName();
|
||||
const uri = packageName + name;
|
||||
|
||||
return resources.getIdentifier(uri, null, null);
|
||||
}
|
||||
export function getPalleteColor(name: string, context: android.content.Context): number {
|
||||
return getPaletteColor(name, context);
|
||||
}
|
||||
export function getPaletteColor(name: string, context: android.content.Context): number {
|
||||
if (attrCache.has(name)) {
|
||||
return attrCache.get(name);
|
||||
}
|
||||
|
||||
let result = 0;
|
||||
try {
|
||||
if (!attr) {
|
||||
attr = java.lang.Class.forName("androidx.appcompat.R$attr");
|
||||
}
|
||||
|
||||
let colorID = 0;
|
||||
let field = attr.getField(name);
|
||||
if (field) {
|
||||
colorID = field.getInt(null);
|
||||
}
|
||||
|
||||
if (colorID) {
|
||||
let typedValue = new android.util.TypedValue();
|
||||
context.getTheme().resolveAttribute(colorID, typedValue, true);
|
||||
result = typedValue.data;
|
||||
}
|
||||
}
|
||||
catch (ex) {
|
||||
traceWrite("Cannot get pallete color: " + name, traceCategories.Error, traceMessageType.error);
|
||||
}
|
||||
|
||||
attrCache.set(name, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function GC() {
|
||||
gc();
|
||||
}
|
||||
|
Reference in New Issue
Block a user