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