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 GlobalExtensionAbility 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 console.info(TAG + 'want: ' + JSON.stringify(want)); 31 32 GlobalContext.store('globalState', want.parameters['ohos.sensitive.resource']); 33 GlobalContext.store('context', this.context); 34 35 try { 36 let dis = display.getDefaultDisplaySync(); 37 let navigationBarRect = { 38 left: 0, 39 top: 0, 40 width: dis.width, 41 height: dis.height 42 }; 43 this.createWindow('globalDialog', window.WindowType.TYPE_VOICE_INTERACTION, navigationBarRect); 44 } catch (exception) { 45 console.error(TAG + 'Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); 46 }; 47 } 48 49 /** 50 * Lifecycle function, called back when a service extension is started or recall. 51 */ 52 onRequest(want, startId): void { 53 console.info(TAG + 'ServiceExtensionAbility onRequest. start id is ' + startId); 54 } 55 56 /** 57 * Lifecycle function, called back before a service extension is destroyed. 58 */ 59 onDestroy(): void { 60 console.info(TAG + 'ServiceExtensionAbility onDestroy.'); 61 let win = GlobalContext.load('globalWin'); 62 win.destroyWindow(); 63 } 64 65 private async createWindow(name: string, windowType: number, rect): Promise<void> { 66 console.info(TAG + 'create window'); 67 try { 68 const win = await window.createWindow({ ctx: this.context, name, windowType }); 69 GlobalContext.store('globalWin', win); 70 await win.moveWindowTo(rect.left, rect.top); 71 await win.resize(rect.width, rect.height); 72 await win.setUIContent('pages/globalSwitch'); 73 await win.setWindowBackgroundColor(BG_COLOR); 74 await win.showWindow(); 75 } catch { 76 console.info(TAG + 'window create failed!'); 77 } 78 } 79};