Skip to content

Fix: correct isRealDevice logic, add null checks, fix typo, improve e… #10751

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 144 additions & 138 deletions packages/core/utils/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,168 +7,174 @@ 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 getApplicationContext(): android.content.Context {
if (!applicationContext) {
applicationContext = getApplication().getApplicationContext();
}
return applicationContext;
}
export function getCurrentActivity() {
if (!Application) {
return null;
}
return Application.android.foregroundActivity || Application.android.startActivity;

export function getCurrentActivity(): android.app.Activity | null {
if (!Application.android) {
return null;
}
return Application.android.foregroundActivity || Application.android.startActivity;
}
export function getApplication() {
if (!application) {
application = Application.android.getNativeApplication();
}

return application;
export function getApplication(): android.app.Application {
if (!application) {
application = Application.android.getNativeApplication();
}
return application;
}
export function getResources() {
if (!contextResources) {
contextResources = getApplication().getResources();
}

return contextResources;
export function getResources(): android.content.res.Resources {
if (!contextResources) {
contextResources = getApplication().getResources();
}
return contextResources;
}
export function getPackageName() {
if (!packageName) {
packageName = getApplicationContext().getPackageName();
}

return packageName;
export function getPackageName(): string {
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 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);
}
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) {
if (!nativeView.hasFocus()) {
return;
}
windowToken = nativeView.getWindowToken();
} else if (getCurrentActivity() instanceof androidx.appcompat.app.AppCompatActivity) {
const modalDialog = (topmost()?._modalParent ?? (topmost()?.modal as any))?._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);
}
const inputManager = getInputMethodManager();
let windowToken: android.os.IBinder | null = null;

if (nativeView instanceof android.view.View) {
if (!nativeView.hasFocus()) {
return;
}
windowToken = nativeView.getWindowToken();
} else {
const currentActivity = getCurrentActivity();
if (currentActivity instanceof androidx.appcompat.app.AppCompatActivity) {
const currentTopmost = topmost();
const modalDialog =
(currentTopmost?._modalParent ?? (currentTopmost?.modal as any))?._dialogFragment?.getDialog();
const window = modalDialog ?? currentActivity.getWindow();
const decorView = window.getDecorView();
if (decorView) {
windowToken = decorView.getWindowToken();
decorView.requestFocus();
}
}
}

if (inputManager && windowToken) {
inputManager.hideSoftInputFromWindow(windowToken, 0);
}
}

export namespace collections {
export function stringArrayToStringSet(str: string[]): java.util.HashSet<string> {
const hashSet = new java.util.HashSet<string>();
if (str !== undefined) {
for (const 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 function stringArrayToStringSet(str: string[]): java.util.HashSet<string> {
const hashSet = new java.util.HashSet<string>();
if (str !== undefined) {
for (const element of str) {
hashSet.add('' + element);
}
}
return hashSet;
}

export function stringSetToStringArray(stringSet: java.util.Set<string>): string[] {
const arr: string[] = [];
if (stringSet !== undefined) {
const it = stringSet.iterator();
while (it.hasNext()) {
const element = '' + it.next();
arr.push(element);
}
}
return arr;
}
}

export namespace 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 getResource(name: string, type?: string): number {
return getResources().getIdentifier(name, type, getPackageName());
}
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;
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;
}
let attr: any;
const attrCache = new Map<string, number>();

export function getDrawableId(name: string): number {
return getId(':drawable/' + name);
}

export function getStringId(name: string): number {
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 getResource(name: string, type?: string): number {
return getResources().getIdentifier(name, type || null, getPackageName());
}

export function getPaletteColor(name: string, context: android.content.Context): number {
if (attrCache.has(name)) {
return attrCache.get(name) as number;
}

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 palette color: ${name} - ${ex}`, Trace.categories.Error, Trace.messageType.error);
}

attrCache.set(name, result);
return result;
}
}

export function isRealDevice(): boolean {
const fingerprint = android.os.Build.FINGERPRINT;

return fingerprint != null && (fingerprint.indexOf('vbox') > -1 || fingerprint.indexOf('generic') > -1);
const fingerprint = android.os.Build.FINGERPRINT || '';
return (
fingerprint !== '' &&
!(
fingerprint.toLowerCase().includes('vbox') ||
fingerprint.toLowerCase().includes('generic') ||
fingerprint.toLowerCase().includes('emulator')
)
);
}
Loading