• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2023 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 */
15import { Configuration } from '@ohos.app.ability.Configuration';
16import type common from '@ohos.app.ability.common'
17import { ConfigChangeListener } from './Interfaces';
18import EnvironmentCallback from '@ohos.app.ability.EnvironmentCallback';
19import { Log } from '../utils/Log';
20import AbilityConstant from '@ohos.app.ability.AbilityConstant';
21import { SingletonHelper } from '../utils/SingletonHelper';
22
23const TAG: string = 'ConfigManager';
24/**
25 * 环境配置信息变化管理类
26 */
27class ConfigManager {
28  /**
29   * 属性变化事件
30   */
31  private mConfigInfo?: Configuration;
32  /**
33   * 应用上下文
34   */
35  private mContext?: common.UIAbilityContext;
36  /**
37   * EnviornmentCallback id
38   */
39  private mCallbackId: number = -1;
40  private mListeners: Map<string, ConfigChangeListener> = new Map();
41  private isStarted: boolean = false;
42  /**
43   * 环境变化回调
44   */
45  private mEnivornmentCallback: EnvironmentCallback = {
46    onConfigurationUpdated(config: Configuration): void {
47      Log.info(TAG, 'onConfigurationUpdated configChange');
48      configMgr.notifyListeners(config);
49    },
50    onMemoryLevel(level: AbilityConstant.MemoryLevel): void {
51      Log.info(TAG, 'Method to trim memory yet not in use, NULL METHOD');
52    }
53  };
54
55  /**
56   * 初始化
57   *
58   * @param context context
59   */
60  onStart(context: common.UIAbilityContext) :void {
61    if (this.isStarted) {
62      return;
63    }
64    this.isStarted = true;
65    this.mConfigInfo = context?.config;
66    this.mContext = context;
67    Log.debug(TAG, `onStart config: ${JSON.stringify(this.mConfigInfo)}`);
68    // 注册环境回调
69    this.mCallbackId = context?.getApplicationContext()?.on('environment', this.mEnivornmentCallback);
70  }
71
72  onStop(): void {
73    if (!this.isStarted) {
74      return;
75    }
76    this.isStarted = false;
77    // 解注册环境回调
78    this.mContext?.getApplicationContext()?.off('environment', this.mCallbackId, (error, data) =>{
79      if (error && error.code !== 0) {
80        Log.error(TAG, `unregisterEnvironmentCallback fail, error: ${JSON.stringify(error)}`);
81      } else {
82        Log.info(TAG, `unregisterEnvironmentCallback success, data: ${JSON.stringify(data)}`);
83      }
84    });
85  }
86
87  registerConfigManager(key: string, listener: ConfigChangeListener): void {
88    Log.info(TAG, `registerConfigManager key: ${key}`);
89    if (key === null || listener === null) {
90      return;
91    }
92    this.mListeners?.set(key, listener);
93  }
94
95  unregisterConfigManager(key: string): void {
96    if (key === null) {
97      return;
98    }
99    this.mListeners?.delete(key);
100  }
101
102  /**
103   * 通知其他listener
104   *
105   * @param config config
106   */
107  notifyListeners(config: Configuration): void {
108    this.mConfigInfo = config;
109    Log.info(TAG, 'notifyListeners enter.');
110    this.mListeners?.forEach((listener, key) => {
111      Log.debug(TAG, `notifyListeners key: ${key}`);
112      listener?.notifyConfigurationChanged(this.mConfigInfo);
113    });
114    Log.info(TAG, 'notifyListeners end.');
115  }
116
117  /**
118   * 获取当前属性
119   *
120   * @returns
121   */
122  getConfiguration(): Configuration {
123    return this.mConfigInfo;
124  }
125}
126
127export let configMgr = SingletonHelper.getInstance(ConfigManager, TAG);