1/* 2 * Copyright (c) 2024 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 { CameraBasicFunction } from '@ohos/common/src/main/ets/default/function/CameraBasicFunction'; 17import { CameraNeedStatus } from '@ohos/common/src/main/ets/default/utils/Constants'; 18import { FeatureManager } from '@ohos/common/src/main/ets/default/featureservice/FeatureManager'; 19import { Log } from '@ohos/common/src/main/ets/default/utils/Log'; 20import { PreferencesService } from '@ohos/common/src/main/ets/default/featurecommon/preferences/PreferencesService'; 21import { ModeMap } from '../common/ModeMap'; 22import { GlobalContext } from '@ohos/common/src/main/ets/default/utils/GlobalContext'; 23import UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility'; 24import Want from '@ohos.app.ability.Want'; 25import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 26import hilog from '@ohos.hilog'; 27 28export default class ExtensionPickerAbility extends UIExtensionAbility { 29 private cameraBasicFunction: any = null; 30 31 onCreate(): void { 32 // Ability is creating, initialize resources for this ability 33 Log.start(Log.ABILITY_WHOLE_LIFE); 34 Log.info('Camera ExtensionPickerAbility onCreate'); 35 GlobalContext.get().setIsPicker(true); 36 GlobalContext.get().setCameraAbilityContext(this.context); 37 GlobalContext.get().setObject('permissionFlag', false); 38 GlobalContext.get().setObject('cameraStartTime', new Date().getTime()); 39 GlobalContext.get().setObject('cameraStartFlag', true); 40 GlobalContext.get().setObject('stopRecordingFlag', false); 41 GlobalContext.get().setObject('doOnForeground', false); 42 GlobalContext.get().setObject('doOnForeground', false); 43 this.cameraBasicFunction = CameraBasicFunction.getInstance(); 44 this.cameraBasicFunction.initCamera({ 45 cameraId: 'BACK', mode: 'PHOTO' 46 }, 'onCreate'); 47 Log.info('Camera ExtensionPickerAbility onCreate.x'); 48 if (GlobalContext.get().getCameraFormParam() != undefined) { 49 new FeatureManager(GlobalContext.get().getCameraFormParam().mode, new ModeMap()); 50 } else { 51 new FeatureManager('PHOTO', new ModeMap()); 52 } 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 GlobalContext.get().setIsPicker(false); 60 this.cameraBasicFunction.startIdentification = false; 61 PreferencesService.getInstance().flush(); 62 Log.info('Camera ExtensionPickerAbility onDestroy.'); 63 } 64 65 onForeground() { 66 Log.start(Log.ABILITY_FOREGROUND_LIFE); 67 GlobalContext.get().setIsPicker(true); 68 GlobalContext.get().setObject('cameraNeedStatus', CameraNeedStatus.CAMERA_NEED_INIT) 69 if (GlobalContext.get().getT<boolean>('doOnForeground')) { 70 Log.info('Camera ExtensionPickerAbility onForeground.'); 71 GlobalContext.get().apply('updateCameraStatus') 72 } else { 73 GlobalContext.get().setObject('doOnForeground', true); 74 } 75 GlobalContext.get().setCameraAbilityContext(this.context); 76 Log.info('Camera ExtensionPickerAbility onForeground'); 77 } 78 79 onBackground() { 80 Log.end(Log.ABILITY_FOREGROUND_LIFE); 81 Log.info('Camera ExtensionPickerAbility onBackground.'); 82 GlobalContext.get().setIsPicker(false); 83 this.cameraBasicFunction.startIdentification = false; 84 GlobalContext.get().setObject('cameraNeedStatus', CameraNeedStatus.CAMERA_NEED_RELEASE) 85 GlobalContext.get().apply('updateCameraStatus'); 86 } 87 88 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 89 Log.info('Camera ExtensionPickerAbility onSessionCreate' + want.uri); 90 GlobalContext.get().setCameraAbilityWant(want); 91 GlobalContext.get().setPickerUri(want.uri); 92 GlobalContext.get().setSession(session); 93 let storage: LocalStorage = new LocalStorage( 94 { 95 'session': session, 96 'mode': 'PHOTO' 97 } 98 ) 99 session.loadContent('pages/index', storage); 100 try { 101 session.setWindowPrivacyMode(true); 102 } catch (e) { 103 Log.error('Camera ExtensionPickerAbility setWindowPrivacyMode error'); 104 } 105 } 106 107 onSessionDestroy(session): void { 108 Log.info('Camera ExtensionPickerAbility onSessionDestroy E'); 109 session.terminateSelf((error) => { 110 Log.info('Camera ExtensionPickerAbility onSessionDestroy' + JSON.stringify(error)); 111 }) 112 Log.info('Camera ExtensionPickerAbility onSessionDestroy X'); 113 } 114}