1/* 2 * Copyright (c) 2022-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 UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility'; 17import { Constants, Log, UserFileManagerAccess } from '@ohos/common'; 18import { ScreenManager } from '@ohos/common/src/main/ets/default/model/common/ScreenManager'; 19import Want from '@ohos.app.ability.Want'; 20import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 21import { SelectParams } from '@ohos/thirdselect/src/main/ets/default/utils/ThirdSelectConstants'; 22import uiExtensionHost from '@ohos.uiExtensionHost'; 23 24const TAG = '[PickerUIExtensionAbility]' 25 26export default class PickerUIExtensionAbility extends UIExtensionAbility { 27 onCreate(): void { 28 Log.info(TAG, 'onCreate'); 29 } 30 31 onDestroy(): void { 32 Log.info(TAG, 'onDestroy'); 33 } 34 35 onForeground(): void { 36 Log.info(TAG, 'onForeground'); 37 } 38 39 onBackground(): void { 40 Log.info(TAG, 'onBackground'); 41 } 42 43 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 44 let extensionWindow: uiExtensionHost.UIExtensionHostWindowProxy | undefined = 45 session.getUIExtensionHostWindowProxy(); 46 AppStorage.setOrCreate('photosAbilityContext',this.context); 47 AppStorage.setOrCreate(Constants.PHOTO_PICKER_EXTENSION_WINDOW, extensionWindow); 48 let storage: LocalStorage = new LocalStorage(); 49 let params: SelectParams = this.parseWant(want); 50 storage.setOrCreate(Constants.PHOTO_PICKER_SESSION_KEY, session); 51 storage.setOrCreate(Constants.PHOTO_PICKER_PARAMS_KEY, params); 52 storage.setOrCreate(Constants.PHOTO_PICKER_EXTENSION_WINDOW, extensionWindow); 53 UserFileManagerAccess.getInstance().onCreate(this.context); 54 ScreenManager.getInstance().initializationSize(undefined).then((): void => { 55 ScreenManager.getInstance().getAvoidArea(); 56 ScreenManager.getInstance().setMainWindow(); 57 session.loadContent('pages/ThirdSelectPhotoGridPage', storage); 58 Log.info(TAG, 'onSessionCreate'); 59 }); 60 } 61 62 onSessionDestroy(session: UIExtensionContentSession) { 63 Log.info(TAG, 'onSessionDestroy'); 64 UserFileManagerAccess.getInstance().onDestroy(); 65 ScreenManager.getInstance().destroyMainWindow(); 66 AppStorage.delete(Constants.PHOTO_PICKER_EXTENSION_WINDOW); 67 } 68 69 private parseWant(want: Want): SelectParams { 70 let wantParam: Record<string, Object> = want.parameters as Record<string, Object>; 71 let params: SelectParams = SelectParams.defaultParam(); 72 let selectType: string = wantParam?.uri as string; 73 if (selectType) { 74 params.isMultiPick = selectType === Constants.WANT_PARAM_URI_SELECT_MULTIPLE; 75 params.maxSelectCount = params.isMultiPick ? wantParam?.maxSelectCount as number : 1; 76 params.bundleName = wantParam[Constants.KEY_WANT_PARAMETERS_CALLER_BUNDLE_NAME] as string; 77 params.filterMediaType = wantParam?.filterMediaType as string; 78 params.cameraAble = (wantParam?.isPhotoTakingSupported as boolean) === undefined ? 79 true : (wantParam?.isPhotoTakingSupported as boolean); 80 params.editAble = (wantParam?.isEditSupported as boolean) === undefined ? 81 true : (wantParam?.isEditSupported as boolean); 82 params.isFirstEnter = true; 83 } else { 84 Log.error(TAG, `invalid selectType: ${JSON.stringify(selectType)}`); 85 } 86 Log.info(TAG, `parseWant: ${JSON.stringify(wantParam)}, params: ${JSON.stringify(params)}`); 87 return params; 88 } 89}