1/**
2 * Copyright (c) 2021 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 LogUtil from '../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';
17
18const TAG: string = 'AbilityContextManager : ';
19
20/*
21Ability Context管理类
222023-12-11
23*/
24export class AbilityContextManager {
25  private contexts: Array<any> = new Array();
26
27  static getInstance(): AbilityContextManager {
28    if (!Boolean(AppStorage.get<AbilityContextManager>('abilityContextManager')).valueOf()) {
29      AppStorage.setOrCreate('abilityContextManager', new AbilityContextManager());
30    }
31    return AppStorage.get<AbilityContextManager>('abilityContextManager') as AbilityContextManager
32  }
33
34  private addAbilityContext(context: any): void {
35    this.contexts.push(context);
36    AppStorage.setOrCreate('pageContext', this.getAbilityContext());
37  }
38
39  private removeAbilityContext(context: any): void {
40    for (let index = 0; index < this.contexts.length; index++) {
41      let current = this.contexts[index];
42      if (current == context) {
43        this.contexts.splice(index, 1);
44      }
45    }
46    AppStorage.setOrCreate('pageContext', this.getAbilityContext());
47  }
48
49  private getAbilityContext(): any {
50    LogUtil.info(`${TAG} getContext length ${this.contexts.length}`);
51    if (this.contexts.length <= 0) {
52      return null;
53    }
54    return this.contexts[0];
55  }
56
57  static addContext(context: any): void {
58    AbilityContextManager.getInstance().addAbilityContext(context);
59  }
60
61  static removeContext(context: any): void {
62    AbilityContextManager.getInstance().removeAbilityContext(context);
63  }
64
65  static getContext(context: any): void {
66    AbilityContextManager.getInstance().getAbilityContext();
67  }
68}
69
70