1/* 2 * Copyright (c) 2021-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 ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility' 17import windowManager from '@ohos.window' 18import display from '@ohos.display' 19import {Log, AbilityManager, sTimeManager} from '@ohos/common' 20import Constants from '../../../../../../features/screenlock/src/main/ets/com/ohos/common/constants' 21import {UIContext} from '@ohos.arkui.UIContext' 22 23const TAG = "ScreenLock-ServiceExtAbility" 24 25class ServiceExtAbility extends ServiceExtension { 26 onCreate(want) { 27 Log.showInfo(TAG, 'onCreate, want:' + want.abilityName); 28 AbilityManager.setContext(AbilityManager.ABILITY_NAME_SCREEN_LOCK, this.context) 29 sTimeManager.init(this.context) 30 this.statusBarWindow() 31 this.createWindow(Constants.WIN_NAME) 32 } 33 34 private createWindow(name: string) { 35 Log.showDebug(TAG, `createWindow name:${name}`) 36 windowManager.create(this.context, name, windowManager.WindowType.TYPE_KEYGUARD).then((win) => { 37 win.loadContent("pages/index").then(() => { 38 const UIContext: UIContext = win.getUIContext(); 39 Log.showInfo(TAG, 'get UIContext successful,' + JSON.stringify(UIContext)); 40 AppStorage.SetOrCreate('UIContext', UIContext); 41 Log.showInfo(TAG, name + " window loadContent in then! ") 42 win.show().then(() => { 43 Log.showInfo(TAG, "then begin " + name + " window show in then! "); 44 }) 45 }) 46 }, (error) => { 47 Log.showError(TAG, name + " window createFailed, error.code = " + error.code) 48 }) 49 } 50 51 private async statusBarWindow() { 52 let dis = await display.getDefaultDisplay(); 53 Log.showDebug(TAG, `api8New onCreate, dis: ${JSON.stringify(dis)}`); 54 let rect; 55 if (dis.width > dis.height) { // Pad、PC horizontalScreen Mode 56 rect = { 57 left: 0, 58 top: 0, 59 width: '100%', 60 height: (48 * dis.width) / 1280, 61 } 62 } else { // Phone verticalScreen Mode 63 rect = { 64 left: 0, 65 top: 0, 66 width: '100%', 67 height: (48 * dis.width) / 720 68 } 69 } 70 AbilityManager.setAbilityData(AbilityManager.ABILITY_NAME_STATUS_BAR, "rect", rect); 71 AbilityManager.setAbilityData(AbilityManager.ABILITY_NAME_STATUS_BAR, "dis", { 72 width: dis.width, 73 height: dis.height, 74 }); 75 Log.showInfo(TAG, `createWindow success.`); 76 } 77 78 onDestroy() { 79 Log.showInfo(TAG, 'api8New onDestroy'); 80 sTimeManager.release() 81 82 } 83 84 onMemoryLevel(level): void { 85 Log.showInfo(TAG, 'onMemoryLevel, level:' + JSON.stringify(level)) 86 } 87} 88 89export default ServiceExtAbility