@akylas/nativescript-inappbrowser
Version:
InAppBrowser for NativeScript
229 lines • 10.7 kB
JavaScript
import { Application, Utils } from '@nativescript/core';
import { BROWSER_ACTIVITY_EVENTS, createDismissIntent, createStartIntent } from './ChromeTabsManagerActivity';
import { BROWSER_TYPES, getDefaultOptions } from './InAppBrowser.common';
import { closeAuthSessionPolyfillAsync, DISMISSED_EVENT, getDrawableId, getPreferredPackages, openAuthSessionPolyfillAsync } from './utils.android';
import { tryParseColor } from './utils.common';
export let CustomTabsIntent;
class InAppBrowserModule {
constructor() {
this.animationIdentifierPattern = new RegExp('^.+:.+/');
}
isAvailable() {
const context = Utils.android.getApplicationContext();
const resolveInfos = getPreferredPackages(context);
return Promise.resolve(!(resolveInfos === null || resolveInfos.isEmpty()));
}
async open(url, options) {
const mOpenBrowserPromise = InAppBrowserModule.redirectResolve;
if (mOpenBrowserPromise) {
this.flowDidFinish();
const result = {
type: BROWSER_TYPES.CANCEL
};
return Promise.resolve(result);
}
this.currentActivity = Application.android.foregroundActivity || Application.android.startActivity;
if (!this.currentActivity) {
return Promise.reject(new Error(InAppBrowserModule.ERROR_CODE));
}
const result = new Promise(function (resolve, reject) {
InAppBrowserModule.redirectResolve = resolve;
InAppBrowserModule.redirectReject = reject;
});
const inAppBrowserOptions = getDefaultOptions(url, options);
if (!CustomTabsIntent) {
CustomTabsIntent = androidx.browser.customtabs.CustomTabsIntent;
}
const builder = new CustomTabsIntent.Builder();
let colorString = inAppBrowserOptions[InAppBrowserModule.KEY_TOOLBAR_COLOR];
if (colorString) {
const color = tryParseColor(colorString, 'Invalid toolbar color');
if (color) {
builder.setToolbarColor(color.android);
}
}
colorString = inAppBrowserOptions[InAppBrowserModule.KEY_SECONDARY_TOOLBAR_COLOR];
if (colorString) {
const color = tryParseColor(colorString, 'Invalid secondary toolbar color');
if (color) {
builder.setSecondaryToolbarColor(color.android);
}
}
colorString = inAppBrowserOptions[InAppBrowserModule.KEY_NAVIGATION_BAR_COLOR];
if (colorString) {
const color = tryParseColor(colorString, 'Invalid navigation bar color');
if (color) {
builder.setNavigationBarColor(color.android);
}
}
colorString = inAppBrowserOptions[InAppBrowserModule.KEY_NAVIGATION_BAR_DIVIDER_COLOR];
if (colorString) {
const color = tryParseColor(colorString, 'Invalid navigation bar divider color');
if (color) {
builder.setNavigationBarDividerColor(color.android);
}
}
if (inAppBrowserOptions[InAppBrowserModule.KEY_DEFAULT_SHARE_MENU_ITEM]) {
builder.addDefaultShareMenuItem();
}
const context = Utils.android.getApplicationContext();
if (inAppBrowserOptions[InAppBrowserModule.KEY_ANIMATIONS]) {
const animations = inAppBrowserOptions[InAppBrowserModule.KEY_ANIMATIONS];
this.applyAnimation(context, builder, animations);
}
if (inAppBrowserOptions.backButtonDrawableId) {
builder.setCloseButtonIcon(android.graphics.BitmapFactory.decodeResource(context.getResources(), getDrawableId(inAppBrowserOptions.backButtonDrawableId)));
}
const customTabsIntent = builder.build();
const intent = customTabsIntent.intent;
const keyHeaders = inAppBrowserOptions[InAppBrowserModule.KEY_HEADERS];
if (keyHeaders) {
const headers = new android.os.Bundle();
for (const key in keyHeaders) {
if (keyHeaders.hasOwnProperty(key)) {
headers.putString(key, keyHeaders[key]);
}
}
intent.putExtra(android.provider.Browser.EXTRA_HEADERS, headers);
}
const Intent = android.content.Intent;
if (inAppBrowserOptions[InAppBrowserModule.KEY_FORCE_CLOSE_ON_REDIRECTION]) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
if (!inAppBrowserOptions[InAppBrowserModule.KEY_SHOW_IN_RECENTS]) {
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
}
intent.putExtra(CustomTabsIntent.EXTRA_ENABLE_URLBAR_HIDING, !!inAppBrowserOptions[InAppBrowserModule.KEY_ENABLE_URL_BAR_HIDING]);
try {
if (inAppBrowserOptions[InAppBrowserModule.KEY_BROWSER_PACKAGE] !== undefined) {
const packageName = inAppBrowserOptions[InAppBrowserModule.KEY_BROWSER_PACKAGE];
if (packageName === null || packageName === void 0 ? void 0 : packageName.length) {
intent.setPackage(packageName);
}
}
else {
const packageName = inAppBrowserOptions[InAppBrowserModule.KEY_BROWSER_PACKAGE];
intent.setPackage(packageName);
}
}
catch (error) {
if (error.printStackTrace) {
error.printStackTrace();
}
}
this.registerEvent();
intent.setData(android.net.Uri.parse(url));
if (inAppBrowserOptions[InAppBrowserModule.KEY_SHOW_PAGE_TITLE]) {
builder.setShowTitle(!!inAppBrowserOptions[InAppBrowserModule.KEY_SHOW_PAGE_TITLE]);
}
else {
intent.putExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE);
}
this.currentActivity.startActivity(createStartIntent(this.currentActivity, intent), customTabsIntent.startAnimationBundle);
return result;
}
close() {
if (!InAppBrowserModule.redirectResolve) {
return;
}
if (!this.currentActivity) {
InAppBrowserModule.redirectReject(new Error(InAppBrowserModule.ERROR_CODE));
this.flowDidFinish();
return;
}
BROWSER_ACTIVITY_EVENTS.off(DISMISSED_EVENT);
const result = {
type: 'dismiss'
};
InAppBrowserModule.redirectResolve(result);
this.flowDidFinish();
this.currentActivity.startActivity(createDismissIntent(this.currentActivity));
}
async openAuth(url, redirectUrl, options) {
let response = null;
try {
response = await openAuthSessionPolyfillAsync((startUrl, opt) => this.open(startUrl, opt), url, redirectUrl, options);
}
finally {
closeAuthSessionPolyfillAsync();
this.close();
}
return response;
}
closeAuth() {
closeAuthSessionPolyfillAsync();
this.close();
}
onEvent(event) {
BROWSER_ACTIVITY_EVENTS.off(DISMISSED_EVENT);
if (!InAppBrowserModule.redirectResolve) {
return;
}
const browserEvent = event.object;
if (browserEvent.isError) {
InAppBrowserModule.redirectReject(new Error(browserEvent.message));
}
else {
InAppBrowserModule.redirectResolve({
type: browserEvent.resultType,
message: browserEvent.message
});
}
this.flowDidFinish();
}
registerEvent() {
BROWSER_ACTIVITY_EVENTS.once(DISMISSED_EVENT, (e) => this.onEvent(e));
}
resolveAnimationIdentifierIfNeeded(context, identifier) {
if (this.animationIdentifierPattern.test(identifier)) {
return context.getResources().getIdentifier(identifier, null, null);
}
else {
return context.getResources().getIdentifier(identifier, 'anim', context.getPackageName());
}
}
applyAnimation(context, builder, animations) {
const startEnterAnimationId = animations[InAppBrowserModule.KEY_ANIMATION_START_ENTER]
? this.resolveAnimationIdentifierIfNeeded(context, animations[InAppBrowserModule.KEY_ANIMATION_START_ENTER])
: -1;
const startExitAnimationId = animations[InAppBrowserModule.KEY_ANIMATION_START_EXIT]
? this.resolveAnimationIdentifierIfNeeded(context, animations[InAppBrowserModule.KEY_ANIMATION_START_EXIT])
: -1;
const endEnterAnimationId = animations[InAppBrowserModule.KEY_ANIMATION_END_ENTER]
? this.resolveAnimationIdentifierIfNeeded(context, animations[InAppBrowserModule.KEY_ANIMATION_END_ENTER])
: -1;
const endExitAnimationId = animations[InAppBrowserModule.KEY_ANIMATION_END_EXIT]
? this.resolveAnimationIdentifierIfNeeded(context, animations[InAppBrowserModule.KEY_ANIMATION_END_EXIT])
: -1;
if (startEnterAnimationId !== -1 && startExitAnimationId !== -1) {
builder.setStartAnimations(context, startEnterAnimationId, startExitAnimationId);
}
if (endEnterAnimationId !== -1 && endExitAnimationId !== -1) {
builder.setExitAnimations(context, endEnterAnimationId, endExitAnimationId);
}
}
flowDidFinish() {
InAppBrowserModule.redirectResolve = null;
InAppBrowserModule.redirectReject = null;
}
}
InAppBrowserModule.ERROR_CODE = 'InAppBrowser';
InAppBrowserModule.KEY_TOOLBAR_COLOR = 'toolbarColor';
InAppBrowserModule.KEY_SECONDARY_TOOLBAR_COLOR = 'secondaryToolbarColor';
InAppBrowserModule.KEY_NAVIGATION_BAR_COLOR = 'navigationBarColor';
InAppBrowserModule.KEY_NAVIGATION_BAR_DIVIDER_COLOR = 'navigationBarDividerColor';
InAppBrowserModule.KEY_ENABLE_URL_BAR_HIDING = 'enableUrlBarHiding';
InAppBrowserModule.KEY_SHOW_PAGE_TITLE = 'showTitle';
InAppBrowserModule.KEY_DEFAULT_SHARE_MENU_ITEM = 'enableDefaultShare';
InAppBrowserModule.KEY_FORCE_CLOSE_ON_REDIRECTION = 'forceCloseOnRedirection';
InAppBrowserModule.KEY_ANIMATIONS = 'animations';
InAppBrowserModule.KEY_HEADERS = 'headers';
InAppBrowserModule.KEY_ANIMATION_START_ENTER = 'startEnter';
InAppBrowserModule.KEY_ANIMATION_START_EXIT = 'startExit';
InAppBrowserModule.KEY_ANIMATION_END_ENTER = 'endEnter';
InAppBrowserModule.KEY_ANIMATION_END_EXIT = 'endExit';
InAppBrowserModule.KEY_BROWSER_PACKAGE = 'browserPackage';
InAppBrowserModule.KEY_SHOW_IN_RECENTS = 'showInRecents';
export const InAppBrowser = new InAppBrowserModule;
//# sourceMappingURL=InAppBrowser.android.js.map