1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { Log } from '../../common/src/main/ets/default/utils/Log' 17import { Constants } from './Constants' 18import { Mode } from './Mode' 19 20export class ModeAssembler { 21 private TAG: string = '[ModeAssembler]:' 22 private mNeedAdd: string[] 23 private mNeedDelete: string[] 24 private mMode = new Mode() 25 private mFunctionsMap: Map<string, any> 26 27 constructor(functionsMap: Map<string, any>) { 28 this.mFunctionsMap = functionsMap 29 } 30 public assembler(preMode: string, currentMode: string): void { 31 Log.info(`${this.TAG} assembler preMode = ${preMode} currentMode = ${currentMode} E `) 32 this.mNeedAdd = [] 33 this.mNeedDelete = [] 34 let preModeFun: string[] = this.mMode.getFunctions(preMode) 35 let currentModeFun: string[] = this.mMode.getFunctions(currentMode) 36 37 Log.info(`${this.TAG} assembler preModeFun = ${preModeFun} currentModeFun = ${currentModeFun} `) 38 if (!preMode) { 39 this.mNeedAdd = JSON.parse(JSON.stringify(currentModeFun)) 40 } else if (preMode != currentMode) { 41 this.mNeedAdd = JSON.parse(JSON.stringify(currentModeFun)) 42 for (let fun of preModeFun) { 43 let index = currentModeFun.indexOf(fun) 44 if (index == -1) { 45 this.mNeedDelete.push(fun) 46 } else { 47 this.mNeedAdd.splice(index, 1) 48 } 49 } 50 } 51 Log.info(`${this.TAG} assembler mNeedAdd = ${this.mNeedAdd} mNeedDelete = ${this.mNeedDelete}`) 52 this.attachFunction(this.mNeedAdd) 53 this.detachFunction(this.mNeedDelete) 54 Log.info(`${this.TAG} assembler X`) 55 } 56 57 private attachFunction(item: string[]): void { 58 for (let fun of item) { 59 Log.info(`${this.TAG} attachFunction fun: ${fun}`) 60 this.mFunctionsMap.get(fun).load() 61 } 62 } 63 64 private detachFunction(item: string[]): void { 65 for (let fun of item) { 66 Log.info(`${this.TAG} disattachFunction fun: ${fun}`) 67 this.mFunctionsMap.get(fun).unload() 68 } 69 } 70}