1c36cf2e9Sopenharmony_ci/* 2c36cf2e9Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 3c36cf2e9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4c36cf2e9Sopenharmony_ci * you may not use this file except in compliance with the License. 5c36cf2e9Sopenharmony_ci * You may obtain a copy of the License at 6c36cf2e9Sopenharmony_ci * 7c36cf2e9Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8c36cf2e9Sopenharmony_ci * 9c36cf2e9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10c36cf2e9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11c36cf2e9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12c36cf2e9Sopenharmony_ci * See the License for the specific language governing permissions and 13c36cf2e9Sopenharmony_ci * limitations under the License. 14c36cf2e9Sopenharmony_ci */ 15c36cf2e9Sopenharmony_ciimport { Configuration } from '@ohos.app.ability.Configuration'; 16c36cf2e9Sopenharmony_ciimport type common from '@ohos.app.ability.common' 17c36cf2e9Sopenharmony_ciimport { ConfigChangeListener } from './Interfaces'; 18c36cf2e9Sopenharmony_ciimport EnvironmentCallback from '@ohos.app.ability.EnvironmentCallback'; 19c36cf2e9Sopenharmony_ciimport { Log } from '../utils/Log'; 20c36cf2e9Sopenharmony_ciimport AbilityConstant from '@ohos.app.ability.AbilityConstant'; 21c36cf2e9Sopenharmony_ciimport { SingletonHelper } from '../utils/SingletonHelper'; 22c36cf2e9Sopenharmony_ci 23c36cf2e9Sopenharmony_ciconst TAG: string = 'ConfigManager'; 24c36cf2e9Sopenharmony_ci/** 25c36cf2e9Sopenharmony_ci * 环境配置信息变化管理类 26c36cf2e9Sopenharmony_ci */ 27c36cf2e9Sopenharmony_ciclass ConfigManager { 28c36cf2e9Sopenharmony_ci /** 29c36cf2e9Sopenharmony_ci * 属性变化事件 30c36cf2e9Sopenharmony_ci */ 31c36cf2e9Sopenharmony_ci private mConfigInfo?: Configuration; 32c36cf2e9Sopenharmony_ci /** 33c36cf2e9Sopenharmony_ci * 应用上下文 34c36cf2e9Sopenharmony_ci */ 35c36cf2e9Sopenharmony_ci private mContext?: common.UIAbilityContext | common.UIExtensionContext; 36c36cf2e9Sopenharmony_ci /** 37c36cf2e9Sopenharmony_ci * EnviornmentCallback id 38c36cf2e9Sopenharmony_ci */ 39c36cf2e9Sopenharmony_ci private mCallbackId: number = -1; 40c36cf2e9Sopenharmony_ci private mListeners: Map<string, ConfigChangeListener> = new Map(); 41c36cf2e9Sopenharmony_ci private isStarted: boolean = false; 42c36cf2e9Sopenharmony_ci /** 43c36cf2e9Sopenharmony_ci * 环境变化回调 44c36cf2e9Sopenharmony_ci */ 45c36cf2e9Sopenharmony_ci private mEnivornmentCallback: EnvironmentCallback = { 46c36cf2e9Sopenharmony_ci onConfigurationUpdated(config: Configuration): void { 47c36cf2e9Sopenharmony_ci Log.info(TAG, 'onConfigurationUpdated configChange'); 48c36cf2e9Sopenharmony_ci configMgr.notifyListeners(config); 49c36cf2e9Sopenharmony_ci }, 50c36cf2e9Sopenharmony_ci onMemoryLevel(level: AbilityConstant.MemoryLevel): void { 51c36cf2e9Sopenharmony_ci Log.info(TAG, 'Method to trim memory yet not in use, NULL METHOD'); 52c36cf2e9Sopenharmony_ci } 53c36cf2e9Sopenharmony_ci }; 54c36cf2e9Sopenharmony_ci 55c36cf2e9Sopenharmony_ci /** 56c36cf2e9Sopenharmony_ci * 初始化 57c36cf2e9Sopenharmony_ci * 58c36cf2e9Sopenharmony_ci * @param context context 59c36cf2e9Sopenharmony_ci */ 60c36cf2e9Sopenharmony_ci onStart(context: common.UIAbilityContext | common.UIExtensionContext) :void { 61c36cf2e9Sopenharmony_ci if (this.isStarted) { 62c36cf2e9Sopenharmony_ci return; 63c36cf2e9Sopenharmony_ci } 64c36cf2e9Sopenharmony_ci this.isStarted = true; 65c36cf2e9Sopenharmony_ci this.mConfigInfo = context?.config; 66c36cf2e9Sopenharmony_ci this.mContext = context; 67c36cf2e9Sopenharmony_ci Log.debug(TAG, `onStart config: ${JSON.stringify(this.mConfigInfo)}`); 68c36cf2e9Sopenharmony_ci // 注册环境回调 69c36cf2e9Sopenharmony_ci this.mCallbackId = context?.getApplicationContext()?.on('environment', this.mEnivornmentCallback); 70c36cf2e9Sopenharmony_ci } 71c36cf2e9Sopenharmony_ci 72c36cf2e9Sopenharmony_ci onStop(): void { 73c36cf2e9Sopenharmony_ci if (!this.isStarted) { 74c36cf2e9Sopenharmony_ci return; 75c36cf2e9Sopenharmony_ci } 76c36cf2e9Sopenharmony_ci this.isStarted = false; 77c36cf2e9Sopenharmony_ci // 解注册环境回调 78c36cf2e9Sopenharmony_ci this.mContext?.getApplicationContext()?.off('environment', this.mCallbackId, (error, data) =>{ 79c36cf2e9Sopenharmony_ci if (error && error.code !== 0) { 80c36cf2e9Sopenharmony_ci Log.error(TAG, `unregisterEnvironmentCallback fail, error: ${JSON.stringify(error)}`); 81c36cf2e9Sopenharmony_ci } else { 82c36cf2e9Sopenharmony_ci Log.info(TAG, `unregisterEnvironmentCallback success, data: ${JSON.stringify(data)}`); 83c36cf2e9Sopenharmony_ci } 84c36cf2e9Sopenharmony_ci }); 85c36cf2e9Sopenharmony_ci } 86c36cf2e9Sopenharmony_ci 87c36cf2e9Sopenharmony_ci registerConfigManager(key: string, listener: ConfigChangeListener): void { 88c36cf2e9Sopenharmony_ci Log.info(TAG, `registerConfigManager key: ${key}`); 89c36cf2e9Sopenharmony_ci if (key === null || listener === null) { 90c36cf2e9Sopenharmony_ci return; 91c36cf2e9Sopenharmony_ci } 92c36cf2e9Sopenharmony_ci this.mListeners?.set(key, listener); 93c36cf2e9Sopenharmony_ci } 94c36cf2e9Sopenharmony_ci 95c36cf2e9Sopenharmony_ci unregisterConfigManager(key: string): void { 96c36cf2e9Sopenharmony_ci if (key === null) { 97c36cf2e9Sopenharmony_ci return; 98c36cf2e9Sopenharmony_ci } 99c36cf2e9Sopenharmony_ci this.mListeners?.delete(key); 100c36cf2e9Sopenharmony_ci } 101c36cf2e9Sopenharmony_ci 102c36cf2e9Sopenharmony_ci /** 103c36cf2e9Sopenharmony_ci * 通知其他listener 104c36cf2e9Sopenharmony_ci * 105c36cf2e9Sopenharmony_ci * @param config config 106c36cf2e9Sopenharmony_ci */ 107c36cf2e9Sopenharmony_ci notifyListeners(config: Configuration): void { 108c36cf2e9Sopenharmony_ci this.mConfigInfo = config; 109c36cf2e9Sopenharmony_ci Log.info(TAG, 'notifyListeners enter.'); 110c36cf2e9Sopenharmony_ci this.mListeners?.forEach((listener, key) => { 111c36cf2e9Sopenharmony_ci Log.debug(TAG, `notifyListeners key: ${key}`); 112c36cf2e9Sopenharmony_ci listener?.notifyConfigurationChanged(this.mConfigInfo); 113c36cf2e9Sopenharmony_ci }); 114c36cf2e9Sopenharmony_ci Log.info(TAG, 'notifyListeners end.'); 115c36cf2e9Sopenharmony_ci } 116c36cf2e9Sopenharmony_ci 117c36cf2e9Sopenharmony_ci /** 118c36cf2e9Sopenharmony_ci * 获取当前属性 119c36cf2e9Sopenharmony_ci * 120c36cf2e9Sopenharmony_ci * @returns 121c36cf2e9Sopenharmony_ci */ 122c36cf2e9Sopenharmony_ci getConfiguration(): Configuration { 123c36cf2e9Sopenharmony_ci return this.mConfigInfo; 124c36cf2e9Sopenharmony_ci } 125c36cf2e9Sopenharmony_ci} 126c36cf2e9Sopenharmony_ci 127c36cf2e9Sopenharmony_ciexport let configMgr = SingletonHelper.getInstance(ConfigManager, TAG);