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 16 17import { UiUtil } from '../../utils/UiUtil'; 18import { Log } from '../../utils/Log'; 19import { Constants } from './Constants'; 20 21const TAG: string = 'common_StatusBarColorController'; 22 23export enum StatusBarColorMode { 24 NORMAL, 25 TRANSPARENT, 26 PHOTO_BROWSER 27} 28 29export class StatusBarColorController { 30 private mode: StatusBarColorMode; 31 private statusBarColor: string; 32 33 private constructor() { 34 Log.info(TAG, 'constructor'); 35 UiUtil.getResourceString($r('app.color.default_background_color')).then((value) => { 36 this.statusBarColor = value; 37 Log.info(TAG, `statusBarColor: ${value}`); 38 }) 39 .catch((error) => { 40 Log.error(TAG, `getResourceString error: ${error}`); 41 }) 42 } 43 44 public static getInstance(): StatusBarColorController { 45 if (AppStorage.get(Constants.APP_KEY_STATUS_BAR_COLOR_CONTROLLER) == null) { 46 AppStorage.setOrCreate( 47 Constants.APP_KEY_STATUS_BAR_COLOR_CONTROLLER, new StatusBarColorController()); 48 } 49 return AppStorage.get(Constants.APP_KEY_STATUS_BAR_COLOR_CONTROLLER); 50 } 51 52 public setMode(mode: StatusBarColorMode): void { 53 this.mode = mode; 54 this.invalidate(); 55 } 56 57 public release() { 58 AppStorage.delete(Constants.APP_KEY_STATUS_BAR_COLOR_CONTROLLER); 59 } 60 61 private invalidate(): void { 62 Log.info(TAG, `invalidate mode: ${this.mode}`); 63 switch (this.mode) { 64 case StatusBarColorMode.TRANSPARENT: 65 UiUtil.getResourceString($r('app.color.transparent')).then(() => { 66 }) 67 .catch((error) => { 68 Log.error(TAG, `getResourceString for transparent color error: ${error}`); 69 }) 70 break; 71 case StatusBarColorMode.PHOTO_BROWSER: 72 this.checkColorAvailable(); 73 break; 74 default: 75 this.checkColorAvailable(); 76 break; 77 } 78 } 79 80 private async checkColorAvailable() { 81 if (this.statusBarColor) { 82 return true; 83 } else { 84 try { 85 let color = await UiUtil.getResourceString($r('app.color.default_background_color')); 86 this.statusBarColor = color; 87 Log.info(TAG, `statusBarColor: ${color}`); 88 return color != null; 89 } catch (error) { 90 Log.error(TAG, `getResourceString error: ${error}`); 91 return false; 92 } 93 } 94 } 95}