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 WindowManagers, { WindowType } from "../../../../../../common/src/main/ets/default/WindowManager"; 19import display from '@ohos.display' 20import Log from '../../../../../../common/src/main/ets/default/Log' 21import Constants from '../../../../../../features/screenlock/src/main/ets/com/ohos/common/constants' 22import AbilityManager from '../../../../../../common/src/main/ets/default/abilitymanager/abilityManager' 23import sTimeManager from '../../../../../../common/src/main/ets/default/TimeManager' 24import inputMethod from '@ohos.inputMethod' 25 26const TAG = "ScreenLock-ServiceExtAbility" 27const AUTO_ROTATION_RETRICTED: number = 8 28 29class ServiceExtAbility extends ServiceExtension { 30 private direction :number; 31 32 33 onCreate(want) { 34 Log.showInfo(TAG, 'onCreate, want:' + want.abilityName); 35 AbilityManager.setContext(AbilityManager.ABILITY_NAME_SCREEN_LOCK, this.context) 36 sTimeManager.init(this.context) 37 38 display.on("change", (id) => { 39 Log.showInfo(TAG, "screenlock display change, data: " + JSON.stringify(id)) 40 display.getAllDisplay().then(async (arrayDisplay) => { 41 Log.showInfo(TAG, "getAllDisplay : " + JSON.stringify(arrayDisplay)) 42 for (let display of arrayDisplay) { 43 Log.showInfo(TAG, "getAllDisplay start : " + JSON.stringify(display)); 44 if (id == display.id) { 45 if (display.width > display.height) { 46 this.direction = 1; 47 } else { 48 this.direction = 2; 49 } 50 Log.showInfo(TAG, "direction change : " + this.direction) 51 AppStorage.SetOrCreate('screenlockdirection', this.direction) 52 this.resetWindow(display.width,display.height) 53 let inputMethodController = inputMethod.getController(); 54 Log.showInfo(TAG, "inputMethodController: "+inputMethodController) 55 inputMethodController.hideSoftKeyboard().then(() => { 56 Log.showInfo(TAG, "Succeeded in hiding softKeyboard") 57 }).catch((err) => { 58 Log.showError(TAG, "failed to hideSoftKeyboard: " + JSON.stringify(err)); 59 }); 60 } 61 } 62 }) 63 }) 64 this.statusBarWindow() 65 this.createWindow(Constants.WIN_NAME) 66 } 67 private async resetWindow(width: number,height:number) { 68 Log.showInfo(TAG, "resetWindow width: " + width +",height:"+height) 69 let window = await windowManager.find(Constants.WIN_NAME); 70 Log.showInfo(TAG, "screenlock window : " + JSON.stringify(window)); 71 await window.resetSize(width,height) 72 } 73 74 private createWindow(name: string) { 75 Log.showDebug(TAG, `createWindow name:${name}`) 76 windowManager.create(this.context, name, 2110).then((win) => { 77 Log.showInfo(TAG, "before begin " + name + " window show!") 78 win.setPreferredOrientation(AUTO_ROTATION_RETRICTED, (err) => { 79 if (err.code) { 80 Log.showError(TAG, "failed to set window Orientation: " + JSON.stringify(err)); 81 } 82 Log.showInfo(TAG, "succeed to set window Orientation"); 83 }) 84 win.loadContent("pages/index").then(() => { 85 win.show().then(() => { 86 Log.showInfo(TAG, "window show in then!"); 87 }) 88 }) 89 }, (error) => { 90 Log.showError(TAG, name + " window createFailed, error.code = " + error.code) 91 }) 92 } 93 94 private async statusBarWindow() { 95 Log.showDebug(TAG, `statusBarWindow`); 96 let dis = await display.getDefaultDisplay(); 97 while (dis === null) { 98 await new Promise((resolve)=>{setTimeout(resolve, 1000)}); 99 dis = await display.getDefaultDisplay(); 100 } 101 Log.showInfo(TAG, `getDefaultDisplay, dis: ${JSON.stringify(dis)}`); 102 let rect; 103 if (dis.width > dis.height) { // Pad and PC horizontalScreen Mode 104 rect = { 105 left: 0, 106 top: 0, 107 width: '100%', 108 height: (44 * dis.width) / 1280, 109 } 110 this.direction =1 111 } else { // Phone verticalScreen Mode 112 rect = { 113 left: 0, 114 top: 0, 115 width: '100%', 116 height: (44 * dis.width) / 800 117 } 118 this.direction =2 119 }; 120 AppStorage.SetOrCreate('screenlockdirection', this.direction); 121 AbilityManager.setAbilityData(AbilityManager.ABILITY_NAME_STATUS_BAR, "rect", rect); 122 AbilityManager.setAbilityData(AbilityManager.ABILITY_NAME_STATUS_BAR, "dis", { 123 width: dis.width, 124 height: dis.height, 125 }); 126 } 127 128 onDestroy() { 129 Log.showInfo(TAG, 'api8New onDestroy'); 130 sTimeManager.release() 131 } 132} 133 134export default ServiceExtAbility