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