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