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 { Want } from '@kit.AbilityKit'; 20import { Log } from '../common/utils/utils'; 21import { GlobalDialogModel } from './GlobalDialogModel'; 22 23let globalDialogModel: GlobalDialogModel = new GlobalDialogModel(); 24let globalWindow: window.Window | null; 25 26export default class GlobalExtensionAbility extends extension { 27 /** 28 * Lifecycle function, called back when a service extension is started for initialization. 29 */ 30 onCreate(want: Want): void { 31 Log.info('ServiceExtensionAbility onCreate, ability name is ' + want.abilityName); 32 33 if (!want.parameters) { 34 return; 35 } 36 let resource: string = want.parameters['ohos.sensitive.resource'] as string ?? ''; 37 38 if (!globalDialogModel.permissionCheck()) { 39 this.context?.terminateSelf(); 40 return; 41 } 42 43 if (!globalDialogModel.statusCheck(this.context, resource)) { 44 this.context?.terminateSelf(); 45 return; 46 } 47 48 try { 49 let dis = display.getDefaultDisplaySync(); 50 let navigationBarRect: display.Rect = { 51 left: 0, 52 top: 0, 53 width: dis.width, 54 height: dis.height 55 }; 56 Log.info('want: ' + JSON.stringify(want)); 57 let property: Record<string, Object> = { 'globalState': resource }; 58 let storage: LocalStorage = new LocalStorage(property); 59 globalDialogModel.createWindow(this.context, navigationBarRect, storage).then(window => { 60 globalWindow = window; 61 }) 62 } catch (exception) { 63 Log.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); 64 }; 65 } 66 67 /** 68 * Lifecycle function, called back when a service extension is started or recall. 69 */ 70 onRequest(want: Want, startId: number): void { 71 Log.info('ServiceExtensionAbility onRequest. start id is ' + startId); 72 } 73 74 /** 75 * Lifecycle function, called back before a service extension is destroyed. 76 */ 77 onDestroy(): void { 78 Log.info('ServiceExtensionAbility onDestroy.'); 79 globalWindow?.destroyWindow(); 80 } 81 82};