1//@ts-nocheck 2/** 3 * Copyright (c) 2021 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import BaseModel from '../../../../../../../../common/utils/src/main/ets/default/model/BaseModel'; 18import ConfigData from '../../../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData'; 19import LogUtil from '../../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil'; 20import Log from '../../../../../../../../common/utils/src/main/ets/default/baseUtil/LogDecorator'; 21import ScreenManager from '@ohos.screen'; 22 23/** 24 * Screen Mode setting 25 */ 26export class ScreenModeModel extends BaseModel{ 27 public sysScreenModeText; 28 private supportedScreenModes; 29 private sysScreenMode; 30 private TAG = ConfigData.TAG + ' ScreenModeModel '; 31 private screenLevel: string; 32 33 constructor() { 34 super(); 35 this.init(); 36 } 37 38 /** 39 * init the sysScreenMode 40 */ 41 @Log 42 public init(): void{ 43 ScreenManager.getAllScreens().then((screens) => { 44 LogUtil.info(`${this.TAG} sysScreenMode AllScreen: ${JSON.stringify(screens)}.`) 45 this.supportedScreenModes = screens[0].supportedModeInfo; 46 this.sysScreenMode = this.supportedScreenModes[screens[0].activeModeIndex] 47 AppStorage.SetOrCreate("sysScreenMode", this.sysScreenMode) 48 this.supportedScreenModes = this.distinct(this.supportedScreenModes); 49 this.sysScreenModeText = `${this.getScreenModeWidth(this.getSysScreenMode())} x ${this.getScreenModeHeight(this.getSysScreenMode())}` 50 AppStorage.SetOrCreate('supportedScreenModes', this.supportedScreenModes); 51 AppStorage.SetOrCreate('sysScreenModeText', this.sysScreenModeText); 52 LogUtil.info(`${this.TAG} sysScreenMode: ${JSON.stringify(this.sysScreenMode)}.`) 53 }, (err) => { 54 LogUtil.info(`${this.TAG} setScreenActiveMode error: ${JSON.stringify(err)}.`) 55 }) 56 return; 57 } 58 59 /** 60 * Remove duplicate item 61 */ 62 @Log 63 public distinct(arr: []){ 64 for(let i = 0; i < arr.length; i++ ){ 65 for(let j=i+1; j < arr.length; j++ ){ 66 if(arr[i].width === arr[j].width && arr[i].height === arr[j].height){ 67 arr.splice(j,1); 68 j--; 69 } 70 } 71 } 72 return arr; 73 } 74 75 /** 76 * get supported screenModes 77 */ 78 @Log 79 public getSupportedScreenModes(){ 80 return this.supportedScreenModes; 81 } 82 83 /** 84 * get sysScreenMode 85 */ 86 @Log 87 public getSysScreenMode(){ 88 return this.sysScreenMode; 89 } 90 91 92 /** 93 * Set sysScreenMode 94 */ 95 @Log 96 public setSysScreenMode(index:number): void{ 97 ScreenManager.getAllScreens().then((screens) => { 98 screens[0].setScreenActiveMode(index).then((ret) => { 99 if (ret) { 100 LogUtil.info(`${this.TAG} setScreenActiveMode ret: ${JSON.stringify(ret)}.`) 101 } 102 }) 103 }, (err) => { 104 LogUtil.info(`${this.TAG} setScreenActiveMode error: ${JSON.stringify(err)}.`) 105 }) 106 return; 107 } 108 109 /** 110 * Set sysScreenMode 111 */ 112 @Log 113 public getScreenModeWidth(screenModeInfo): string{ 114 return screenModeInfo.width.toString(); 115 } 116 117 /** 118 * Set sysScreenMode 119 */ 120 @Log 121 public getScreenModeHeight(screenModeInfo): string{ 122 return screenModeInfo.height.toString(); 123 } 124 125 /** 126 * is SysScreenMode 127 */ 128 @Log 129 public isSysScreenMode(screenModeInfo): boolean{ 130 return JSON.stringify(AppStorage.Get("sysScreenMode")) === JSON.stringify(screenModeInfo); 131 } 132 133 /** 134 * get Screen Level 135 */ 136 @Log 137 public getScreenLevel(screenModeInfo): string{ 138 return ''; 139 } 140 141 /** 142 * Register observer 143 */ 144 @Log 145 public registerObserver(){ 146 LogUtil.info(`${this.TAG} registerObserver.`); 147 try { 148 ScreenManager.on('change', (ScreenEvent) => { 149 this.init(); 150 }) 151 } catch (err) { 152 LogUtil.error(`${this.TAG} registerObserver on change error` + err); 153 } 154 LogUtil.info(`${this.TAG} registerObserver success.`); 155 return; 156 } 157 158 /** 159 * Unregister observer 160 */ 161 @Log 162 public unregisterObserver() { 163 LogUtil.info(`${this.TAG} unregisterObserver.`); 164 try { 165 ScreenManager.off('change', (ScreenEvent) => { 166 LogUtil.info(`${this.TAG} unregisterObserver success.`); 167 }) 168 } catch (err) { 169 LogUtil.error(`${this.TAG} unregisterObserver off change error` + err); 170 } 171 return; 172 } 173}