1/* 2 * Copyright (c) 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 */ 15 16import AspectRatio from '../setting/settingitem/AspectRatio' 17import AssistiveGrid from '../setting/settingitem/AssistiveGrid' 18import { CameraId } from './settingitem/CameraId' 19import { Log } from '../utils/Log' 20import { Constants } from '../utils/Constants' 21import DisplayCalculator from '../setting/DisplayCalculator' 22import { RdbStoreManager } from '../setting/storage/RdbStoreManager' 23import Resolution from '../setting/settingitem/Resolution' 24import SaveGeoLocation from '../setting/settingitem/SaveGeoLocation' 25import SelfMirror from '../setting/settingitem/SelfMirror' 26import SettingItemInfo from '../setting/storage/SettingItemInfo' 27import Timer from '../setting/settingitem/Timer' 28import VideoCodec from '../setting/settingitem/VideoCodec' 29import { Voice } from '../setting/settingitem/Voice' 30import { EventBusManager } from '../worker/eventbus/EventBusManager'; 31 32export class SettingManager { 33 private static TAG = '[SettingManager]:' 34 private mRdbStoreManager: RdbStoreManager = RdbStoreManager.getInstance() 35 private mEventBus = EventBusManager.getInstance().getEventBus() 36 private mSettingsList = [ 37 AspectRatio, 38 Resolution, 39 AssistiveGrid, 40 Timer, 41 SelfMirror, 42 SaveGeoLocation, 43 VideoCodec, 44 Voice 45 ] 46 private mAspectRatio: Resource 47 private mResolution: Resource 48 private mAssistiveGrid: string 49 private mIsLoad = false 50 private mTimer: Resource 51 private mSelfMirror: string 52 private mSaveGeoLocation: string 53 private mCurGeoLocation = undefined 54 private mVideoCodec: string 55 private mCaptureMute: string 56 57 public static getInstance(): SettingManager { 58 Log.info(`${this.TAG} getInstance`) 59 if (!AppStorage.Has(Constants.APP_KEY_SETTINGS_UTILS)) { 60 AppStorage.SetOrCreate(Constants.APP_KEY_SETTINGS_UTILS, new SettingManager()) 61 Log.info(`${this.TAG} getInstance 'build new SettingManager`) 62 } 63 return AppStorage.Get(Constants.APP_KEY_SETTINGS_UTILS) 64 } 65 66 public setSettingValue(settingAlias, itemValue, mode: string): void { 67 Log.info(`${SettingManager.TAG} settingAlias = ${settingAlias} itemValue= ${itemValue}`) 68 let needCommit = false 69 if (settingAlias == AspectRatio.ALIAS) { 70 this.mAspectRatio = itemValue 71 this.mEventBus.emit('AspectRatio', [this.getPreviewDisplaySize(mode)]) 72 needCommit = true 73 } else if (settingAlias == Resolution.ALIAS) { 74 this.mResolution = itemValue 75 this.mEventBus.emit('Resolution', [this.getPreviewDisplaySize(mode)]) 76 needCommit = true 77 } else if (settingAlias == AssistiveGrid.ALIAS) { 78 this.mAssistiveGrid = itemValue 79 this.mEventBus.emit('AssistiveGrid', [this.mAssistiveGrid]) 80 needCommit = true 81 } else if (settingAlias == Timer.ALIAS) { 82 this.mTimer = itemValue 83 needCommit = true 84 } else if (settingAlias == SelfMirror.ALIAS) { 85 this.mSelfMirror = itemValue 86 needCommit = true 87 } else if (settingAlias == SaveGeoLocation.ALIAS) { 88 this.mSaveGeoLocation = itemValue 89 needCommit = true 90 } else if (settingAlias == VideoCodec.ALIAS) { 91 this.mVideoCodec = itemValue 92 needCommit = true 93 } else if (settingAlias == Voice.ALIAS) { 94 this.mCaptureMute = itemValue 95 needCommit = true 96 } 97 const value = this.convertToString(settingAlias, itemValue) 98 if (needCommit) { 99 this.commit(settingAlias, value) 100 } 101 } 102 103 public getSettingValue(settingAlias) { 104 if (settingAlias == AspectRatio.ALIAS) { 105 return this.mAspectRatio 106 } else if (settingAlias == Resolution.ALIAS) { 107 return this.mResolution 108 } else if (settingAlias == Timer.ALIAS) { 109 return this.mTimer 110 } else if (settingAlias == AssistiveGrid.ALIAS) { 111 return this.mAssistiveGrid 112 } else if (settingAlias == SelfMirror.ALIAS) { 113 return this.mSelfMirror 114 } else if (settingAlias == SaveGeoLocation.ALIAS) { 115 return this.mSaveGeoLocation 116 } else if (settingAlias == VideoCodec.ALIAS) { 117 return this.mVideoCodec 118 } else if (settingAlias == Voice.ALIAS) { 119 return this.mCaptureMute 120 } 121 Log.info(`${SettingManager.TAG} ${settingAlias} don't have setting value`) 122 } 123 124 public async loadAllSetting() { 125 Log.info(`${SettingManager.TAG} loadAllSetting E`) 126 if (!this.mIsLoad) { 127 for (let index = 0; index < this.mSettingsList.length; index++) { 128 const settingAlias = this.mSettingsList[index].ALIAS 129 const resultList: SettingItemInfo[] = await this.mRdbStoreManager.getSettingByItem(settingAlias) 130 Log.info(`${SettingManager.TAG} loadAllSetting settingAlias=${settingAlias} resultList = ${JSON.stringify(resultList)}`) 131 if (resultList.length != 0) { 132 Log.info(`${SettingManager.TAG} loadAllSetting `) 133 for (let i = 0; i < resultList.length; i++) { 134 Log.info(`${SettingManager.TAG} loadAllSetting getSettingValue` + +i + ': ' + resultList[i].itemValue) 135 } 136 const value = this.convertToResource(settingAlias, resultList[0].itemValue) 137 if (settingAlias == AspectRatio.ALIAS) { 138 this.mAspectRatio = value 139 } else if (settingAlias == Resolution.ALIAS) { 140 this.mResolution = value 141 } else if (settingAlias == AssistiveGrid.ALIAS) { 142 Log.log(SettingManager.TAG + ' this.mAssistiveGrid ' + this.mAssistiveGrid) 143 this.mAssistiveGrid = value 144 } else if (settingAlias == Timer.ALIAS) { 145 this.mTimer = value 146 } else if (settingAlias == SelfMirror.ALIAS) { 147 this.mSelfMirror = value 148 } else if (settingAlias == SaveGeoLocation.ALIAS) { 149 this.mSaveGeoLocation = value 150 } else if (settingAlias == VideoCodec.ALIAS) { 151 this.mVideoCodec = value 152 } else if (settingAlias == Voice.ALIAS) { 153 this.mCaptureMute = value 154 } 155 } 156 } 157 this.setDefault(false) 158 this.mIsLoad = true 159 } 160 Log.info(`${SettingManager.TAG} loadAllSetting X`) 161 } 162 163 private setDefault(force: boolean): void { 164 if (!this.mAspectRatio || force) { 165 this.mAspectRatio = AspectRatio.DEFAULT_VALUE 166 } 167 if (!this.mResolution || force) { 168 this.mResolution = Resolution.DEFAULT_VALUE 169 } 170 if (!this.mAssistiveGrid || force) { 171 this.mAssistiveGrid = AssistiveGrid.DEFAULT_VALUE 172 } 173 if (!this.mTimer || force) { 174 this.mTimer = Timer.DEFAULT_VALUE 175 } 176 if (!this.mSaveGeoLocation || force) { 177 this.mSaveGeoLocation = '1' 178 } 179 if (!this.mCaptureMute || force) { 180 this.mCaptureMute = Voice.MUTE 181 } 182 } 183 184 public restoreValues(mode: string): void { 185 for (let i = 0; i < this.mSettingsList.length; i++) { 186 this.setSettingValue(this.mSettingsList[i].ALIAS, this.mSettingsList[i].DEFAULT_VALUE, mode) 187 } 188 } 189 190 private async commit(settingAlias, itemValue) { 191 Log.info(`${SettingManager.TAG} getInstance settingAlias: ${settingAlias} itemValue ${itemValue}`) 192 const settingItemInfo: SettingItemInfo = new SettingItemInfo() 193 settingItemInfo.itemName = settingAlias 194 settingItemInfo.itemValue = itemValue 195 await this.mRdbStoreManager.updateValue(settingItemInfo) 196 } 197 198 public mScreenWidth: number 199 public mScreenHeight: number 200 private mPlatformCapability 201 private mCameraId: string 202 203 public setCameraPlatformCapability(platformCapability): void { 204 this.mPlatformCapability = platformCapability 205 } 206 207 public setCameraId(cameraId: string): void { 208 this.mCameraId = cameraId 209 } 210 211 public getImageSize() { 212 return AspectRatio.getImageSize(this.mPlatformCapability, this.mCameraId, this.mAspectRatio) 213 } 214 215 public getVideoSize() { 216 return Resolution.getVideoFrameSize(this.mPlatformCapability, this.mCameraId, this.mResolution) 217 } 218 219 public setScreenWidth(width: number): void { 220 this.mScreenWidth = width 221 } 222 223 public setScreenHeight(height: number): void { 224 this.mScreenHeight = height 225 } 226 227 public getPreviewSize(mode: string) { 228 if (mode == 'VIDEO') { 229 return Resolution.getVideoPreviewSize(this.mPlatformCapability, this.mCameraId, this.mResolution) 230 } else { 231 return AspectRatio.getPhotoPreviewSize(this.mPlatformCapability, this.mCameraId, this.mAspectRatio) 232 } 233 } 234 235 public getPreviewDisplaySize(mode: string) { 236 const preViewSize = this.getPreviewSize(mode) 237 return DisplayCalculator.calcSurfaceDisplaySize(this.mScreenWidth, this.mScreenHeight, preViewSize.width, preViewSize.height) 238 } 239 240 public getAssistiveGrid() { 241 return this.mAssistiveGrid 242 } 243 244 public getTimeLapse() { 245 return this.mTimer 246 } 247 248 public getSelfMirror() { 249 return this.mSelfMirror === '1' 250 } 251 252 public getSaveGeoLocation() { 253 return this.mSaveGeoLocation === '1' 254 } 255 256 public setCurGeoLocation(location): void { 257 this.mCurGeoLocation = location 258 } 259 260 public getCurGeoLocation() { 261 if (this.getSaveGeoLocation()) { 262 return this.mCurGeoLocation 263 } else { 264 return undefined 265 } 266 } 267 268 public getVideoCodec(): string { 269 if (this.mVideoCodec === '1') { 270 return 'video/mp4v-es' 271 } else { 272 return 'video/mp4v-es' 273 } 274 } 275 276 public getCaptureMute() { 277 return this.mCaptureMute 278 } 279 280 private convertToString(settingAlias, itemValue) { 281 if (settingAlias == AspectRatio.ALIAS) { 282 return AspectRatio.convertToString(itemValue) 283 } else if (settingAlias == Resolution.ALIAS) { 284 return Resolution.convertToString(itemValue) 285 } else if (settingAlias == Timer.ALIAS) { 286 return Timer.convertToString(itemValue) 287 } else { 288 return itemValue 289 } 290 } 291 292 private convertToResource(settingAlias, value) { 293 if (settingAlias == AspectRatio.ALIAS) { 294 return AspectRatio.convertToResource(value) 295 } else if (settingAlias == Resolution.ALIAS) { 296 return Resolution.convertToResource(value) 297 } else if (settingAlias == Timer.ALIAS) { 298 return Timer.convertToResource(value) 299 } else { 300 return value 301 } 302 } 303}