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