@bubblewrap/core
Version:
Core Library to generate, build and sign TWA projects
150 lines (149 loc) • 6.58 kB
JavaScript
;
/*
* Copyright 2020 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeatureManager = void 0;
const AppsFlyerFeature_1 = require("./AppsFlyerFeature");
const LocationDelegationFeature_1 = require("./LocationDelegationFeature");
const PlayBillingFeature_1 = require("./PlayBillingFeature");
const FirstRunFlagFeature_1 = require("./FirstRunFlagFeature");
const Log_1 = require("../Log");
const ArCoreFeature_1 = require("./ArCoreFeature");
const ANDROID_BROWSER_HELPER_VERSIONS = {
stable: 'com.google.androidbrowserhelper:androidbrowserhelper:2.5.0',
alpha: 'com.google.androidbrowserhelper:androidbrowserhelper:2.5.0',
};
/**
* Analyzes a TwaManifest to collect enable features and aggregates all customizations that will
* be applied when generating the Android project.
*/
class FeatureManager {
/**
* Builds a new intance from a TwaManifest.
*/
constructor(twaManifest, log = new Log_1.ConsoleLog('FeatureManager')) {
var _a, _b, _c, _d, _e, _f, _g;
this.buildGradle = {
repositories: new Set(),
dependencies: new Set(),
};
this.androidManifest = {
permissions: new Set(),
components: new Array(),
applicationMetadata: new Array(),
};
this.applicationClass = {
imports: new Set(),
variables: new Array(),
onCreate: new Array(),
};
this.launcherActivity = {
imports: new Set(),
methods: new Set(),
variables: new Set(),
launchUrl: new Array(),
};
this.delegationService = {
imports: new Set(),
onCreate: new Array(),
};
if ((_a = twaManifest.features.locationDelegation) === null || _a === void 0 ? void 0 : _a.enabled) {
this.addFeature(new LocationDelegationFeature_1.LocationDelegationFeature());
}
if ((_b = twaManifest.features.playBilling) === null || _b === void 0 ? void 0 : _b.enabled) {
if ((_c = twaManifest.alphaDependencies) === null || _c === void 0 ? void 0 : _c.enabled) {
this.addFeature(new PlayBillingFeature_1.PlayBillingFeature());
}
else {
log.error('Skipping PlayBillingFeature. ' +
'Enable alphaDependencies to add PlayBillingFeature.');
}
}
if ((_d = twaManifest.features.appsFlyer) === null || _d === void 0 ? void 0 : _d.enabled) {
this.addFeature(new AppsFlyerFeature_1.AppsFlyerFeature(twaManifest.features.appsFlyer));
}
if ((_e = twaManifest.features.firstRunFlag) === null || _e === void 0 ? void 0 : _e.enabled) {
this.addFeature(new FirstRunFlagFeature_1.FirstRunFlagFeature(twaManifest.features.firstRunFlag));
}
// The WebView fallback needs the INTERNET permission.
if (twaManifest.fallbackType === 'webview') {
this.androidManifest.permissions.add('android.permission.INTERNET');
}
if ((_f = twaManifest.alphaDependencies) === null || _f === void 0 ? void 0 : _f.enabled) {
this.buildGradle.dependencies.add(ANDROID_BROWSER_HELPER_VERSIONS.alpha);
}
else {
this.buildGradle.dependencies.add(ANDROID_BROWSER_HELPER_VERSIONS.stable);
}
if ((_g = twaManifest.features.arCore) === null || _g === void 0 ? void 0 : _g.enabled) {
this.addFeature(new ArCoreFeature_1.ArCoreFeature());
}
// Android T+ needs permission to request sending notifications.
if (twaManifest.enableNotifications) {
this.androidManifest.permissions.add('android.permission.POST_NOTIFICATIONS');
}
}
addFeature(feature) {
// Adds properties to build.
feature.buildGradle.repositories.forEach((repo) => {
this.buildGradle.repositories.add(repo);
});
feature.buildGradle.dependencies.forEach((dep) => {
this.buildGradle.dependencies.add(dep);
});
// Adds properties to application.
feature.applicationClass.imports.forEach((imp) => {
this.applicationClass.imports.add(imp);
});
feature.applicationClass.variables.forEach((imp) => {
this.applicationClass.variables.push(imp);
});
if (feature.applicationClass.onCreate) {
this.applicationClass.onCreate.push(feature.applicationClass.onCreate);
}
// Adds properties to AndroidManifest.xml.
feature.androidManifest.permissions.forEach((permission) => {
this.androidManifest.permissions.add(permission);
});
feature.androidManifest.components.forEach((component) => {
this.androidManifest.components.push(component);
});
feature.androidManifest.applicationMetadata.forEach((metadata) => {
this.androidManifest.applicationMetadata.push(metadata);
});
// Adds properties to launcherActivity.
feature.launcherActivity.imports.forEach((imp) => {
this.launcherActivity.imports.add(imp);
});
feature.launcherActivity.variables.forEach((imp) => {
this.launcherActivity.variables.add(imp);
});
feature.launcherActivity.methods.forEach((imp) => {
this.launcherActivity.methods.add(imp);
});
if (feature.launcherActivity.launchUrl) {
this.launcherActivity.launchUrl.push(feature.launcherActivity.launchUrl);
}
// Adds properties to delegationService.
feature.delegationService.imports.forEach((imp) => {
this.delegationService.imports.add(imp);
});
if (feature.delegationService.onCreate) {
this.delegationService.onCreate.push(feature.delegationService.onCreate);
}
}
}
exports.FeatureManager = FeatureManager;