1/* 2 * Copyright (c) 2021-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'; 19import deviceInfo from '@ohos.deviceInfo'; 20import Constant from '../common/constant'; 21import type Want from '@ohos.app.ability.Want'; 22 23const TAG = '[DeviceManagerUI:Confirm]==>'; 24 25export default class ServiceExtAbility extends extension { 26 isCreatingWindow: boolean = false; 27 onCreate(want: Want): void { 28 globalThis.confirmContext = this.context; 29 globalThis.confirmWindowNum = 0; 30 AppStorage.SetOrCreate('confirmContext', this.context); 31 AppStorage.SetOrCreate('confirmWindowNum', 0); 32 let confirmWindowNum: number = AppStorage.get('confirmWindowNum') as number; 33 console.log('confirmWindowNum' + confirmWindowNum); 34 this.getShareStyle(); 35 } 36 37 onRequest(want: Want, startId: number): void { 38 console.log(TAG + 'onRequest execute' + JSON.stringify(want.parameters)); 39 let confirmWindowNum: number = AppStorage.get('confirmWindowNum'); 40 if (confirmWindowNum !== 0 || this.isCreatingWindow) { 41 console.log(TAG + 'onRequest window number is not zero or creating window.'); 42 return; 43 } 44 this.isCreatingWindow = true; 45 AppStorage.SetOrCreate('abilityWant', want); 46 display.getDefaultDisplay().then((dis: display.Display) => { 47 let density: number = dis.densityPixels; 48 let dialogRect: { left: number; top: number; width: number; height: number; } = { 49 left: (dis.width - (globalThis.style.shareWidth * density)) * Constant.HALF, 50 top: (dis.height - (globalThis.style.shareHeight * density)) * Constant.HALF - 51 (globalThis.style.shareHeight * density) / Constant.TOP_OFFSET_PROPORTION, 52 width: globalThis.style.shareWidth * density, 53 height: globalThis.style.shareHeight * density * Constant.DIALOG_HEIGHT_PROPORTION 54 }; 55 this.createWindow('picker Dialog:' + startId, window.WindowType.TYPE_FLOAT, dialogRect); 56 }); 57 } 58 59 getShareStyle(): void { 60 globalThis.style = {}; 61 if (deviceInfo.deviceType === 'phone' || deviceInfo.deviceType === 'default') { 62 globalThis.style.shareWidth = Constant.SHARE_WIDTH_PHONE; 63 globalThis.style.shareHeight = Constant.SHARE_HEIGHT_PHONE; 64 } else { 65 globalThis.style.shareWidth = Constant.SHARE_WIDTH_PAD; 66 globalThis.style.shareHeight = Constant.SHARE_HEIGHT_PAD; 67 } 68 } 69 70 onDestroy(): void { 71 console.log(TAG + 'ServiceExtAbility destroyed'); 72 } 73 74 private async createWindow(name: string, windowType: window.WindowType, 75 rect: { left: number; top: number; width: number; height: number; }): Promise<void> { 76 console.log(TAG + 'createWindow execute'); 77 try { 78 const win: window.Window = await window.create(this.context, name, windowType); 79 AppStorage.SetOrCreate('confirmWin', win); 80 await win.moveTo(rect.left, rect.top); 81 await win.resetSize(rect.width, rect.height); 82 await win.setCornerRadius(Constant.SHARE_RADIUS); 83 await win.loadContent('pages/ConfirmDialog'); 84 await win.show(); 85 let windowNum: number = AppStorage.get('confirmWindowNum') as number; 86 windowNum++; 87 AppStorage.SetOrCreate('confirmWindowNum', windowNum); 88 this.isCreatingWindow = false; 89 console.log(TAG + 'window create successfully'); 90 } catch { 91 this.isCreatingWindow = false; 92 console.info(TAG + 'window create failed'); 93 } 94 } 95};