@@ -7,168 +7,174 @@ let applicationContext: android.content.Context;
77let contextResources : android . content . res . Resources ;
88let packageName : string ;
99
10- export function getApplicationContext ( ) {
11- if ( ! applicationContext ) {
12- applicationContext = getApplication ( ) . getApplicationContext ( ) ;
13- }
14-
15- return applicationContext ;
10+ export function getApplicationContext ( ) : android . content . Context {
11+ if ( ! applicationContext ) {
12+ applicationContext = getApplication ( ) . getApplicationContext ( ) ;
13+ }
14+ return applicationContext ;
1615}
17- export function getCurrentActivity ( ) {
18- if ( ! Application ) {
19- return null ;
20- }
21- return Application . android . foregroundActivity || Application . android . startActivity ;
16+
17+ export function getCurrentActivity ( ) : android . app . Activity | null {
18+ if ( ! Application . android ) {
19+ return null ;
20+ }
21+ return Application . android . foregroundActivity || Application . android . startActivity ;
2222}
23- export function getApplication ( ) {
24- if ( ! application ) {
25- application = Application . android . getNativeApplication ( ) ;
26- }
2723
28- return application ;
24+ export function getApplication ( ) : android . app . Application {
25+ if ( ! application ) {
26+ application = Application . android . getNativeApplication ( ) ;
27+ }
28+ return application ;
2929}
30- export function getResources ( ) {
31- if ( ! contextResources ) {
32- contextResources = getApplication ( ) . getResources ( ) ;
33- }
3430
35- return contextResources ;
31+ export function getResources ( ) : android . content . res . Resources {
32+ if ( ! contextResources ) {
33+ contextResources = getApplication ( ) . getResources ( ) ;
34+ }
35+ return contextResources ;
3636}
37- export function getPackageName ( ) {
38- if ( ! packageName ) {
39- packageName = getApplicationContext ( ) . getPackageName ( ) ;
40- }
4137
42- return packageName ;
38+ export function getPackageName ( ) : string {
39+ if ( ! packageName ) {
40+ packageName = getApplicationContext ( ) . getPackageName ( ) ;
41+ }
42+ return packageName ;
4343}
4444
4545let inputMethodManager : android . view . inputmethod . InputMethodManager ;
46- export function getInputMethodManager ( ) : android . view . inputmethod . InputMethodManager {
47- if ( ! inputMethodManager ) {
48- inputMethodManager = < android . view . inputmethod . InputMethodManager > getApplicationContext ( ) . getSystemService ( android . content . Context . INPUT_METHOD_SERVICE ) ;
49- }
5046
51- return inputMethodManager ;
47+ export function getInputMethodManager ( ) : android . view . inputmethod . InputMethodManager {
48+ if ( ! inputMethodManager ) {
49+ inputMethodManager = < android . view . inputmethod . InputMethodManager > (
50+ getApplicationContext ( ) . getSystemService ( android . content . Context . INPUT_METHOD_SERVICE )
51+ ) ;
52+ }
53+ return inputMethodManager ;
5254}
5355
5456export function showSoftInput ( nativeView : android . view . View ) : void {
55- const inputManager = getInputMethodManager ( ) ;
56- if ( inputManager && nativeView instanceof android . view . View ) {
57- inputManager . showSoftInput ( nativeView , android . view . inputmethod . InputMethodManager . SHOW_IMPLICIT ) ;
58- }
57+ const inputManager = getInputMethodManager ( ) ;
58+ if ( inputManager && nativeView instanceof android . view . View ) {
59+ inputManager . showSoftInput ( nativeView , android . view . inputmethod . InputMethodManager . SHOW_IMPLICIT ) ;
60+ }
5961}
6062
6163export function dismissSoftInput ( nativeView ?: android . view . View ) : void {
62- const inputManager = getInputMethodManager ( ) ;
63- let windowToken : android . os . IBinder ;
64-
65- if ( nativeView instanceof android . view . View ) {
66- if ( ! nativeView . hasFocus ( ) ) {
67- return ;
68- }
69- windowToken = nativeView . getWindowToken ( ) ;
70- } else if ( getCurrentActivity ( ) instanceof androidx . appcompat . app . AppCompatActivity ) {
71- const modalDialog = ( topmost ( ) ?. _modalParent ?? ( topmost ( ) ?. modal as any ) ) ?. _dialogFragment ?. getDialog ( ) ;
72- const window = ( modalDialog ?? getCurrentActivity ( ) ) . getWindow ( ) ;
73- const decorView = window . getDecorView ( ) ;
74- if ( decorView ) {
75- windowToken = decorView . getWindowToken ( ) ;
76- decorView . requestFocus ( ) ;
77- } else {
78- windowToken = null ;
79- }
80- }
81-
82- if ( inputManager && windowToken ) {
83- inputManager . hideSoftInputFromWindow ( windowToken , 0 ) ;
84- }
64+ const inputManager = getInputMethodManager ( ) ;
65+ let windowToken : android . os . IBinder | null = null ;
66+
67+ if ( nativeView instanceof android . view . View ) {
68+ if ( ! nativeView . hasFocus ( ) ) {
69+ return ;
70+ }
71+ windowToken = nativeView . getWindowToken ( ) ;
72+ } else {
73+ const currentActivity = getCurrentActivity ( ) ;
74+ if ( currentActivity instanceof androidx . appcompat . app . AppCompatActivity ) {
75+ const currentTopmost = topmost ( ) ;
76+ const modalDialog =
77+ ( currentTopmost ?. _modalParent ?? ( currentTopmost ?. modal as any ) ) ?. _dialogFragment ?. getDialog ( ) ;
78+ const window = modalDialog ?? currentActivity . getWindow ( ) ;
79+ const decorView = window . getDecorView ( ) ;
80+ if ( decorView ) {
81+ windowToken = decorView . getWindowToken ( ) ;
82+ decorView . requestFocus ( ) ;
83+ }
84+ }
85+ }
86+
87+ if ( inputManager && windowToken ) {
88+ inputManager . hideSoftInputFromWindow ( windowToken , 0 ) ;
89+ }
8590}
8691
8792export namespace collections {
88- export function stringArrayToStringSet ( str : string [ ] ) : java . util . HashSet < string > {
89- const hashSet = new java . util . HashSet < string > ( ) ;
90- if ( str !== undefined ) {
91- for ( const element in str ) {
92- hashSet . add ( '' + str [ element ] ) ;
93- }
94- }
95-
96- return hashSet ;
97- }
98-
99- export function stringSetToStringArray ( stringSet : any ) : string [ ] {
100- const arr = [ ] ;
101- if ( stringSet !== undefined ) {
102- const it = stringSet . iterator ( ) ;
103- while ( it . hasNext ( ) ) {
104- const element = '' + it . next ( ) ;
105- arr . push ( element ) ;
106- }
107- }
108-
109- return arr ;
110- }
93+ export function stringArrayToStringSet ( str : string [ ] ) : java . util . HashSet < string > {
94+ const hashSet = new java . util . HashSet < string > ( ) ;
95+ if ( str !== undefined ) {
96+ for ( const element of str ) {
97+ hashSet . add ( '' + element ) ;
98+ }
99+ }
100+ return hashSet ;
101+ }
102+
103+ export function stringSetToStringArray ( stringSet : java . util . Set < string > ) : string [ ] {
104+ const arr : string [ ] = [ ] ;
105+ if ( stringSet !== undefined ) {
106+ const it = stringSet . iterator ( ) ;
107+ while ( it . hasNext ( ) ) {
108+ const element = '' + it . next ( ) ;
109+ arr . push ( element ) ;
110+ }
111+ }
112+ return arr ;
113+ }
111114}
112115
113116export namespace resources {
114- let attr ;
115- const attrCache = new Map < string , number > ( ) ;
116-
117- export function getDrawableId ( name ) {
118- return getId ( ':drawable/' + name ) ;
119- }
120-
121- export function getStringId ( name ) {
122- return getId ( ':string/' + name ) ;
123- }
124-
125- export function getId ( name : string ) : number {
126- const resources = getResources ( ) ;
127- const packageName = getPackageName ( ) ;
128- const uri = packageName + name ;
129-
130- return resources . getIdentifier ( uri , null , null ) ;
131- }
132- export function getResource ( name : string , type ?: string ) : number {
133- return getResources ( ) . getIdentifier ( name , type , getPackageName ( ) ) ;
134- }
135- export function getPalleteColor ( name : string , context : android . content . Context ) : number {
136- return getPaletteColor ( name , context ) ;
137- }
138- export function getPaletteColor ( name : string , context : android . content . Context ) : number {
139- if ( attrCache . has ( name ) ) {
140- return attrCache . get ( name ) ;
141- }
142-
143- let result = 0 ;
144- try {
145- if ( ! attr ) {
146- attr = java . lang . Class . forName ( 'androidx.appcompat.R$attr' ) ;
147- }
148-
149- let colorID = 0 ;
150- const field = attr . getField ( name ) ;
151- if ( field ) {
152- colorID = field . getInt ( null ) ;
153- }
154-
155- if ( colorID ) {
156- const typedValue = new android . util . TypedValue ( ) ;
157- context . getTheme ( ) . resolveAttribute ( colorID , typedValue , true ) ;
158- result = typedValue . data ;
159- }
160- } catch ( ex ) {
161- Trace . write ( 'Cannot get pallete color: ' + name , Trace . categories . Error , Trace . messageType . error ) ;
162- }
163-
164- attrCache . set ( name , result ) ;
165-
166- return result ;
167- }
117+ let attr : any ;
118+ const attrCache = new Map < string , number > ( ) ;
119+
120+ export function getDrawableId ( name : string ) : number {
121+ return getId ( ':drawable/' + name ) ;
122+ }
123+
124+ export function getStringId ( name : string ) : number {
125+ return getId ( ':string/' + name ) ;
126+ }
127+
128+ export function getId ( name : string ) : number {
129+ const resources = getResources ( ) ;
130+ const packageName = getPackageName ( ) ;
131+ const uri = packageName + name ;
132+ return resources . getIdentifier ( uri , null , null ) ;
133+ }
134+
135+ export function getResource ( name : string , type ?: string ) : number {
136+ return getResources ( ) . getIdentifier ( name , type || null , getPackageName ( ) ) ;
137+ }
138+
139+ export function getPaletteColor ( name : string , context : android . content . Context ) : number {
140+ if ( attrCache . has ( name ) ) {
141+ return attrCache . get ( name ) as number ;
142+ }
143+
144+ let result = 0 ;
145+ try {
146+ if ( ! attr ) {
147+ attr = java . lang . Class . forName ( 'androidx.appcompat.R$attr' ) ;
148+ }
149+
150+ let colorID = 0 ;
151+ const field = attr . getField ( name ) ;
152+ if ( field ) {
153+ colorID = field . getInt ( null ) ;
154+ }
155+
156+ if ( colorID ) {
157+ const typedValue = new android . util . TypedValue ( ) ;
158+ context . getTheme ( ) . resolveAttribute ( colorID , typedValue , true ) ;
159+ result = typedValue . data ;
160+ }
161+ } catch ( ex ) {
162+ Trace . write ( `Cannot get palette color: ${ name } - ${ ex } ` , Trace . categories . Error , Trace . messageType . error ) ;
163+ }
164+
165+ attrCache . set ( name , result ) ;
166+ return result ;
167+ }
168168}
169169
170170export function isRealDevice ( ) : boolean {
171- const fingerprint = android . os . Build . FINGERPRINT ;
172-
173- return fingerprint != null && ( fingerprint . indexOf ( 'vbox' ) > - 1 || fingerprint . indexOf ( 'generic' ) > - 1 ) ;
171+ const fingerprint = android . os . Build . FINGERPRINT || '' ;
172+ return (
173+ fingerprint !== '' &&
174+ ! (
175+ fingerprint . toLowerCase ( ) . includes ( 'vbox' ) ||
176+ fingerprint . toLowerCase ( ) . includes ( 'generic' ) ||
177+ fingerprint . toLowerCase ( ) . includes ( 'emulator' )
178+ )
179+ ) ;
174180}
0 commit comments