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 = session.getUIExtensionHostWindowProxy(); 45 AppStorage.setOrCreate('photosAbilityContext',this.context); 46 AppStorage.setOrCreate(Constants.PHOTO_PICKER_EXTENSION_WINDOW, extensionWindow); 47 let storage: LocalStorage = new LocalStorage(); 48 let params: SelectParams = this.parseWant(want); 49 storage.setOrCreate(Constants.PHOTO_PICKER_SESSION_KEY, session); 50 storage.setOrCreate(Constants.PHOTO_PICKER_PARAMS_KEY, params); 51 storage.setOrCreate(Constants.PHOTO_PICKER_EXTENSION_WINDOW, extensionWindow); 52 UserFileManagerAccess.getInstance().onCreate(this.context); 53 ScreenManager.getInstance().initializationSize(undefined).then((): void => { 54 ScreenManager.getInstance().getAvoidArea(); 55 ScreenManager.getInstance().setMainWindow(); 56 session.loadContent('pages/ThirdSelectPhotoGridPage', storage); 57 Log.info(TAG, 'onSessionCreate'); 58 }); 59 } 60 61 onSessionDestroy(session: UIExtensionContentSession) { 62 Log.info(TAG, "onSessionDestroy"); 63 UserFileManagerAccess.getInstance().onDestroy(); 64 ScreenManager.getInstance().destroyMainWindow(); 65 AppStorage.delete(Constants.PHOTO_PICKER_EXTENSION_WINDOW); 66 } 67 68 private parseWant(want: Want): SelectParams { 69 let wantParam: Record<string, Object> = want.parameters as Record<string, Object>; 70 let params: SelectParams = SelectParams.defaultParam(); 71 let selectType: string = wantParam?.uri as string; 72 if (selectType) { 73 params.isMultiPick = selectType === Constants.WANT_PARAM_URI_SELECT_MULTIPLE; 74 params.maxSelectCount = params.isMultiPick ? wantParam?.maxSelectCount as number : 1; 75 params.bundleName = wantParam[Constants.KEY_WANT_PARAMETERS_CALLER_BUNDLE_NAME] as string; 76 params.filterMediaType = wantParam?.filterMediaType as string; 77 params.cameraAble = (wantParam?.isPhotoTakingSupported as boolean) === undefined ? true : (wantParam?.isPhotoTakingSupported as boolean); 78 params.editAble = (wantParam?.isEditSupported as boolean) === undefined ? true : (wantParam?.isEditSupported as boolean); 79 params.isFirstEnter = true; 80 } else { 81 Log.error(TAG, `invalid selectType: ${JSON.stringify(selectType)}`); 82 } 83 Log.info(TAG, `parseWant: ${JSON.stringify(wantParam)}, params: ${JSON.stringify(params)}`); 84 return params; 85 } 86}