1- import { Color , Utils , Application , EventData } from '@nativescript/core' ;
21import Uri = android . net . Uri ;
32import Bundle = android . os . Bundle ;
3+ import TextUtils = android . text . TextUtils ;
44import Intent = android . content . Intent ;
55import Context = android . content . Context ;
6+ import Color = android . graphics . Color ;
7+ import BitmapFactory = android . graphics . BitmapFactory ;
68import Browser = android . provider . Browser ;
79import Pattern = java . util . regex . Pattern ;
810import AssertionError = java . lang . AssertionError ;
911
12+ import { Utils , Application , EventData } from '@nativescript/core' ;
1013import {
1114 ChromeTabsEvent ,
1215 BROWSER_ACTIVITY_EVENTS ,
1316 createStartIntent ,
1417 createDismissIntent
1518} from './ChromeTabsManagerActivity' ;
16-
1719import {
20+ Animations ,
1821 BrowserResult ,
19- AuthSessionResult ,
20- getDefaultOptions
22+ getDefaultOptions ,
23+ InAppBrowserOptions ,
24+ InAppBrowserClassMethods
2125} from './InAppBrowser.common' ;
22-
2326import {
27+ Builder ,
28+ getDrawableId ,
29+ toolbarIsLight ,
30+ CustomTabsIntent ,
31+ ARROW_BACK_WHITE ,
32+ ARROW_BACK_BLACK ,
33+ getPreferredPackages ,
2434 openAuthSessionPolyfillAsync ,
25- closeAuthSessionPolyfillAsync
35+ closeAuthSessionPolyfillAsync ,
2636} from './utils.android' ;
27- import { InAppBrowserClassMethods } from '.' ;
2837
2938declare let global : any ;
3039
31- type Builder = androidx . browser . customtabs . CustomTabsIntent . Builder ;
32- const CustomTabsIntent = ( useAndroidX ( ) ? androidx . browser : android . support ) . customtabs . CustomTabsIntent ;
33- function useAndroidX ( ) {
34- return global . androidx && global . androidx . browser ;
35- }
36-
37- type Animations = {
38- startEnter : string ,
39- startExit : string ,
40- endEnter : string ,
41- endExit : string
42- } ;
43-
44- type InAppBrowserOptions = {
45- showTitle ?: boolean ,
46- toolbarColor ?: string ,
47- secondaryToolbarColor ?: string ,
48- enableUrlBarHiding ?: boolean ,
49- enableDefaultShare ?: boolean ,
50- forceCloseOnRedirection ?: boolean ,
51- animations ?: Animations ,
52- headers ?: { [ key : string ] : string }
53- } ;
54-
5540let InAppBrowserModuleInstance : InAppBrowserClassMethods ;
5641
5742function setup ( ) {
5843 @NativeClass ( )
59- class InAppBrowserModule extends java . lang . Object {
44+ class InAppBrowserModule extends java . lang . Object implements InAppBrowserClassMethods {
6045 private static ERROR_CODE = "InAppBrowser" ;
6146 private static KEY_TOOLBAR_COLOR = "toolbarColor" ;
6247 private static KEY_SECONDARY_TOOLBAR_COLOR = "secondaryToolbarColor" ;
@@ -70,9 +55,13 @@ function setup() {
7055 private static KEY_ANIMATION_START_EXIT = "startExit" ;
7156 private static KEY_ANIMATION_END_ENTER = "endEnter" ;
7257 private static KEY_ANIMATION_END_EXIT = "endExit" ;
58+ private static KEY_HAS_BACK_BUTTON = "hasBackButton" ;
59+ private static KEY_BROWSER_PACKAGE = "browserPackage" ;
60+ private static KEY_SHOW_IN_RECENTS = "showInRecents" ;
7361
7462 private static redirectResolve : any ;
7563 private static redirectReject : any ;
64+ private isLightTheme : Boolean ;
7665 private currentActivity : any ;
7766 private animationIdentifierPattern = Pattern . compile ( "^.+:.+/" ) ;
7867
@@ -81,13 +70,15 @@ function setup() {
8170 return global . __native ( this ) ;
8271 }
8372
84- isAvailable ( ) : Promise < boolean > {
85- return Promise . resolve ( true ) ;
73+ isAvailable ( ) {
74+ const context = Utils . android . getApplicationContext ( ) ;
75+ const resolveInfos = getPreferredPackages ( context ) ;
76+ return Promise . resolve ( ! ( resolveInfos === null || resolveInfos . isEmpty ( ) ) ) ;
8677 }
8778
8879 open (
8980 url : string ,
90- options : InAppBrowserOptions = { }
81+ options ? : InAppBrowserOptions ,
9182 ) : Promise < BrowserResult > {
9283 const mOpenBrowserPromise = InAppBrowserModule . redirectResolve ;
9384 if ( mOpenBrowserPromise ) {
@@ -103,13 +94,14 @@ function setup() {
10394 return Promise . reject ( new Error ( InAppBrowserModule . ERROR_CODE ) ) ;
10495 }
10596
106- const inAppBrowserOptions : InAppBrowserOptions = getDefaultOptions ( url , options ) ;
97+ const inAppBrowserOptions = getDefaultOptions ( url , options ) ;
10798
10899 const builder = new CustomTabsIntent . Builder ( ) ;
109100 if ( inAppBrowserOptions [ InAppBrowserModule . KEY_TOOLBAR_COLOR ] ) {
110101 const colorString = inAppBrowserOptions [ InAppBrowserModule . KEY_TOOLBAR_COLOR ] ;
111102 try {
112- builder . setToolbarColor ( new Color ( colorString ) . android ) ;
103+ builder . setToolbarColor ( Color . parseColor ( colorString ) ) ;
104+ this . isLightTheme = toolbarIsLight ( colorString ) ;
113105 } catch ( error ) {
114106 throw new Error (
115107 "Invalid toolbar color '" + colorString + "': " + error . message ) ;
@@ -118,25 +110,32 @@ function setup() {
118110 if ( inAppBrowserOptions [ InAppBrowserModule . KEY_SECONDARY_TOOLBAR_COLOR ] ) {
119111 const colorString = inAppBrowserOptions [ InAppBrowserModule . KEY_SECONDARY_TOOLBAR_COLOR ] ;
120112 try {
121- builder . setSecondaryToolbarColor ( new Color ( colorString ) . android ) ;
113+ builder . setSecondaryToolbarColor ( Color . parseColor ( colorString ) ) ;
122114 } catch ( error ) {
123115 throw new Error (
124116 "Invalid secondary toolbar color '" + colorString + "': " + error . message ) ;
125117 }
126118 }
127- if ( inAppBrowserOptions [ InAppBrowserModule . KEY_ENABLE_URL_BAR_HIDING ] ) {
128- builder . enableUrlBarHiding ( ) ;
129- }
119+
130120 if ( inAppBrowserOptions [ InAppBrowserModule . KEY_DEFAULT_SHARE_MENU_ITEM ] ) {
131121 builder . addDefaultShareMenuItem ( ) ;
132122 }
133- const context = Utils . android . getApplicationContext ( ) ;
123+ const context = Utils . android . getApplicationContext ( ) as Context ;
134124 if ( inAppBrowserOptions [ InAppBrowserModule . KEY_ANIMATIONS ] ) {
135125 const animations = inAppBrowserOptions [ InAppBrowserModule . KEY_ANIMATIONS ] ;
136126 this . applyAnimation ( context , builder , animations ) ;
137127 }
128+ if ( inAppBrowserOptions [ InAppBrowserModule . KEY_HAS_BACK_BUTTON ] ) {
129+ builder . setCloseButtonIcon ( BitmapFactory . decodeResource (
130+ context . getResources ( ) ,
131+ this . isLightTheme
132+ ? getDrawableId ( ARROW_BACK_BLACK )
133+ : getDrawableId ( ARROW_BACK_WHITE )
134+ ) ) ;
135+ }
138136
139137 const customTabsIntent = builder . build ( ) ;
138+ const intent = customTabsIntent . intent ;
140139
141140 const keyHeaders = inAppBrowserOptions [ InAppBrowserModule . KEY_HEADERS ] ;
142141 if ( keyHeaders ) {
@@ -146,16 +145,36 @@ function setup() {
146145 headers . putString ( key , keyHeaders [ key ] ) ;
147146 }
148147 }
149- customTabsIntent . intent . putExtra ( Browser . EXTRA_HEADERS , headers ) ;
148+ intent . putExtra ( Browser . EXTRA_HEADERS , headers ) ;
150149 }
151150
152151 if ( inAppBrowserOptions [ InAppBrowserModule . KEY_FORCE_CLOSE_ON_REDIRECTION ] ) {
153- customTabsIntent . intent . addFlags ( Intent . FLAG_ACTIVITY_NO_HISTORY ) ;
154- customTabsIntent . intent . addFlags ( Intent . FLAG_ACTIVITY_NEW_TASK ) ;
155- customTabsIntent . intent . addFlags ( Intent . FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS ) ;
152+ intent . addFlags ( Intent . FLAG_ACTIVITY_NEW_TASK ) ;
156153 }
157-
158- const intent = customTabsIntent . intent ;
154+
155+ if ( ! inAppBrowserOptions [ InAppBrowserModule . KEY_SHOW_IN_RECENTS ] ) {
156+ intent . addFlags ( Intent . FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS ) ;
157+ intent . addFlags ( Intent . FLAG_ACTIVITY_NO_HISTORY ) ;
158+ }
159+
160+ intent . putExtra (
161+ CustomTabsIntent . EXTRA_ENABLE_URLBAR_HIDING ,
162+ ! ! inAppBrowserOptions [ InAppBrowserModule . KEY_ENABLE_URL_BAR_HIDING ]
163+ ) ;
164+ try {
165+ if ( inAppBrowserOptions [ InAppBrowserModule . KEY_BROWSER_PACKAGE ] !== undefined ) {
166+ const packageName = inAppBrowserOptions [ InAppBrowserModule . KEY_BROWSER_PACKAGE ] ;
167+ if ( ! TextUtils . isEmpty ( packageName ) ) {
168+ intent . setPackage ( packageName ) ;
169+ }
170+ } else {
171+ const packageName = inAppBrowserOptions [ InAppBrowserModule . KEY_BROWSER_PACKAGE ] ;
172+ intent . setPackage ( packageName ) ;
173+ }
174+ } catch ( error ) {
175+ if ( error . printStackTrace ) error . printStackTrace ( ) ;
176+ }
177+
159178 intent . setData ( Uri . parse ( url ) ) ;
160179 if ( inAppBrowserOptions [ InAppBrowserModule . KEY_SHOW_PAGE_TITLE ] ) {
161180 builder . setShowTitle ( ! ! inAppBrowserOptions [ InAppBrowserModule . KEY_SHOW_PAGE_TITLE ] ) ;
@@ -177,7 +196,7 @@ function setup() {
177196 } ) ;
178197 }
179198
180- public close ( ) : void {
199+ public close ( ) {
181200 if ( ! InAppBrowserModule . redirectResolve ) {
182201 return ;
183202 }
@@ -204,17 +223,18 @@ function setup() {
204223 async openAuth (
205224 url : string ,
206225 redirectUrl : string ,
207- options : InAppBrowserOptions = { }
208- ) : Promise < AuthSessionResult > {
209- const inAppBrowserOptions = getDefaultOptions ( url , options ) ;
226+ options ? : InAppBrowserOptions ,
227+ ) {
228+ let response = null ;
210229 try {
211- return await openAuthSessionPolyfillAsync (
212- url , redirectUrl , inAppBrowserOptions , ( startUrl , opt ) => this . open ( startUrl , opt )
230+ response = await openAuthSessionPolyfillAsync (
231+ ( startUrl , opt ) => this . open ( startUrl , opt ) , url , redirectUrl , options
213232 ) ;
214233 } finally {
215234 closeAuthSessionPolyfillAsync ( ) ;
216235 this . close ( ) ;
217236 }
237+ return response ;
218238 }
219239
220240 public closeAuth ( ) : void {
0 commit comments