• 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 display from '@ohos.display';
17
18import { Action } from '../../../../../../common/src/main/ets/default/redux/actions/Action'
19import { CameraWorker } from '../../../../../../common/src/main/ets/default/worker/CameraWorker'
20import { Log } from '../../../../../../common/src/main/ets/default/utils/Log'
21import { EventBus } from '../../../../../../common/src/main/ets/default/worker/eventbus/EventBus'
22import EventBusManager from '../../../../../../common/src/main/ets/default/worker/eventbus/EventBusManager'
23import { FootBarLand } from './FootBarLand'
24import getStore, { OhCombinedState } from '../../../../../../common/src/main/ets/default/redux/store'
25import { ModeConfig } from '../common/ModeConfig'
26import {MoreList
27} from '../../../../../../common/src/main/ets/default/featurecommon/moreList/moreList'
28import { PlaySound
29} from '../../../../../../common/src/main/ets/default/featurecommon/playsound/playSound'
30import { PreviewAreaLand } from './PreviewAreaLand'
31import RdbStoreManager from '../../../../../../common/src/main/ets/default/setting/storage/RdbStoreManager'
32import { TabBarLand
33} from '../../../../../../common/src/main/ets/default/featurecommon/tabbar/TabBarLand'
34import { ZoomViewLand
35} from '../../../../../../common/src/main/ets/default/featurecommon/zoomview/ZoomViewLand'
36import ComponentPosition from '../../../../../../common/src/main/ets/default/utils/ComponentPosition'
37import { Constants } from '../../../../../../common/src/main/ets/default/utils/Constants';
38import { SettingView } from './SettingView'
39import { PreferencesService, PersistType
40} from '../../../../../../common/src/main/ets/default/featurecommon/preferences/PreferencesService'
41import ScreenLockManager from '../../../../../../common/src/main/ets/default/featurecommon/screenlock/ScreenLockManager'
42import abilityAccessCtrl from '@ohos.abilityAccessCtrl'
43
44var cameraWorker = CameraWorker.getInstance()
45var mScreenLockManager = new ScreenLockManager()
46
47let indexState = (state: OhCombinedState) => {
48  return {
49    permissionFlag: state.ContextReducer.permissionFlag,
50    mode: state.ModeReducer.mode,
51    curMode: state.ModeReducer.curMode,
52    isBigVideoTimerVisible: state.RecordReducer.isBigVideoTimerVisible,
53    isSmallVideoTimerVisible: state.RecordReducer.isSmallVideoTimerVisible,
54    videoState: state.RecordReducer.videoState,
55    isShowtimeLapse: state.SettingReducer.isShowtimeLapse,
56    isShowMoreList: state.ModeReducer.isShowMoreList,
57    isThirdPartyCall: state.ContextReducer.isThirdPartyCall,
58    showZoomLabelValue: state.ZoomReducer.showZoomLabelValue,
59    cameraPosition: state.CameraReducer.cameraPosition,
60    isShowPinch: state.ZoomReducer.isShowPinch,
61    xComponentWidth: state.PreviewReducer.xComponentWidth,
62    xComponentHeight: state.PreviewReducer.xComponentHeight,
63    isShowPageView: state.SettingReducer.isShowSettingView,
64    isInitiated: state.ModeReducer.isInitiated,
65    initShowFlag: state.ContextReducer.initShowFlag
66  }
67}
68
69let indexDispatcher = (dispatch) => {
70  return {
71    setPermissionFlag: (permissionFlag: boolean) => {
72      console.info(`CameraApp setPermissionFlag: ${permissionFlag}`)
73      dispatch(Action.setPermissionFlag(permissionFlag))
74    },
75    initAction: (action: string) => {
76      dispatch(Action.initAction(action))
77    },
78    initCameraPosition: (cameraPosition: string) => {
79      dispatch(Action.setCameraPosition(cameraPosition))
80    },
81    initMode: (mode: string) => {
82      dispatch(Action.initMode(mode))
83    },
84    changeTimeLapse: (isShowtimeLapse: boolean) => {
85      dispatch(Action.changeTimeLapse(isShowtimeLapse))
86    },
87    stopRecording: () => {
88      dispatch(Action.stopRecording())
89      dispatch(Action.updateVideoState('beforeTakeVideo'))
90      dispatch(Action.updateBigVideoTimerVisible(false))
91      dispatch(Action.updateSmallVideoTimerVisible(false))
92    },
93    resetRecordingTime: () => {
94      dispatch(Action.updateRecordingTime(0))
95      dispatch(Action.updateRecordingTimeDisplay('00:00'))
96    },
97    hideSettingView: () => {
98      dispatch(Action.showSettingView(false))
99    },
100    updateModeIndex: (index: number) => {
101      dispatch(Action.updateModeIndex(index))
102    },
103    faCall: (isFaCall: boolean) => {
104      dispatch(Action.faCall(isFaCall))
105    },
106    updateInitShowFlag: (initShowFlag: boolean) => {
107      dispatch(Action.updateInitShowFlag(initShowFlag))
108    },
109    thirdPartyCall: (isThirdPartyCall: boolean, action: string) => {
110      dispatch(Action.thirdPartyCall(isThirdPartyCall, action))
111    },
112    resetZoomRatio: () => {
113      dispatch(Action.changeZoomRatio(1))
114    },
115  }
116}
117
118PersistentStorage.PersistProp("storageCameraId", "")
119
120@Entry
121@Component
122struct Index {
123  private TAG: string = '[IndexLand]:'
124  private modeConfig: ModeConfig = new ModeConfig()
125  appEventBus: EventBus = EventBusManager.getInstance().getEventBus()
126  @State state: any = {}
127  @State screenSize: any = { width: 0, height: 0 }
128  private mPlaySound: PlaySound = PlaySound.getInstance()
129  private modeArray: Array<string> = ['MULTI', 'PHOTO', 'VIDEO']
130  protected mPreferencesService: PreferencesService = PreferencesService.getInstance()
131
132  aboutToAppear(): void {
133    Log.info(`${this.TAG} aboutToAppear E`)
134    let dbStore = RdbStoreManager.getInstance()
135    dbStore.initRdbConfig()
136    getStore().connect(indexState, indexDispatcher)(this.state)
137    mScreenLockManager.init()
138
139    if (!this.state.permissionFlag) {
140      let permissionList: Array<string> = [
141        "ohos.permission.MEDIA_LOCATION",
142        "ohos.permission.READ_MEDIA",
143        "ohos.permission.WRITE_MEDIA",
144        "ohos.permission.CAMERA",
145        "ohos.permission.MICROPHONE",
146        "ohos.permission.DISTRIBUTED_DATASYNC",
147        "ohos.permission.LOCATION",
148        "ohos.permission.LOCATION_IN_BACKGROUND",
149        "ohos.permission.APPROXIMATELY_LOCATION"
150      ]
151      Log.info(`${this.TAG} permissions need to require from user: ${JSON.stringify(permissionList)}`)
152      let atManager = abilityAccessCtrl.createAtManager()
153      try {
154        // @ts-ignore
155        atManager.requestPermissionsFromUser(globalThis.cameraAbilityContext, permissionList).then((data) => {
156          Log.info(`${this.TAG} data permissions: ${JSON.stringify(data.permissions)}`)
157          Log.info(`${this.TAG} data authResult: ${JSON.stringify(data.authResults)}`)
158          let sum = 0
159          for (let i = 0; i < data.authResults.length; i++) {
160            sum += data.authResults[i]
161          }
162          if (sum >= 0) {
163            globalThis.permissionFlag = true
164            this.state.setPermissionFlag(true)
165          } else {
166            globalThis.permissionFlag = false
167            this.state.setPermissionFlag(false)
168          }
169          Log.info(`${this.TAG} request permissions result: ${globalThis.permissionFlag}`)
170        }, (err) => {
171          Log.error(`${this.TAG} Failed to start ability err code: ${err.code}`)
172        });
173      } catch (error) {
174        Log.info(`${this.TAG} catch error: ${JSON.stringify(error)}`)
175      }
176    }
177
178    if (globalThis.cameraFormParam != undefined) {
179      this.state.initAction(globalThis.cameraFormParam.action)
180      this.state.initMode(globalThis.cameraFormParam.mode)
181      globalThis.cameraFormParam = undefined
182    }
183
184    globalThis.stopCameraRecording = (() => {
185      this.stopCameraRecording()
186    })
187
188    globalThis.resetZoomRatio = (() => {
189      this.state.resetZoomRatio()
190    })
191
192    if (AppStorage.Has(Constants.APP_KEY_WINDOW_SIZE)) {
193      this.screenSize = AppStorage.Get(Constants.APP_KEY_WINDOW_SIZE)
194    } else {
195      display.getDefaultDisplay().then((dis) => {
196        this.screenSize.width = px2vp(dis.width)
197        this.screenSize.height = px2vp(dis.height)
198      })
199    }
200
201    if (!this.state.isInitiated) {
202      let initIndex = this.mPreferencesService.getModeValue(PersistType.FOR_AWHILE, 1)
203      Log.info(`${this.TAG} initModeIndex: ${initIndex}`)
204      this.state.initMode(this.modeArray[initIndex])
205      this.state.updateModeIndex(initIndex)
206    }
207
208    Log.info(`${this.TAG} aboutToAppear X`)
209  }
210
211  onPageShow(): void {
212    Log.info(`${this.TAG} onPageShow E this.permissionFlag: ${this.state.permissionFlag} globalThis.permissionFlag: ${globalThis.permissionFlag}`)
213    this.state.setPermissionFlag(globalThis.permissionFlag)
214    let cameraId = AppStorage.Get<string>('storageCameraId')
215    if(cameraId) this.state.initCameraPosition(cameraId)
216    Log.info(`${this.TAG} initCameraPosition ${cameraId}`)
217    this.state.resetRecordingTime()
218    Log.info(`${this.TAG} onPageShow X`)
219  }
220
221  private onBackClicked() {
222    Log.info(`${this.TAG} onBackClicked E`)
223    this.terminateSelfWithResult()
224  }
225
226  onBackPress(): boolean {
227    Log.info(`${this.TAG} onBackPress E`)
228    if (this.state.isShowPageView) {
229      this.state.hideSettingView()
230      return true
231    } else if (this.state.isShowtimeLapse) {
232      this.state.changeTimeLapse(false)
233      return true
234    } else if (this.state.isThirdPartyCall) {
235      this.terminateSelfWithResult()
236    } else {
237      if(this.state.videoState === 'startTakeVideo' || this.state.videoState === 'pauseTakeVideo'){
238        this.state.stopRecording()
239        return true
240      }
241      Log.info(`${this.TAG} onBackPress X`)
242      return false
243    }
244  }
245
246  onPageHide(): void {
247    Log.info(`${this.TAG} onPageHide E`)
248    this.stopCameraRecording()
249    Log.info(`${this.TAG} onPageHide X`)
250  }
251
252  public stopCameraRecording() {
253    Log.info(`${this.TAG} stopCameraRecording E`)
254    if (this.state.isShowtimeLapse) {
255      this.state.changeTimeLapse(false)
256    }
257    if(this.state.videoState === 'startTakeVideo' || this.state.videoState === 'pauseTakeVideo') {
258      this.state.stopRecording()
259    }
260    Log.info(`${this.TAG} stopCameraRecording X`)
261  }
262
263  terminateSelfWithResult() {
264    Log.info(`${this.TAG} terminateSelfWithResult start`)
265    let abilityResult = {
266      "resultCode": 200,
267      "want": {
268        "parameters": {
269          "resourceUri": "",
270          "width": "",
271          "height": ""
272        },
273        "bundleName": "com.ohos.camera",
274        "abilityName": "com.ohos.camera.MainAbility"
275      }
276    };
277
278    globalThis.cameraAbilityContext.terminateSelfWithResult(abilityResult, (error, data) => {
279      if (error) {
280        Log.error(`${this.TAG} Operation failed. Cause: ${error}`);
281        return;
282      }
283      Log.info(`${this.TAG} Operation succeeded: ${data}`);
284    });
285  }
286
287  private componentVisibility(condition: boolean): Visibility {
288    return (!this.state.isShowtimeLapse && this.state.initShowFlag && condition) ? Visibility.Visible : Visibility.Hidden
289  }
290
291  build() {
292    Stack({ alignContent: Alignment.TopStart }) {
293      Column() {
294        PreviewAreaLand({ screenSize: $screenSize })
295      }
296      .position(this.state.isThirdPartyCall ? ComponentPosition.previewTabletPosition(this.screenSize.width, this.screenSize.height,
297        this.state.xComponentWidth, this.state.xComponentHeight) : ComponentPosition.previewPosition(this.screenSize.width, this.screenSize.height,
298        this.state.xComponentWidth, this.state.xComponentHeight))
299      Column() {
300        if ((this.state.mode === "PHOTO" || this.state.mode === "VIDEO") && this.state.cameraPosition !== 'FRONT') {
301          ZoomViewLand()
302        } else {
303          Column() {
304          }.width(82).margin({ right: '20vp' })
305        }
306      }.zIndex(2).height('100%')
307      .position(this.state.isThirdPartyCall ? {x: 984, y: 0} : ComponentPosition.zoomViewPosition(this.screenSize.width, this.screenSize.height,
308        this.state.xComponentWidth, this.state.xComponentHeight, this.state.videoState))
309      .visibility(this.componentVisibility(!this.state.isShowPinch))
310
311      Column(){
312        FootBarLand({ screenSize: $screenSize })
313      }.zIndex(2).height('100%')
314      .position(this.state.isThirdPartyCall ? {x: 1124, y: 0} : ComponentPosition.footBarPosition(this.screenSize.width, this.screenSize.height,
315        this.state.xComponentWidth, this.state.xComponentHeight))
316      .visibility(this.componentVisibility(this.state.showZoomLabelValue))
317
318      Column() {
319        TabBarLand({onBackClicked: () => this.onBackClicked(), screenSize: $screenSize})
320      }.zIndex(3)
321      .visibility(this.componentVisibility(this.state.videoState === "beforeTakeVideo" && this.state.showZoomLabelValue))
322      .position(this.state.isThirdPartyCall ? {x: 0, y: 44} : ComponentPosition.tabBarPosition(this.screenSize.width, this.screenSize.height,
323      this.state.xComponentWidth, this.state.xComponentHeight))
324      .height(712)
325
326      if (this.state.isShowMoreList) {
327        Column() {
328          MoreList()
329        }.zIndex(1).height('100%').width('100%')
330        .visibility(this.componentVisibility(this.state.showZoomLabelValue))
331      }
332      if (this.state.isShowPageView) {
333        SettingView().width('100%').height('100%').zIndex(4)
334      }
335    }.width('100%').height('100%').backgroundColor('#000')
336  }
337}