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