@nativescript/core
Version:
A JavaScript library providing an easy to use api for interacting with iOS and Android platform APIs.
160 lines • 5.43 kB
JavaScript
import { Application } from '../../application';
import { Trace } from '../../trace';
import { topmost } from '../../ui/frame/frame-stack';
let application;
let applicationContext;
let contextResources;
let packageName;
export function getApplicationContext() {
if (!applicationContext) {
applicationContext = getApplication().getApplicationContext();
}
return applicationContext;
}
export function getCurrentActivity() {
if (!Application) {
return null;
}
return Application.android.foregroundActivity || Application.android.startActivity;
}
export function getApplication() {
if (!application) {
application = Application.android.getNativeApplication();
}
return application;
}
export function getResources() {
if (!contextResources) {
contextResources = getApplication().getResources();
}
return contextResources;
}
export function getPackageName() {
if (!packageName) {
packageName = getApplicationContext().getPackageName();
}
return packageName;
}
let inputMethodManager;
export function getInputMethodManager() {
if (!inputMethodManager) {
inputMethodManager = getApplicationContext().getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
}
return inputMethodManager;
}
export function showSoftInput(nativeView) {
const inputManager = getInputMethodManager();
if (inputManager && nativeView instanceof android.view.View) {
inputManager.showSoftInput(nativeView, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
}
}
export function dismissSoftInput(nativeView) {
const inputManager = getInputMethodManager();
let windowToken;
if (nativeView instanceof android.view.View) {
if (!nativeView.hasFocus()) {
return;
}
windowToken = nativeView.getWindowToken();
}
else if (getCurrentActivity() instanceof androidx.appcompat.app.AppCompatActivity) {
const modalDialog = (topmost()?._modalParent ?? topmost()?.modal)?._dialogFragment?.getDialog();
const window = (modalDialog ?? getCurrentActivity()).getWindow();
const decorView = window.getDecorView();
if (decorView) {
windowToken = decorView.getWindowToken();
decorView.requestFocus();
}
else {
windowToken = null;
}
}
if (inputManager && windowToken) {
inputManager.hideSoftInputFromWindow(windowToken, 0);
}
}
export var collections;
(function (collections) {
function stringArrayToStringSet(str) {
const hashSet = new java.util.HashSet();
if (str !== undefined) {
for (const element in str) {
hashSet.add('' + str[element]);
}
}
return hashSet;
}
collections.stringArrayToStringSet = stringArrayToStringSet;
function stringSetToStringArray(stringSet) {
const arr = [];
if (stringSet !== undefined) {
const it = stringSet.iterator();
while (it.hasNext()) {
const element = '' + it.next();
arr.push(element);
}
}
return arr;
}
collections.stringSetToStringArray = stringSetToStringArray;
})(collections || (collections = {}));
export var resources;
(function (resources_1) {
let attr;
const attrCache = new Map();
function getDrawableId(name) {
return getId(':drawable/' + name);
}
resources_1.getDrawableId = getDrawableId;
function getStringId(name) {
return getId(':string/' + name);
}
resources_1.getStringId = getStringId;
function getId(name) {
const resources = getResources();
const packageName = getPackageName();
const uri = packageName + name;
return resources.getIdentifier(uri, null, null);
}
resources_1.getId = getId;
function getResource(name, type) {
return getResources().getIdentifier(name, type, getPackageName());
}
resources_1.getResource = getResource;
function getPalleteColor(name, context) {
return getPaletteColor(name, context);
}
resources_1.getPalleteColor = getPalleteColor;
function getPaletteColor(name, context) {
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;
const field = attr.getField(name);
if (field) {
colorID = field.getInt(null);
}
if (colorID) {
const typedValue = new android.util.TypedValue();
context.getTheme().resolveAttribute(colorID, typedValue, true);
result = typedValue.data;
}
}
catch (ex) {
Trace.write('Cannot get pallete color: ' + name, Trace.categories.Error, Trace.messageType.error);
}
attrCache.set(name, result);
return result;
}
resources_1.getPaletteColor = getPaletteColor;
})(resources || (resources = {}));
export function isRealDevice() {
const fingerprint = android.os.Build.FINGERPRINT;
return fingerprint != null && (fingerprint.indexOf('vbox') > -1 || fingerprint.indexOf('generic') > -1);
}
//# sourceMappingURL=index.js.map