UNPKG

@bubblewrap/core

Version:

Core Library to generate, build and sign TWA projects

160 lines (159 loc) 7.09 kB
"use strict"; /* * 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 ProtocolHandlersFeature_1 = require("./ProtocolHandlersFeature"); const FileHandlingFeature_1 = require("./FileHandlingFeature"); const ANDROID_BROWSER_HELPER_VERSIONS = { stable: 'com.google.androidbrowserhelper:androidbrowserhelper:2.6.2', alpha: 'com.google.androidbrowserhelper:androidbrowserhelper:2.6.2', }; /** * 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; this.buildGradle = { repositories: new Set(), dependencies: new Set(), configs: new Set(), }; this.androidManifest = { permissions: new Set(), components: new Array(), applicationMetadata: new Array(), launcherActivityEntries: 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) { this.addFeature(new PlayBillingFeature_1.PlayBillingFeature()); } if ((_c = twaManifest.features.appsFlyer) === null || _c === void 0 ? void 0 : _c.enabled) { this.addFeature(new AppsFlyerFeature_1.AppsFlyerFeature(twaManifest.features.appsFlyer)); } if ((_d = twaManifest.features.firstRunFlag) === null || _d === void 0 ? void 0 : _d.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 ((_e = twaManifest.alphaDependencies) === null || _e === void 0 ? void 0 : _e.enabled) { this.buildGradle.dependencies.add(ANDROID_BROWSER_HELPER_VERSIONS.alpha); } else { this.buildGradle.dependencies.add(ANDROID_BROWSER_HELPER_VERSIONS.stable); } if ((_f = twaManifest.features.arCore) === null || _f === void 0 ? void 0 : _f.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'); } if (twaManifest.protocolHandlers) { this.addFeature(new ProtocolHandlersFeature_1.ProtocolHandlersFeature(twaManifest.protocolHandlers)); } if (twaManifest.fileHandlers) { this.addFeature(new FileHandlingFeature_1.FileHandlingFeature(twaManifest.fileHandlers)); } } 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); }); feature.buildGradle.configs.forEach((dep) => { this.buildGradle.configs.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); }); feature.androidManifest.launcherActivityEntries.forEach((entry) => { this.androidManifest.launcherActivityEntries.push(entry); }); // 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;