142365ec6Sopenharmony_ci/*
242365ec6Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
342365ec6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
442365ec6Sopenharmony_ci * you may not use this file except in compliance with the License.
542365ec6Sopenharmony_ci * You may obtain a copy of the License at
642365ec6Sopenharmony_ci *
742365ec6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
842365ec6Sopenharmony_ci *
942365ec6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1042365ec6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1142365ec6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1242365ec6Sopenharmony_ci * See the License for the specific language governing permissions and
1342365ec6Sopenharmony_ci * limitations under the License.
1442365ec6Sopenharmony_ci */
1542365ec6Sopenharmony_ci
1642365ec6Sopenharmony_ciimport AspectRatio from '../setting/settingitem/AspectRatio'
1742365ec6Sopenharmony_ciimport AssistiveGrid from '../setting/settingitem/AssistiveGrid'
1842365ec6Sopenharmony_ciimport { CameraId } from './settingitem/CameraId'
1942365ec6Sopenharmony_ciimport { Log } from '../utils/Log'
2042365ec6Sopenharmony_ciimport { Constants } from '../utils/Constants'
2142365ec6Sopenharmony_ciimport DisplayCalculator from '../setting/DisplayCalculator'
2242365ec6Sopenharmony_ciimport { RdbStoreManager } from '../setting/storage/RdbStoreManager'
2342365ec6Sopenharmony_ciimport Resolution from '../setting/settingitem/Resolution'
2442365ec6Sopenharmony_ciimport SaveGeoLocation from '../setting/settingitem/SaveGeoLocation'
2542365ec6Sopenharmony_ciimport SelfMirror from '../setting/settingitem/SelfMirror'
2642365ec6Sopenharmony_ciimport SettingItemInfo from '../setting/storage/SettingItemInfo'
2742365ec6Sopenharmony_ciimport Timer from '../setting/settingitem/Timer'
2842365ec6Sopenharmony_ciimport VideoCodec from '../setting/settingitem/VideoCodec'
2942365ec6Sopenharmony_ciimport { Voice } from '../setting/settingitem/Voice'
3042365ec6Sopenharmony_ciimport { EventBusManager } from '../worker/eventbus/EventBusManager';
3142365ec6Sopenharmony_ci
3242365ec6Sopenharmony_ciexport class SettingManager {
3342365ec6Sopenharmony_ci  private static TAG = '[SettingManager]:'
3442365ec6Sopenharmony_ci  private mRdbStoreManager: RdbStoreManager = RdbStoreManager.getInstance()
3542365ec6Sopenharmony_ci  private mEventBus = EventBusManager.getInstance().getEventBus()
3642365ec6Sopenharmony_ci  private mSettingsList = [
3742365ec6Sopenharmony_ci    AspectRatio,
3842365ec6Sopenharmony_ci    Resolution,
3942365ec6Sopenharmony_ci    AssistiveGrid,
4042365ec6Sopenharmony_ci    Timer,
4142365ec6Sopenharmony_ci    SelfMirror,
4242365ec6Sopenharmony_ci    SaveGeoLocation,
4342365ec6Sopenharmony_ci    VideoCodec,
4442365ec6Sopenharmony_ci    Voice
4542365ec6Sopenharmony_ci  ]
4642365ec6Sopenharmony_ci  private mAspectRatio: Resource
4742365ec6Sopenharmony_ci  private mResolution: Resource
4842365ec6Sopenharmony_ci  private mAssistiveGrid: string
4942365ec6Sopenharmony_ci  private mIsLoad = false
5042365ec6Sopenharmony_ci  private mTimer: Resource
5142365ec6Sopenharmony_ci  private mSelfMirror: string
5242365ec6Sopenharmony_ci  private mSaveGeoLocation: string
5342365ec6Sopenharmony_ci  private mCurGeoLocation = undefined
5442365ec6Sopenharmony_ci  private mVideoCodec: string
5542365ec6Sopenharmony_ci  private mCaptureMute: string
5642365ec6Sopenharmony_ci
5742365ec6Sopenharmony_ci  public static getInstance(): SettingManager {
5842365ec6Sopenharmony_ci    Log.info(`${this.TAG} getInstance`)
5942365ec6Sopenharmony_ci    if (!AppStorage.Has(Constants.APP_KEY_SETTINGS_UTILS)) {
6042365ec6Sopenharmony_ci      AppStorage.SetOrCreate(Constants.APP_KEY_SETTINGS_UTILS, new SettingManager())
6142365ec6Sopenharmony_ci      Log.info(`${this.TAG} getInstance 'build new SettingManager`)
6242365ec6Sopenharmony_ci    }
6342365ec6Sopenharmony_ci    return AppStorage.Get(Constants.APP_KEY_SETTINGS_UTILS)
6442365ec6Sopenharmony_ci  }
6542365ec6Sopenharmony_ci
6642365ec6Sopenharmony_ci  public setSettingValue(settingAlias, itemValue, mode: string): void {
6742365ec6Sopenharmony_ci    Log.info(`${SettingManager.TAG} settingAlias = ${settingAlias} itemValue= ${itemValue}`)
6842365ec6Sopenharmony_ci    let needCommit = false
6942365ec6Sopenharmony_ci    if (settingAlias == AspectRatio.ALIAS) {
7042365ec6Sopenharmony_ci      this.mAspectRatio = itemValue
7142365ec6Sopenharmony_ci      this.mEventBus.emit('AspectRatio', [this.getPreviewDisplaySize(mode)])
7242365ec6Sopenharmony_ci      needCommit = true
7342365ec6Sopenharmony_ci    } else if (settingAlias == Resolution.ALIAS) {
7442365ec6Sopenharmony_ci      this.mResolution = itemValue
7542365ec6Sopenharmony_ci      this.mEventBus.emit('Resolution', [this.getPreviewDisplaySize(mode)])
7642365ec6Sopenharmony_ci      needCommit = true
7742365ec6Sopenharmony_ci    } else if (settingAlias == AssistiveGrid.ALIAS) {
7842365ec6Sopenharmony_ci      this.mAssistiveGrid = itemValue
7942365ec6Sopenharmony_ci      this.mEventBus.emit('AssistiveGrid', [this.mAssistiveGrid])
8042365ec6Sopenharmony_ci      needCommit = true
8142365ec6Sopenharmony_ci    } else if (settingAlias == Timer.ALIAS) {
8242365ec6Sopenharmony_ci      this.mTimer = itemValue
8342365ec6Sopenharmony_ci      needCommit = true
8442365ec6Sopenharmony_ci    } else if (settingAlias == SelfMirror.ALIAS) {
8542365ec6Sopenharmony_ci      this.mSelfMirror = itemValue
8642365ec6Sopenharmony_ci      needCommit = true
8742365ec6Sopenharmony_ci    } else if (settingAlias == SaveGeoLocation.ALIAS) {
8842365ec6Sopenharmony_ci      this.mSaveGeoLocation = itemValue
8942365ec6Sopenharmony_ci      needCommit = true
9042365ec6Sopenharmony_ci    } else if (settingAlias == VideoCodec.ALIAS) {
9142365ec6Sopenharmony_ci      this.mVideoCodec = itemValue
9242365ec6Sopenharmony_ci      needCommit = true
9342365ec6Sopenharmony_ci    } else if (settingAlias == Voice.ALIAS) {
9442365ec6Sopenharmony_ci      this.mCaptureMute = itemValue
9542365ec6Sopenharmony_ci      needCommit = true
9642365ec6Sopenharmony_ci    }
9742365ec6Sopenharmony_ci    const value = this.convertToString(settingAlias, itemValue)
9842365ec6Sopenharmony_ci    if (needCommit) {
9942365ec6Sopenharmony_ci      this.commit(settingAlias, value)
10042365ec6Sopenharmony_ci    }
10142365ec6Sopenharmony_ci  }
10242365ec6Sopenharmony_ci
10342365ec6Sopenharmony_ci  public getSettingValue(settingAlias) {
10442365ec6Sopenharmony_ci    if (settingAlias == AspectRatio.ALIAS) {
10542365ec6Sopenharmony_ci      return this.mAspectRatio
10642365ec6Sopenharmony_ci    } else if (settingAlias == Resolution.ALIAS) {
10742365ec6Sopenharmony_ci      return this.mResolution
10842365ec6Sopenharmony_ci    } else if (settingAlias == Timer.ALIAS) {
10942365ec6Sopenharmony_ci      return this.mTimer
11042365ec6Sopenharmony_ci    } else if (settingAlias == AssistiveGrid.ALIAS) {
11142365ec6Sopenharmony_ci      return this.mAssistiveGrid
11242365ec6Sopenharmony_ci    } else if (settingAlias == SelfMirror.ALIAS) {
11342365ec6Sopenharmony_ci      return this.mSelfMirror
11442365ec6Sopenharmony_ci    } else if (settingAlias == SaveGeoLocation.ALIAS) {
11542365ec6Sopenharmony_ci      return this.mSaveGeoLocation
11642365ec6Sopenharmony_ci    } else if (settingAlias == VideoCodec.ALIAS) {
11742365ec6Sopenharmony_ci      return this.mVideoCodec
11842365ec6Sopenharmony_ci    } else if (settingAlias == Voice.ALIAS) {
11942365ec6Sopenharmony_ci      return this.mCaptureMute
12042365ec6Sopenharmony_ci    }
12142365ec6Sopenharmony_ci    Log.info(`${SettingManager.TAG}  ${settingAlias} don't have setting value`)
12242365ec6Sopenharmony_ci  }
12342365ec6Sopenharmony_ci
12442365ec6Sopenharmony_ci  public async loadAllSetting() {
12542365ec6Sopenharmony_ci    Log.info(`${SettingManager.TAG} loadAllSetting E`)
12642365ec6Sopenharmony_ci    if (!this.mIsLoad) {
12742365ec6Sopenharmony_ci      for (let index = 0; index < this.mSettingsList.length; index++) {
12842365ec6Sopenharmony_ci        const settingAlias = this.mSettingsList[index].ALIAS
12942365ec6Sopenharmony_ci        const resultList: SettingItemInfo[] = await this.mRdbStoreManager.getSettingByItem(settingAlias)
13042365ec6Sopenharmony_ci        Log.info(`${SettingManager.TAG} loadAllSetting  settingAlias=${settingAlias} resultList = ${JSON.stringify(resultList)}`)
13142365ec6Sopenharmony_ci        if (resultList.length != 0) {
13242365ec6Sopenharmony_ci          Log.info(`${SettingManager.TAG} loadAllSetting `)
13342365ec6Sopenharmony_ci          for (let i = 0; i < resultList.length; i++) {
13442365ec6Sopenharmony_ci            Log.info(`${SettingManager.TAG} loadAllSetting getSettingValue` + +i + ': ' + resultList[i].itemValue)
13542365ec6Sopenharmony_ci          }
13642365ec6Sopenharmony_ci          const value = this.convertToResource(settingAlias, resultList[0].itemValue)
13742365ec6Sopenharmony_ci          if (settingAlias == AspectRatio.ALIAS) {
13842365ec6Sopenharmony_ci            this.mAspectRatio = value
13942365ec6Sopenharmony_ci          } else if (settingAlias == Resolution.ALIAS) {
14042365ec6Sopenharmony_ci            this.mResolution = value
14142365ec6Sopenharmony_ci          } else if (settingAlias == AssistiveGrid.ALIAS) {
14242365ec6Sopenharmony_ci            Log.log(SettingManager.TAG + ' this.mAssistiveGrid ' + this.mAssistiveGrid)
14342365ec6Sopenharmony_ci            this.mAssistiveGrid = value
14442365ec6Sopenharmony_ci          } else if (settingAlias == Timer.ALIAS) {
14542365ec6Sopenharmony_ci            this.mTimer = value
14642365ec6Sopenharmony_ci          } else if (settingAlias == SelfMirror.ALIAS) {
14742365ec6Sopenharmony_ci            this.mSelfMirror = value
14842365ec6Sopenharmony_ci          } else if (settingAlias == SaveGeoLocation.ALIAS) {
14942365ec6Sopenharmony_ci            this.mSaveGeoLocation = value
15042365ec6Sopenharmony_ci          } else if (settingAlias == VideoCodec.ALIAS) {
15142365ec6Sopenharmony_ci            this.mVideoCodec = value
15242365ec6Sopenharmony_ci          } else if (settingAlias == Voice.ALIAS) {
15342365ec6Sopenharmony_ci            this.mCaptureMute = value
15442365ec6Sopenharmony_ci          }
15542365ec6Sopenharmony_ci        }
15642365ec6Sopenharmony_ci      }
15742365ec6Sopenharmony_ci      this.setDefault(false)
15842365ec6Sopenharmony_ci      this.mIsLoad = true
15942365ec6Sopenharmony_ci    }
16042365ec6Sopenharmony_ci    Log.info(`${SettingManager.TAG} loadAllSetting X`)
16142365ec6Sopenharmony_ci  }
16242365ec6Sopenharmony_ci
16342365ec6Sopenharmony_ci  private setDefault(force: boolean): void {
16442365ec6Sopenharmony_ci    if (!this.mAspectRatio || force) {
16542365ec6Sopenharmony_ci      this.mAspectRatio = AspectRatio.DEFAULT_VALUE
16642365ec6Sopenharmony_ci    }
16742365ec6Sopenharmony_ci    if (!this.mResolution || force) {
16842365ec6Sopenharmony_ci      this.mResolution = Resolution.DEFAULT_VALUE
16942365ec6Sopenharmony_ci    }
17042365ec6Sopenharmony_ci    if (!this.mAssistiveGrid || force) {
17142365ec6Sopenharmony_ci      this.mAssistiveGrid = AssistiveGrid.DEFAULT_VALUE
17242365ec6Sopenharmony_ci    }
17342365ec6Sopenharmony_ci    if (!this.mTimer || force) {
17442365ec6Sopenharmony_ci      this.mTimer = Timer.DEFAULT_VALUE
17542365ec6Sopenharmony_ci    }
17642365ec6Sopenharmony_ci    if (!this.mSaveGeoLocation || force) {
17742365ec6Sopenharmony_ci      this.mSaveGeoLocation = '1'
17842365ec6Sopenharmony_ci    }
17942365ec6Sopenharmony_ci    if (!this.mCaptureMute || force) {
18042365ec6Sopenharmony_ci      this.mCaptureMute = Voice.MUTE
18142365ec6Sopenharmony_ci    }
18242365ec6Sopenharmony_ci  }
18342365ec6Sopenharmony_ci
18442365ec6Sopenharmony_ci  public restoreValues(mode: string): void {
18542365ec6Sopenharmony_ci    for (let i = 0; i < this.mSettingsList.length; i++) {
18642365ec6Sopenharmony_ci      this.setSettingValue(this.mSettingsList[i].ALIAS, this.mSettingsList[i].DEFAULT_VALUE, mode)
18742365ec6Sopenharmony_ci    }
18842365ec6Sopenharmony_ci  }
18942365ec6Sopenharmony_ci
19042365ec6Sopenharmony_ci  private async commit(settingAlias, itemValue) {
19142365ec6Sopenharmony_ci    Log.info(`${SettingManager.TAG} getInstance  settingAlias: ${settingAlias} itemValue ${itemValue}`)
19242365ec6Sopenharmony_ci    const settingItemInfo: SettingItemInfo = new SettingItemInfo()
19342365ec6Sopenharmony_ci    settingItemInfo.itemName = settingAlias
19442365ec6Sopenharmony_ci    settingItemInfo.itemValue = itemValue
19542365ec6Sopenharmony_ci    await this.mRdbStoreManager.updateValue(settingItemInfo)
19642365ec6Sopenharmony_ci  }
19742365ec6Sopenharmony_ci
19842365ec6Sopenharmony_ci  public mScreenWidth: number
19942365ec6Sopenharmony_ci  public mScreenHeight: number
20042365ec6Sopenharmony_ci  private mPlatformCapability
20142365ec6Sopenharmony_ci  private mCameraId: string
20242365ec6Sopenharmony_ci
20342365ec6Sopenharmony_ci  public setCameraPlatformCapability(platformCapability): void {
20442365ec6Sopenharmony_ci    this.mPlatformCapability = platformCapability
20542365ec6Sopenharmony_ci  }
20642365ec6Sopenharmony_ci
20742365ec6Sopenharmony_ci  public setCameraId(cameraId: string): void {
20842365ec6Sopenharmony_ci    this.mCameraId = cameraId
20942365ec6Sopenharmony_ci  }
21042365ec6Sopenharmony_ci
21142365ec6Sopenharmony_ci  public getImageSize() {
21242365ec6Sopenharmony_ci    return AspectRatio.getImageSize(this.mPlatformCapability, this.mCameraId, this.mAspectRatio)
21342365ec6Sopenharmony_ci  }
21442365ec6Sopenharmony_ci
21542365ec6Sopenharmony_ci  public getVideoSize() {
21642365ec6Sopenharmony_ci    return Resolution.getVideoFrameSize(this.mPlatformCapability, this.mCameraId, this.mResolution)
21742365ec6Sopenharmony_ci  }
21842365ec6Sopenharmony_ci
21942365ec6Sopenharmony_ci  public setScreenWidth(width: number): void {
22042365ec6Sopenharmony_ci    this.mScreenWidth = width
22142365ec6Sopenharmony_ci  }
22242365ec6Sopenharmony_ci
22342365ec6Sopenharmony_ci  public setScreenHeight(height: number): void {
22442365ec6Sopenharmony_ci    this.mScreenHeight = height
22542365ec6Sopenharmony_ci  }
22642365ec6Sopenharmony_ci
22742365ec6Sopenharmony_ci  public getPreviewSize(mode: string) {
22842365ec6Sopenharmony_ci    if (mode == 'VIDEO') {
22942365ec6Sopenharmony_ci      return Resolution.getVideoPreviewSize(this.mPlatformCapability, this.mCameraId, this.mResolution)
23042365ec6Sopenharmony_ci    } else {
23142365ec6Sopenharmony_ci      return AspectRatio.getPhotoPreviewSize(this.mPlatformCapability, this.mCameraId, this.mAspectRatio)
23242365ec6Sopenharmony_ci    }
23342365ec6Sopenharmony_ci  }
23442365ec6Sopenharmony_ci
23542365ec6Sopenharmony_ci  public getPreviewDisplaySize(mode: string) {
23642365ec6Sopenharmony_ci    const preViewSize = this.getPreviewSize(mode)
23742365ec6Sopenharmony_ci    return DisplayCalculator.calcSurfaceDisplaySize(this.mScreenWidth, this.mScreenHeight, preViewSize.width, preViewSize.height)
23842365ec6Sopenharmony_ci  }
23942365ec6Sopenharmony_ci
24042365ec6Sopenharmony_ci  public getAssistiveGrid() {
24142365ec6Sopenharmony_ci    return this.mAssistiveGrid
24242365ec6Sopenharmony_ci  }
24342365ec6Sopenharmony_ci
24442365ec6Sopenharmony_ci  public getTimeLapse() {
24542365ec6Sopenharmony_ci    return this.mTimer
24642365ec6Sopenharmony_ci  }
24742365ec6Sopenharmony_ci
24842365ec6Sopenharmony_ci  public getSelfMirror() {
24942365ec6Sopenharmony_ci    return this.mSelfMirror === '1'
25042365ec6Sopenharmony_ci  }
25142365ec6Sopenharmony_ci
25242365ec6Sopenharmony_ci  public getSaveGeoLocation() {
25342365ec6Sopenharmony_ci    return this.mSaveGeoLocation === '1'
25442365ec6Sopenharmony_ci  }
25542365ec6Sopenharmony_ci
25642365ec6Sopenharmony_ci  public setCurGeoLocation(location): void {
25742365ec6Sopenharmony_ci    this.mCurGeoLocation = location
25842365ec6Sopenharmony_ci  }
25942365ec6Sopenharmony_ci
26042365ec6Sopenharmony_ci  public getCurGeoLocation() {
26142365ec6Sopenharmony_ci    if (this.getSaveGeoLocation()) {
26242365ec6Sopenharmony_ci      return this.mCurGeoLocation
26342365ec6Sopenharmony_ci    } else {
26442365ec6Sopenharmony_ci      return undefined
26542365ec6Sopenharmony_ci    }
26642365ec6Sopenharmony_ci  }
26742365ec6Sopenharmony_ci
26842365ec6Sopenharmony_ci  public getVideoCodec(): string {
26942365ec6Sopenharmony_ci    if (this.mVideoCodec === '1') {
27042365ec6Sopenharmony_ci      return 'video/mp4v-es'
27142365ec6Sopenharmony_ci    } else {
27242365ec6Sopenharmony_ci      return 'video/mp4v-es'
27342365ec6Sopenharmony_ci    }
27442365ec6Sopenharmony_ci  }
27542365ec6Sopenharmony_ci
27642365ec6Sopenharmony_ci  public getCaptureMute() {
27742365ec6Sopenharmony_ci    return this.mCaptureMute
27842365ec6Sopenharmony_ci  }
27942365ec6Sopenharmony_ci
28042365ec6Sopenharmony_ci  private convertToString(settingAlias, itemValue) {
28142365ec6Sopenharmony_ci    if (settingAlias == AspectRatio.ALIAS) {
28242365ec6Sopenharmony_ci      return AspectRatio.convertToString(itemValue)
28342365ec6Sopenharmony_ci    } else if (settingAlias == Resolution.ALIAS) {
28442365ec6Sopenharmony_ci      return Resolution.convertToString(itemValue)
28542365ec6Sopenharmony_ci    } else if (settingAlias == Timer.ALIAS) {
28642365ec6Sopenharmony_ci      return Timer.convertToString(itemValue)
28742365ec6Sopenharmony_ci    } else {
28842365ec6Sopenharmony_ci      return itemValue
28942365ec6Sopenharmony_ci    }
29042365ec6Sopenharmony_ci  }
29142365ec6Sopenharmony_ci
29242365ec6Sopenharmony_ci  private convertToResource(settingAlias, value) {
29342365ec6Sopenharmony_ci    if (settingAlias == AspectRatio.ALIAS) {
29442365ec6Sopenharmony_ci      return AspectRatio.convertToResource(value)
29542365ec6Sopenharmony_ci    } else if (settingAlias == Resolution.ALIAS) {
29642365ec6Sopenharmony_ci      return Resolution.convertToResource(value)
29742365ec6Sopenharmony_ci    } else if (settingAlias == Timer.ALIAS) {
29842365ec6Sopenharmony_ci      return Timer.convertToResource(value)
29942365ec6Sopenharmony_ci    } else {
30042365ec6Sopenharmony_ci      return value
30142365ec6Sopenharmony_ci    }
30242365ec6Sopenharmony_ci  }
30342365ec6Sopenharmony_ci}