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 Ability from '@ohos.app.ability.UIAbility' 17import window from '@ohos.window'; 18import wantConstant from '@ohos.ability.wantConstant' 19import Trace from '../../../../../../common/src/main/ets/default/utils/Trace' 20import { CameraBasicFunction } from '../../../../../../common/src/main/ets/default/function/CameraBasicFunction' 21import { EventBus } from '../../../../../../common/src/main/ets/default/worker/eventbus/EventBus' 22import EventBusManager from '../../../../../../common/src/main/ets/default/worker/eventbus/EventBusManager' 23import { Constants, CameraNeedStatus } from '../../../../../../common/src/main/ets/default/utils/Constants' 24import { Log } from '../../../../../../common/src/main/ets/default/utils/Log'; 25import { PreferencesService, PersistType } from '../../../../../../common/src/main/ets/default/featurecommon/preferences/PreferencesService' 26 27export default class MainAbility extends Ability { 28 private cameraBasicFunction: any = null 29 appEventBus: EventBus = EventBusManager.getInstance().getEventBus() 30 private readonly foreRoundCountLimit: number = 1 31 private foreRoundOverCount: number = 0 32 onCreate(want, launchParam) { 33 // Ability is creating, initialize resources for this ability 34 Trace.start(Trace.ABILITY_WHOLE_LIFE) 35 Log.info('Camera MainAbility onCreate.') 36 globalThis.cameraAbilityContext = this.context 37 globalThis.cameraAbilityWant = this.launchWant 38 globalThis.permissionFlag = false 39 40 Log.info(`Camera MainAbility onCreate launchWant. ${JSON.stringify(globalThis.cameraAbilityWant )}`) 41 globalThis.cameraStartTime = new Date().getTime() 42 globalThis.cameraStartFlag = true 43 globalThis.stopRecordingFlag = false; 44 globalThis.doOnForeground = false 45 this.cameraBasicFunction = CameraBasicFunction.getInstance() 46 this.cameraBasicFunction.initCamera({ cameraId: 'BACK', mode: 'PHOTO' }, 'onCreate') 47 } 48 49 onDestroy() { 50 // Ability is creating, release resources for this ability 51 Trace.end(Trace.ABILITY_WHOLE_LIFE) 52 Trace.end(Trace.APPLICATION_WHOLE_LIFE) 53 this.cameraBasicFunction.startIdentification = false 54 PreferencesService.getInstance().flush() 55 Log.info('Camera MainAbility onDestroy.') 56 } 57 58 onWindowStageCreate(windowStage) { 59 // Main window is created, set main page for this ability 60 Trace.start(Trace.ABILITY_VISIBLE_LIFE) 61 Log.info('Camera MainAbility onWindowStageCreate.') 62 windowStage.on('windowStageEvent', (event) => { 63 Log.info('Camera MainAbility onWindowStageEvent: ' + JSON.stringify(event)) 64 if (event === window.WindowStageEventType.SHOWN) { 65 if (++this.foreRoundOverCount > 1) { 66 this.foreRoundOverCount = 1 67 Log.info("multi task interface: reset zoomRatio to 1") 68 globalThis?.resetZoomRatio && globalThis.resetZoomRatio() 69 } 70 } else if (event === window.WindowStageEventType.HIDDEN) { 71 this.foreRoundOverCount-- 72 } 73 globalThis.cameraWindowStageEvent = event 74 if (event === window.WindowStageEventType.INACTIVE) { 75 globalThis.stopRecordingFlag = true 76 globalThis?.stopCameraRecording && globalThis.stopCameraRecording() 77 } else { 78 globalThis.stopRecordingFlag = false 79 } 80 }) 81 82 windowStage.getMainWindow().then((win) => { 83 try { 84 win.setLayoutFullScreen(true).then(() => { 85 Log.info('Camera setFullScreen finished.') 86 win.setSystemBarEnable(['navigation']).then(() => { 87 Log.info('Camera setSystemBarEnable finished.') 88 }) 89 }) 90 91 win.setSystemBarProperties({ 92 navigationBarColor: '#00000000', navigationBarContentColor: '#B3B3B3' 93 }).then(() => { 94 Log.info('Camera setSystemBarProperties.') 95 }) 96 97 win.on('windowSizeChange', (data) => { 98 data.width = (data.height != 1600) ? px2vp(data.width) - 8 : px2vp(data.width) 99 data.height = (data.height != 1600) ? px2vp(data.height) - 43 : px2vp(data.height) 100 AppStorage.SetOrCreate(Constants.APP_KEY_WINDOW_SIZE, data) 101 this.appEventBus.emit("windowSize", [data]) 102 }); 103 104 globalThis.cameraWinClass = win 105 106 } catch (err) { 107 Log.error('Camera setFullScreen err: ' + err) 108 } 109 }) 110 111 if (this.launchWant.parameters.uri === 'capture') { 112 globalThis.cameraFormParam = { 113 action: 'capture', 114 cameraPosition: 'PHOTO', 115 mode: 'PHOTO' 116 } 117 } else if (this.launchWant.parameters.uri === 'video') { 118 globalThis.cameraFormParam = { 119 action: 'video', 120 cameraPosition: 'VIDEO', 121 mode: 'VIDEO' 122 } 123 } 124 125 windowStage.setUIContent(this.context, 'pages/indexLand', null) 126 } 127 128 onWindowStageDestroy() { 129 Trace.end(Trace.ABILITY_VISIBLE_LIFE) 130 Log.info('Camera MainAbility onWindowStageDestroy.') 131 } 132 133 onForeground() { 134 Trace.start(Trace.ABILITY_FOREGROUND_LIFE) 135 Log.info('Camera MainAbility onForeground.') 136 globalThis.cameraNeedStatus = CameraNeedStatus.CAMERA_NEED_INIT 137 if (globalThis?.doOnForeground && globalThis.doOnForeground) { 138 console.info('Camera MainAbility onForeground.') 139 globalThis?.updateCameraStatus && globalThis.updateCameraStatus() 140 } else { 141 globalThis.doOnForeground = true 142 } 143 Log.info('Camera MainAbility onForeground end.') 144 } 145 146 onBackground() { 147 Trace.end(Trace.ABILITY_FOREGROUND_LIFE) 148 Log.info('Camera MainAbility onBackground.') 149 this.cameraBasicFunction.startIdentification = false 150 globalThis.cameraNeedStatus = CameraNeedStatus.CAMERA_NEED_RELEASE 151 globalThis?.updateCameraStatus && globalThis.updateCameraStatus() 152 } 153 154 onNewWant(want) { 155 Log.info('Camera MainAbility onNewWant.') 156 globalThis.cameraAbilityWant = want 157 Log.info(`Camera MainAbility E newWantAction: ${JSON.stringify(globalThis.cameraAbilityWant )}`) 158 } 159}