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