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 */ 15import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 16import common from '@ohos.app.ability.common'; 17import window from '@ohos.window'; 18import Want from '@ohos.app.ability.Want'; 19import display from '@ohos.display'; 20import GlobalContext from './GlobalContext'; 21import { BusinessError } from '@ohos.base'; 22 23const TAG = 'PasteboardProgressAbility: '; 24const BG_COLOR = '#00000000'; 25let createWindowStatus = 'created'; 26 27interface IRect { 28 left: number; 29 top: number; 30 width: number; 31 height: number; 32} 33 34interface StorageItem { 35 want: Want, 36 win: window.Window, 37 serviceContext: common.ServiceExtensionContext, 38 globalContext: GlobalContext 39} 40 41export default class PasteboardProgressAbility extends ServiceExtensionAbility { 42 onCreate(want: Want): void { 43 console.info(TAG + 'PasteboardProgressAbility onCreate, ability name is ' + want.abilityName); 44 } 45 46 async onRequest(want: Want, startId: number) { 47 try { 48 console.info(TAG + 'PasteboardProgressAbility onRequest, startId is' + startId); 49 console.info(TAG + 'want: ' + JSON.stringify(want)); 50 let displayClass = display.getDefaultDisplaySync(); 51 let navigationBarRect: IRect = { 52 left: 0, 53 top: 0, 54 width: displayClass.width, 55 height: displayClass.height, 56 }; 57 if (canIUse('SystemCapability.Window.SessionManager')) { 58 await window.getVisibleWindowInfo().then((infos: window.WindowInfo[]) => { 59 const targetWindowId = want.parameters?.windowId as number; 60 const targetWindowInfo = infos.find(info => info.windowId === targetWindowId); 61 navigationBarRect = targetWindowInfo?.rect as IRect; 62 console.info(TAG + `targetWindowInfo rect is ${JSON.stringify(navigationBarRect)}`); 63 }).catch((err: BusinessError) => { 64 console.error(TAG + 'Failed to getWindowInfo. Cause: ' + JSON.stringify(err)); 65 }); 66 } 67 this.createWindow(`PasteboardProgressDialog${startId}`, navigationBarRect, want); 68 } catch (exception) { 69 console.error(TAG + 'Failed to obtain the default display object. Code:' + JSON.stringify(exception)) 70 } 71 } 72 73 onDestroy(): void { 74 console.info(TAG + 'PasteboardProgressAbility onDestroy.') 75 } 76 77 private async createWindow(name: string, rect: IRect, want: Want): Promise<void> { 78 if (createWindowStatus === 'created') { 79 createWindowStatus = 'creating'; 80 console.info(TAG + 'create progressWindow'); 81 } else { 82 console.warn(TAG + 'window is creating'); 83 return; 84 } 85 const progressKey = want.parameters?.['progressKey'] as string; 86 if (GlobalContext.getInstance().dialogSet.has(progressKey)) { 87 console.info(TAG + 'window already exists'); 88 return; 89 } 90 try { 91 const win = await window.createWindow({ 92 ctx: this.context, 93 name, 94 windowType: window.WindowType.TYPE_DIALOG 95 }); 96 await win.bindDialogTarget((want.parameters?.['tokenKey'] as ESObject).value, () => { 97 win.destroyWindow(); 98 console.info(TAG + 'destroyWindow'); 99 GlobalContext.getInstance().dialogSet.delete(progressKey); 100 if (GlobalContext.getInstance().dialogSet.size === 0) { 101 this.context.terminateSelf(); 102 } 103 }); 104 let storageItem: StorageItem = { 105 want, 106 win, 107 globalContext: GlobalContext.getInstance(), 108 serviceContext: this.context 109 } 110 let storage: LocalStorage = new LocalStorage(storageItem); 111 await win.moveWindowTo(rect.left, rect.top); 112 await win.resize(rect.width, rect.height); 113 await win.loadContent('pages/PasteboardProgress', storage); 114 win.setWindowBackgroundColor(BG_COLOR); 115 await win.showWindow(); 116 GlobalContext.getInstance().dialogSet.add(progressKey); 117 console.info(TAG + `dialogSet add, size is ${GlobalContext.getInstance().dialogSet.size}`); 118 } catch (err) { 119 console.error(TAG + `window create failed! err: ${JSON.stringify(err)}`); 120 } finally { 121 createWindowStatus = 'created'; 122 } 123 } 124}