1/* 2 * Copyright (c) 2022 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 extension from '@ohos.app.ability.ServiceExtensionAbility'; 17import window from '@ohos.window'; 18import display from '@ohos.display'; 19import { GlobalContext } from '../common/utils/globalContext'; 20import dialogRequest from '@ohos.app.ability.dialogRequest'; 21 22const TAG = 'PermissionManager_Log: '; 23const BG_COLOR = '#00000000'; 24const DEFAULT_CORNER_RADIUS_L = 16; 25 26export default class ServiceExtensionAbility extends extension { 27 /** 28 * Lifecycle function, called back when a service extension is started for initialization. 29 */ 30 onCreate(want): void { 31 console.info(TAG + 'ServiceExtensionAbility onCreate, ability name is ' + want.abilityName); 32 33 globalThis.windowNum = 0; 34 } 35 36 /** 37 * Lifecycle function, called back when a service extension is started or recall. 38 */ 39 onRequest(want, startId): void { 40 console.info(TAG + 'ServiceExtensionAbility onRequest. start id is ' + startId); 41 console.info(TAG + 'want: ' + JSON.stringify(want)); 42 43 try { 44 let dis = display.getDefaultDisplaySync(); 45 let navigationBarRect = { 46 left: 0, 47 top: 0, 48 width: dis.width, 49 height: dis.height 50 }; 51 this.createWindow('permissionDialog' + startId, window.WindowType.TYPE_DIALOG, navigationBarRect, want); 52 } catch (exception) { 53 console.error(TAG + 'Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); 54 }; 55 } 56 57 /** 58 * Lifecycle function, called back before a service extension is destroyed. 59 */ 60 onDestroy(): void { 61 console.info(TAG + 'ServiceExtensionAbility onDestroy.'); 62 } 63 64 private async createWindow(name: string, windowType, rect, want): Promise<void> { 65 let requestInfo: dialogRequest.RequestInfo; 66 try { 67 requestInfo = dialogRequest.getRequestInfo(want); 68 } catch (err) { 69 console.error(`getRequestInfo err= ${JSON.stringify(err)}`); 70 } 71 72 console.info(TAG + 'create window start, requestInfo: ' + JSON.stringify(requestInfo)); 73 let rectInfo = requestInfo ? requestInfo.windowRect : rect; 74 rectInfo = rectInfo.width === 0 ? rect : rectInfo; 75 try { 76 const win = await window.createWindow({ ctx: this.context, name, windowType }); 77 console.info(TAG + 'createWindow end.'); 78 let storage: LocalStorage = new LocalStorage({ 'want': want, 'win': win }); 79 await win.bindDialogTarget(want.parameters['ohos.ability.params.token'].value, () => { 80 let windowNum = GlobalContext.load('windowNum'); 81 windowNum --; 82 GlobalContext.store('windowNum', windowNum); 83 win.destroyWindow(); 84 if (windowNum === 0) { 85 this.context.terminateSelf(); 86 } 87 }); 88 console.info(TAG + 'bindDialogTarget end.'); 89 await win.moveWindowTo(rectInfo.left, rectInfo.top); 90 console.info(TAG + 'moveWindowTo end.'); 91 await win.resize(rectInfo.width, rectInfo.height); 92 console.info(TAG + 'resize end.'); 93 await win.loadContent('pages/dialogPlus', storage); 94 win.setWindowBackgroundColor(BG_COLOR); 95 if (rectInfo.width < rect.width) { 96 win.setCornerRadius(DEFAULT_CORNER_RADIUS_L); 97 } 98 await win.showWindow(); 99 console.info(TAG + 'showWindow end.'); 100 await win.setWindowLayoutFullScreen(true); 101 console.info(TAG + 'setWindowLayoutFullScreen end.'); 102 globalThis.windowNum ++; 103 GlobalContext.store('windowNum', globalThis.windowNum); 104 } catch { 105 console.info(TAG + 'window create failed!'); 106 } 107 } 108};