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